wangsheng commited on
Commit
9475c70
·
verified ·
1 Parent(s): 646d476

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -186
app.py CHANGED
@@ -225,153 +225,9 @@ def generate_response_stream(
225
  def create_demo():
226
  """Create the Gradio interface"""
227
 
228
- # Custom CSS
229
- custom_css = """
230
- :root {
231
- --primary: #667eea;
232
- --secondary: #764ba2;
233
- }
234
-
235
- .deepseek-header {
236
- text-align: center;
237
- margin-bottom: 20px;
238
- padding: 30px;
239
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
240
- border-radius: 16px;
241
- color: white;
242
- }
243
-
244
- .deepseek-header h1 {
245
- font-size: 2.8em;
246
- font-weight: 800;
247
- margin: 0;
248
- text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
249
- }
250
-
251
- .deepseek-header p {
252
- font-size: 1.2em;
253
- opacity: 0.95;
254
- margin: 10px 0 0 0;
255
- }
256
-
257
- .model-info {
258
- background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
259
- padding: 20px;
260
- border-radius: 12px;
261
- margin-bottom: 20px;
262
- border: 1px solid #e0e0e0;
263
- }
264
-
265
- .benchmark-grid {
266
- display: grid;
267
- grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
268
- gap: 12px;
269
- margin: 15px 0;
270
- }
271
-
272
- .benchmark-item {
273
- background: white;
274
- padding: 12px;
275
- border-radius: 8px;
276
- text-align: center;
277
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
278
- transition: transform 0.2s;
279
- }
280
-
281
- .benchmark-item:hover {
282
- transform: translateY(-2px);
283
- box-shadow: 0 4px 8px rgba(0,0,0,0.15);
284
- }
285
-
286
- .benchmark-item .value {
287
- font-size: 1.5em;
288
- font-weight: 700;
289
- color: #667eea;
290
- }
291
-
292
- .benchmark-item .label {
293
- font-size: 0.85em;
294
- color: #666;
295
- margin-top: 4px;
296
- }
297
-
298
- .chat-container {
299
- border: 1px solid #e0e0e0;
300
- border-radius: 12px;
301
- overflow: hidden;
302
- }
303
-
304
- .thinking-box {
305
- background: #f8f9fa;
306
- border-left: 4px solid #667eea;
307
- padding: 15px;
308
- margin: 10px 0;
309
- border-radius: 8px;
310
- font-style: italic;
311
- color: #555;
312
- }
313
-
314
- .thinking-box::before {
315
- content: "🧠 Thinking Process";
316
- display: block;
317
- font-weight: 600;
318
- color: #667eea;
319
- margin-bottom: 8px;
320
- }
321
-
322
- .response-box {
323
- background: white;
324
- padding: 15px;
325
- border-radius: 8px;
326
- line-height: 1.6;
327
- }
328
-
329
- .status-bar {
330
- padding: 10px;
331
- background: #f5f5f5;
332
- border-radius: 8px;
333
- font-family: monospace;
334
- font-size: 0.9em;
335
- }
336
-
337
- .mode-indicator {
338
- display: inline-block;
339
- padding: 4px 12px;
340
- border-radius: 20px;
341
- font-size: 0.85em;
342
- font-weight: 600;
343
- margin-right: 8px;
344
- }
345
-
346
- .mode-non-think {
347
- background: #e3f2fd;
348
- color: #1976d2;
349
- }
350
-
351
- .mode-think-high {
352
- background: #f3e5f5;
353
- color: #7b1fa2;
354
- }
355
-
356
- .mode-think-max {
357
- background: #fce4ec;
358
- color: #c62828;
359
- }
360
-
361
- .api-key-warning {
362
- background: #fff3cd;
363
- border: 1px solid #ffc107;
364
- color: #856404;
365
- padding: 15px;
366
- border-radius: 8px;
367
- margin: 10px 0;
368
- }
369
- """
370
-
371
  with gr.Blocks(
372
  title="DeepSeek-V4 Pro - API Demo",
373
- theme=gr.themes.Soft(),
374
- css=custom_css,
375
  analytics_enabled=False
376
  ) as demo:
377
 
@@ -518,21 +374,16 @@ def create_demo():
518
 
519
  # Right - Chat Interface
520
  with gr.Column(scale=2):
521
- # Chatbot
522
  chatbot = gr.Chatbot(
523
  label="💬 Chat with DeepSeek-V4 Pro",
524
  height=550,
525
- show_copy_button=True,
526
  bubble_full_width=False,
527
- avatar_images=(
528
- "https://api.dicebear.com/7.x/bottts/svg?seed=user&backgroundColor=667eea",
529
- "https://api.dicebear.com/7.x/bottts/svg?seed=assistant&backgroundColor=764ba2"
530
- ),
531
- layout="panel"
532
  )
533
 
534
  # Thinking process display
535
- with gr.Accordion("🧠 Thinking Process", open=True, visible=True):
536
  thinking_display = gr.Markdown(
537
  value="*The model's reasoning will appear here...*",
538
  elem_classes="thinking-box"
@@ -558,7 +409,6 @@ def create_demo():
558
  # Control buttons
559
  with gr.Row():
560
  clear_btn = gr.Button("🗑️ Clear Chat", size="sm")
561
- stop_btn = gr.Button("⏹️ Stop", size="sm", variant="stop", visible=False)
562
  retry_btn = gr.Button("🔄 Retry", size="sm", variant="secondary")
563
 
564
  # Status bar
@@ -591,7 +441,7 @@ def create_demo():
591
 
592
  def process_message(
593
  message: str,
594
- history: List[Tuple[str, str]],
595
  thinking_mode: str,
596
  show_thinking: bool,
597
  system_prompt: str,
@@ -602,35 +452,61 @@ def create_demo():
602
  ):
603
  """Process message with streaming or non-streaming mode"""
604
  if not message.strip():
605
- return message, history, "", "Please enter a message."
 
606
 
607
  # Check API key
608
  if not os.environ.get('DEEPSEEK_API_KEY'):
609
- return (
610
- message,
611
- history + [(message, "⚠️ **API Key Missing**\n\nPlease set your `DEEPSEEK_API_KEY` environment variable.\nGet one at: https://platform.deepseek.com/api_keys")],
612
- "",
613
- " API Key not configured"
614
- )
 
615
 
616
  if stream_output:
617
  # Use streaming
 
 
 
 
 
 
 
618
  for msg, hist, content, thinking, status in generate_response_stream(
619
- message, history, thinking_mode, max_tokens,
620
  temperature, top_p, system_prompt, show_thinking
621
  ):
622
- yield msg, hist, thinking, status
 
 
 
 
 
623
  else:
624
  # Use non-streaming
 
 
 
 
 
 
625
  result = generate_response(
626
- message, history, thinking_mode, max_tokens,
627
  temperature, top_p, system_prompt, show_thinking
628
  )
629
  msg, hist, content, thinking, status = result
630
- yield msg, hist, thinking, status
 
 
 
 
 
 
631
 
632
  # Wire up send button
633
- send_event = send_btn.click(
634
  fn=process_message,
635
  inputs=[
636
  message_input, chatbot, thinking_mode, show_thinking,
@@ -641,7 +517,7 @@ def create_demo():
641
  )
642
 
643
  # Wire up Enter key
644
- enter_event = message_input.submit(
645
  fn=process_message,
646
  inputs=[
647
  message_input, chatbot, thinking_mode, show_thinking,
@@ -666,32 +542,30 @@ def create_demo():
666
 
667
  # Retry last message
668
  def retry_last(history):
669
- if not history:
670
- return history, "No message to retry."
671
- last_message = history[-1][0]
672
- history = history[:-1]
673
- return history, last_message
 
674
 
675
  retry_btn.click(
676
  fn=retry_last,
677
  inputs=[chatbot],
678
  outputs=[chatbot, message_input]
679
  )
680
-
681
- # Mode change indicator
682
- def update_mode_indicator(mode):
683
- mode_classes = {
684
- "Non-think": "mode-non-think",
685
- "Think High": "mode-think-high",
686
- "Think Max": "mode-think-max"
687
- }
688
- class_name = mode_classes.get(mode, "")
689
- return f'<span class="mode-indicator {class_name}">{mode}</span>'
690
 
691
  return demo
692
 
693
  # ==================== Main ====================
694
  if __name__ == "__main__":
 
 
 
 
 
 
 
695
  # Check environment
696
  api_key = os.environ.get('DEEPSEEK_API_KEY')
697
  if not api_key:
@@ -706,11 +580,101 @@ if __name__ == "__main__":
706
  print(' echo DEEPSEEK_API_KEY=your-key-here > .env')
707
  print("\n" + "=" * 60 + "\n")
708
 
709
- # Create and launch demo
710
  demo = create_demo()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
711
  demo.queue(
712
  max_size=50,
713
- concurrency_count=10,
714
  default_concurrency_limit=10
715
  ).launch(
716
  server_name="0.0.0.0",
@@ -718,5 +682,6 @@ if __name__ == "__main__":
718
  share=False,
719
  debug=False,
720
  show_error=True,
721
- favicon_path=None
 
722
  )
 
225
  def create_demo():
226
  """Create the Gradio interface"""
227
 
228
+ # Use gr.Blocks without theme and css (moved to launch)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  with gr.Blocks(
230
  title="DeepSeek-V4 Pro - API Demo",
 
 
231
  analytics_enabled=False
232
  ) as demo:
233
 
 
374
 
375
  # Right - Chat Interface
376
  with gr.Column(scale=2):
377
+ # Chatbot - Gradio 6.0 compatible
378
  chatbot = gr.Chatbot(
379
  label="💬 Chat with DeepSeek-V4 Pro",
380
  height=550,
 
381
  bubble_full_width=False,
382
+ type="messages"
 
 
 
 
383
  )
384
 
385
  # Thinking process display
386
+ with gr.Accordion("🧠 Thinking Process", open=True):
387
  thinking_display = gr.Markdown(
388
  value="*The model's reasoning will appear here...*",
389
  elem_classes="thinking-box"
 
409
  # Control buttons
410
  with gr.Row():
411
  clear_btn = gr.Button("🗑️ Clear Chat", size="sm")
 
412
  retry_btn = gr.Button("🔄 Retry", size="sm", variant="secondary")
413
 
414
  # Status bar
 
441
 
442
  def process_message(
443
  message: str,
444
+ history: list,
445
  thinking_mode: str,
446
  show_thinking: bool,
447
  system_prompt: str,
 
452
  ):
453
  """Process message with streaming or non-streaming mode"""
454
  if not message.strip():
455
+ yield message, history, "", "Please enter a message."
456
+ return
457
 
458
  # Check API key
459
  if not os.environ.get('DEEPSEEK_API_KEY'):
460
+ error_msg = "⚠️ **API Key Missing**\n\nPlease set your `DEEPSEEK_API_KEY` environment variable.\nGet one at: https://platform.deepseek.com/api_keys"
461
+ if history is None:
462
+ history = []
463
+ history.append({"role": "user", "content": message})
464
+ history.append({"role": "assistant", "content": error_msg})
465
+ yield "", history, "", "❌ API Key not configured"
466
+ return
467
 
468
  if stream_output:
469
  # Use streaming
470
+ # Convert history format for internal use
471
+ internal_history = []
472
+ if history:
473
+ for i in range(0, len(history), 2):
474
+ if i + 1 < len(history):
475
+ internal_history.append((history[i]["content"], history[i+1]["content"]))
476
+
477
  for msg, hist, content, thinking, status in generate_response_stream(
478
+ message, internal_history, thinking_mode, max_tokens,
479
  temperature, top_p, system_prompt, show_thinking
480
  ):
481
+ # Convert hist back to Gradio format
482
+ gr_history = []
483
+ for user_msg, assistant_msg in hist:
484
+ gr_history.append({"role": "user", "content": user_msg})
485
+ gr_history.append({"role": "assistant", "content": assistant_msg})
486
+ yield msg, gr_history, thinking, status
487
  else:
488
  # Use non-streaming
489
+ internal_history = []
490
+ if history:
491
+ for i in range(0, len(history), 2):
492
+ if i + 1 < len(history):
493
+ internal_history.append((history[i]["content"], history[i+1]["content"]))
494
+
495
  result = generate_response(
496
+ message, internal_history, thinking_mode, max_tokens,
497
  temperature, top_p, system_prompt, show_thinking
498
  )
499
  msg, hist, content, thinking, status = result
500
+
501
+ gr_history = []
502
+ for user_msg, assistant_msg in hist:
503
+ gr_history.append({"role": "user", "content": user_msg})
504
+ gr_history.append({"role": "assistant", "content": assistant_msg})
505
+
506
+ yield msg, gr_history, thinking, status
507
 
508
  # Wire up send button
509
+ send_btn.click(
510
  fn=process_message,
511
  inputs=[
512
  message_input, chatbot, thinking_mode, show_thinking,
 
517
  )
518
 
519
  # Wire up Enter key
520
+ message_input.submit(
521
  fn=process_message,
522
  inputs=[
523
  message_input, chatbot, thinking_mode, show_thinking,
 
542
 
543
  # Retry last message
544
  def retry_last(history):
545
+ if not history or len(history) < 2:
546
+ return history, ""
547
+ # Remove last assistant message, keep last user message
548
+ last_user_msg = history[-2]["content"] if len(history) >= 2 else ""
549
+ new_history = history[:-2] if len(history) >= 2 else []
550
+ return new_history, last_user_msg
551
 
552
  retry_btn.click(
553
  fn=retry_last,
554
  inputs=[chatbot],
555
  outputs=[chatbot, message_input]
556
  )
 
 
 
 
 
 
 
 
 
 
557
 
558
  return demo
559
 
560
  # ==================== Main ====================
561
  if __name__ == "__main__":
562
+ # Try to load .env file
563
+ try:
564
+ from dotenv import load_dotenv
565
+ load_dotenv()
566
+ except ImportError:
567
+ pass
568
+
569
  # Check environment
570
  api_key = os.environ.get('DEEPSEEK_API_KEY')
571
  if not api_key:
 
580
  print(' echo DEEPSEEK_API_KEY=your-key-here > .env')
581
  print("\n" + "=" * 60 + "\n")
582
 
583
+ # Create demo
584
  demo = create_demo()
585
+
586
+ # Custom CSS
587
+ custom_css = """
588
+ :root {
589
+ --primary: #667eea;
590
+ --secondary: #764ba2;
591
+ }
592
+
593
+ .deepseek-header {
594
+ text-align: center;
595
+ margin-bottom: 20px;
596
+ padding: 30px;
597
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
598
+ border-radius: 16px;
599
+ color: white;
600
+ }
601
+
602
+ .deepseek-header h1 {
603
+ font-size: 2.8em;
604
+ font-weight: 800;
605
+ margin: 0;
606
+ text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
607
+ }
608
+
609
+ .deepseek-header p {
610
+ font-size: 1.2em;
611
+ opacity: 0.95;
612
+ margin: 10px 0 0 0;
613
+ }
614
+
615
+ .model-info {
616
+ background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
617
+ padding: 20px;
618
+ border-radius: 12px;
619
+ margin-bottom: 20px;
620
+ border: 1px solid #e0e0e0;
621
+ }
622
+
623
+ .benchmark-grid {
624
+ display: grid;
625
+ grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
626
+ gap: 12px;
627
+ margin: 15px 0;
628
+ }
629
+
630
+ .benchmark-item {
631
+ background: white;
632
+ padding: 12px;
633
+ border-radius: 8px;
634
+ text-align: center;
635
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
636
+ transition: transform 0.2s;
637
+ }
638
+
639
+ .benchmark-item:hover {
640
+ transform: translateY(-2px);
641
+ box-shadow: 0 4px 8px rgba(0,0,0,0.15);
642
+ }
643
+
644
+ .benchmark-item .value {
645
+ font-size: 1.5em;
646
+ font-weight: 700;
647
+ color: #667eea;
648
+ }
649
+
650
+ .benchmark-item .label {
651
+ font-size: 0.85em;
652
+ color: #666;
653
+ margin-top: 4px;
654
+ }
655
+
656
+ .thinking-box {
657
+ background: #f8f9fa;
658
+ border-left: 4px solid #667eea;
659
+ padding: 15px;
660
+ margin: 10px 0;
661
+ border-radius: 8px;
662
+ font-style: italic;
663
+ color: #555;
664
+ }
665
+
666
+ .status-bar {
667
+ padding: 10px;
668
+ background: #f5f5f5;
669
+ border-radius: 8px;
670
+ font-family: monospace;
671
+ font-size: 0.9em;
672
+ }
673
+ """
674
+
675
+ # Launch with Gradio 6.0 compatible parameters
676
  demo.queue(
677
  max_size=50,
 
678
  default_concurrency_limit=10
679
  ).launch(
680
  server_name="0.0.0.0",
 
682
  share=False,
683
  debug=False,
684
  show_error=True,
685
+ theme=gr.themes.Soft(),
686
+ css=custom_css
687
  )