Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,24 +28,22 @@ tokenizer, model = load_model()
|
|
| 28 |
|
| 29 |
def remove_think_tags(text):
|
| 30 |
"""
|
| 31 |
-
Remove <think>...</think> tags from text
|
| 32 |
"""
|
| 33 |
cleaned_text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
|
| 34 |
return cleaned_text.strip()
|
| 35 |
|
| 36 |
def generate_response(message, history, temperature=0.7, max_length=256):
|
| 37 |
"""
|
| 38 |
-
Generate a response using Qwen3-0.6B
|
| 39 |
"""
|
| 40 |
if tokenizer is None or model is None:
|
| 41 |
return "β οΈ Model is not loaded properly. Please check the console logs."
|
| 42 |
|
| 43 |
try:
|
| 44 |
-
# Convert history to messages format
|
| 45 |
messages = []
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
for human_msg, assistant_msg in recent_history:
|
| 49 |
messages.extend([
|
| 50 |
{"role": "user", "content": human_msg},
|
| 51 |
{"role": "assistant", "content": assistant_msg}
|
|
@@ -54,7 +52,7 @@ def generate_response(message, history, temperature=0.7, max_length=256):
|
|
| 54 |
# Add current message
|
| 55 |
messages.append({"role": "user", "content": message})
|
| 56 |
|
| 57 |
-
# Apply chat template
|
| 58 |
inputs = tokenizer.apply_chat_template(
|
| 59 |
messages,
|
| 60 |
add_generation_prompt=True,
|
|
@@ -93,22 +91,22 @@ def generate_response(message, history, temperature=0.7, max_length=256):
|
|
| 93 |
|
| 94 |
def chat_interface(message, history, temperature, max_length):
|
| 95 |
"""
|
| 96 |
-
Main chat interface function
|
| 97 |
"""
|
| 98 |
if not message or not message.strip():
|
| 99 |
return "", history or []
|
| 100 |
|
| 101 |
-
# Generate response
|
| 102 |
bot_response = generate_response(message, history or [], temperature, max_length)
|
| 103 |
|
| 104 |
-
# Update history
|
| 105 |
new_history = (history or []) + [[message, bot_response]]
|
| 106 |
|
| 107 |
return "", new_history
|
| 108 |
|
| 109 |
def clear_chat():
|
| 110 |
"""
|
| 111 |
-
Clear the chat history
|
| 112 |
"""
|
| 113 |
return []
|
| 114 |
|
|
@@ -131,334 +129,151 @@ def retry_last_response(history, temperature, max_length):
|
|
| 131 |
|
| 132 |
return new_history
|
| 133 |
|
| 134 |
-
#
|
| 135 |
custom_css = """
|
| 136 |
.gradio-container {
|
| 137 |
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
|
| 138 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 139 |
min-height: 100vh;
|
| 140 |
-
padding:
|
| 141 |
-
margin: 0;
|
| 142 |
}
|
| 143 |
|
| 144 |
.main-container {
|
| 145 |
-
max-width:
|
| 146 |
margin: 0 auto;
|
| 147 |
background: white;
|
| 148 |
-
border-radius:
|
| 149 |
-
box-shadow: 0
|
| 150 |
-
|
| 151 |
-
display: flex;
|
| 152 |
-
flex-direction: column;
|
| 153 |
}
|
| 154 |
|
| 155 |
.header {
|
| 156 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 157 |
color: white;
|
| 158 |
-
padding:
|
| 159 |
text-align: center;
|
| 160 |
-
position: relative;
|
| 161 |
-
overflow: hidden;
|
| 162 |
-
}
|
| 163 |
-
|
| 164 |
-
.header::before {
|
| 165 |
-
content: '';
|
| 166 |
-
position: absolute;
|
| 167 |
-
top: 0;
|
| 168 |
-
left: 0;
|
| 169 |
-
right: 0;
|
| 170 |
-
bottom: 0;
|
| 171 |
-
background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 100" opacity="0.1"><polygon points="0,0 1000,100 1000,0" fill="white"/></svg>');
|
| 172 |
-
background-size: cover;
|
| 173 |
}
|
| 174 |
|
| 175 |
.header h1 {
|
| 176 |
margin: 0;
|
| 177 |
-
font-size:
|
| 178 |
-
font-weight:
|
| 179 |
-
position: relative;
|
| 180 |
-
text-shadow: 0 2px 10px rgba(0,0,0,0.3);
|
| 181 |
}
|
| 182 |
|
| 183 |
.header p {
|
| 184 |
-
margin:
|
| 185 |
-
opacity: 0.
|
| 186 |
-
font-size: 1.
|
| 187 |
-
font-weight: 300;
|
| 188 |
-
position: relative;
|
| 189 |
-
max-width: 600px;
|
| 190 |
-
margin-left: auto;
|
| 191 |
-
margin-right: auto;
|
| 192 |
}
|
| 193 |
|
| 194 |
-
.
|
| 195 |
-
flex: 1;
|
| 196 |
display: flex;
|
| 197 |
-
|
| 198 |
-
min-height: 70vh;
|
| 199 |
}
|
| 200 |
|
| 201 |
-
.chat-
|
| 202 |
-
flex:
|
| 203 |
display: flex;
|
| 204 |
flex-direction: column;
|
| 205 |
-
background: white;
|
| 206 |
-
position: relative;
|
| 207 |
}
|
| 208 |
|
| 209 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
flex: 1;
|
|
|
|
|
|
|
| 211 |
min-height: 500px;
|
| 212 |
-
max-height: 65vh;
|
| 213 |
-
border-bottom: 1px solid #eef2f7;
|
| 214 |
}
|
| 215 |
|
| 216 |
#chatbot {
|
|
|
|
| 217 |
min-height: 500px !important;
|
| 218 |
-
max-height: 65vh !important;
|
| 219 |
border: none !important;
|
| 220 |
background: white !important;
|
| 221 |
-
padding:
|
| 222 |
margin: 0 !important;
|
| 223 |
-
border-radius: 0 !important;
|
| 224 |
-
overflow-y: auto !important;
|
| 225 |
}
|
| 226 |
|
| 227 |
#chatbot .message {
|
| 228 |
-
padding: 20px
|
| 229 |
-
margin:
|
| 230 |
-
border-radius:
|
| 231 |
-
max-width:
|
| 232 |
-
line-height: 1.6 !important;
|
| 233 |
-
font-size: 16px !important;
|
| 234 |
-
box-shadow: 0 2px 10px rgba(0,0,0,0.08) !important;
|
| 235 |
-
border: 1px solid rgba(0,0,0,0.05) !important;
|
| 236 |
}
|
| 237 |
|
| 238 |
#chatbot .user-message {
|
| 239 |
-
background:
|
| 240 |
color: white !important;
|
| 241 |
margin-left: auto !important;
|
| 242 |
-
margin-right: 0 !important;
|
| 243 |
}
|
| 244 |
|
| 245 |
#chatbot .bot-message {
|
| 246 |
-
background:
|
| 247 |
-
color: #
|
| 248 |
-
margin-left: 0 !important;
|
| 249 |
margin-right: auto !important;
|
| 250 |
-
border: 1px solid #e2e8f0 !important;
|
| 251 |
-
}
|
| 252 |
-
|
| 253 |
-
.input-section {
|
| 254 |
-
background: #fafbfc;
|
| 255 |
-
padding: 30px;
|
| 256 |
-
border-top: 1px solid #eef2f7;
|
| 257 |
}
|
| 258 |
|
| 259 |
.input-container {
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
.controls-section {
|
| 265 |
-
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
|
| 266 |
-
padding: 40px 30px;
|
| 267 |
-
border-top: 1px solid #e2e8f0;
|
| 268 |
-
}
|
| 269 |
-
|
| 270 |
-
.controls-container {
|
| 271 |
-
max-width: 1200px;
|
| 272 |
-
margin: 0 auto;
|
| 273 |
-
}
|
| 274 |
-
|
| 275 |
-
.controls-grid {
|
| 276 |
-
display: grid;
|
| 277 |
-
grid-template-columns: 1fr 1fr;
|
| 278 |
-
gap: 30px;
|
| 279 |
-
margin-bottom: 30px;
|
| 280 |
}
|
| 281 |
|
| 282 |
.control-panel {
|
| 283 |
background: white;
|
| 284 |
-
padding:
|
| 285 |
-
border-radius:
|
| 286 |
-
|
| 287 |
-
|
| 288 |
}
|
| 289 |
|
| 290 |
-
.model-info
|
| 291 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 292 |
color: white;
|
| 293 |
-
padding:
|
| 294 |
-
border-radius:
|
| 295 |
-
box-shadow: 0 10px 30px rgba(102, 126, 234, 0.3);
|
| 296 |
-
position: relative;
|
| 297 |
-
overflow: hidden;
|
| 298 |
-
}
|
| 299 |
-
|
| 300 |
-
.model-info-panel::before {
|
| 301 |
-
content: '';
|
| 302 |
-
position: absolute;
|
| 303 |
-
top: 0;
|
| 304 |
-
right: 0;
|
| 305 |
-
width: 100px;
|
| 306 |
-
height: 100px;
|
| 307 |
-
background: rgba(255,255,255,0.1);
|
| 308 |
-
border-radius: 50%;
|
| 309 |
-
transform: translate(30px, -30px);
|
| 310 |
-
}
|
| 311 |
-
|
| 312 |
-
.control-group {
|
| 313 |
-
margin-bottom: 25px;
|
| 314 |
-
}
|
| 315 |
-
|
| 316 |
-
.control-group:last-child {
|
| 317 |
-
margin-bottom: 0;
|
| 318 |
-
}
|
| 319 |
-
|
| 320 |
-
.control-label {
|
| 321 |
-
display: block;
|
| 322 |
-
font-weight: 600;
|
| 323 |
-
color: #2d3748;
|
| 324 |
-
margin-bottom: 12px;
|
| 325 |
-
font-size: 16px;
|
| 326 |
-
}
|
| 327 |
-
|
| 328 |
-
.control-info {
|
| 329 |
-
font-size: 14px;
|
| 330 |
-
color: #718096;
|
| 331 |
-
margin-top: 8px;
|
| 332 |
-
line-height: 1.5;
|
| 333 |
}
|
| 334 |
|
| 335 |
.gr-button {
|
| 336 |
-
background: linear-gradient(
|
| 337 |
border: none !important;
|
| 338 |
color: white !important;
|
| 339 |
-
border-radius:
|
| 340 |
-
padding:
|
| 341 |
font-weight: 600 !important;
|
| 342 |
-
|
| 343 |
-
transition: all 0.3s ease !important;
|
| 344 |
-
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3) !important;
|
| 345 |
-
}
|
| 346 |
-
|
| 347 |
-
.gr-button:hover {
|
| 348 |
-
transform: translateY(-2px) !important;
|
| 349 |
-
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4) !important;
|
| 350 |
}
|
| 351 |
|
| 352 |
.clear-btn {
|
| 353 |
-
background: linear-gradient(
|
| 354 |
-
box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3) !important;
|
| 355 |
}
|
| 356 |
|
| 357 |
.retry-btn {
|
| 358 |
-
background: linear-gradient(
|
| 359 |
-
box-shadow: 0 4px 15px rgba(0, 184, 148, 0.3) !important;
|
| 360 |
}
|
| 361 |
|
| 362 |
.textbox {
|
| 363 |
-
border-radius:
|
| 364 |
-
border: 2px solid #
|
| 365 |
-
padding:
|
| 366 |
font-size: 16px !important;
|
| 367 |
-
background: white !important;
|
| 368 |
-
box-shadow: 0 2px 10px rgba(0,0,0,0.05) !important;
|
| 369 |
}
|
| 370 |
|
| 371 |
.textbox:focus {
|
| 372 |
border-color: #667eea !important;
|
| 373 |
-
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1)
|
| 374 |
-
}
|
| 375 |
-
|
| 376 |
-
.slider-container {
|
| 377 |
-
margin: 20px 0;
|
| 378 |
-
}
|
| 379 |
-
|
| 380 |
-
.model-features {
|
| 381 |
-
margin-top: 20px;
|
| 382 |
-
}
|
| 383 |
-
|
| 384 |
-
.feature-list {
|
| 385 |
-
list-style: none;
|
| 386 |
-
padding: 0;
|
| 387 |
-
margin: 15px 0 0 0;
|
| 388 |
-
}
|
| 389 |
-
|
| 390 |
-
.feature-list li {
|
| 391 |
-
padding: 8px 0;
|
| 392 |
-
position: relative;
|
| 393 |
-
padding-left: 25px;
|
| 394 |
-
}
|
| 395 |
-
|
| 396 |
-
.feature-list li::before {
|
| 397 |
-
content: 'β';
|
| 398 |
-
position: absolute;
|
| 399 |
-
left: 0;
|
| 400 |
-
color: #48bb78;
|
| 401 |
-
font-weight: bold;
|
| 402 |
}
|
| 403 |
|
| 404 |
.examples-panel {
|
| 405 |
background: white;
|
| 406 |
-
padding:
|
| 407 |
-
border-top: 1px solid #
|
| 408 |
-
}
|
| 409 |
-
|
| 410 |
-
.examples-container {
|
| 411 |
-
max-width: 1200px;
|
| 412 |
-
margin: 0 auto;
|
| 413 |
-
}
|
| 414 |
-
|
| 415 |
-
.examples-grid {
|
| 416 |
-
display: grid;
|
| 417 |
-
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| 418 |
-
gap: 15px;
|
| 419 |
-
margin-top: 20px;
|
| 420 |
-
}
|
| 421 |
-
|
| 422 |
-
@media (max-width: 768px) {
|
| 423 |
-
.controls-grid {
|
| 424 |
-
grid-template-columns: 1fr;
|
| 425 |
-
}
|
| 426 |
-
|
| 427 |
-
.header h1 {
|
| 428 |
-
font-size: 2.2em;
|
| 429 |
-
}
|
| 430 |
-
|
| 431 |
-
.header p {
|
| 432 |
-
font-size: 1.1em;
|
| 433 |
-
}
|
| 434 |
-
}
|
| 435 |
-
|
| 436 |
-
.status-indicator {
|
| 437 |
-
display: inline-block;
|
| 438 |
-
width: 12px;
|
| 439 |
-
height: 12px;
|
| 440 |
-
border-radius: 50%;
|
| 441 |
-
margin-right: 8px;
|
| 442 |
-
}
|
| 443 |
-
|
| 444 |
-
.status-online {
|
| 445 |
-
background: #48bb78;
|
| 446 |
-
box-shadow: 0 0 10px #48bb78;
|
| 447 |
-
}
|
| 448 |
-
|
| 449 |
-
.status-offline {
|
| 450 |
-
background: #ed8936;
|
| 451 |
-
box-shadow: 0 0 10px #ed8936;
|
| 452 |
-
}
|
| 453 |
-
|
| 454 |
-
.chat-history-info {
|
| 455 |
-
text-align: center;
|
| 456 |
-
color: #718096;
|
| 457 |
-
font-size: 14px;
|
| 458 |
-
padding: 10px;
|
| 459 |
-
background: #f7fafc;
|
| 460 |
-
border-radius: 10px;
|
| 461 |
-
margin: 10px 0;
|
| 462 |
}
|
| 463 |
"""
|
| 464 |
|
|
@@ -468,16 +283,15 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 468 |
with gr.Column(elem_classes="main-container"):
|
| 469 |
# Header
|
| 470 |
with gr.Column(elem_classes="header"):
|
| 471 |
-
gr.Markdown("#
|
| 472 |
-
gr.Markdown("
|
| 473 |
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
with gr.Column(elem_classes="chat-
|
| 477 |
-
|
| 478 |
-
with gr.Column(elem_classes="chatbot-wrapper"):
|
| 479 |
chatbot = gr.Chatbot(
|
| 480 |
-
value=[
|
| 481 |
label="",
|
| 482 |
elem_id="chatbot",
|
| 483 |
show_copy_button=True,
|
|
@@ -487,139 +301,104 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 487 |
show_label=False
|
| 488 |
)
|
| 489 |
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
)
|
| 502 |
-
with gr.Column(scale=1):
|
| 503 |
-
submit_btn = gr.Button("Send Message π", size="lg")
|
| 504 |
-
|
| 505 |
-
with gr.Row():
|
| 506 |
-
clear_btn = gr.Button("ποΈ Clear Chat", elem_classes="clear-btn")
|
| 507 |
-
retry_btn = gr.Button("π Retry Last", elem_classes="retry-btn")
|
| 508 |
-
gr.HTML("""<div style="flex: 1; text-align: center; color: #666; font-size: 14px; padding: 12px;">
|
| 509 |
-
β¨ Powered by Qwen3-0.6B β’ Context limited to 6 recent messages
|
| 510 |
-
</div>""")
|
| 511 |
-
|
| 512 |
-
# Controls Section (Below Chat)
|
| 513 |
-
with gr.Column(elem_classes="controls-section"):
|
| 514 |
-
with gr.Column(elem_classes="controls-container"):
|
| 515 |
-
with gr.Row(elem_classes="controls-grid"):
|
| 516 |
-
# Settings Panel
|
| 517 |
-
with gr.Column(elem_classes="control-panel"):
|
| 518 |
-
gr.Markdown("### βοΈ Generation Settings")
|
| 519 |
-
|
| 520 |
-
with gr.Column(elem_classes="control-group"):
|
| 521 |
-
gr.Markdown("**ποΈ Temperature**")
|
| 522 |
-
temperature = gr.Slider(
|
| 523 |
-
minimum=0.1,
|
| 524 |
-
maximum=1.5,
|
| 525 |
-
value=0.7,
|
| 526 |
-
step=0.1,
|
| 527 |
-
label="",
|
| 528 |
-
show_label=False,
|
| 529 |
-
info=""
|
| 530 |
-
)
|
| 531 |
-
gr.Markdown("<div class='control-info'>Lower values for more predictable responses, higher values for more creativity</div>")
|
| 532 |
-
|
| 533 |
-
with gr.Column(elem_classes="control-group"):
|
| 534 |
-
gr.Markdown("**π Max Response Length**")
|
| 535 |
-
max_length = gr.Slider(
|
| 536 |
-
minimum=50,
|
| 537 |
-
maximum=1000,
|
| 538 |
-
value=256,
|
| 539 |
-
step=50,
|
| 540 |
-
label="",
|
| 541 |
-
show_label=False,
|
| 542 |
-
info=""
|
| 543 |
-
)
|
| 544 |
-
gr.Markdown("<div class='control-info'>Number of tokens in the generated response (50-1000)</div>")
|
| 545 |
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
gr.
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
|
| 576 |
# Examples Section
|
| 577 |
with gr.Column(elem_classes="examples-panel"):
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
inputs=msg,
|
| 593 |
-
label="",
|
| 594 |
-
examples_per_page=6
|
| 595 |
-
)
|
| 596 |
|
| 597 |
# Event handlers
|
| 598 |
-
def handle_submit(message, history, temp, max_len):
|
| 599 |
-
"""Wrapper function to handle chat submission"""
|
| 600 |
-
if not message or not message.strip():
|
| 601 |
-
return "", history or []
|
| 602 |
-
return chat_interface(message, history, temp, max_len)
|
| 603 |
-
|
| 604 |
submit_event = msg.submit(
|
| 605 |
-
|
| 606 |
inputs=[msg, chatbot, temperature, max_length],
|
| 607 |
outputs=[msg, chatbot]
|
| 608 |
)
|
| 609 |
|
| 610 |
submit_btn.click(
|
| 611 |
-
|
| 612 |
inputs=[msg, chatbot, temperature, max_length],
|
| 613 |
outputs=[msg, chatbot]
|
| 614 |
)
|
| 615 |
|
| 616 |
clear_btn.click(
|
| 617 |
-
|
| 618 |
outputs=[chatbot]
|
| 619 |
)
|
| 620 |
|
| 621 |
retry_btn.click(
|
| 622 |
-
|
| 623 |
inputs=[chatbot, temperature, max_length],
|
| 624 |
outputs=[chatbot]
|
| 625 |
)
|
|
|
|
| 28 |
|
| 29 |
def remove_think_tags(text):
|
| 30 |
"""
|
| 31 |
+
Remove <think>...</think> tags from text - METHOD 1
|
| 32 |
"""
|
| 33 |
cleaned_text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
|
| 34 |
return cleaned_text.strip()
|
| 35 |
|
| 36 |
def generate_response(message, history, temperature=0.7, max_length=256):
|
| 37 |
"""
|
| 38 |
+
Generate a response using Qwen3-0.6B with your specified method
|
| 39 |
"""
|
| 40 |
if tokenizer is None or model is None:
|
| 41 |
return "β οΈ Model is not loaded properly. Please check the console logs."
|
| 42 |
|
| 43 |
try:
|
| 44 |
+
# Convert history to messages format
|
| 45 |
messages = []
|
| 46 |
+
for human_msg, assistant_msg in history:
|
|
|
|
|
|
|
| 47 |
messages.extend([
|
| 48 |
{"role": "user", "content": human_msg},
|
| 49 |
{"role": "assistant", "content": assistant_msg}
|
|
|
|
| 52 |
# Add current message
|
| 53 |
messages.append({"role": "user", "content": message})
|
| 54 |
|
| 55 |
+
# Apply chat template exactly as in your example
|
| 56 |
inputs = tokenizer.apply_chat_template(
|
| 57 |
messages,
|
| 58 |
add_generation_prompt=True,
|
|
|
|
| 91 |
|
| 92 |
def chat_interface(message, history, temperature, max_length):
|
| 93 |
"""
|
| 94 |
+
Main chat interface function
|
| 95 |
"""
|
| 96 |
if not message or not message.strip():
|
| 97 |
return "", history or []
|
| 98 |
|
| 99 |
+
# Generate response
|
| 100 |
bot_response = generate_response(message, history or [], temperature, max_length)
|
| 101 |
|
| 102 |
+
# Update history
|
| 103 |
new_history = (history or []) + [[message, bot_response]]
|
| 104 |
|
| 105 |
return "", new_history
|
| 106 |
|
| 107 |
def clear_chat():
|
| 108 |
"""
|
| 109 |
+
Clear the chat history
|
| 110 |
"""
|
| 111 |
return []
|
| 112 |
|
|
|
|
| 129 |
|
| 130 |
return new_history
|
| 131 |
|
| 132 |
+
# Custom CSS for beautiful UI with settings on the side
|
| 133 |
custom_css = """
|
| 134 |
.gradio-container {
|
| 135 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 136 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 137 |
min-height: 100vh;
|
| 138 |
+
padding: 20px;
|
|
|
|
| 139 |
}
|
| 140 |
|
| 141 |
.main-container {
|
| 142 |
+
max-width: 1200px;
|
| 143 |
margin: 0 auto;
|
| 144 |
background: white;
|
| 145 |
+
border-radius: 20px;
|
| 146 |
+
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
|
| 147 |
+
overflow: hidden;
|
|
|
|
|
|
|
| 148 |
}
|
| 149 |
|
| 150 |
.header {
|
| 151 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 152 |
color: white;
|
| 153 |
+
padding: 30px;
|
| 154 |
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
}
|
| 156 |
|
| 157 |
.header h1 {
|
| 158 |
margin: 0;
|
| 159 |
+
font-size: 2.5em;
|
| 160 |
+
font-weight: 700;
|
|
|
|
|
|
|
| 161 |
}
|
| 162 |
|
| 163 |
.header p {
|
| 164 |
+
margin: 10px 0 0 0;
|
| 165 |
+
opacity: 0.9;
|
| 166 |
+
font-size: 1.2em;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
}
|
| 168 |
|
| 169 |
+
.content {
|
|
|
|
| 170 |
display: flex;
|
| 171 |
+
min-height: 600px;
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
+
.chat-column {
|
| 175 |
+
flex: 3;
|
| 176 |
display: flex;
|
| 177 |
flex-direction: column;
|
|
|
|
|
|
|
| 178 |
}
|
| 179 |
|
| 180 |
+
.control-column {
|
| 181 |
+
flex: 1;
|
| 182 |
+
background: #f8f9fa;
|
| 183 |
+
padding: 25px;
|
| 184 |
+
border-left: 1px solid #e1e5e9;
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
.chatbot-container {
|
| 188 |
flex: 1;
|
| 189 |
+
display: flex;
|
| 190 |
+
flex-direction: column;
|
| 191 |
min-height: 500px;
|
|
|
|
|
|
|
| 192 |
}
|
| 193 |
|
| 194 |
#chatbot {
|
| 195 |
+
flex: 1;
|
| 196 |
min-height: 500px !important;
|
|
|
|
| 197 |
border: none !important;
|
| 198 |
background: white !important;
|
| 199 |
+
padding: 20px !important;
|
| 200 |
margin: 0 !important;
|
|
|
|
|
|
|
| 201 |
}
|
| 202 |
|
| 203 |
#chatbot .message {
|
| 204 |
+
padding: 15px 20px !important;
|
| 205 |
+
margin: 10px 0 !important;
|
| 206 |
+
border-radius: 15px !important;
|
| 207 |
+
max-width: 80% !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
}
|
| 209 |
|
| 210 |
#chatbot .user-message {
|
| 211 |
+
background: #667eea !important;
|
| 212 |
color: white !important;
|
| 213 |
margin-left: auto !important;
|
|
|
|
| 214 |
}
|
| 215 |
|
| 216 |
#chatbot .bot-message {
|
| 217 |
+
background: #f1f3f4 !important;
|
| 218 |
+
color: #333 !important;
|
|
|
|
| 219 |
margin-right: auto !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
}
|
| 221 |
|
| 222 |
.input-container {
|
| 223 |
+
background: #f8f9fa;
|
| 224 |
+
padding: 20px;
|
| 225 |
+
border-top: 1px solid #e1e5e9;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
}
|
| 227 |
|
| 228 |
.control-panel {
|
| 229 |
background: white;
|
| 230 |
+
padding: 20px;
|
| 231 |
+
border-radius: 15px;
|
| 232 |
+
margin-bottom: 20px;
|
| 233 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
| 234 |
}
|
| 235 |
|
| 236 |
+
.model-info {
|
| 237 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 238 |
color: white;
|
| 239 |
+
padding: 20px;
|
| 240 |
+
border-radius: 15px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
}
|
| 242 |
|
| 243 |
.gr-button {
|
| 244 |
+
background: linear-gradient(45deg, #667eea, #764ba2) !important;
|
| 245 |
border: none !important;
|
| 246 |
color: white !important;
|
| 247 |
+
border-radius: 10px !important;
|
| 248 |
+
padding: 12px 24px !important;
|
| 249 |
font-weight: 600 !important;
|
| 250 |
+
margin: 5px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
}
|
| 252 |
|
| 253 |
.clear-btn {
|
| 254 |
+
background: linear-gradient(45deg, #ff6b6b, #ee5a24) !important;
|
|
|
|
| 255 |
}
|
| 256 |
|
| 257 |
.retry-btn {
|
| 258 |
+
background: linear-gradient(45deg, #00b894, #00a085) !important;
|
|
|
|
| 259 |
}
|
| 260 |
|
| 261 |
.textbox {
|
| 262 |
+
border-radius: 12px !important;
|
| 263 |
+
border: 2px solid #e1e5e9 !important;
|
| 264 |
+
padding: 15px !important;
|
| 265 |
font-size: 16px !important;
|
|
|
|
|
|
|
| 266 |
}
|
| 267 |
|
| 268 |
.textbox:focus {
|
| 269 |
border-color: #667eea !important;
|
| 270 |
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
}
|
| 272 |
|
| 273 |
.examples-panel {
|
| 274 |
background: white;
|
| 275 |
+
padding: 20px;
|
| 276 |
+
border-top: 1px solid #e1e5e9;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
}
|
| 278 |
"""
|
| 279 |
|
|
|
|
| 283 |
with gr.Column(elem_classes="main-container"):
|
| 284 |
# Header
|
| 285 |
with gr.Column(elem_classes="header"):
|
| 286 |
+
gr.Markdown("# π€ Qwen3-0.6B Chatbot")
|
| 287 |
+
gr.Markdown("Chat with Alibaba's advanced Qwen3-0.6B model - Think tags automatically removed!")
|
| 288 |
|
| 289 |
+
with gr.Row(elem_classes="content"):
|
| 290 |
+
# Left Column - Chat (70%)
|
| 291 |
+
with gr.Column(elem_classes="chat-column"):
|
| 292 |
+
with gr.Column(elem_classes="chatbot-container"):
|
|
|
|
| 293 |
chatbot = gr.Chatbot(
|
| 294 |
+
value=[["Hello! How can I assist you today? π", ""]],
|
| 295 |
label="",
|
| 296 |
elem_id="chatbot",
|
| 297 |
show_copy_button=True,
|
|
|
|
| 301 |
show_label=False
|
| 302 |
)
|
| 303 |
|
| 304 |
+
with gr.Column(elem_classes="input-container"):
|
| 305 |
+
with gr.Row():
|
| 306 |
+
msg = gr.Textbox(
|
| 307 |
+
label="",
|
| 308 |
+
placeholder="Type your message here...",
|
| 309 |
+
lines=2,
|
| 310 |
+
scale=4,
|
| 311 |
+
container=False,
|
| 312 |
+
show_label=False
|
| 313 |
+
)
|
| 314 |
+
submit_btn = gr.Button("Send π", size="lg", scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
+
with gr.Row():
|
| 317 |
+
clear_btn = gr.Button("ποΈ Clear Chat", elem_classes="clear-btn")
|
| 318 |
+
retry_btn = gr.Button("π Retry Last", elem_classes="retry-btn")
|
| 319 |
+
gr.HTML("""<div style="flex: 1; text-align: center; color: #666; font-size: 12px; padding: 10px;">
|
| 320 |
+
Press Enter to send β’ Shift+Enter for new line
|
| 321 |
+
</div>""")
|
| 322 |
+
|
| 323 |
+
# Right Column - Controls (30%)
|
| 324 |
+
with gr.Column(elem_classes="control-column"):
|
| 325 |
+
with gr.Column(elem_classes="control-panel"):
|
| 326 |
+
gr.Markdown("### βοΈ Generation Settings")
|
| 327 |
+
|
| 328 |
+
temperature = gr.Slider(
|
| 329 |
+
minimum=0.1,
|
| 330 |
+
maximum=1.5,
|
| 331 |
+
value=0.7,
|
| 332 |
+
step=0.1,
|
| 333 |
+
label="Temperature",
|
| 334 |
+
info="Lower = more predictable, Higher = more creative"
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
max_length = gr.Slider(
|
| 338 |
+
minimum=50,
|
| 339 |
+
maximum=1000,
|
| 340 |
+
value=256,
|
| 341 |
+
step=50,
|
| 342 |
+
label="Max Response Length",
|
| 343 |
+
info="Tokens in generated response"
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
with gr.Column(elem_classes="model-info"):
|
| 347 |
+
gr.Markdown("### βΉοΈ Model Info")
|
| 348 |
+
if tokenizer and model:
|
| 349 |
+
gr.Markdown("""
|
| 350 |
+
**Model:** Qwen3-0.6B β
|
| 351 |
+
**Status:** Ready to chat!
|
| 352 |
+
**Think Tags:** Auto-removed β
|
| 353 |
+
|
| 354 |
+
**Features:**
|
| 355 |
+
β’ 0.6B parameters
|
| 356 |
+
β’ 128K context
|
| 357 |
+
β’ Multilingual
|
| 358 |
+
""")
|
| 359 |
+
else:
|
| 360 |
+
gr.Markdown("""
|
| 361 |
+
**Status:** β οΈ Loading failed
|
| 362 |
+
**Check console for errors**
|
| 363 |
+
""")
|
| 364 |
|
| 365 |
# Examples Section
|
| 366 |
with gr.Column(elem_classes="examples-panel"):
|
| 367 |
+
gr.Markdown("### π‘ Try These Examples")
|
| 368 |
+
gr.Examples(
|
| 369 |
+
examples=[
|
| 370 |
+
"Explain quantum computing in simple terms",
|
| 371 |
+
"Write a short poem about artificial intelligence",
|
| 372 |
+
"What are the benefits of renewable energy?",
|
| 373 |
+
"How do I learn programming effectively?",
|
| 374 |
+
"Tell me an interesting fact about space exploration",
|
| 375 |
+
"Help me plan a healthy weekly meal plan"
|
| 376 |
+
],
|
| 377 |
+
inputs=msg,
|
| 378 |
+
label="Click any example to start chatting!",
|
| 379 |
+
examples_per_page=6
|
| 380 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
submit_event = msg.submit(
|
| 384 |
+
chat_interface,
|
| 385 |
inputs=[msg, chatbot, temperature, max_length],
|
| 386 |
outputs=[msg, chatbot]
|
| 387 |
)
|
| 388 |
|
| 389 |
submit_btn.click(
|
| 390 |
+
chat_interface,
|
| 391 |
inputs=[msg, chatbot, temperature, max_length],
|
| 392 |
outputs=[msg, chatbot]
|
| 393 |
)
|
| 394 |
|
| 395 |
clear_btn.click(
|
| 396 |
+
clear_chat,
|
| 397 |
outputs=[chatbot]
|
| 398 |
)
|
| 399 |
|
| 400 |
retry_btn.click(
|
| 401 |
+
retry_last_response,
|
| 402 |
inputs=[chatbot, temperature, max_length],
|
| 403 |
outputs=[chatbot]
|
| 404 |
)
|