Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,83 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
import torch
|
| 4 |
import time
|
| 5 |
|
| 6 |
-
#
|
| 7 |
@torch.no_grad()
|
| 8 |
def load_model():
|
| 9 |
-
print("Loading model...")
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
print("Model loaded successfully!")
|
| 18 |
-
return
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
|
| 23 |
-
def
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
|
| 39 |
# Generate response
|
| 40 |
-
|
| 41 |
-
outputs = model.generate(
|
| 42 |
-
inputs,
|
| 43 |
-
max_length=inputs.shape[1] + max_length,
|
| 44 |
-
temperature=temperature,
|
| 45 |
-
do_sample=True,
|
| 46 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 47 |
-
top_p=0.9,
|
| 48 |
-
repetition_penalty=1.1,
|
| 49 |
-
early_stopping=True
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
response = response[len(full_prompt):].strip()
|
| 57 |
|
| 58 |
-
# Clean up
|
| 59 |
-
if "
|
| 60 |
-
response = response.split("
|
| 61 |
|
| 62 |
return response
|
| 63 |
|
|
@@ -85,52 +105,115 @@ def clear_chat():
|
|
| 85 |
"""
|
| 86 |
return []
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# Custom CSS for beautiful styling
|
| 89 |
custom_css = """
|
| 90 |
-
#chatbot {
|
| 91 |
-
background-color: #f8f9fa;
|
| 92 |
-
border-radius: 10px;
|
| 93 |
-
padding: 20px;
|
| 94 |
-
height: 500px;
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
.gradio-container {
|
| 98 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
|
|
| 99 |
}
|
| 100 |
|
| 101 |
.contain {
|
| 102 |
background-color: white;
|
| 103 |
-
border-radius:
|
| 104 |
-
padding:
|
| 105 |
-
box-shadow: 0
|
|
|
|
| 106 |
}
|
| 107 |
|
| 108 |
.dark .contain {
|
| 109 |
background-color: #1e1e1e;
|
| 110 |
}
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
.gr-button {
|
| 113 |
background: linear-gradient(45deg, #FF6B6B, #FF8E53) !important;
|
| 114 |
border: none !important;
|
| 115 |
color: white !important;
|
| 116 |
border-radius: 25px !important;
|
| 117 |
-
padding:
|
|
|
|
|
|
|
| 118 |
}
|
| 119 |
|
| 120 |
.gr-button:hover {
|
| 121 |
transform: translateY(-2px);
|
| 122 |
-
box-shadow: 0
|
| 123 |
}
|
| 124 |
|
| 125 |
-
.
|
| 126 |
background: white;
|
| 127 |
-
padding:
|
| 128 |
-
border-radius:
|
| 129 |
-
|
|
|
|
| 130 |
}
|
| 131 |
|
| 132 |
-
.dark .
|
| 133 |
background: #2d2d2d;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
}
|
| 135 |
"""
|
| 136 |
|
|
@@ -138,63 +221,76 @@ custom_css = """
|
|
| 138 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 139 |
gr.Markdown(
|
| 140 |
"""
|
| 141 |
-
#
|
| 142 |
-
**Chat with
|
| 143 |
-
"""
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
-
with gr.Row():
|
| 147 |
with gr.Column(scale=3):
|
| 148 |
chatbot = gr.Chatbot(
|
| 149 |
-
label="Chat
|
| 150 |
elem_id="chatbot",
|
| 151 |
show_copy_button=True,
|
| 152 |
-
avatar_images=("π€", "π€")
|
|
|
|
|
|
|
| 153 |
)
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
msg = gr.Textbox(
|
| 157 |
-
label="
|
| 158 |
-
placeholder="
|
| 159 |
lines=2,
|
| 160 |
-
scale=4
|
|
|
|
| 161 |
)
|
| 162 |
-
|
|
|
|
| 163 |
|
| 164 |
with gr.Row():
|
| 165 |
-
clear_btn = gr.Button("Clear Chat
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
with gr.Column(scale=1):
|
| 168 |
-
gr.
|
| 169 |
-
|
| 170 |
-
|
| 171 |
temperature = gr.Slider(
|
| 172 |
minimum=0.1,
|
| 173 |
-
maximum=1.
|
| 174 |
value=0.7,
|
| 175 |
step=0.1,
|
| 176 |
-
label="Temperature",
|
| 177 |
-
info="
|
| 178 |
)
|
| 179 |
|
| 180 |
max_length = gr.Slider(
|
| 181 |
-
minimum=
|
| 182 |
-
maximum=
|
| 183 |
-
value=
|
| 184 |
-
step=
|
| 185 |
-
label="Max Response Length",
|
| 186 |
-
info="
|
| 187 |
)
|
| 188 |
|
| 189 |
-
gr.
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
# Event handlers
|
| 200 |
submit_event = msg.submit(
|
|
@@ -214,14 +310,29 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 214 |
outputs=[chatbot]
|
| 215 |
)
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
outputs=[
|
| 221 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
if __name__ == "__main__":
|
| 224 |
demo.launch(
|
| 225 |
server_name="0.0.0.0",
|
| 226 |
-
share=False
|
|
|
|
| 227 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
import torch
|
| 4 |
import time
|
| 5 |
|
| 6 |
+
# Initialize the pipeline
|
| 7 |
@torch.no_grad()
|
| 8 |
def load_model():
|
| 9 |
+
print("Loading Qwen3-0.6B model...")
|
| 10 |
+
pipe = pipeline(
|
| 11 |
+
"text-generation",
|
| 12 |
+
model="Qwen/Qwen3-0.6B",
|
| 13 |
+
torch_dtype=torch.float16,
|
| 14 |
+
device_map="auto",
|
| 15 |
+
trust_remote_code=True
|
| 16 |
+
)
|
| 17 |
print("Model loaded successfully!")
|
| 18 |
+
return pipe
|
| 19 |
|
| 20 |
+
# Load the model
|
| 21 |
+
pipe = load_model()
|
| 22 |
|
| 23 |
+
def format_chat_template(messages):
|
| 24 |
"""
|
| 25 |
+
Format messages using the model's chat template
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
+
# Use the model's built-in chat template
|
| 29 |
+
formatted_prompt = pipe.tokenizer.apply_chat_template(
|
| 30 |
+
messages,
|
| 31 |
+
tokenize=False,
|
| 32 |
+
add_generation_prompt=True
|
| 33 |
+
)
|
| 34 |
+
return formatted_prompt
|
| 35 |
+
except Exception as e:
|
| 36 |
+
# Fallback formatting
|
| 37 |
+
conversation = ""
|
| 38 |
+
for msg in messages:
|
| 39 |
+
if msg["role"] == "user":
|
| 40 |
+
conversation += f"User: {msg['content']}\n\nAssistant:"
|
| 41 |
+
elif msg["role"] == "assistant":
|
| 42 |
+
conversation += f" {msg['content']}\n\n"
|
| 43 |
+
return conversation
|
| 44 |
+
|
| 45 |
+
def generate_response(message, history, temperature=0.7, max_length=512):
|
| 46 |
+
"""
|
| 47 |
+
Generate a response using Qwen3-0.6B
|
| 48 |
+
"""
|
| 49 |
+
try:
|
| 50 |
+
# Convert Gradio history to messages format
|
| 51 |
+
messages = []
|
| 52 |
+
for human_msg, assistant_msg in history:
|
| 53 |
+
messages.extend([
|
| 54 |
+
{"role": "user", "content": human_msg},
|
| 55 |
+
{"role": "assistant", "content": assistant_msg}
|
| 56 |
+
])
|
| 57 |
|
| 58 |
+
# Add current message
|
| 59 |
+
messages.append({"role": "user", "content": message})
|
| 60 |
|
| 61 |
# Generate response
|
| 62 |
+
formatted_prompt = format_chat_template(messages)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
outputs = pipe(
|
| 65 |
+
formatted_prompt,
|
| 66 |
+
max_new_tokens=max_length,
|
| 67 |
+
temperature=temperature,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
top_p=0.9,
|
| 70 |
+
repetition_penalty=1.1,
|
| 71 |
+
pad_token_id=pipe.tokenizer.eos_token_id,
|
| 72 |
+
eos_token_id=pipe.tokenizer.eos_token_id,
|
| 73 |
+
return_full_text=False # Only return the generated part
|
| 74 |
+
)
|
| 75 |
|
| 76 |
+
response = outputs[0]['generated_text'].strip()
|
|
|
|
| 77 |
|
| 78 |
+
# Clean up response
|
| 79 |
+
if "User:" in response:
|
| 80 |
+
response = response.split("User:")[0].strip()
|
| 81 |
|
| 82 |
return response
|
| 83 |
|
|
|
|
| 105 |
"""
|
| 106 |
return []
|
| 107 |
|
| 108 |
+
def retry_last_response(history, temperature, max_length):
|
| 109 |
+
"""
|
| 110 |
+
Retry the last user message
|
| 111 |
+
"""
|
| 112 |
+
if not history:
|
| 113 |
+
return history
|
| 114 |
+
|
| 115 |
+
# Remove the last assistant response
|
| 116 |
+
last_conversation = history[:-1]
|
| 117 |
+
last_user_message = history[-1][0]
|
| 118 |
+
|
| 119 |
+
# Regenerate response
|
| 120 |
+
bot_response = generate_response(last_user_message, last_conversation, temperature, max_length)
|
| 121 |
+
|
| 122 |
+
# Update history
|
| 123 |
+
last_conversation.append([last_user_message, bot_response])
|
| 124 |
+
|
| 125 |
+
return last_conversation
|
| 126 |
+
|
| 127 |
# Custom CSS for beautiful styling
|
| 128 |
custom_css = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
.gradio-container {
|
| 130 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 131 |
+
min-height: 100vh;
|
| 132 |
}
|
| 133 |
|
| 134 |
.contain {
|
| 135 |
background-color: white;
|
| 136 |
+
border-radius: 20px;
|
| 137 |
+
padding: 30px;
|
| 138 |
+
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
|
| 139 |
+
margin: 20px;
|
| 140 |
}
|
| 141 |
|
| 142 |
.dark .contain {
|
| 143 |
background-color: #1e1e1e;
|
| 144 |
}
|
| 145 |
|
| 146 |
+
#chatbot {
|
| 147 |
+
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
| 148 |
+
border-radius: 15px;
|
| 149 |
+
padding: 25px;
|
| 150 |
+
height: 500px;
|
| 151 |
+
border: none;
|
| 152 |
+
box-shadow: inset 0 2px 10px rgba(0,0,0,0.1);
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
.dark #chatbot {
|
| 156 |
+
background: linear-gradient(135deg, #2d3748 0%, #4a5568 100%);
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
.gr-button {
|
| 160 |
background: linear-gradient(45deg, #FF6B6B, #FF8E53) !important;
|
| 161 |
border: none !important;
|
| 162 |
color: white !important;
|
| 163 |
border-radius: 25px !important;
|
| 164 |
+
padding: 12px 25px !important;
|
| 165 |
+
font-weight: 600 !important;
|
| 166 |
+
transition: all 0.3s ease !important;
|
| 167 |
}
|
| 168 |
|
| 169 |
.gr-button:hover {
|
| 170 |
transform: translateY(-2px);
|
| 171 |
+
box-shadow: 0 8px 20px rgba(255,107,107,0.4);
|
| 172 |
}
|
| 173 |
|
| 174 |
+
.control-panel {
|
| 175 |
background: white;
|
| 176 |
+
padding: 25px;
|
| 177 |
+
border-radius: 15px;
|
| 178 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
|
| 179 |
+
border: 1px solid #e1e5e9;
|
| 180 |
}
|
| 181 |
|
| 182 |
+
.dark .control-panel {
|
| 183 |
background: #2d2d2d;
|
| 184 |
+
border-color: #404040;
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
.slider-container {
|
| 188 |
+
margin: 15px 0;
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
.model-info {
|
| 192 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 193 |
+
color: white;
|
| 194 |
+
padding: 20px;
|
| 195 |
+
border-radius: 15px;
|
| 196 |
+
margin: 15px 0;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
.textbox {
|
| 200 |
+
border-radius: 15px !important;
|
| 201 |
+
padding: 15px !important;
|
| 202 |
+
border: 2px solid #e1e5e9 !important;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
.textbox:focus {
|
| 206 |
+
border-color: #667eea !important;
|
| 207 |
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
.markdown-container h1 {
|
| 211 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 212 |
+
-webkit-background-clip: text;
|
| 213 |
+
-webkit-text-fill-color: transparent;
|
| 214 |
+
background-clip: text;
|
| 215 |
+
text-fill-color: transparent;
|
| 216 |
+
font-weight: 700 !important;
|
| 217 |
}
|
| 218 |
"""
|
| 219 |
|
|
|
|
| 221 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 222 |
gr.Markdown(
|
| 223 |
"""
|
| 224 |
+
# π Qwen3-0.6B Chatbot
|
| 225 |
+
**Chat with Alibaba's advanced Qwen3-0.6B model!** Experience more fluent and intelligent conversations.
|
| 226 |
+
""",
|
| 227 |
+
elem_classes="markdown-container"
|
| 228 |
)
|
| 229 |
|
| 230 |
+
with gr.Row(equal_height=False):
|
| 231 |
with gr.Column(scale=3):
|
| 232 |
chatbot = gr.Chatbot(
|
| 233 |
+
label="π¬ Chat with Qwen3",
|
| 234 |
elem_id="chatbot",
|
| 235 |
show_copy_button=True,
|
| 236 |
+
avatar_images=("π€", "π€"),
|
| 237 |
+
height=500,
|
| 238 |
+
bubble_full_width=False
|
| 239 |
)
|
| 240 |
|
| 241 |
with gr.Row():
|
| 242 |
msg = gr.Textbox(
|
| 243 |
+
label="π Your message",
|
| 244 |
+
placeholder="Ask me anything...",
|
| 245 |
lines=2,
|
| 246 |
+
scale=4,
|
| 247 |
+
container=False
|
| 248 |
)
|
| 249 |
+
with gr.Column(scale=1):
|
| 250 |
+
submit_btn = gr.Button("Send π", size="lg")
|
| 251 |
|
| 252 |
with gr.Row():
|
| 253 |
+
clear_btn = gr.Button("ποΈ Clear Chat", size="sm")
|
| 254 |
+
retry_btn = gr.Button("π Retry", size="sm")
|
| 255 |
+
gr.HTML("""<div style="text-align: center; color: #666; font-size: 12px;">
|
| 256 |
+
Pro tip: Use Shift+Enter for new line, Enter to send
|
| 257 |
+
</div>""")
|
| 258 |
|
| 259 |
with gr.Column(scale=1):
|
| 260 |
+
with gr.Group(elem_classes="control-panel"):
|
| 261 |
+
gr.Markdown("### βοΈ Generation Settings")
|
| 262 |
+
|
| 263 |
temperature = gr.Slider(
|
| 264 |
minimum=0.1,
|
| 265 |
+
maximum=1.5,
|
| 266 |
value=0.7,
|
| 267 |
step=0.1,
|
| 268 |
+
label="ποΈ Temperature",
|
| 269 |
+
info="Lower = more deterministic, Higher = more creative"
|
| 270 |
)
|
| 271 |
|
| 272 |
max_length = gr.Slider(
|
| 273 |
+
minimum=64,
|
| 274 |
+
maximum=1024,
|
| 275 |
+
value=512,
|
| 276 |
+
step=64,
|
| 277 |
+
label="π Max Response Length",
|
| 278 |
+
info="Tokens in generated response"
|
| 279 |
)
|
| 280 |
|
| 281 |
+
with gr.Group(elem_classes="model-info"):
|
| 282 |
+
gr.Markdown("### βΉοΈ Model Info")
|
| 283 |
+
gr.Markdown("""
|
| 284 |
+
**Model:** Qwen3-0.6B
|
| 285 |
+
**Provider:** Alibaba
|
| 286 |
+
**Context:** 128K tokens
|
| 287 |
+
**Language:** Multilingual
|
| 288 |
+
|
| 289 |
+
π‘ **Tips:**
|
| 290 |
+
- Be specific and clear
|
| 291 |
+
- Use proper punctuation
|
| 292 |
+
- Adjust temperature for creativity
|
| 293 |
+
""")
|
| 294 |
|
| 295 |
# Event handlers
|
| 296 |
submit_event = msg.submit(
|
|
|
|
| 310 |
outputs=[chatbot]
|
| 311 |
)
|
| 312 |
|
| 313 |
+
retry_btn.click(
|
| 314 |
+
retry_last_response,
|
| 315 |
+
inputs=[chatbot, temperature, max_length],
|
| 316 |
+
outputs=[chatbot]
|
| 317 |
)
|
| 318 |
+
|
| 319 |
+
# Additional examples
|
| 320 |
+
with gr.Accordion("π‘ Example Prompts", open=False):
|
| 321 |
+
gr.Examples(
|
| 322 |
+
examples=[
|
| 323 |
+
"Explain quantum computing in simple terms",
|
| 324 |
+
"Write a short poem about artificial intelligence",
|
| 325 |
+
"What are the benefits of renewable energy?",
|
| 326 |
+
"How do I learn programming effectively?",
|
| 327 |
+
"Tell me an interesting fact about space"
|
| 328 |
+
],
|
| 329 |
+
inputs=msg,
|
| 330 |
+
label="Click any example to try it out!"
|
| 331 |
+
)
|
| 332 |
|
| 333 |
if __name__ == "__main__":
|
| 334 |
demo.launch(
|
| 335 |
server_name="0.0.0.0",
|
| 336 |
+
share=False,
|
| 337 |
+
show_error=True
|
| 338 |
)
|