File size: 8,361 Bytes
10e9b7d 23060b1 10e9b7d 3c4371f 23060b1 10e9b7d 6c511d6 d6b82a1 23060b1 31243f4 6c511d6 31243f4 6c511d6 3c4371f 7e4a06b 23060b1 7e4a06b 23060b1 3c4371f 7e4a06b 31243f4 e80aab9 31243f4 6c511d6 31243f4 23060b1 c1fd3d2 3c4371f 23060b1 eccf8e4 31243f4 7d65c66 31243f4 23060b1 e80aab9 23060b1 3c4371f 23060b1 7d65c66 23060b1 e80aab9 7d65c66 23060b1 31243f4 23060b1 31243f4 23060b1 31243f4 6c511d6 23060b1 6c511d6 23060b1 31243f4 23060b1 6c511d6 23060b1 31243f4 23060b1 31243f4 23060b1 31243f4 e80aab9 23060b1 e80aab9 7d65c66 e80aab9 31243f4 23060b1 e80aab9 23060b1 6c511d6 e80aab9 23060b1 e80aab9 23060b1 e80aab9 23060b1 31243f4 6c511d6 3c4371f 23060b1 3c4371f 6c511d6 e80aab9 23060b1 31243f4 6c511d6 7d65c66 23060b1 31243f4 6c511d6 e80aab9 d6b82a1 0ee0419 6c511d6 e514fd7 d6b82a1 e514fd7 6c511d6 d6b82a1 e514fd7 e80aab9 7e4a06b 23060b1 6c511d6 e80aab9 23060b1 e80aab9 23060b1 3c4371f 6c511d6 7d65c66 3c4371f 23060b1 3c4371f 23060b1 7d65c66 6c511d6 23060b1 7d65c66 23060b1 7d65c66 23060b1 d6b82a1 23060b1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | import os
import gradio as gr
import pandas as pd
import requests
from agent import GaiaAgent
from config import (
DEFAULT_API_URL,
HF_PLANNER_USE_RESPONSE_FORMAT,
HF_TEXT_MODEL,
HF_VISION_MODEL,
)
def run_and_submit_all(profile: gr.OAuthProfile | None):
"""
拉取全部问题,使用 GaiaAgent 逐题作答,提交全部答案,并展示评测结果。
"""
space_id = os.getenv("SPACE_ID")
if profile:
username = f"{profile.username}"
print(f"用户已登录:{username}")
else:
print("用户未登录。")
return "请先点击按钮登录 Hugging Face。", None
api_url = DEFAULT_API_URL
questions_url = f"{api_url}/questions"
submit_url = f"{api_url}/submit"
try:
agent = GaiaAgent()
except Exception as e:
print(f"实例化 Agent 时出错:{e}")
return f"Agent 初始化失败:{e}", None
agent_code = (
f"https://huggingface.co/spaces/{space_id}/tree/main"
if space_id
else "本地运行,未提供 SPACE_ID。"
)
print(agent_code)
print(f"正在从以下地址拉取问题:{questions_url}")
try:
response = requests.get(questions_url, timeout=15)
response.raise_for_status()
questions_data = response.json()
if not questions_data:
print("拉取到的问题列表为空。")
return "拉取到的问题列表为空,或返回格式无效。", None
print(f"已拉取 {len(questions_data)} 个问题。")
except requests.exceptions.RequestException as e:
print(f"拉取问题时发生网络错误:{e}")
return f"拉取问题失败:{e}", None
except requests.exceptions.JSONDecodeError as e:
print(f"解析问题接口的 JSON 响应时出错:{e}")
print(f"响应文本:{response.text[:500]}")
return f"解析问题接口响应失败:{e}", None
except Exception as e:
print(f"拉取问题时发生未预期错误:{e}")
return f"拉取问题时发生未预期错误:{e}", None
results_log = []
answers_payload = []
print(f"正在让 Agent 处理 {len(questions_data)} 个问题...")
for item in questions_data:
task_id = item.get("task_id")
question_text = item.get("question")
file_name = item.get("file_name") or ""
if not task_id or question_text is None:
print(f"跳过缺少 task_id 或 question 的条目:{item}")
continue
try:
result = agent.answer_task(question_text, task_id=task_id, file_name=file_name)
submitted_answer = result.answer or "无法确定"
answers_payload.append(
{"task_id": task_id, "submitted_answer": submitted_answer}
)
results_log.append(
{
"任务 ID": task_id,
"附件": file_name,
"问题": question_text,
"提交答案": submitted_answer,
"来源": result.source,
"置信度": result.confidence,
"错误": result.error,
}
)
except Exception as e:
print(f"Agent 处理任务 {task_id} 时出错:{e}")
results_log.append(
{
"任务 ID": task_id,
"附件": file_name,
"问题": question_text,
"提交答案": f"AGENT ERROR: {e}",
"来源": "agent.exception",
"置信度": "low",
"错误": str(e),
}
)
if not answers_payload:
print("Agent 没有生成任何可提交的答案。")
return "Agent 没有生成任何可提交的答案。", pd.DataFrame(results_log)
submission_data = {
"username": username.strip(),
"agent_code": agent_code,
"answers": answers_payload,
}
status_update = f"Agent 已完成作答。正在为用户 '{username}' 提交 {len(answers_payload)} 个答案..."
print(status_update)
print(f"正在向以下地址提交 {len(answers_payload)} 个答案:{submit_url}")
try:
response = requests.post(submit_url, json=submission_data, timeout=60)
response.raise_for_status()
result_data = response.json()
final_status = (
f"提交成功!\n"
f"用户:{result_data.get('username')}\n"
f"总分:{result_data.get('score', 'N/A')}% "
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} 正确)\n"
f"消息:{result_data.get('message', '未收到消息。')}"
)
print("提交成功。")
return final_status, pd.DataFrame(results_log)
except requests.exceptions.HTTPError as e:
error_detail = f"服务器返回状态码 {e.response.status_code}。"
try:
error_json = e.response.json()
error_detail += f" 详情:{error_json.get('detail', e.response.text)}"
except requests.exceptions.JSONDecodeError:
error_detail += f" 响应:{e.response.text[:500]}"
status_message = f"提交失败:{error_detail}"
print(status_message)
return status_message, pd.DataFrame(results_log)
except requests.exceptions.Timeout:
status_message = "提交失败:请求超时。"
print(status_message)
return status_message, pd.DataFrame(results_log)
except requests.exceptions.RequestException as e:
status_message = f"提交失败:网络错误 - {e}"
print(status_message)
return status_message, pd.DataFrame(results_log)
except Exception as e:
status_message = f"提交过程中发生未预期错误:{e}"
print(status_message)
return status_message, pd.DataFrame(results_log)
with gr.Blocks() as demo:
gr.Markdown("# LangGraph GAIA Agent 评测运行器")
gr.Markdown(
f"""
**当前策略:**
1. LangGraph 先调用 LLM 做题型分类,明确应该使用哪类工具。
2. 工作流按题型进入规则、Python、Excel、Wikipedia、体育统计、网页读取/搜索等处理节点。
3. 工具结果统一交给 LLM 做最终答案格式化;LLM 失败时使用工具候选答案兜底。
4. 未识别类型或工具无结果时进入兜底节点,按低成本工具顺序尝试。
5. 音频、视频和棋局图工具当前禁用;这类题会返回“无法确定”。
6. 当前文本模型:`{HF_TEXT_MODEL}`;当前视觉模型:`{HF_VISION_MODEL or "未启用"}`。
7. 强制 JSON response_format:`{HF_PLANNER_USE_RESPONSE_FORMAT}`。
**提交前检查:**
Space Secrets 至少需要 `HF_TOKEN`,且该 token 必须允许调用 Inference Providers。
"""
)
gr.LoginButton()
run_button = gr.Button("运行评测并提交全部答案")
status_output = gr.Textbox(label="运行状态 / 提交结果", lines=5, interactive=False)
results_table = gr.DataFrame(label="问题与 Agent 答案", wrap=True)
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
if __name__ == "__main__":
print("\n" + "-" * 30 + " 应用启动中 " + "-" * 30)
space_host_startup = os.getenv("SPACE_HOST")
space_id_startup = os.getenv("SPACE_ID")
if space_host_startup:
print(f"找到 SPACE_HOST:{space_host_startup}")
print(f" 运行地址应为:https://{space_host_startup}.hf.space")
else:
print("未找到 SPACE_HOST 环境变量(可能是在本地运行)。")
if space_id_startup:
print(f"找到 SPACE_ID:{space_id_startup}")
print(f" 仓库地址:https://huggingface.co/spaces/{space_id_startup}")
print(f" 代码树地址:https://huggingface.co/spaces/{space_id_startup}/tree/main")
else:
print("未找到 SPACE_ID 环境变量(可能是在本地运行)。无法确定仓库地址。")
print("-" * (60 + len(" 应用启动中 ")) + "\n")
print("正在启动 LangGraph GAIA Agent 评测 Gradio 界面...")
demo.launch(debug=True, share=False)
|