DevNumb commited on
Commit
16e4e90
Β·
verified Β·
1 Parent(s): 54eaa5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +408 -20
app.py CHANGED
@@ -1,31 +1,419 @@
1
  import gradio as gr
2
- import random
 
 
3
 
4
- # Simple fallback responses
5
- fallback_responses = [
6
- "I understand you want me to explain quantum computing. It's a complex topic that deals with how very small particles behave differently from objects in our everyday world.",
7
- "Quantum computing uses quantum bits or qubits, which can exist in multiple states at once, unlike regular computer bits that are only 0 or 1.",
8
- "This allows quantum computers to solve certain problems much faster than traditional computers.",
9
- "I'd be happy to help you learn about quantum computing! It's a fascinating field of physics and computer science.",
10
- "Quantum computers use phenomena like superposition and entanglement to perform calculations in new ways."
11
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- def simple_chat(message, history):
14
- if not message.strip():
15
- return "", history
 
 
 
16
 
17
- # Simulate AI thinking
18
- response = random.choice(fallback_responses)
19
 
20
  # Update history
21
- new_history = history + [[message, response]]
22
 
23
  return "", new_history
24
 
25
- with gr.Blocks() as demo:
26
- chatbot = gr.Chatbot(height=400)
27
- msg = gr.Textbox(label="Your message")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- msg.submit(simple_chat, [msg, chatbot], [msg, chatbot])
 
 
 
 
30
 
31
- demo.launch()
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+ import re
5
 
6
+ # Initialize the model and tokenizer
7
+ @torch.no_grad()
8
+ def load_model():
9
+ print("Loading Qwen3-0.6B model...")
10
+
11
+ try:
12
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B", trust_remote_code=True)
13
+ model = AutoModelForCausalLM.from_pretrained(
14
+ "Qwen/Qwen3-0.6B",
15
+ torch_dtype=torch.float16,
16
+ device_map="auto",
17
+ trust_remote_code=True
18
+ )
19
+ print("Qwen3-0.6B model loaded successfully!")
20
+ return tokenizer, model
21
+
22
+ except Exception as e:
23
+ print(f"Error loading Qwen3-0.6B: {e}")
24
+ return None, None
25
+
26
+ # Load the model
27
+ tokenizer, model = load_model()
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}
50
+ ])
51
+
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,
59
+ tokenize=True,
60
+ return_dict=True,
61
+ return_tensors="pt",
62
+ ).to(model.device)
63
+
64
+ # Generate response
65
+ with torch.no_grad():
66
+ outputs = model.generate(
67
+ **inputs,
68
+ max_new_tokens=max_length,
69
+ temperature=temperature,
70
+ do_sample=True if temperature > 0.1 else False,
71
+ top_p=0.9,
72
+ repetition_penalty=1.1,
73
+ eos_token_id=tokenizer.eos_token_id,
74
+ pad_token_id=tokenizer.eos_token_id
75
+ )
76
+
77
+ # Extract only the new generated text
78
+ response = tokenizer.decode(
79
+ outputs[0][inputs["input_ids"].shape[-1]:],
80
+ skip_special_tokens=True
81
+ )
82
+
83
+ # Clean think tags from response
84
+ clean_response = remove_think_tags(response)
85
+
86
+ return clean_response if clean_response else "I'm here to help! What would you like to know?"
87
+
88
+ except Exception as e:
89
+ print(f"Generation error: {e}")
90
+ return f"⚠️ Error generating response: {str(e)}"
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
+
113
+ def retry_last_response(history, temperature, max_length):
114
+ """
115
+ Retry the last user message
116
+ """
117
+ if not history:
118
+ return history
119
+
120
+ # Remove the last assistant response
121
+ last_conversation = history[:-1]
122
+ last_user_message = history[-1][0]
123
+
124
+ # Regenerate response
125
+ bot_response = generate_response(last_user_message, last_conversation, temperature, max_length)
126
+
127
+ # Update history
128
+ new_history = last_conversation + [[last_user_message, bot_response]]
129
+
130
+ return new_history
131
+
132
+ # Custom CSS for beautiful UI
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
+ .loading {
280
+ display: flex;
281
+ justify-content: center;
282
+ align-items: center;
283
+ height: 200px;
284
+ color: #666;
285
+ }
286
+ """
287
+
288
+ # Create the Gradio interface
289
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
290
+
291
+ with gr.Column(elem_classes="main-container"):
292
+ # Header
293
+ with gr.Column(elem_classes="header"):
294
+ gr.Markdown("# πŸ€– Qwen3-0.6B Chatbot")
295
+ gr.Markdown("Chat with Alibaba's advanced Qwen3-0.6B model - Think tags automatically removed!")
296
+
297
+ with gr.Row(elem_classes="content"):
298
+ # Left Column - Chat (70%)
299
+ with gr.Column(elem_classes="chat-column"):
300
+ with gr.Column(elem_classes="chatbot-container"):
301
+ chatbot = gr.Chatbot(
302
+ value=[],
303
+ label="",
304
+ elem_id="chatbot",
305
+ show_copy_button=True,
306
+ avatar_images=("πŸ‘€", "πŸ€–"),
307
+ height=500,
308
+ container=True,
309
+ show_label=False
310
+ )
311
+
312
+ with gr.Column(elem_classes="input-container"):
313
+ with gr.Row():
314
+ msg = gr.Textbox(
315
+ label="",
316
+ placeholder="Type your message here...",
317
+ lines=2,
318
+ scale=4,
319
+ container=False,
320
+ show_label=False
321
+ )
322
+ submit_btn = gr.Button("Send πŸš€", size="lg", scale=1)
323
+
324
+ with gr.Row():
325
+ clear_btn = gr.Button("πŸ—‘οΈ Clear Chat", elem_classes="clear-btn")
326
+ retry_btn = gr.Button("πŸ”„ Retry Last", elem_classes="retry-btn")
327
+ gr.HTML("""<div style="flex: 1; text-align: center; color: #666; font-size: 12px; padding: 10px;">
328
+ Press Enter to send β€’ Shift+Enter for new line
329
+ </div>""")
330
+
331
+ # Right Column - Controls (30%)
332
+ with gr.Column(elem_classes="control-column"):
333
+ with gr.Column(elem_classes="control-panel"):
334
+ gr.Markdown("### βš™οΈ Generation Settings")
335
+
336
+ temperature = gr.Slider(
337
+ minimum=0.1,
338
+ maximum=1.5,
339
+ value=0.7,
340
+ step=0.1,
341
+ label="Temperature",
342
+ info="Lower = more predictable, Higher = more creative"
343
+ )
344
+
345
+ max_length = gr.Slider(
346
+ minimum=50,
347
+ maximum=1000,
348
+ value=256,
349
+ step=50,
350
+ label="Max Response Length",
351
+ info="Tokens in generated response"
352
+ )
353
+
354
+ with gr.Column(elem_classes="model-info"):
355
+ gr.Markdown("### ℹ️ Model Info")
356
+ if tokenizer and model:
357
+ gr.Markdown("""
358
+ **Model:** Qwen3-0.6B βœ…
359
+ **Status:** Ready to chat!
360
+ **Think Tags:** Auto-removed βœ…
361
+
362
+ **Features:**
363
+ β€’ 0.6B parameters
364
+ β€’ 128K context
365
+ β€’ Multilingual
366
+ """)
367
+ else:
368
+ gr.Markdown("""
369
+ **Status:** ⚠️ Loading failed
370
+ **Check console for errors**
371
+ """)
372
+
373
+ # Examples Section
374
+ with gr.Column(elem_classes="examples-panel"):
375
+ gr.Markdown("### πŸ’‘ Try These Examples")
376
+ gr.Examples(
377
+ examples=[
378
+ "Explain quantum computing in simple terms",
379
+ "Write a short poem about artificial intelligence",
380
+ "What are the benefits of renewable energy?",
381
+ "How do I learn programming effectively?",
382
+ "Tell me an interesting fact about space exploration",
383
+ "Help me plan a healthy weekly meal plan"
384
+ ],
385
+ inputs=msg,
386
+ label="Click any example to start chatting!",
387
+ examples_per_page=6
388
+ )
389
+
390
+ # Event handlers
391
+ submit_event = msg.submit(
392
+ chat_interface,
393
+ inputs=[msg, chatbot, temperature, max_length],
394
+ outputs=[msg, chatbot]
395
+ )
396
+
397
+ submit_btn.click(
398
+ chat_interface,
399
+ inputs=[msg, chatbot, temperature, max_length],
400
+ outputs=[msg, chatbot]
401
+ )
402
+
403
+ clear_btn.click(
404
+ clear_chat,
405
+ outputs=[chatbot]
406
+ )
407
 
408
+ retry_btn.click(
409
+ retry_last_response,
410
+ inputs=[chatbot, temperature, max_length],
411
+ outputs=[chatbot]
412
+ )
413
 
414
+ if __name__ == "__main__":
415
+ demo.launch(
416
+ server_name="0.0.0.0",
417
+ share=False,
418
+ show_error=True
419
+ )