朱东升 commited on
Commit
8c0f360
·
1 Parent(s): 3ae05a4
Files changed (1) hide show
  1. app.py +131 -69
app.py CHANGED
@@ -453,41 +453,58 @@ def ui_get_queue_info():
453
  </tr>
454
  """
455
 
 
 
 
 
 
 
 
 
456
  # 添加平均处理时间显示
457
  avg_time_html = f"""<p><b>平均处理时间:</b> {format_time(queue_info.get('avg_process_time', 0))}</p>"""
458
 
459
  return f"""
460
- <div class="queue-info-card">
461
- <h3>队列状态</h3>
462
- <div class="queue-stats">
463
- <div class="stat-item">
464
- <div class="stat-value">{queue_info['waiting_tasks']}</div>
465
- <div class="stat-label">等待中</div>
466
- </div>
467
- <div class="stat-item">
468
- <div class="stat-value">{queue_info['active_tasks']}</div>
469
- <div class="stat-label">处理中</div>
 
 
 
 
 
 
470
  </div>
471
- <div class="stat-item">
472
- <div class="stat-value">{queue_info['worker_threads']}</div>
473
- <div class="stat-label">工作线程</div>
 
 
474
  </div>
475
  </div>
476
-
477
- <div class="wait-time">
478
- {avg_time_html}
479
- <p><b>当前预计等待时间:</b> {format_time(queue_info['estimated_wait'])}</p>
 
 
 
 
 
 
 
 
 
 
 
480
  </div>
481
-
482
- <h4>最近处理的任务</h4>
483
- <table class="recent-tasks">
484
- <tr>
485
- <th>任务ID</th>
486
- <th>请求时间</th>
487
- <th>处理时间</th>
488
- </tr>
489
- {tasks_html}
490
- </table>
491
  </div>
492
  """
493
 
@@ -526,14 +543,36 @@ custom_css = """
526
  .container {
527
  max-width: 1200px;
528
  margin: 0 auto;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529
  }
530
 
531
  .status-card, .queue-info-card {
532
- background: #f7f7f7;
533
- border-radius: 10px;
534
- padding: 15px;
535
  margin: 10px 0;
536
- box-shadow: 0 2px 5px rgba(0,0,0,0.1);
 
 
 
 
 
 
 
 
537
  }
538
 
