FreshmanD commited on
Commit
01d940e
·
verified ·
1 Parent(s): 1c12d40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -25
app.py CHANGED
@@ -393,19 +393,11 @@ def create_demo():
393
  interactive=False
394
  )
395
 
396
- # 分数演进图表
397
- score_chart = gr.LinePlot(
398
- label="分数演进",
399
- x="iteration",
400
- y="score",
401
- y_lim=[1.0, 0],
402
- x_title="迭代次数",
403
- y_title="分数",
404
- height=250
405
- )
406
 
407
- # 额外显示分数列表
408
- score_list = gr.JSON(label="分数历史")
409
 
410
  gr.Markdown("### 迭代详情")
411
 
@@ -422,20 +414,17 @@ def create_demo():
422
 
423
  def run_task(task: str, max_iter: int, target: float):
424
  if not task or not task.strip():
425
- yield "错误: 请输入任务描述", pd.DataFrame(columns=["iteration", "score"]), [], "", "", "", ""
426
  return
427
 
428
  chart_data = []
429
  current_score = 0.0
430
 
431
  empty_md = "*等待开始...*"
432
- # 状态:显示"准备中"
433
- yield "状态: 准备执行任务...", pd.DataFrame(columns=["iteration", "score"]), [], empty_md, empty_md, empty_md, empty_md
434
-
435
- # 存储所有迭代的完整历史
436
- all_history = []
437
 
438
- # 先执行完整的一次 PEES 迭代,然后一起更新
439
  for i in range(1, int(max_iter) + 1):
440
  # 执行完整迭代
441
  results, current_score = run_pees_iteration(task, i, current_score, target)
@@ -446,7 +435,7 @@ def create_demo():
446
  execute2_result = results[2]
447
  summary_result = results[3]
448
 
449
- # 格式化每个阶段的输出(更美观)
450
  plan_md = f"""### 迭代 {i} - Plan 计划
451
  **时间**: {plan_result['timestamp']}
452
 
@@ -499,12 +488,36 @@ def create_demo():
499
  </details>
500
  """
501
 
502
- # 更新图表数据
503
  chart_data.append({"iteration": i, "score": round(current_score, 2)})
504
 
505
- # 每次迭代完后才更新 UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
506
  status = f"状态: 第 {i}/{int(max_iter)} 次迭代完成 (分数: {current_score:.2f})"
507
- yield status, pd.DataFrame(chart_data), chart_data, plan_md, exec1_md, exec2_md, summary_md
508
 
509
  # 检查是否达到目标
510
  if current_score >= target:
@@ -513,12 +526,12 @@ def create_demo():
513
  time.sleep(0.3)
514
 
515
  final_status = f"状态: 任务完成\n最终分数: {current_score:.2f}\n总迭代次数: {len(chart_data)}"
516
- yield final_status, pd.DataFrame(chart_data), chart_data, plan_md, exec1_md, exec2_md, summary_md
517
 
518
  run_btn.click(
519
  fn=run_task,
520
  inputs=[task_input, max_iterations, target_score],
521
- outputs=[status_output, score_chart, score_list, plan_output, execute1_output, execute2_output, summary_output]
522
  )
523
 
524
  gr.Markdown("""
 
393
  interactive=False
394
  )
395
 
396
+ # 分数演进 - 用 HTML 进度条
397
+ score_display = gr.HTML(label="分数演进")
 
 
 
 
 
 
 
 
398
 
399
+ # 分数历史
400
+ score_list = gr.JSON(label="分数历史", visible=False)
401
 
402
  gr.Markdown("### 迭代详情")
403
 
 
414
 
415
  def run_task(task: str, max_iter: int, target: float):
416
  if not task or not task.strip():
417
+ yield "错误: 请输入任务描述", "", "", "", "", ""
418
  return
419
 
420
  chart_data = []
421
  current_score = 0.0
422
 
423
  empty_md = "*等待开始...*"
424
+ empty_html = "<p>等待开...</p>"
425
+ # 初始状态
426
+ yield "状态: 准备执行任务...", empty_html, empty_md, empty_md, empty_md, empty_md
 
 
427
 
 
428
  for i in range(1, int(max_iter) + 1):
429
  # 执行完整迭代
430
  results, current_score = run_pees_iteration(task, i, current_score, target)
 
435
  execute2_result = results[2]
436
  summary_result = results[3]
437
 
438
+ # 格式化每个阶段的输出
439
  plan_md = f"""### 迭代 {i} - Plan 计划
440
  **时间**: {plan_result['timestamp']}
441
 
 
488
  </details>
489
  """
490
 
491
+ # 更新数据
492
  chart_data.append({"iteration": i, "score": round(current_score, 2)})
493
 
494
+ # HTML 进度条 - 完全可控
495
+ bars_html = "<div style='display: flex; gap: 10px; align-items: flex-end; height: 200px; padding: 20px; background: #f9f9f9; border-radius: 8px;'>"
496
+ for item in chart_data:
497
+ score_val = item["score"]
498
+ iter_num = item["iteration"]
499
+ # 颜色:低=红,中=黄,高=绿
500
+ if score_val < 0.4:
501
+ color = "#ef4444"
502
+ elif score_val < 0.7:
503
+ color = "#eab308"
504
+ else:
505
+ color = "#22c55e"
506
+ bar_height = score_val * 100
507
+ bars_html += f"""
508
+ <div style='display: flex; flex-direction: column; align-items: center; flex: 1;'>
509
+ <div style='font-weight: bold; margin-bottom: 5px;'>{score_val:.2f}</div>
510
+ <div style='width: 100%; background: #e5e7eb; border-radius: 4px; height: 150px; position: relative;'>
511
+ <div style='position: absolute; bottom: 0; width: 100%; background: {color}; border-radius: 4px; height: {bar_height}%; transition: height 0.3s;'></div>
512
+ </div>
513
+ <div style='margin-top: 5px;'>迭代{iter_num}</div>
514
+ </div>
515
+ """
516
+ bars_html += "</div>"
517
+
518
+ # 每次迭代完成后更新 UI
519
  status = f"状态: 第 {i}/{int(max_iter)} 次迭代完成 (分数: {current_score:.2f})"
520
+ yield status, bars_html, plan_md, exec1_md, exec2_md, summary_md
521
 
522
  # 检查是否达到目标
523
  if current_score >= target:
 
526
  time.sleep(0.3)
527
 
528
  final_status = f"状态: 任务完成\n最终分数: {current_score:.2f}\n总迭代次数: {len(chart_data)}"
529
+ yield final_status, bars_html, plan_md, exec1_md, exec2_md, summary_md
530
 
531
  run_btn.click(
532
  fn=run_task,
533
  inputs=[task_input, max_iterations, target_score],
534
+ outputs=[status_output, score_display, plan_output, execute1_output, execute2_output, summary_output]
535
  )
536
 
537
  gr.Markdown("""