Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
pipe = pipeline(
|
| 6 |
"text-generation",
|
| 7 |
model="Qwen/Qwen2.5-0.5B-Instruct",
|
|
@@ -13,21 +13,36 @@ def chat(message, history):
|
|
| 13 |
if not history:
|
| 14 |
history = []
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
response = pipe(
|
| 21 |
-
|
| 22 |
max_new_tokens=1024,
|
| 23 |
temperature=0.8,
|
| 24 |
-
do_sample=True,
|
| 25 |
top_p=0.9,
|
|
|
|
| 26 |
return_full_text=False
|
| 27 |
)
|
|
|
|
| 28 |
reply = response[0]['generated_text'].strip()
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
-
reply = f"Error: {str(e)}"
|
| 31 |
|
| 32 |
history.append([message, reply])
|
| 33 |
return history
|
|
@@ -37,7 +52,7 @@ with gr.Blocks(title="Holodeck AI") as demo:
|
|
| 37 |
|
| 38 |
chatbot = gr.Chatbot(height=500)
|
| 39 |
msg = gr.Textbox(
|
| 40 |
-
placeholder="Type your message here...",
|
| 41 |
label="Your Message",
|
| 42 |
lines=3
|
| 43 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load the tiny model
|
| 5 |
pipe = pipeline(
|
| 6 |
"text-generation",
|
| 7 |
model="Qwen/Qwen2.5-0.5B-Instruct",
|
|
|
|
| 13 |
if not history:
|
| 14 |
history = []
|
| 15 |
|
| 16 |
+
# Build messages in correct format for Qwen
|
| 17 |
+
messages = [{"role": "system", "content": "You are a helpful, creative assistant good at coding, vibing, and worldbuilding."}]
|
| 18 |
+
|
| 19 |
+
for user_msg, assistant_msg in history:
|
| 20 |
+
messages.append({"role": "user", "content": user_msg})
|
| 21 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 22 |
+
|
| 23 |
+
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
try:
|
| 26 |
+
# Use the model's chat template
|
| 27 |
+
text = pipe.tokenizer.apply_chat_template(
|
| 28 |
+
messages,
|
| 29 |
+
tokenize=False,
|
| 30 |
+
add_generation_prompt=True
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
response = pipe(
|
| 34 |
+
text,
|
| 35 |
max_new_tokens=1024,
|
| 36 |
temperature=0.8,
|
|
|
|
| 37 |
top_p=0.9,
|
| 38 |
+
do_sample=True,
|
| 39 |
return_full_text=False
|
| 40 |
)
|
| 41 |
+
|
| 42 |
reply = response[0]['generated_text'].strip()
|
| 43 |
+
|
| 44 |
except Exception as e:
|
| 45 |
+
reply = f"Error generating response: {str(e)}"
|
| 46 |
|
| 47 |
history.append([message, reply])
|
| 48 |
return history
|
|
|
|
| 52 |
|
| 53 |
chatbot = gr.Chatbot(height=500)
|
| 54 |
msg = gr.Textbox(
|
| 55 |
+
placeholder="Type your message here and click Send...",
|
| 56 |
label="Your Message",
|
| 57 |
lines=3
|
| 58 |
)
|