Spaces:
Runtime error
Runtime error
File size: 2,942 Bytes
dafbfa4 ff54bae b717fcf dafbfa4 981a2dd b717fcf 981a2dd dafbfa4 981a2dd dafbfa4 ff54bae dafbfa4 b717fcf dafbfa4 b717fcf 981a2dd b717fcf ff54bae 981a2dd ff54bae b717fcf ff54bae b717fcf ff54bae b717fcf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import gradio as gr
import os
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, pipeline
from threading import Thread
import random
# Local model setup π€
model_name = "HuggingFaceH4/zephyr-7b-beta"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map="auto",
load_in_4bit=True # Reduces VRAM usage
)
# Safety tools π‘οΈ
BLOCKED_WORDS = ["violence", "hate", "gun", "personal"]
SAFE_IDEAS = [
"Design a robot to clean parks π³",
"Code a game about recycling β»οΈ",
"Plan an AI tool for school safety πΈ"
]
safety_checker = pipeline("text-classification", model="unitary/toxic-bert")
def is_safe(text):
text = text.lower()
if any(bad_word in text for bad_word in BLOCKED_WORDS):
return False
result = safety_checker(text)[0]
return not (result["label"] == "toxic" and result["score"] > 0.7)
def respond(message, history, system_message, max_tokens, temperature, top_p):
# Safety check first π
if not is_safe(message):
return f"π« Let's focus on positive projects! Try: {random.choice(SAFE_IDEAS)}"
# Prepare chat history
messages = [{"role": "system", "content": system_message}]
for user_msg, bot_msg in history[-5:]: # Keep last 5 exchanges
if user_msg:
messages.append({"role": "user", "content": user_msg})
if bot_msg:
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": message})
# Tokenize and prepare streaming
inputs = tokenizer.apply_chat_template(
messages,
return_tensors="pt"
).to(model.device)
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
generation_kwargs = {
"inputs": inputs,
"max_new_tokens": max_tokens,
"temperature": temperature,
"top_p": top_p,
"streamer": streamer
}
# Start generation in thread
thread = Thread(target=model.generate, kwargs=generation_kwargs)
thread.start()
# Stream output
partial_message = ""
for new_token in streamer:
partial_message += new_token
yield partial_message
with gr.Blocks() as demo:
gr.Markdown("# π€ REACT Ethical AI Lab")
gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox("You help students create ethical AI projects.", label="Guidelines"),
gr.Slider(128, 1024, value=512, label="Max Response Length"),
gr.Slider(0.1, 1.0, value=0.3, label="Creativity Level"),
gr.Slider(0.7, 1.0, value=0.85, label="Focus Level")
],
examples=[
["How to build a robot that plants trees?"],
["Python code for a pollution sensor"]
]
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0") |