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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -27
app.py CHANGED
@@ -421,9 +421,9 @@ def create_demo():
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
  # 执行完整迭代
@@ -491,33 +491,54 @@ def create_demo():
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,7 +547,7 @@ def create_demo():
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,
 
421
  current_score = 0.0
422
 
423
  empty_md = "*等待开始...*"
424
+ empty_svg = '<svg width="400" height="250"><text x="200" y="130" text-anchor="middle" fill="#999">等待开始...</text></svg>'
425
  # 初始状态
426
+ yield "状态: 准备执行任务...", empty_svg, empty_md, empty_md, empty_md, empty_md
427
 
428
  for i in range(1, int(max_iter) + 1):
429
  # 执行完整迭代
 
491
  # 更新数据
492
  chart_data.append({"iteration": i, "score": round(current_score, 2)})
493
 
494
+ # 生成 HTML 折线图 - SVG 实现
495
+ if len(chart_data) == 1:
496
+ # 只有一个点,画一个点
497
+ svg = f'''
498
+ <svg width="400" height="250" style="border:1px solid #ccc; background:white;">
499
+ <text x="200" y="130" text-anchor="middle" fill="#666">分数: {chart_data[0]["score"]:.2f}</text>
500
+ <circle cx="50" cy="{200 - chart_data[0]["score"]*180}" r="8" fill="#22c55e"/>
501
+ </svg>
502
+ '''
503
+ else:
504
+ # 多个点,画折线
505
+ width = 400
506
+ height = 250
507
+ padding = 40
508
+ plot_width = width - padding * 2
509
+ plot_height = height - padding * 2
510
+
511
+ # 生成点和线的 SVG
512
+ points_svg = ""
513
+ lines_svg = ""
514
+ for idx, item in enumerate(chart_data):
515
+ x = padding + idx * (plot_width / (len(chart_data) - 1))
516
+ y = padding + plot_height - item["score"] * plot_height
517
+ points_svg += f'<circle cx="{x}" cy="{y}" r="6" fill="#22c55e" stroke="white" stroke-width="2"/>'
518
+ points_svg += f'<text x="{x}" y="{y-15}" text-anchor="middle" font-size="12" fill="#333">{item["score"]:.2f}</text>'
519
+ if idx > 0:
520
+ prev_x = padding + (idx - 1) * (plot_width / (len(chart_data) - 1))
521
+ prev_y = padding + plot_height - chart_data[idx-1]["score"] * plot_height
522
+ lines_svg += f'<line x1="{prev_x}" y1="{prev_y}" x2="{x}" y2="{y}" stroke="#22c55e" stroke-width="3"/>'
523
+
524
+ # 添加坐标轴
525
+ svg = f'''
526
+ <svg width="{width}" height="{height}" style="border:1px solid #ccc; background:white; border-radius:8px;">
527
+ <!-- Y轴标签 -->
528
+ <text x="15" y="50" font-size="12" fill="#666">1.0</text>
529
+ <text x="15" y="{padding + plot_height/2}" font-size="12" fill="#666">0.5</text>
530
+ <text x="15" y="{height-20}" font-size="12" fill="#666">0.0</text>
531
+ <!-- X轴标签 -->
532
+ <text x="{width/2}" y="{height-5}" font-size="12" fill="#666" text-anchor="middle">迭代次数</text>
533
+ <!-- 折线 -->
534
+ {lines_svg}
535
+ {points_svg}
536
+ </svg>
537
+ '''
538
 
539
  # 每次迭代完成后更新 UI
540
  status = f"状态: 第 {i}/{int(max_iter)} 次迭代完成 (分数: {current_score:.2f})"
541
+ yield status, svg, plan_md, exec1_md, exec2_md, summary_md
542
 
543
  # 检查是否达到目标
544
  if current_score >= target:
 
547
  time.sleep(0.3)
548
 
549
  final_status = f"状态: 任务完成\n最终分数: {current_score:.2f}\n总迭代次数: {len(chart_data)}"
550
+ yield final_status, svg, plan_md, exec1_md, exec2_md, summary_md
551
 
552
  run_btn.click(
553
  fn=run_task,