lethaq commited on
Commit
d10d5ac
·
verified ·
1 Parent(s): c70edc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -7,7 +7,7 @@ import requests
7
  import pandas as pd
8
  from dotenv import load_dotenv
9
  from agent import Agent # 引入你自己写的简易 agent.py
10
-
11
  # 加载 .env 中的 GOOGLE_API_KEY(agent.py 会使用)
12
  load_dotenv()
13
 
@@ -44,38 +44,33 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
44
  return f"Error fetching questions: {e}", None
45
 
46
  # 2. 遍历题目并调用 Agent 获取答案
47
- results_log = []
48
  answers_payload = []
49
 
50
  for item in questions_data:
51
- task_id = item.get("task_id")
52
- question_text = item.get("question")
53
- if not task_id or question_text is None:
54
  continue
55
-
56
  try:
57
- # 调用你在 agent.py 中定义的 Agent
58
- submitted_answer = agent(question_text)
59
 
60
  answers_payload.append({
61
  "task_id": task_id,
62
- "submitted_answer": submitted_answer
63
  })
64
  results_log.append({
65
  "Task ID": task_id,
66
- "Question": question_text,
67
- "Submitted Answer": submitted_answer
68
  })
69
 
70
- # 避免 API 速率限制
71
- time.sleep(0.5)
72
  except Exception as e:
73
  err = f"ERROR: {e}"
74
- results_log.append({
75
- "Task ID": task_id,
76
- "Question": question_text,
77
- "Submitted Answer": err
78
- })
79
 
80
  if not answers_payload:
81
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
7
  import pandas as pd
8
  from dotenv import load_dotenv
9
  from agent import Agent # 引入你自己写的简易 agent.py
10
+ agent = Agent()
11
  # 加载 .env 中的 GOOGLE_API_KEY(agent.py 会使用)
12
  load_dotenv()
13
 
 
44
  return f"Error fetching questions: {e}", None
45
 
46
  # 2. 遍历题目并调用 Agent 获取答案
47
+ results_log = []
48
  answers_payload = []
49
 
50
  for item in questions_data:
51
+ task_id = item.get("task_id")
52
+ question = item.get("question")
53
+ if not task_id or question is None:
54
  continue
 
55
  try:
56
+ # 只调用一次,带 task_id
57
+ answer = agent(question, task_id=task_id)
58
 
59
  answers_payload.append({
60
  "task_id": task_id,
61
+ "submitted_answer": answer
62
  })
63
  results_log.append({
64
  "Task ID": task_id,
65
+ "Question": question,
66
+ "Submitted Answer": answer
67
  })
68
 
69
+ time.sleep(0.3) # 小延迟防止 QPS 超限
 
70
  except Exception as e:
71
  err = f"ERROR: {e}"
72
+ answers_payload.append({"task_id": task_id, "submitted_answer": err})
73
+ results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": err})
 
 
 
74
 
75
  if not answers_payload:
76
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)