s1123725 commited on
Commit
b7c912b
·
verified ·
1 Parent(s): 4c6c550

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -48
app.py CHANGED
@@ -3,13 +3,10 @@ import gradio as gr
3
  import requests
4
  import pandas as pd
5
 
6
- # -------------------------------
7
- # Constants
8
- # -------------------------------
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # -------------------------------
12
- # Basic Agent (可以換成你的 GAIA Agent)
13
  # -------------------------------
14
  class BasicAgent:
15
  def __init__(self):
@@ -17,70 +14,53 @@ class BasicAgent:
17
 
18
  def __call__(self, question: str) -> str:
19
  print(f"Agent received question (first 50 chars): {question[:50]}...")
20
- # TODO: 這裡改成你的 GAIA Agent 邏輯
21
  return "This is a default answer."
22
 
23
  # -------------------------------
24
  # Run & Submit Function
25
  # -------------------------------
26
  def run_and_submit_all(profile=None):
27
- """
28
- Fetch questions, run the agent, submit answers, return results
29
- profile: Either HF profile (from LoginButton) or mock_profile dict
30
- """
31
- # --- HF Username ---
32
  if profile and hasattr(profile, "username"):
33
  username = profile.username
34
- elif isinstance(profile, dict) and "username" in profile:
35
- username = profile["username"]
36
  else:
37
- return "❌ Please login with your Hugging Face account.", None
 
38
 
39
- # --- Agent & Code Link ---
40
  agent = BasicAgent()
41
  space_id = os.getenv("SPACE_ID", "unknown")
42
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
43
 
44
- # --- Fetch Questions ---
45
  try:
46
- response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
47
- response.raise_for_status()
48
- questions_data = response.json()
49
  except Exception as e:
50
  return f"❌ Failed to fetch questions: {e}", None
51
 
52
- if not questions_data:
53
  return "❌ No questions fetched.", None
54
 
55
- # --- Run Agent ---
56
  results_log = []
57
  answers_payload = []
58
- for item in questions_data:
59
  task_id = item.get("task_id")
60
  question_text = item.get("question")
61
  if not task_id or not question_text:
62
  continue
63
- try:
64
- ans = agent(question_text)
65
- answers_payload.append({"task_id": task_id, "submitted_answer": ans})
66
- results_log.append({
67
- "Task ID": task_id,
68
- "Question": question_text,
69
- "Submitted Answer": ans
70
- })
71
- except Exception as e:
72
- results_log.append({
73
- "Task ID": task_id,
74
- "Question": question_text,
75
- "Submitted Answer": f"AGENT ERROR: {e}"
76
- })
77
-
78
- if not answers_payload:
79
- return "❌ Agent did not produce any answers.", pd.DataFrame(results_log)
80
-
81
- # --- Submit Answers ---
82
  submission_data = {
83
- "username": username.strip(),
84
  "agent_code": agent_code,
85
  "answers": answers_payload
86
  }
@@ -107,26 +87,23 @@ with gr.Blocks() as demo:
107
  gr.Markdown("# Basic GAIA Agent Evaluation Runner")
108
  gr.Markdown(
109
  """
110
- 1. Log in with Hugging Face (Space only) or test locally with mock user.
 
111
  2. Click 'Run Evaluation & Submit All Answers'.
112
  """
113
  )
114
 
115
- # HF LoginButton only works in HF Space
116
  login_btn = gr.LoginButton()
117
-
118
  run_button = gr.Button("Run Evaluation & Submit All Answers")
119
-
120
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
121
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
122
 
123
- # For HF Space, login_btn gives profile; locally we can pass mock_profile
124
  run_button.click(
125
- fn=lambda profile: run_and_submit_all(profile or {"username": "local_user"}),
126
  inputs=login_btn,
127
  outputs=[status_output, results_table]
128
  )
129
 
130
  if __name__ == "__main__":
131
- print("Launching Gradio Interface...")
132
  demo.launch(debug=True, share=False)
 
3
  import requests
4
  import pandas as pd
5
 
 
 
 
6
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
7
 
8
  # -------------------------------
9
+ # Basic GAIA Agent
10
  # -------------------------------
11
  class BasicAgent:
12
  def __init__(self):
 
14
 
15
  def __call__(self, question: str) -> str:
16
  print(f"Agent received question (first 50 chars): {question[:50]}...")
 
17
  return "This is a default answer."
18
 
19
  # -------------------------------
20
  # Run & Submit Function
21
  # -------------------------------
22
  def run_and_submit_all(profile=None):
23
+ # HF Space Login Profile or local mock
 
 
 
 
24
  if profile and hasattr(profile, "username"):
25
  username = profile.username
 
 
26
  else:
27
+ # 本地測試用 mock profile
28
+ username = "local_user"
29
 
 
30
  agent = BasicAgent()
31
  space_id = os.getenv("SPACE_ID", "unknown")
32
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
33
 
34
+ # Fetch Questions
35
  try:
36
+ resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
37
+ resp.raise_for_status()
38
+ questions = resp.json()
39
  except Exception as e:
40
  return f"❌ Failed to fetch questions: {e}", None
41
 
42
+ if not questions:
43
  return "❌ No questions fetched.", None
44
 
45
+ # Run Agent
46
  results_log = []
47
  answers_payload = []
48
+ for item in questions:
49
  task_id = item.get("task_id")
50
  question_text = item.get("question")
51
  if not task_id or not question_text:
52
  continue
53
+ ans = agent(question_text)
54
+ answers_payload.append({"task_id": task_id, "submitted_answer": ans})
55
+ results_log.append({
56
+ "Task ID": task_id,
57
+ "Question": question_text,
58
+ "Submitted Answer": ans
59
+ })
60
+
61
+ # Submit
 
 
 
 
 
 
 
 
 
 
62
  submission_data = {
63
+ "username": username,
64
  "agent_code": agent_code,
65
  "answers": answers_payload
66
  }
 
87
  gr.Markdown("# Basic GAIA Agent Evaluation Runner")
88
  gr.Markdown(
89
  """
90
+ **Instructions:**
91
+ 1. Log in with Hugging Face (Space only) or test locally with mock user.
92
  2. Click 'Run Evaluation & Submit All Answers'.
93
  """
94
  )
95
 
 
96
  login_btn = gr.LoginButton()
 
97
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
98
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
99
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
100
 
101
+ # HF Space or local: 使用 lambda 包裝 profile
102
  run_button.click(
103
+ fn=lambda profile: run_and_submit_all(profile),
104
  inputs=login_btn,
105
  outputs=[status_output, results_table]
106
  )
107
 
108
  if __name__ == "__main__":
 
109
  demo.launch(debug=True, share=False)