Spaces:
Sleeping
Sleeping
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
app.py
CHANGED
|
@@ -67,13 +67,15 @@ agent = CodeAgent(
|
|
| 67 |
prompt_templates=prompt_templates
|
| 68 |
)
|
| 69 |
|
| 70 |
-
# ==========
|
|
|
|
| 71 |
@spaces.GPU
|
| 72 |
-
def
|
| 73 |
-
return agent.run(
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 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()
|
|
|