stevengu9999 commited on
Commit
d9a862c
·
verified ·
1 Parent(s): 179d97b

Update app.py

Browse files

## 原理说明

1. 不再修改 agent 实例本身,**避免 pickle 序列化跨进程报错**(之前 TypeError 元凶)
2. `@spaces.GPU` 加在 UI 的 predict 入口函数,ZeroGPU 启动扫描代码时,检测到这个装饰器,消除`No @spaces.GPU function detected`报错
3. 当你在网页输入问题,Gradio 触发`ui.predict`,自动进入 ZeroGPU worker 分配 GPU,给文生图工具使用
4. agent 对象只在主进程创建,**不会被序列化送到子进程**,完美规避 pickle TypeError

Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -67,13 +67,15 @@ agent = CodeAgent(
67
  prompt_templates=prompt_templates
68
  )
69
 
70
- # ========== ZeroGPU 包装函数,放在agent实例创建之后 ==========
 
71
  @spaces.GPU
72
- def agent_run_wrapper(query):
73
- return agent.run(query)
74
 
75
- # 重写agent.run,调用带GPU装饰器的函数
76
- agent.run = agent_run_wrapper
 
 
77
 
78
- # 去掉ssr=False!旧版gradio不支持这个参数
79
- GradioUI(agent).launch()
 
67
  prompt_templates=prompt_templates
68
  )
69
 
70
+ # ====================== 【核心修复】======================
71
+ # 不要修改agent.run!我们新建带GPU装饰器的入口函数
72
  @spaces.GPU
73
+ def run_agent_query(user_input):
74
+ return agent.run(user_input)
75
 
76
+ # 覆盖GradioUI内部的submit函数,调用我们带@spaces.GPU的包装函数
77
+ ui = GradioUI(agent)
78
+ # 替换ui内部的predict函数
79
+ ui.predict = run_agent_query
80
 
81
+ ui.launch()