Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# 1. Initialize the pipeline
|
| 5 |
-
# device_map="auto" automatically loads the model onto the GPU if available
|
| 6 |
print("Loading model...")
|
|
|
|
| 7 |
pipe = pipeline(
|
| 8 |
"image-text-to-text",
|
| 9 |
model="huihui-ai/Huihui-Qwen3.5-2B-abliterated",
|
|
@@ -12,7 +11,7 @@ pipe = pipeline(
|
|
| 12 |
print("Model loaded successfully!")
|
| 13 |
|
| 14 |
def respond(message, history):
|
| 15 |
-
#
|
| 16 |
messages =[]
|
| 17 |
|
| 18 |
for user_msg, assistant_msg in history:
|
|
@@ -24,38 +23,35 @@ def respond(message, history):
|
|
| 24 |
if assistant_msg:
|
| 25 |
messages.append({
|
| 26 |
"role": "assistant",
|
| 27 |
-
"content":
|
| 28 |
})
|
| 29 |
|
| 30 |
-
#
|
| 31 |
messages.append({
|
| 32 |
"role": "user",
|
| 33 |
"content": [{"type": "text", "text": message}]
|
| 34 |
})
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
#
|
| 38 |
-
response = pipe(text=messages, max_new_tokens=
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
# The pipeline returns the full conversation history.
|
| 42 |
-
# The newly generated response is ALWAYS the last item in the list [-1]
|
| 43 |
generated_messages = response[0]['generated_text']
|
| 44 |
assistant_reply = generated_messages[-1]['content']
|
| 45 |
|
| 46 |
-
#
|
| 47 |
if isinstance(assistant_reply, list):
|
| 48 |
reply_text = "".join([item.get("text", "") for item in assistant_reply if item.get("type") == "text"])
|
| 49 |
return reply_text
|
| 50 |
else:
|
| 51 |
return assistant_reply
|
| 52 |
|
| 53 |
-
#
|
| 54 |
demo = gr.ChatInterface(
|
| 55 |
fn=respond,
|
| 56 |
title="Huihui Qwen3.5 Chatbot",
|
| 57 |
description="A conversational AI using the `huihui-ai/Huihui-Qwen3.5-2B-abliterated` model.",
|
| 58 |
-
theme="soft",
|
| 59 |
examples=["How to make a book?", "Explain quantum physics to a child.", "Write a poem about AI."]
|
| 60 |
)
|
| 61 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
|
|
|
|
|
| 4 |
print("Loading model...")
|
| 5 |
+
# device_map="auto" automatically loads the model onto the GPU if available
|
| 6 |
pipe = pipeline(
|
| 7 |
"image-text-to-text",
|
| 8 |
model="huihui-ai/Huihui-Qwen3.5-2B-abliterated",
|
|
|
|
| 11 |
print("Model loaded successfully!")
|
| 12 |
|
| 13 |
def respond(message, history):
|
| 14 |
+
# Reconstruct the message history in the format expected by the model
|
| 15 |
messages =[]
|
| 16 |
|
| 17 |
for user_msg, assistant_msg in history:
|
|
|
|
| 23 |
if assistant_msg:
|
| 24 |
messages.append({
|
| 25 |
"role": "assistant",
|
| 26 |
+
"content":[{"type": "text", "text": assistant_msg}]
|
| 27 |
})
|
| 28 |
|
| 29 |
+
# Add the current user message
|
| 30 |
messages.append({
|
| 31 |
"role": "user",
|
| 32 |
"content": [{"type": "text", "text": message}]
|
| 33 |
})
|
| 34 |
|
| 35 |
+
# Generate the response
|
| 36 |
+
# FIX: max_new_tokens=2048 prevents the output from cutting off at 250 characters
|
| 37 |
+
response = pipe(text=messages, max_new_tokens=2048)
|
| 38 |
|
| 39 |
+
# Extract the assistant's response dynamically
|
|
|
|
|
|
|
| 40 |
generated_messages = response[0]['generated_text']
|
| 41 |
assistant_reply = generated_messages[-1]['content']
|
| 42 |
|
| 43 |
+
# Return the text properly whether the model outputs a list of dicts or a raw string
|
| 44 |
if isinstance(assistant_reply, list):
|
| 45 |
reply_text = "".join([item.get("text", "") for item in assistant_reply if item.get("type") == "text"])
|
| 46 |
return reply_text
|
| 47 |
else:
|
| 48 |
return assistant_reply
|
| 49 |
|
| 50 |
+
# FIX: Removed `theme="soft"` which was causing the TypeError crash in Gradio 4
|
| 51 |
demo = gr.ChatInterface(
|
| 52 |
fn=respond,
|
| 53 |
title="Huihui Qwen3.5 Chatbot",
|
| 54 |
description="A conversational AI using the `huihui-ai/Huihui-Qwen3.5-2B-abliterated` model.",
|
|
|
|
| 55 |
examples=["How to make a book?", "Explain quantum physics to a child.", "Write a poem about AI."]
|
| 56 |
)
|
| 57 |
|