Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,10 @@ LoongFlow HuggingFace Spaces Demo
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
-
import
|
| 10 |
import time
|
| 11 |
import random
|
| 12 |
-
from typing import List, Dict, Any
|
| 13 |
|
| 14 |
# ============================================================================
|
| 15 |
# PES 工作流程模拟
|
|
@@ -98,7 +98,7 @@ def solve_task(task):
|
|
| 98 |
"role": "Executor",
|
| 99 |
"action": "编写并执行代码",
|
| 100 |
"code": code,
|
| 101 |
-
"result": "代码执行成功,所有测试通过
|
| 102 |
"timestamp": time.strftime("%H:%M:%S")
|
| 103 |
}
|
| 104 |
|
|
@@ -132,14 +132,14 @@ def simulate_summary(iteration: int, score: float) -> Dict[str, Any]:
|
|
| 132 |
}
|
| 133 |
|
| 134 |
|
| 135 |
-
def run_pes_iteration(task: str, iteration: int, current_score: float) -> List[Dict[str, Any]]:
|
| 136 |
"""运行一次完整的 PES 迭代"""
|
| 137 |
results = []
|
| 138 |
|
| 139 |
# Phase 1: Plan
|
| 140 |
planner_result = simulate_planner(task)
|
| 141 |
results.append({
|
| 142 |
-
"phase": "
|
| 143 |
"content": planner_result["thought"],
|
| 144 |
"detail": planner_result["plan"],
|
| 145 |
"timestamp": planner_result["timestamp"]
|
|
@@ -148,18 +148,18 @@ def run_pes_iteration(task: str, iteration: int, current_score: float) -> List[D
|
|
| 148 |
# Phase 2: Execute
|
| 149 |
executor_result = simulate_executor(task, planner_result["plan"])
|
| 150 |
results.append({
|
| 151 |
-
"phase": "
|
| 152 |
"content": executor_result["action"],
|
| 153 |
-
"detail": f"{executor_result['code']}\n\n
|
| 154 |
"timestamp": executor_result["timestamp"]
|
| 155 |
})
|
| 156 |
|
| 157 |
# Phase 3: Summary
|
| 158 |
summary_result = simulate_summary(iteration, current_score)
|
| 159 |
results.append({
|
| 160 |
-
"phase": "
|
| 161 |
"content": summary_result["reflection"],
|
| 162 |
-
"detail": f"
|
| 163 |
"timestamp": summary_result["timestamp"]
|
| 164 |
})
|
| 165 |
|
|
@@ -175,7 +175,7 @@ def create_demo():
|
|
| 175 |
|
| 176 |
with gr.Blocks(title="LoongFlow PES Demo", theme=gr.themes.Soft()) as demo:
|
| 177 |
gr.Markdown("""
|
| 178 |
-
#
|
| 179 |
|
| 180 |
**LoongFlow** 是一个进化式 Agent 开发框架,采用 **PES (Plan-Execute-Summary)** 思考范式。
|
| 181 |
|
|
@@ -186,16 +186,10 @@ def create_demo():
|
|
| 186 |
### PES 工作流程
|
| 187 |
|
| 188 |
```
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
│ │
|
| 194 |
-
│ 迭代改进 ◀───────────────────┘
|
| 195 |
-
│
|
| 196 |
-
┌─────────────┐
|
| 197 |
-
│ 目标达成 │
|
| 198 |
-
└─────────────┘
|
| 199 |
```
|
| 200 |
""")
|
| 201 |
|
|
@@ -217,85 +211,75 @@ def create_demo():
|
|
| 217 |
label="目标分数"
|
| 218 |
)
|
| 219 |
|
| 220 |
-
run_btn = gr.Button("
|
| 221 |
|
| 222 |
with gr.Column(scale=1):
|
| 223 |
-
status_output = gr.
|
| 224 |
label="执行状态",
|
| 225 |
-
|
|
|
|
| 226 |
)
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
)
|
| 235 |
|
| 236 |
-
with gr.Accordion("
|
| 237 |
-
iteration_output = gr.
|
| 238 |
label="迭代历史",
|
| 239 |
-
|
|
|
|
| 240 |
)
|
| 241 |
|
| 242 |
-
# 状态变量
|
| 243 |
-
iterations_data = []
|
| 244 |
-
|
| 245 |
def run_task(task: str, max_iter: int, target: float):
|
| 246 |
-
if not task.strip():
|
| 247 |
-
yield
|
| 248 |
return
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
current_score = 0.0
|
| 254 |
-
all_results = []
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
}, [], gr.update(visible=True)
|
| 262 |
|
| 263 |
results, current_score = run_pes_iteration(task, i, current_score)
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
|
| 266 |
# 更新图表数据
|
| 267 |
-
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
"iteration": i,
|
| 272 |
-
"score": current_score
|
| 273 |
-
}, all_results, gr.update(
|
| 274 |
-
value=iterations_data,
|
| 275 |
-
visible=True
|
| 276 |
-
)
|
| 277 |
|
| 278 |
# 检查是否达到目标
|
| 279 |
if current_score >= target:
|
| 280 |
break
|
| 281 |
|
| 282 |
-
time.sleep(0.
|
| 283 |
|
| 284 |
-
final_status = {
|
| 285 |
-
|
| 286 |
-
"final_score": current_score,
|
| 287 |
-
"iterations": len(iterations_data)
|
| 288 |
-
}
|
| 289 |
-
|
| 290 |
-
yield final_status, all_results, gr.update(
|
| 291 |
-
value=iterations_data,
|
| 292 |
-
visible=True
|
| 293 |
-
)
|
| 294 |
|
| 295 |
run_btn.click(
|
| 296 |
fn=run_task,
|
| 297 |
inputs=[task_input, max_iterations, target_score],
|
| 298 |
-
outputs=[status_output,
|
| 299 |
)
|
| 300 |
|
| 301 |
gr.Markdown("""
|
|
@@ -305,10 +289,10 @@ def create_demo():
|
|
| 305 |
|
| 306 |
LoongFlow 是一个面向复杂任务的进化式 Agent 框架,特别适用于:
|
| 307 |
|
| 308 |
-
-
|
| 309 |
-
-
|
| 310 |
-
-
|
| 311 |
-
-
|
| 312 |
|
| 313 |
了解更多: [GitHub](https://github.com/your-repo/loongflow)
|
| 314 |
""")
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
+
import pandas as pd
|
| 10 |
import time
|
| 11 |
import random
|
| 12 |
+
from typing import List, Dict, Any, Tuple
|
| 13 |
|
| 14 |
# ============================================================================
|
| 15 |
# PES 工作流程模拟
|
|
|
|
| 98 |
"role": "Executor",
|
| 99 |
"action": "编写并执行代码",
|
| 100 |
"code": code,
|
| 101 |
+
"result": "代码执行成功,所有测试通过",
|
| 102 |
"timestamp": time.strftime("%H:%M:%S")
|
| 103 |
}
|
| 104 |
|
|
|
|
| 132 |
}
|
| 133 |
|
| 134 |
|
| 135 |
+
def run_pes_iteration(task: str, iteration: int, current_score: float) -> Tuple[List[Dict[str, Any]], float]:
|
| 136 |
"""运行一次完整的 PES 迭代"""
|
| 137 |
results = []
|
| 138 |
|
| 139 |
# Phase 1: Plan
|
| 140 |
planner_result = simulate_planner(task)
|
| 141 |
results.append({
|
| 142 |
+
"phase": "Plan (计划)",
|
| 143 |
"content": planner_result["thought"],
|
| 144 |
"detail": planner_result["plan"],
|
| 145 |
"timestamp": planner_result["timestamp"]
|
|
|
|
| 148 |
# Phase 2: Execute
|
| 149 |
executor_result = simulate_executor(task, planner_result["plan"])
|
| 150 |
results.append({
|
| 151 |
+
"phase": "Execute (执行)",
|
| 152 |
"content": executor_result["action"],
|
| 153 |
+
"detail": f"{executor_result['code']}\n\n执行结果: {executor_result['result']}",
|
| 154 |
"timestamp": executor_result["timestamp"]
|
| 155 |
})
|
| 156 |
|
| 157 |
# Phase 3: Summary
|
| 158 |
summary_result = simulate_summary(iteration, current_score)
|
| 159 |
results.append({
|
| 160 |
+
"phase": "Summary (总结)",
|
| 161 |
"content": summary_result["reflection"],
|
| 162 |
+
"detail": f"改进建议: {summary_result['improvement']}\n\n当前分数: {summary_result['score']:.2f}",
|
| 163 |
"timestamp": summary_result["timestamp"]
|
| 164 |
})
|
| 165 |
|
|
|
|
| 175 |
|
| 176 |
with gr.Blocks(title="LoongFlow PES Demo", theme=gr.themes.Soft()) as demo:
|
| 177 |
gr.Markdown("""
|
| 178 |
+
# LoongFlow PES Agent Demo
|
| 179 |
|
| 180 |
**LoongFlow** 是一个进化式 Agent 开发框架,采用 **PES (Plan-Execute-Summary)** 思考范式。
|
| 181 |
|
|
|
|
| 186 |
### PES 工作流程
|
| 187 |
|
| 188 |
```
|
| 189 |
+
Plan (计划) --> Execute (执行) --> Summary (总结)
|
| 190 |
+
^ |
|
| 191 |
+
|____________________________________|
|
| 192 |
+
迭代改进
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
```
|
| 194 |
""")
|
| 195 |
|
|
|
|
| 211 |
label="目标分数"
|
| 212 |
)
|
| 213 |
|
| 214 |
+
run_btn = gr.Button("开始执行任务", variant="primary")
|
| 215 |
|
| 216 |
with gr.Column(scale=1):
|
| 217 |
+
status_output = gr.Textbox(
|
| 218 |
label="执行状态",
|
| 219 |
+
lines=5,
|
| 220 |
+
interactive=False
|
| 221 |
)
|
| 222 |
|
| 223 |
+
score_chart = gr.LinePlot(
|
| 224 |
+
label="分数演进",
|
| 225 |
+
x="iteration",
|
| 226 |
+
y="score",
|
| 227 |
+
height=300
|
| 228 |
+
)
|
|
|
|
| 229 |
|
| 230 |
+
with gr.Accordion("迭代详情", open=True):
|
| 231 |
+
iteration_output = gr.Textbox(
|
| 232 |
label="迭代历史",
|
| 233 |
+
lines=15,
|
| 234 |
+
interactive=False
|
| 235 |
)
|
| 236 |
|
|
|
|
|
|
|
|
|
|
| 237 |
def run_task(task: str, max_iter: int, target: float):
|
| 238 |
+
if not task or not task.strip():
|
| 239 |
+
yield "错误: 请输入任务描述", pd.DataFrame(columns=["iteration", "score"]), ""
|
| 240 |
return
|
| 241 |
|
| 242 |
+
all_results_text = ""
|
| 243 |
+
chart_data = []
|
|
|
|
| 244 |
current_score = 0.0
|
|
|
|
| 245 |
|
| 246 |
+
yield "状态: 开始执行任务...", pd.DataFrame(columns=["iteration", "score"]), ""
|
| 247 |
+
|
| 248 |
+
for i in range(1, int(max_iter) + 1):
|
| 249 |
+
status = f"状态: 正在执行第 {i} 次迭代..."
|
| 250 |
+
yield status, pd.DataFrame(chart_data) if chart_data else pd.DataFrame(columns=["iteration", "score"]), all_results_text
|
|
|
|
| 251 |
|
| 252 |
results, current_score = run_pes_iteration(task, i, current_score)
|
| 253 |
+
|
| 254 |
+
# 构建迭代结果文本
|
| 255 |
+
all_results_text += f"\n{'='*50}\n"
|
| 256 |
+
all_results_text += f"迭代 {i}\n"
|
| 257 |
+
all_results_text += f"{'='*50}\n"
|
| 258 |
+
|
| 259 |
+
for r in results:
|
| 260 |
+
all_results_text += f"\n[{r['timestamp']}] {r['phase']}\n"
|
| 261 |
+
all_results_text += f" {r['content']}\n"
|
| 262 |
+
all_results_text += f" {r['detail']}\n"
|
| 263 |
|
| 264 |
# 更新图表数据
|
| 265 |
+
chart_data.append({"iteration": i, "score": current_score})
|
| 266 |
|
| 267 |
+
status = f"状态: 第 {i} 次迭代完成 (分数: {current_score:.2f})"
|
| 268 |
+
yield status, pd.DataFrame(chart_data), all_results_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
# 检查是否达到目标
|
| 271 |
if current_score >= target:
|
| 272 |
break
|
| 273 |
|
| 274 |
+
time.sleep(0.3)
|
| 275 |
|
| 276 |
+
final_status = f"状态: 任务完成\n最终分数: {current_score:.2f}\n总迭代次数: {len(chart_data)}"
|
| 277 |
+
yield final_status, pd.DataFrame(chart_data), all_results_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
run_btn.click(
|
| 280 |
fn=run_task,
|
| 281 |
inputs=[task_input, max_iterations, target_score],
|
| 282 |
+
outputs=[status_output, score_chart, iteration_output]
|
| 283 |
)
|
| 284 |
|
| 285 |
gr.Markdown("""
|
|
|
|
| 289 |
|
| 290 |
LoongFlow 是一个面向复杂任务的进化式 Agent 框架,特别适用于:
|
| 291 |
|
| 292 |
+
- **数学推理**: 开放式数学问题求解
|
| 293 |
+
- **机器学习**: AutoML 和算法优化
|
| 294 |
+
- **代码生成**: 复杂编程任务
|
| 295 |
+
- **科学研究**: 实验设计和分析
|
| 296 |
|
| 297 |
了解更多: [GitHub](https://github.com/your-repo/loongflow)
|
| 298 |
""")
|