s1123725 commited on
Commit
e0f9e6a
·
verified ·
1 Parent(s): bd49552

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -136
app.py CHANGED
@@ -1,164 +1,112 @@
1
- # ===========================
2
- # app.py
3
- # ===========================
4
  import os
5
  import gradio as gr
6
  import requests
7
  import pandas as pd
8
- import re
9
- import time
10
 
11
- # --- Constants ---
 
 
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
- # ===========================
15
- # GAIA Hybrid Agent
16
- # ===========================
17
- def strip_refs(text: str) -> str:
18
- text = re.sub(r"<ref[^>]*>.*?</ref>", "", text, flags=re.DOTALL)
19
- text = re.sub(r"<ref[^/>]*/>", "", text)
20
- return text
21
-
22
- def fetch_wiki(title: str) -> str | None:
23
- try:
24
- params = {"action": "parse", "page": title, "prop": "wikitext", "format": "json", "formatversion": 2}
25
- r = requests.get("https://en.wikipedia.org/w/api.php", params=params, timeout=10)
26
- r.raise_for_status()
27
- return r.json()["parse"]["wikitext"]
28
- except:
29
- return None
30
-
31
- def solve_reverse_left(q: str) -> str | None:
32
- if "tfel" in q:
33
- return "right"
34
- return None
35
-
36
- def solve_not_commutative_subset(q: str) -> str | None:
37
- if "table defining * on the set S" in q and "provide the subset of S" in q:
38
- return "b, e"
39
- return None
40
-
41
- def solve_botany_vegetables(q: str) -> str | None:
42
- if "professor of botany" in q and "botanical fruits" in q and "vegetables" in q:
43
- return "broccoli, celery, fresh basil, lettuce, sweet potatoes"
44
- return None
45
-
46
- def solve_actor_ray_polish_to_magda_m(q: str) -> str | None:
47
- if "Polish-language version of Everybody Loves Raymond" not in q or "Magda M" not in q:
48
- return None
49
- wt = fetch_wiki("Wszyscy kochają Romana")
50
- if not wt:
51
- return None
52
- wt = strip_refs(wt)
53
- actor = None
54
- for line in wt.splitlines():
55
- if line.strip().startswith(("*", "#")) and "[[" in line:
56
- m = re.search(r"\[\[([^\|\]]+)", line)
57
- if m and " " in m.group(1):
58
- actor = m.group(1).strip()
59
- break
60
- if not actor:
61
- return None
62
- actor_wt = strip_refs(fetch_wiki(actor) or "")
63
- role_line = next((line for line in actor_wt.splitlines() if "Magda M" in line), None)
64
- if not role_line:
65
- return None
66
- m = re.search(r"(?:as|–|-)\s*([A-ZĄĆĘŁŃÓŚŹŻ][A-Za-zĄĆĘŁŃÓŚŹŻąćęłńóśźż\.\- ]+)", role_line)
67
- if m:
68
- return m.group(1).split()[0]
69
- return None
70
-
71
- class HybridAgent:
72
  def __init__(self):
73
- self.guaranteed_solvers = [
74
- solve_reverse_left,
75
- solve_not_commutative_subset,
76
- solve_botany_vegetables,
77
- solve_actor_ray_polish_to_magda_m,
78
- ]
79
-
80
  def __call__(self, question: str) -> str:
81
- for solver in self.guaranteed_solvers:
82
- try:
83
- answer = solver(question)
84
- if answer:
85
- return answer
86
- except:
87
- pass
88
- return self._fallback_solver(question)
89
-
90
- def _fallback_solver(self, q: str) -> str:
91
- q_lower = q.lower()
92
- numbers = re.findall(r'\b\d+\b', q)
93
-
94
- if 'how many' in q_lower and numbers:
95
- return numbers[-1]
96
- if q.strip().endswith('?'):
97
- starters = ['is', 'are', 'was', 'were', 'does', 'do', 'did']
98
- if any(q_lower.startswith(w) for w in starters):
99
- return "No" if any(neg in q_lower for neg in ["not","never","n't"]) else "Yes"
100
- if numbers:
101
- return numbers[0]
102
- return "Unknown"
103
-
104
- # ===========================
105
- # Main Submission Function
106
- # ===========================
107
  def run_and_submit_all(profile: gr.OAuthProfile | None):
108
- if not profile:
109
  return "❌ Please login with your Hugging Face account.", None
110
-
111
  username = profile.username
112
- agent_code = f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main"
113
-
 
 
 
 
 
 
 
114
  try:
115
- questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()
 
 
 
 
116
  except Exception as e:
117
- return f" Failed to fetch questions: {e}", None
118
-
119
- agent = HybridAgent()
120
- answers_payload = []
121
  results_log = []
122
-
123
- for task in questions:
124
- task_id = task.get("task_id")
125
- q_text = task.get("question")
126
- if not task_id or not q_text:
127
  continue
128
- answer = agent(q_text)
 
 
 
129
  answers_payload.append({"task_id": task_id, "submitted_answer": answer})
130
- results_log.append({"Task ID": task_id, "Question": q_text[:100]+"...", "Answer": answer})
131
- time.sleep(0.05)
132
-
133
- submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
134
  try:
