DevNumb commited on
Commit
54eaa5b
·
verified ·
1 Parent(s): 592b13a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -397
app.py CHANGED
@@ -1,407 +1,31 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import torch
4
- import time
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
- # Load tokenizer and model
13
- tokenizer = AutoTokenizer.from_pretrained(
14
- "Qwen/Qwen3-0.6B",
15
- trust_remote_code=True
16
- )
17
-
18
- model = AutoModelForCausalLM.from_pretrained(
19
- "Qwen/Qwen3-0.6B",
20
- torch_dtype=torch.float16,
21
- device_map="auto",
22
- trust_remote_code=True
23
- )
24
-
25
- print("Model loaded successfully!")
26
- return tokenizer, model
27
-
28
- except Exception as e:
29
- print(f"Error loading model: {e}")
30
- # Fallback to a smaller model if Qwen fails
31
- print("Trying fallback model...")
32
- tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
33
- model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
34
- return tokenizer, model
35
-
36
- # Load the model
37
- tokenizer, model = load_model()
38
-
39
- def format_messages(history, new_message):
40
- """
41
- Format chat history and new message into the required format
42
- """
43
- messages = []
44
-
45
- # Add history
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 new message
53
- messages.append({"role": "user", "content": new_message})
54
-
55
- return messages
56
-
57
- def generate_response(message, history, temperature=0.7, max_length=512):
58
- """
59
- Generate a response using the model
60
- """
61
- if tokenizer is None or model is None:
62
- return "Model is not loaded properly. Please check the logs."
63
-
64
- try:
65
- # Format messages
66
- messages = format_messages(history, message)
67
-
68
- # Apply chat template
69
- text = tokenizer.apply_chat_template(
70
- messages,
71
- tokenize=False,
72
- add_generation_prompt=True
73
- )
74
-
75
- # Tokenize
76
- model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
77
-
78
- # Generate response
79
- with torch.no_grad():
80
- generated_ids = model.generate(
81
- **model_inputs,
82
- max_new_tokens=max_length,
83
- temperature=temperature,
84
- do_sample=True if temperature > 0.1 else False,
85
- top_p=0.9,
86
- repetition_penalty=1.1,
87
- eos_token_id=tokenizer.eos_token_id,
88
- pad_token_id=tokenizer.eos_token_id
89
- )
90
-
91
- # Decode response
92
- generated_ids = [
93
- output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
94
- ]
95
-
96
- response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
97
-
98
- # Clean up response
99
- response = response.strip()
100
- if "<|im_end|>" in response:
101
- response = response.split("<|im_end|>")[0].strip()
102
-
103
- return response
104
-
105
- except Exception as e:
106
- return f"Sorry, I encountered an error: {str(e)}"
107
-
108
- def chat_interface(message, history, temperature, max_length):
109
- """
110
- Main chat interface function
111
- """
112
  if not message.strip():
113
  return "", history
114
 
115
- # Generate response
116
- bot_response = generate_response(message, history, temperature, max_length)
117
 
118
  # Update history
119
- history.append([message, bot_response])
120
 
