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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -57
app.py CHANGED
@@ -3,103 +3,130 @@ import gradio as gr
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):
13
  print("BasicAgent initialized.")
14
 
15
  def __call__(self, question: str) -> str:
 
 
16
  return "This is a default answer."
17
 
18
- # -------------------------
19
  # Run & Submit Function
20
- # -------------------------
21
- def run_and_submit_all(profile):
22
- if not profile:
 
 
 
 
 
 
 
 
 
23
  return "❌ Please login with your Hugging Face account.", None
24
 
25
- username = profile.get("username", "unknown_user")
26
- print(f"Logged in as: {username}")
27
-
28
- space_id = os.getenv("SPACE_ID", "unknown-space")
29
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
30
-
31
- # Instantiate Agent
32
  agent = BasicAgent()
 
 
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
- if not questions:
40
- return "No questions fetched.", pd.DataFrame()
41
  except Exception as e:
42
- return f"Error fetching questions: {e}", pd.DataFrame()
43
 
44
- # Run Agent
 
 
 
45
  results_log = []
46
  answers_payload = []
47
- for q in questions:
48
- task_id = q.get("task_id")
49
- question_text = q.get("question")
50
- if not task_id or question_text is None:
51
  continue
52
  try:
53
- answer = agent(question_text)
 
 
 
 
 
 
54
  except Exception as e:
55
- answer = f"AGENT ERROR: {e}"
56
- answers_payload.append({"task_id": task_id, "submitted_answer": answer})
57
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- # Submit answers
60
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
61
  try:
62
  resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
63
  resp.raise_for_status()
64
- result = resp.json()
65
- status = (
66
- f"Submission Complete!\n"
67
- f"User: {result.get('username')}\n"
68
- f"Score: {result.get('score', 'N/A')}% "
69
- f"({result.get('correct_count', '?')}/{result.get('total_attempted', '?')} correct)\n"
70
- f"Message: {result.get('message', 'No message')}"
71
  )
 
72
  except Exception as e:
73
- status = f"❌ Submission failed: {e}"
74
-
75
- return status, pd.DataFrame(results_log)
76
 
77
- # -------------------------
78
  # Gradio Interface
79
- # -------------------------
80
  with gr.Blocks() as demo:
81
- gr.Markdown("# 🎯 GAIA Agent Evaluation")
82
  gr.Markdown(
83
  """
84
- **Instructions:**
85
- 1. Log in with your Hugging Face account.
86
- 2. Click 'Run Evaluation & Submit All Answers' to fetch questions and submit your agent's answers.
87
  """
88
  )
89
 
90
- # 使用 gr.State() 存使用者資訊
91
- user_state = gr.State()
92
-
93
- # Login button,沒有任何參數
94
  login_btn = gr.LoginButton()
95
- login_btn.click(lambda profile: profile, inputs=None, outputs=user_state)
96
 
97
- run_btn = gr.Button("🚀 Run Evaluation & Submit All Answers")
98
- status_box = 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
- # Run button使用user_state當profile
102
- run_btn.click(run_and_submit_all, inputs=user_state, outputs=[status_box, results_table])
 
 
 
 
103
 
104
  if __name__ == "__main__":
 
105
  demo.launch(debug=True, share=False)
 
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):
16
  print("BasicAgent initialized.")
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
+ }
87
 
 
 
88
  try:
89
  resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
90
  resp.raise_for_status()
91
+ result_data = resp.json()
92
+ status_text = (
93
+ f"Submission Successful!\n"
94
+ f"User: {result_data.get('username')}\n"
95
+ f"Score: {result_data.get('score', 'N/A')}% "
96
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
97
+ f"Message: {result_data.get('message', '')}"
98
  )
99
+ return status_text, pd.DataFrame(results_log)
100
  except Exception as e:
101
+ return f"❌ Submission failed: {e}", pd.DataFrame(results_log)
 
 
102
 
103
+ # -------------------------------
104
  # Gradio Interface
105
+ # -------------------------------
106
  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)