Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from threading import Thread
|
| 5 |
import random
|
| 6 |
|
| 7 |
-
#
|
| 8 |
model_name = "HuggingFaceH4/zephyr-7b-beta"
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
load_in_4bit=True
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Safety tools 🛡️
|
| 17 |
BLOCKED_WORDS = ["violence", "hate", "gun", "personal"]
|
| 18 |
SAFE_IDEAS = [
|
|
@@ -20,7 +47,11 @@ SAFE_IDEAS = [
|
|
| 20 |
"Code a game about recycling ♻️",
|
| 21 |
"Plan an AI tool for school safety 🚸"
|
| 22 |
]
|
| 23 |
-
safety_checker = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def is_safe(text):
|
| 26 |
text = text.lower()
|
|
@@ -30,14 +61,12 @@ def is_safe(text):
|
|
| 30 |
return not (result["label"] == "toxic" and result["score"] > 0.7)
|
| 31 |
|
| 32 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 33 |
-
# Safety check first 🔒
|
| 34 |
if not is_safe(message):
|
| 35 |
return f"🚫 Let's focus on positive projects! Try: {random.choice(SAFE_IDEAS)}"
|
| 36 |
|
| 37 |
-
# Prepare chat history
|
| 38 |
messages = [{"role": "system", "content": system_message}]
|
| 39 |
|
| 40 |
-
for user_msg, bot_msg in history[-5:]:
|
| 41 |
if user_msg:
|
| 42 |
messages.append({"role": "user", "content": user_msg})
|
| 43 |
if bot_msg:
|
|
@@ -45,7 +74,6 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 45 |
|
| 46 |
messages.append({"role": "user", "content": message})
|
| 47 |
|
| 48 |
-
# Tokenize and prepare streaming
|
| 49 |
inputs = tokenizer.apply_chat_template(
|
| 50 |
messages,
|
| 51 |
return_tensors="pt"
|
|
@@ -60,11 +88,9 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 60 |
"streamer": streamer
|
| 61 |
}
|
| 62 |
|
| 63 |
-
# Start generation in thread
|
| 64 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 65 |
thread.start()
|
| 66 |
|
| 67 |
-
# Stream output
|
| 68 |
partial_message = ""
|
| 69 |
for new_token in streamer:
|
| 70 |
partial_message += new_token
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import (
|
| 4 |
+
AutoModelForCausalLM,
|
| 5 |
+
AutoTokenizer,
|
| 6 |
+
TextIteratorStreamer,
|
| 7 |
+
pipeline,
|
| 8 |
+
BitsAndBytesConfig
|
| 9 |
+
)
|
| 10 |
from threading import Thread
|
| 11 |
import random
|
| 12 |
|
| 13 |
+
# Configuration 🛠️
|
| 14 |
model_name = "HuggingFaceH4/zephyr-7b-beta"
|
| 15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
+
|
| 17 |
+
# Quantization setup
|
| 18 |
+
quantization_config = BitsAndBytesConfig(
|
| 19 |
+
load_in_4bit=True,
|
| 20 |
+
bnb_4bit_quant_type="nf4",
|
| 21 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 22 |
+
bnb_4bit_use_double_quant=True,
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Model loading with fallback
|
| 26 |
+
try:
|
| 27 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 28 |
+
model_name,
|
| 29 |
+
quantization_config=quantization_config if device == "cuda" else None,
|
| 30 |
+
device_map="auto",
|
| 31 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 32 |
+
)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Error loading model with GPU: {e}")
|
| 35 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 36 |
+
model_name,
|
| 37 |
+
device_map="cpu",
|
| 38 |
+
torch_dtype=torch.float32
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 42 |
+
|
| 43 |
# Safety tools 🛡️
|
| 44 |
BLOCKED_WORDS = ["violence", "hate", "gun", "personal"]
|
| 45 |
SAFE_IDEAS = [
|
|
|
|
| 47 |
"Code a game about recycling ♻️",
|
| 48 |
"Plan an AI tool for school safety 🚸"
|
| 49 |
]
|
| 50 |
+
safety_checker = pipeline(
|
| 51 |
+
"text-classification",
|
| 52 |
+
model="unitary/toxic-bert",
|
| 53 |
+
device=0 if device == "cuda" else -1
|
| 54 |
+
)
|
| 55 |
|
| 56 |
def is_safe(text):
|
| 57 |
text = text.lower()
|
|
|
|
| 61 |
return not (result["label"] == "toxic" and result["score"] > 0.7)
|
| 62 |
|
| 63 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
| 64 |
if not is_safe(message):
|
| 65 |
return f"🚫 Let's focus on positive projects! Try: {random.choice(SAFE_IDEAS)}"
|
| 66 |
|
|
|
|
| 67 |
messages = [{"role": "system", "content": system_message}]
|
| 68 |
|
| 69 |
+
for user_msg, bot_msg in history[-5:]:
|
| 70 |
if user_msg:
|
| 71 |
messages.append({"role": "user", "content": user_msg})
|
| 72 |
if bot_msg:
|
|
|
|
| 74 |
|
| 75 |
messages.append({"role": "user", "content": message})
|
| 76 |
|
|
|
|
| 77 |
inputs = tokenizer.apply_chat_template(
|
| 78 |
messages,
|
| 79 |
return_tensors="pt"
|
|
|
|
| 88 |
"streamer": streamer
|
| 89 |
}
|
| 90 |
|
|
|
|
| 91 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 92 |
thread.start()
|
| 93 |
|
|
|
|
| 94 |
partial_message = ""
|
| 95 |
for new_token in streamer:
|
| 96 |
partial_message += new_token
|