Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,13 @@ import torch
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
MODEL_NAME = "Qwen/Qwen2.5-3B-Instruct"
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
-
MODEL_NAME,
|
| 9 |
-
trust_remote_code=True
|
| 10 |
)
|
| 11 |
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -16,6 +18,9 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 16 |
trust_remote_code=True
|
| 17 |
)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
SYSTEM_MESSAGE = {
|
| 20 |
"role": "system",
|
| 21 |
"content": (
|
|
@@ -26,21 +31,23 @@ SYSTEM_MESSAGE = {
|
|
| 26 |
)
|
| 27 |
}
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
def chat(user_input, messages):
|
| 30 |
if not messages:
|
| 31 |
messages = [SYSTEM_MESSAGE]
|
| 32 |
|
| 33 |
-
# Ensure
|
| 34 |
messages = [
|
| 35 |
{"role": m["role"], "content": str(m["content"])}
|
| 36 |
for m in messages
|
| 37 |
]
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"content": str(user_input)
|
| 42 |
-
})
|
| 43 |
|
|
|
|
| 44 |
prompt = tokenizer.apply_chat_template(
|
| 45 |
messages,
|
| 46 |
tokenize=False,
|
|
@@ -49,9 +56,10 @@ def chat(user_input, messages):
|
|
| 49 |
|
| 50 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 51 |
|
|
|
|
| 52 |
outputs = model.generate(
|
| 53 |
**inputs,
|
| 54 |
-
max_new_tokens=384, #
|
| 55 |
temperature=0.7,
|
| 56 |
top_p=0.9,
|
| 57 |
do_sample=True
|
|
@@ -62,22 +70,20 @@ def chat(user_input, messages):
|
|
| 62 |
skip_special_tokens=True
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
"content": response
|
| 68 |
-
})
|
| 69 |
|
| 70 |
return messages, ""
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
gr.Markdown("## π€ Qwen 2.5 β 3B Chatbot (English & Kiswahili)")
|
| 74 |
|
| 75 |
-
chatbot = gr.Chatbot()
|
| 76 |
-
msg = gr.Textbox(
|
| 77 |
-
|
| 78 |
-
autofocus=True
|
| 79 |
-
)
|
| 80 |
-
state = gr.State([])
|
| 81 |
|
| 82 |
msg.submit(
|
| 83 |
chat,
|
|
@@ -85,8 +91,9 @@ with gr.Blocks() as demo:
|
|
| 85 |
outputs=[chatbot, state]
|
| 86 |
)
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.launch(
|
| 92 |
server_name="0.0.0.0",
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
+
# ----------------------------
|
| 6 |
+
# Model configuration
|
| 7 |
+
# ----------------------------
|
| 8 |
MODEL_NAME = "Qwen/Qwen2.5-3B-Instruct"
|
| 9 |
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 11 |
+
MODEL_NAME, trust_remote_code=True
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 18 |
trust_remote_code=True
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# ----------------------------
|
| 22 |
+
# System prompt for CBE marking
|
| 23 |
+
# ----------------------------
|
| 24 |
SYSTEM_MESSAGE = {
|
| 25 |
"role": "system",
|
| 26 |
"content": (
|
|
|
|
| 31 |
)
|
| 32 |
}
|
| 33 |
|
| 34 |
+
# ----------------------------
|
| 35 |
+
# Chat function
|
| 36 |
+
# ----------------------------
|
| 37 |
def chat(user_input, messages):
|
| 38 |
if not messages:
|
| 39 |
messages = [SYSTEM_MESSAGE]
|
| 40 |
|
| 41 |
+
# Ensure all contents are strings
|
| 42 |
messages = [
|
| 43 |
{"role": m["role"], "content": str(m["content"])}
|
| 44 |
for m in messages
|
| 45 |
]
|
| 46 |
|
| 47 |
+
# Add user input
|
| 48 |
+
messages.append({"role": "user", "content": str(user_input)})
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Create Qwen prompt
|
| 51 |
prompt = tokenizer.apply_chat_template(
|
| 52 |
messages,
|
| 53 |
tokenize=False,
|
|
|
|
| 56 |
|
| 57 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 58 |
|
| 59 |
+
# Generate response
|
| 60 |
outputs = model.generate(
|
| 61 |
**inputs,
|
| 62 |
+
max_new_tokens=384, # safe for 3B
|
| 63 |
temperature=0.7,
|
| 64 |
top_p=0.9,
|
| 65 |
do_sample=True
|
|
|
|
| 70 |
skip_special_tokens=True
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# Append assistant response
|
| 74 |
+
messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
| 75 |
|
| 76 |
return messages, ""
|
| 77 |
|
| 78 |
+
# ----------------------------
|
| 79 |
+
# Gradio UI
|
| 80 |
+
# ----------------------------
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
gr.Markdown("## π€ Qwen 2.5 β 3B Chatbot (English & Kiswahili)")
|
| 83 |
|
| 84 |
+
chatbot = gr.Chatbot() # old Gradio safe
|
| 85 |
+
msg = gr.Textbox(label="Your message / Ujumbe wako", autofocus=True)
|
| 86 |
+
state = gr.State([]) # keeps messages
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
msg.submit(
|
| 89 |
chat,
|
|
|
|
| 91 |
outputs=[chatbot, state]
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# ----------------------------
|
| 95 |
+
# Launch safely on HF Spaces
|
| 96 |
+
# ----------------------------
|
| 97 |
if __name__ == "__main__":
|
| 98 |
demo.launch(
|
| 99 |
server_name="0.0.0.0",
|