135
- response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
136
- response.raise_for_status()
137
- result_data = response.json()
138
- final_status = (
139
- f"Submission Successful!\n"
140
- f"User: {result_data.get('username')}\n"
141
- f"Score: {result_data.get('score', 'N/A')}% "
142
- f"({result_data.get('correct_count','?')}/{result_data.get('total_attempted','?')} correct)\n"
143
- f"Message: {result_data.get('message','No message')}"
144
  )
145
- return final_status, pd.DataFrame(results_log)
146
  except Exception as e:
147
- return f"❌ Submission Failed: {e}", pd.DataFrame(results_log)
148
 
149
- # ===========================
 
 
150
  # Gradio Interface
151
- # ===========================
152
  with gr.Blocks() as demo:
153
- gr.Markdown("# 🎯 GAIA Hybrid Agent")
154
- gr.Markdown("4 Guaranteed Solvers + Fallback\n\nPlease login and submit your answers.")
155
-
 
 
 
 
 
 
 
156
  login_btn = gr.LoginButton()
157
  run_btn = gr.Button("🚀 Run Evaluation & Submit All Answers")
158
- status_box = gr.Textbox(label="Run Status / Submission Result", lines=10)
159
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
160
-
161
- run_btn.click(run_and_submit_all, inputs=login_btn, outputs=[status_box, results_table])
 
 
 
 
162
 
163
  if __name__ == "__main__":
164
- demo.launch()
 
 
 
 
 
1
  import os
2
  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 Definition
13
+ # -------------------------
14
+ class BasicAgent:
15
+ """Your GAIA Agent logic can go here."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def __init__(self):
17
+ print("BasicAgent initialized.")
18
+
 
 
 
 
 
19
  def __call__(self, question: str) -> str:
20
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
21
+ # TODO: Replace with real agent logic
22
+ fixed_answer = "This is a default answer."
23
+ print(f"Agent returning fixed answer: {fixed_answer}")
24
+ return fixed_answer
25
+
26
+ # -------------------------
27
+ # Run & Submit Function
28
+ # -------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  def run_and_submit_all(profile: gr.OAuthProfile | None):
30
+ if profile is None:
31
  return "❌ Please login with your Hugging Face account.", None
32
+
33
  username = profile.username
34
+ print(f"Logged in as: {username}")
35
+
36
+ space_id = os.getenv("SPACE_ID", "unknown-space")
37
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
38
+
39
+ # Instantiate Agent
40
+ agent = BasicAgent()
41
+
42
+ # Fetch questions
43
  try:
44
+ resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
45
+ resp.raise_for_status()
46
+ questions = resp.json()
47
+ if not questions:
48
+ return "No questions fetched.", pd.DataFrame()
49
  except Exception as e:
50
+ return f"Error fetching questions: {e}", pd.DataFrame()
51
+
52
+ # Run Agent
 
53
  results_log = []
54
+ answers_payload = []
55
+ for q in questions:
56
+ task_id = q.get("task_id")
57
+ question_text = q.get("question")
58
+ if not task_id or question_text is None:
59
  continue
60
+ try:
61
+ answer = agent(question_text)
62
+ except Exception as e:
63
+ answer = f"AGENT ERROR: {e}"
64
  answers_payload.append({"task_id": task_id, "submitted_answer": answer})
65
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
66
+
67
+ # Submit answers
68
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
69
  try:
70
+ resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
71
+ resp.raise_for_status()
72
+ result = resp.json()
73
+ status = (
74
+ f"Submission Complete!\n"
75
+ f"User: {result.get('username')}\n"
76
+ f"Score: {result.get('score', 'N/A')}% "
77
+ f"({result.get('correct_count', '?')}/{result.get('total_attempted', '?')} correct)\n"
78
+ f"Message: {result.get('message', 'No message')}"
79
  )
 
80
  except Exception as e:
81
+ status = f"❌ Submission failed: {e}"
82
 
83
+ return status, pd.DataFrame(results_log)
84
+
85
+ # -------------------------
86
  # Gradio Interface
87
+ # -------------------------
88
  with gr.Blocks() as demo:
89
+ gr.Markdown("# 🎯 GAIA Agent Evaluation")
90
+ gr.Markdown(
91
+ """
92
+ **Instructions:**
93
+ 1. Log in with your Hugging Face account.
94
+ 2. Click 'Run Evaluation & Submit All Answers' to fetch questions and submit your agent's answers.
95
+ """
96
+ )
97
+
98
+ user_state = gr.State()
99
  login_btn = gr.LoginButton()
100
  run_btn = gr.Button("🚀 Run Evaluation & Submit All Answers")
101
+ status_box = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
102
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
103
+
104
+ # Login event stores profile in user_state
105
+ login_btn.login(lambda profile: profile, outputs=user_state)
106
+
107
+ # Run button uses user_state as profile
108
+ run_btn.click(run_and_submit_all, inputs=user_state, outputs=[status_box, results_table])
109
 
110
  if __name__ == "__main__":
111
+ print("Launching GAIA Agent Evaluation App...")
112
+ demo.launch(debug=True, share=False)