Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 4 |
import random
|
| 5 |
|
| 6 |
# Configuration 🛠️
|
| 7 |
-
model_name = "microsoft/phi-3-mini-4k-instruct"
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
| 10 |
# Load model with optimizations
|
|
@@ -19,55 +19,47 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
| 19 |
# Safety tools 🛡️
|
| 20 |
SAFE_RESPONSES = [
|
| 21 |
"Let's focus on positive tech projects! 🌱",
|
| 22 |
-
"How about designing an eco-friendly robot? 🤖"
|
| 23 |
-
"Let's explore renewable energy solutions! ☀️"
|
| 24 |
]
|
| 25 |
|
| 26 |
def generate_response(message, history):
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if any(word in message.lower() for word in ["violence", "hate", "gun"]):
|
| 29 |
return random.choice(SAFE_RESPONSES)
|
| 30 |
|
| 31 |
-
# Format prompt
|
| 32 |
-
prompt = f"<|user|>\n{message}<|end|>\n<|assistant|>"
|
| 33 |
-
|
| 34 |
-
# Tokenize input
|
| 35 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 36 |
-
|
| 37 |
# Generate response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
outputs = model.generate(
|
| 39 |
-
inputs
|
| 40 |
max_new_tokens=256,
|
| 41 |
temperature=0.7,
|
| 42 |
-
do_sample=True
|
| 43 |
-
pad_token_id=tokenizer.eos_token_id
|
| 44 |
)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
return tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
|
| 48 |
|
| 49 |
-
# Create Gradio interface
|
| 50 |
demo = gr.ChatInterface(
|
| 51 |
-
|
| 52 |
examples=[
|
| 53 |
"How to make a solar-powered robot?",
|
| 54 |
"Python code for air quality sensor"
|
| 55 |
],
|
| 56 |
title="🤖 REACT Ethical AI Lab",
|
| 57 |
-
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
# Explicit API setup
|
| 61 |
-
api = gr.mount_gradio_app(
|
| 62 |
-
app=demo.app,
|
| 63 |
-
blocks=demo,
|
| 64 |
-
path="/api"
|
| 65 |
)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
demo.launch(
|
| 69 |
server_name="0.0.0.0",
|
| 70 |
server_port=7860,
|
| 71 |
-
enable_queue
|
| 72 |
-
share=False
|
| 73 |
)
|
|
|
|
| 4 |
import random
|
| 5 |
|
| 6 |
# Configuration 🛠️
|
| 7 |
+
model_name = "microsoft/phi-3-mini-4k-instruct"
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
| 10 |
# Load model with optimizations
|
|
|
|
| 19 |
# Safety tools 🛡️
|
| 20 |
SAFE_RESPONSES = [
|
| 21 |
"Let's focus on positive tech projects! 🌱",
|
| 22 |
+
"How about designing an eco-friendly robot? 🤖"
|
|
|
|
| 23 |
]
|
| 24 |
|
| 25 |
def generate_response(message, history):
|
| 26 |
+
# Convert history to new message format
|
| 27 |
+
messages = [{"role": "user", "content": msg} for msg, _ in history]
|
| 28 |
+
messages += [{"role": "assistant", "content": res} for _, res in history]
|
| 29 |
+
|
| 30 |
+
# Safety check
|
| 31 |
if any(word in message.lower() for word in ["violence", "hate", "gun"]):
|
| 32 |
return random.choice(SAFE_RESPONSES)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Generate response
|
| 35 |
+
inputs = tokenizer.apply_chat_template(
|
| 36 |
+
[{"role": "user", "content": message}],
|
| 37 |
+
return_tensors="pt"
|
| 38 |
+
).to(model.device)
|
| 39 |
+
|
| 40 |
outputs = model.generate(
|
| 41 |
+
inputs,
|
| 42 |
max_new_tokens=256,
|
| 43 |
temperature=0.7,
|
| 44 |
+
do_sample=True
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
+
return tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
|
|
|
|
| 48 |
|
| 49 |
+
# Create Gradio interface with updated message format
|
| 50 |
demo = gr.ChatInterface(
|
| 51 |
+
generate_response,
|
| 52 |
examples=[
|
| 53 |
"How to make a solar-powered robot?",
|
| 54 |
"Python code for air quality sensor"
|
| 55 |
],
|
| 56 |
title="🤖 REACT Ethical AI Lab",
|
| 57 |
+
chatbot=gr.Chatbot(height=500, likeable=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo.launch(
|
| 62 |
server_name="0.0.0.0",
|
| 63 |
server_port=7860,
|
| 64 |
+
share=False # Remove enable_queue parameter
|
|
|
|
| 65 |
)
|