121
- return "", history
122
-
123
- def clear_chat():
124
- """
125
- Clear the chat history
126
- """
127
- return []
128
-
129
- def retry_last_response(history, temperature, max_length):
130
- """
131
- Retry the last user message
132
- """
133
- if not history:
134
- return history
135
-
136
- # Remove the last assistant response
137
- last_conversation = history[:-1]
138
- last_user_message = history[-1][0]
139
-
140
- # Regenerate response
141
- bot_response = generate_response(last_user_message, last_conversation, temperature, max_length)
142
-
143
- # Update history
144
- last_conversation.append([last_user_message, bot_response])
145
-
146
- return last_conversation
147
-
148
- # Custom CSS for beautiful styling with better layout
149
- custom_css = """
150
- .gradio-container {
151
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
152
- }
153
-
154
- .main-container {
155
- max-width: 1200px !important;
156
- margin: 0 auto !important;
157
- padding: 20px !important;
158
- }
159
-
160
- .chat-column {
161
- min-height: 600px;
162
- }
163
-
164
- .control-column {
165
- background: #f8f9fa;
166
- border-radius: 15px;
167
- padding: 20px;
168
- height: fit-content;
169
- position: sticky;
170
- top: 20px;
171
- }
172
 
173
- .chatbot-container {
174
- border: 2px solid #e1e5e9;
175
- border-radius: 15px;
176
- padding: 0;
177
- overflow: hidden;
178
- background: white;
179
- box-shadow: 0 4px 12px rgba(0,0,0,0.1);
180
- }
181
-
182
- #chatbot {
183
- min-height: 500px;
184
- max-height: 500px;
185
- border: none !important;
186
- background: white !important;
187
- padding: 20px !important;
188
- }
189
-
190
- .input-container {
191
- background: white;
192
- padding: 20px;
193
- border-top: 1px solid #e1e5e9;
194
- }
195
-
196
- .control-panel {
197
- background: white;
198
- padding: 20px;
199
- border-radius: 10px;
200
- margin-bottom: 20px;
201
- box-shadow: 0 2px 8px rgba(0,0,0,0.1);
202
- }
203
-
204
- .model-info-card {
205
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
206
- color: white;
207
- padding: 20px;
208
- border-radius: 10px;
209
- margin-top: 20px;
210
- }
211
-
212
- .gr-button {
213
- background: linear-gradient(45deg, #667eea, #764ba2) !important;
214
- border: none !important;
215
- color: white !important;
216
- border-radius: 10px !important;
217
- padding: 10px 20px !important;
218
- font-weight: 600 !important;
219
- margin: 5px !important;
220
- }
221
-
222
- .gr-button:hover {
223
- transform: translateY(-2px);
224
- box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
225
- }
226
-
227
- .clear-btn {
228
- background: linear-gradient(45deg, #ff6b6b, #ee5a24) !important;
229
- }
230
-
231
- .retry-btn {
232
- background: linear-gradient(45deg, #00b894, #00a085) !important;
233
- }
234
-
235
- .textbox {
236
- border-radius: 10px !important;
237
- border: 2px solid #e1e5e9 !important;
238
- padding: 15px !important;
239
- }
240
-
241
- .textbox:focus {
242
- border-color: #667eea !important;
243
- box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
244
- }
245
-
246
- .slider-container {
247
- margin: 15px 0;
248
- }
249
-
250
- .examples-panel {
251
- background: white;
252
- padding: 15px;
253
- border-radius: 10px;
254
- margin-top: 20px;
255
- border: 1px solid #e1e5e9;
256
- }
257
-
258
- .header {
259
- text-align: center;
260
- margin-bottom: 30px !important;
261
- }
262
-
263
- .header h1 {
264
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
265
- -webkit-background-clip: text;
266
- -webkit-text-fill-color: transparent;
267
- background-clip: text;
268
- font-size: 2.5em !important;
269
- margin-bottom: 10px !important;
270
- }
271
-
272
- .header p {
273
- color: #666;
274
- font-size: 1.2em;
275
- margin: 0 !important;
276
- }
277
- """
278
-
279
- # Create the Gradio interface with better layout
280
- with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
281
-
282
- with gr.Column(elem_classes="main-container"):
283
- # Header
284
- with gr.Column(elem_classes="header"):
285
- gr.Markdown("# 🤖 Qwen3-0.6B Chatbot")
286
- gr.Markdown("Chat with Alibaba's advanced Qwen3-0.6B model! Experience fluent and intelligent conversations.")
287
-
288
- with gr.Row(equal_height=False):
289
- # Left Column - Chat Interface (Larger)
290
- with gr.Column(scale=3, elem_classes="chat-column"):
291
- with gr.Column(elem_classes="chatbot-container"):
292
- chatbot = gr.Chatbot(
293
- value=[["Hello! How can I help you today?", ""]],
294
- label="💬 Chat with Qwen3",
295
- elem_id="chatbot",
296
- show_copy_button=True,
297
- avatar_images=("👤", "🤖"),
298
- height=500,
299
- show_label=True,
300
- container=True
301
- )
302
-
303
- with gr.Column(elem_classes="input-container"):
304
- with gr.Row():
305
- msg = gr.Textbox(
306
- label="",
307
- placeholder="Type your message here...",
308
- lines=2,
309
- scale=4,
310
- container=False,
311
- show_label=False
312
- )
313
- with gr.Column(scale=1):
314
- submit_btn = gr.Button("Send 🚀", size="lg")
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
- 💡 Tip: Press Enter to send, Shift+Enter for new line
321
- </div>""")
322
-
323
- # Right Column - Controls (Smaller)
324
- with gr.Column(scale=1, 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="Controls creativity: lower = more focused, higher = more creative"
335
- )
336
-
337
- max_length = gr.Slider(
338
- minimum=64,
339
- maximum=1024,
340
- value=256,
341
- step=64,
342
- label="Max Response Length",
343
- info="Maximum tokens in generated response"
344
- )
345
-
346
- with gr.Column(elem_classes="model-info-card"):
347
- gr.Markdown("### ℹ️ About This Model")
348
- gr.Markdown("""
349
- **Model:** Qwen3-0.6B
350
- **Provider:** Alibaba Group
351
- **Context:** 128K tokens
352
- **Languages:** Multilingual
353
-
354
- ✨ **Capabilities:**
355
- - Natural conversations
356
- - Creative writing
357
- - Problem solving
358
- - Code generation
359
- """)
360
-
361
- # Examples Section
362
- with gr.Column(elem_classes="examples-panel"):
363
- gr.Markdown("### 💡 Try These Examples")
364
- gr.Examples(
365
- examples=[
366
- "Explain quantum computing in simple terms",
367
- "Write a short poem about artificial intelligence",
368
- "What are the benefits of renewable energy?",
369
- "How do I learn programming effectively?",
370
- "Tell me an interesting fact about space exploration",
371
- "Help me plan a healthy weekly meal plan"
372
- ],
373
- inputs=msg,
374
- label="Click any example to start chatting!",
375
- examples_per_page=6
376
- )
377
-
378
- # Event handlers
379
- submit_event = msg.submit(
380
- chat_interface,
381
- inputs=[msg, chatbot, temperature, max_length],
382
- outputs=[msg, chatbot]
383
- )
384
-
385
- submit_btn.click(
386
- chat_interface,
387
- inputs=[msg, chatbot, temperature, max_length],
388
- outputs=[msg, chatbot]
389
- )
390
-
391
- clear_btn.click(
392
- clear_chat,
393
- outputs=[chatbot]
394
- )
395
 
396
- retry_btn.click(
397
- retry_last_response,
398
- inputs=[chatbot, temperature, max_length],
399
- outputs=[chatbot]
400
- )
401
 
402
- if __name__ == "__main__":
403
- demo.launch(
404
- server_name="0.0.0.0",
405
- share=False,
406
- show_error=True
407
- )
 
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()