539
  .status-card.success {
@@ -569,45 +608,88 @@ custom_css = """
569
 
570
  .stat-item {
571
  text-align: center;
 
 
 
 
 
 
 
 
 
 
572
  }
573
 
574
  .stat-value {
575
- font-size: 24px;
576
  font-weight: bold;
577
- color: #007bff;
 
578
  }
579
 
580
  .stat-label {
581
- color: #6c757d;
 
582
  }
583
 
584
  .wait-time {
585
  text-align: center;
586
- margin: 15px 0;
587
- padding: 10px;
588
- background: #e2e3e5;
589
- border-radius: 5px;
 
 
 
 
 
 
 
590
  }
591
 
592
  .recent-tasks {
593
  width: 100%;
594
  border-collapse: collapse;
595
- margin-top: 10px;
 
 
596
  }
597
 
598
  .recent-tasks th, .recent-tasks td {
599
- border: 1px solid #dee2e6;
600
- padding: 8px;
601
  text-align: center;
602
  }
603
 
604
  .recent-tasks th {
605
- background-color: #e9ecef;
 
 
 
 
 
 
606
  }
607
 
608
  .tabs {
609
  margin-top: 20px;
610
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
611
  """
612
 
613
  # 初始化并启动工作线程
@@ -618,41 +700,21 @@ with gr.Blocks(css=custom_css) as demo:
618
  gr.Markdown("# 代码评估服务")
619
  gr.Markdown("支持多种编程语言的代码评估服务,采用队列机制处理请求")
620
 
621
- with gr.Tab("提交任务"):
622
- with gr.Row():
623
- with gr.Column(scale=2):
624
- input_json = gr.JSON(label="输入数据")
625
- submit_btn = gr.Button("提交任务", variant="primary")
626
- task_id_output = gr.Textbox(label="任务ID", visible=True)
627
- status_html = gr.HTML(label="状态")
628
-
629
- with gr.Column(scale=1):
630
- queue_info_html = gr.HTML()
631
- refresh_queue_btn = gr.Button("刷新队列状态")
632
-
633
- with gr.Tab("查询任务"):
634
- with gr.Row():
635
- with gr.Column(scale=1):
636
- check_task_id = gr.Textbox(label="任务ID")
637
- check_btn = gr.Button("查询状态", variant="primary")
638
- get_result_btn = gr.Button("获取结果")
639
-
640
- with gr.Column(scale=2):
641
- check_status_html = gr.HTML()
642
- result_output = gr.JSON(label="任务结果")
643
 
644
  # 定义更新函数
645
  def update_queue_info():
646
  return ui_get_queue_info()
647
 
648
  # 定时更新队列信息
649
- demo.load(update_queue_info, None, queue_info_html, every=5)
650
 
651
- # 事件处理
652
- submit_btn.click(ui_submit, inputs=[input_json], outputs=[task_id_output, status_html])
653
  refresh_queue_btn.click(update_queue_info, None, queue_info_html)
654
- check_btn.click(ui_check_status, inputs=[check_task_id], outputs=[check_status_html])
655
- get_result_btn.click(ui_get_result, inputs=[check_task_id], outputs=[result_output])
656
 
657
  # 添加兼容原有接口的评估端点
658
  demo.queue()
 
453
  </tr>
454
  """
455
 
456
+ # 如果没有任务历史,显示提示信息
457
+ if not tasks_html:
458
+ tasks_html = """
459
+ <tr>
460
+ <td colspan="3" style="text-align: center; padding: 20px;">暂无历史任务</td>
461
+ </tr>
462
+ """
463
+
464
  # 添加平均处理时间显示
465
  avg_time_html = f"""<p><b>平均处理时间:</b> {format_time(queue_info.get('avg_process_time', 0))}</p>"""
466
 
467
  return f"""
468
+ <div class="dashboard">
469
+ <div class="queue-info-card main-card">
470
+ <h3 class="card-title">队列状态监控</h3>
471
+ <div class="queue-stats">
472
+ <div class="stat-item">
473
+ <div class="stat-value">{queue_info['waiting_tasks']}</div>
474
+ <div class="stat-label">等待中</div>
475
+ </div>
476
+ <div class="stat-item">
477
+ <div class="stat-value">{queue_info['active_tasks']}</div>
478
+ <div class="stat-label">处理中</div>
479
+ </div>
480
+ <div class="stat-item">
481
+ <div class="stat-value">{queue_info['worker_threads']}</div>
482
+ <div class="stat-label">工作线程</div>
483
+ </div>
484
  </div>
485
+
486
+ <div class="wait-time">
487
+ {avg_time_html}
488
+ <p><b>当前预计等待时间:</b> {format_time(queue_info['estimated_wait'])}</p>
489
+ <p class="last-update"><small>最后更新: {datetime.now().strftime('%H:%M:%S')}</small></p>
490
  </div>
491
  </div>
492
+
493
+ <div class="queue-info-card history-card">
494
+ <h3 class="card-title">最近处理的任务</h3>
495
+ <table class="recent-tasks">
496
+ <thead>
497
+ <tr>
498
+ <th>任务ID</th>
499
+ <th>请求时间</th>
500
+ <th>处理时间</th>
501
+ </tr>
502
+ </thead>
503
+ <tbody>
504
+ {tasks_html}
505
+ </tbody>
506
+ </table>
507
  </div>
 
 
 
 
 
 
 
 
 
 
508
  </div>
509
  """
510
 
 
543
  .container {
544
  max-width: 1200px;
545
  margin: 0 auto;
546
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
547
+ }
548
+
549
+ .dashboard {
550
+ display: flex;
551
+ flex-direction: column;
552
+ gap: 20px;
553
+ }
554
+
555
+ .card-title {
556
+ color: #333;
557
+ border-bottom: 2px solid #ddd;
558
+ padding-bottom: 10px;
559
+ margin-top: 0;
560
  }
561
 
562
  .status-card, .queue-info-card {
563
+ background: #fff;
564
+ border-radius: 12px;
565
+ padding: 20px;
566
  margin: 10px 0;
567
+ box-shadow: 0 4px 15px rgba(0,0,0,0.08);
568
+ }
569
+
570
+ .main-card {
571
+ border-top: 5px solid #4285f4;
572
+ }
573
+
574
+ .history-card {
575
+ border-top: 5px solid #34a853;
576
  }
577
 
578
  .status-card.success {
 
608
 
609
  .stat-item {
610
  text-align: center;
611
+ padding: 15px;
612
+ background: #f8f9fa;
613
+ border-radius: 10px;
614
+ min-width: 120px;
615
+ transition: transform 0.3s ease;
616
+ }
617
+
618
+ .stat-item:hover {
619
+ transform: translateY(-5px);
620
+ box-shadow: 0 5px 15px rgba(0,0,0,0.1);
621
  }
622
 
623
  .stat-value {
624
+ font-size: 32px;
625
  font-weight: bold;
626
+ color: #4285f4;
627
+ margin-bottom: 5px;
628
  }
629
 
630
  .stat-label {
631
+ color: #5f6368;
632
+ font-size: 16px;
633
  }
634
 
635
  .wait-time {
636
  text-align: center;
637
+ margin: 20px 0;
638
+ padding: 15px;
639
+ background: #f1f3f4;
640
+ border-radius: 8px;
641
+ font-size: 18px;
642
+ }
643
+
644
+ .last-update {
645
+ color: #80868b;
646
+ margin-top: 10px;
647
+ margin-bottom: 0;
648
  }
649
 
650
  .recent-tasks {
651
  width: 100%;
652
  border-collapse: collapse;
653
+ margin-top: 15px;
654
+ background: white;
655
+ box-shadow: 0 1px 3px rgba(0,0,0,0.05);
656
  }
657
 
658
  .recent-tasks th, .recent-tasks td {
659
+ border: 1px solid #e0e0e0;
660
+ padding: 12px 15px;
661
  text-align: center;
662
  }
663
 
664
  .recent-tasks th {
665
+ background-color: #f1f3f4;
666
+ color: #202124;
667
+ font-weight: 500;
668
+ }
669
+
670
+ .recent-tasks tbody tr:hover {
671
+ background-color: #f8f9fa;
672
  }
673
 
674
  .tabs {
675
  margin-top: 20px;
676
  }
677
+
678
+ button.primary {
679
+ background-color: #4285f4;
680
+ color: white;
681
+ padding: 10px 20px;
682
+ border: none;
683
+ border-radius: 4px;
684
+ cursor: pointer;
685
+ font-size: 16px;
686
+ font-weight: 500;
687
+ transition: background-color 0.3s;
688
+ }
689
+
690
+ button.primary:hover {
691
+ background-color: #3367d6;
692
+ }
693
  """
694
 
695
  # 初始化并启动工作线程
 
700
  gr.Markdown("# 代码评估服务")
701
  gr.Markdown("支持多种编程语言的代码评估服务,采用队列机制处理请求")
702
 
703
+ with gr.Row():
704
+ with gr.Column(scale=3):
705
+ # 队列状态信息卡片
706
+ queue_info_html = gr.HTML()
707
+ refresh_queue_btn = gr.Button("刷新队列状态", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
708
 
709
  # 定义更新函数
710
  def update_queue_info():
711
  return ui_get_queue_info()
712
 
713
  # 定时更新队列信息
714
+ demo.load(update_queue_info, None, queue_info_html, every=3)
715
 
716
+ # 刷新按钮事件
 
717
  refresh_queue_btn.click(update_queue_info, None, queue_info_html)
 
 
718
 
719
  # 添加兼容原有接口的评估端点
720
  demo.queue()