Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 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
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
model_name,
|
| 14 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
|
@@ -17,57 +16,58 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 17 |
)
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 19 |
|
| 20 |
-
# Safety tools 🛡️
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
|
| 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 |
-
#
|
| 33 |
-
prompt = f"
|
| 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 |
-
#
|
| 41 |
-
|
| 42 |
inputs.input_ids,
|
| 43 |
-
max_new_tokens=
|
| 44 |
-
temperature=
|
| 45 |
-
top_p=top_p,
|
| 46 |
do_sample=True,
|
| 47 |
pad_token_id=tokenizer.eos_token_id
|
| 48 |
)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
outputs =
|
| 52 |
-
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
|
| 53 |
-
|
| 54 |
-
yield response
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 4 |
import random
|
| 5 |
|
| 6 |
# Configuration 🛠️
|
| 7 |
+
model_name = "microsoft/phi-3-mini-4k-instruct" # Smaller model for memory constraints
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
| 10 |
+
# Load model with optimizations
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
model_name,
|
| 13 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
|
|
|
| 16 |
)
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
|
| 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 |
+
# Simple safety check
|
| 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.input_ids,
|
| 40 |
+
max_new_tokens=256,
|
| 41 |
+
temperature=0.7,
|
|
|
|
| 42 |
do_sample=True,
|
| 43 |
pad_token_id=tokenizer.eos_token_id
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# Decode and return
|
| 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 |
+
fn=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 |
+
description="Safe AI project assistant for students"
|
| 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=True,
|
| 72 |
+
share=False
|
| 73 |
+
)
|