Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,71 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
from threading import Thread
|
| 5 |
import random
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
model_name = "
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
-
# Load model with
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
model_name,
|
|
|
|
| 14 |
device_map="auto",
|
| 15 |
-
torch_dtype=torch.float32,
|
| 16 |
low_cpu_mem_usage=True
|
| 17 |
)
|
| 18 |
-
|
| 19 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
|
| 21 |
-
# Safety tools 🛡️
|
| 22 |
BLOCKED_WORDS = ["violence", "hate", "gun", "personal"]
|
| 23 |
-
SAFE_IDEAS = [
|
| 24 |
-
"Design a robot to clean parks 🌳",
|
| 25 |
-
"Code a game about recycling ♻️",
|
| 26 |
-
"Plan an AI tool for school safety 🚸"
|
| 27 |
-
]
|
| 28 |
-
safety_checker = pipeline("text-classification", model="unitary/toxic-bert")
|
| 29 |
|
| 30 |
def is_safe(text):
|
| 31 |
text = text.lower()
|
| 32 |
-
|
| 33 |
-
return False
|
| 34 |
-
result = safety_checker(text)[0]
|
| 35 |
-
return not (result["label"] == "toxic" and result["score"] > 0.7)
|
| 36 |
|
| 37 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 38 |
if not is_safe(message):
|
| 39 |
return f"🚫 Let's focus on positive projects! Try: {random.choice(SAFE_IDEAS)}"
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
for
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
if bot_msg:
|
| 47 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"temperature": temperature,
|
| 61 |
-
"top_p": top_p,
|
| 62 |
-
"streamer": streamer
|
| 63 |
-
}
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
for new_token in streamer:
|
| 70 |
-
partial_message += new_token
|
| 71 |
-
yield partial_message
|
| 72 |
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
gr.Markdown("# 🤖 REACT Ethical AI Lab")
|
| 75 |
gr.ChatInterface(
|
| 76 |
respond,
|
| 77 |
additional_inputs=[
|
| 78 |
-
gr.Textbox("
|
| 79 |
-
gr.Slider(64,
|
| 80 |
-
gr.Slider(0.1,
|
| 81 |
-
gr.Slider(0.
|
| 82 |
],
|
| 83 |
examples=[
|
| 84 |
-
["How to
|
| 85 |
-
["
|
| 86 |
]
|
| 87 |
)
|
| 88 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
from threading import Thread
|
| 5 |
import random
|
| 6 |
|
| 7 |
+
# Configuration 🛠️
|
| 8 |
+
model_name = "microsoft/phi-3-mini-4k-instruct"
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
+
# Load model with memory optimizations
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
model_name,
|
| 14 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 15 |
device_map="auto",
|
|
|
|
| 16 |
low_cpu_mem_usage=True
|
| 17 |
)
|
|
|
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 19 |
|
| 20 |
+
# Safety tools 🛡️ (simplified)
|
| 21 |
BLOCKED_WORDS = ["violence", "hate", "gun", "personal"]
|
| 22 |
+
SAFE_IDEAS = ["Design a robot to clean parks 🌳", "Code a recycling game ♻️"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def is_safe(text):
|
| 25 |
text = text.lower()
|
| 26 |
+
return not any(bad_word in text for bad_word in BLOCKED_WORDS)
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 29 |
if not is_safe(message):
|
| 30 |
return f"🚫 Let's focus on positive projects! Try: {random.choice(SAFE_IDEAS)}"
|
| 31 |
|
| 32 |
+
# Create prompt with limited history
|
| 33 |
+
prompt = f"System: {system_message}\n"
|
| 34 |
+
for user, bot in history[-2:]: # Keep only last 2 exchanges
|
| 35 |
+
prompt += f"User: {user}\nAssistant: {bot}\n"
|
| 36 |
+
prompt += f"User: {message}\nAssistant:"
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 39 |
|
| 40 |
+
# Generation settings
|
| 41 |
+
generation_kwargs = dict(
|
| 42 |
+
inputs.input_ids,
|
| 43 |
+
max_new_tokens=min(max_tokens, 256),
|
| 44 |
+
temperature=min(temperature, 0.7),
|
| 45 |
+
top_p=top_p,
|
| 46 |
+
do_sample=True,
|
| 47 |
+
pad_token_id=tokenizer.eos_token_id
|
| 48 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Generate response
|
| 51 |
+
outputs = model.generate(**generation_kwargs)
|
| 52 |
+
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
|
| 53 |
|
| 54 |
+
yield response
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
gr.Markdown("# 🤖 REACT Ethical AI Lab")
|
| 58 |
gr.ChatInterface(
|
| 59 |
respond,
|
| 60 |
additional_inputs=[
|
| 61 |
+
gr.Textbox("Help students create ethical AI projects", label="Guidelines"),
|
| 62 |
+
gr.Slider(64, 256, value=128, label="Max Length"),
|
| 63 |
+
gr.Slider(0.1, 0.7, value=0.3, label="Creativity"),
|
| 64 |
+
gr.Slider(0.5, 1.0, value=0.9, label="Focus")
|
| 65 |
],
|
| 66 |
examples=[
|
| 67 |
+
["How to make a solar-powered robot?"],
|
| 68 |
+
["Simple air quality sensor code"]
|
| 69 |
]
|
| 70 |
)
|
| 71 |
|