Annessha18 commited on
Commit
5d9be4a
·
verified ·
1 Parent(s): d20fc11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -105
app.py CHANGED
@@ -3,17 +3,16 @@ import gradio as gr
3
  import requests
4
  import re
5
  import pandas as pd
 
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
- # --- Basic Agent Definition ---
11
- from transformers import pipeline, set_seed
12
-
13
  class BasicAgent:
14
  def __init__(self):
15
  print("BasicAgent initialized.")
16
- set_seed(42) # ensure reproducible outputs
17
  self.generator = pipeline(
18
  "text2text-generation",
19
  model="google/flan-t5-large",
@@ -21,90 +20,91 @@ class BasicAgent:
21
  )
22
 
23
  def __call__(self, question: str) -> str:
24
- prompt = (
25
- "Give ONLY the exact answer. "
26
- "No explanation. No extra words.\n"
27
- f"{question}"
28
- )
29
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  result = self.generator(prompt)[0]["generated_text"]
31
 
32
- # --- Strong cleanup for GAIA exact answers ---
33
  answer = result.strip()
34
- # Remove common prefixes
35
- answer = re.sub(r"(?i)^(the answer is|answer:)\s*", "", answer)
36
- # Take only first line or sentence
37
- answer = re.split(r"[.\n]", answer)[0].strip()
38
- # Remove trailing punctuation
39
- answer = answer.rstrip(".,;:")
40
-
41
- print(f"Final answer: {answer}")
 
42
  return answer
43
 
44
 
45
  # --- Run and Submit Function ---
46
  def run_and_submit_all(profile: gr.OAuthProfile | None):
47
- """
48
- Fetches all questions, runs the BasicAgent on them, submits all answers,
49
- and displays the results.
50
- """
51
  space_id = os.getenv("SPACE_ID")
52
 
53
  if profile:
54
- username = f"{profile.username}"
55
- print(f"User logged in: {username}")
56
  else:
57
- print("User not logged in.")
58
  return "Please Login to Hugging Face with the button.", pd.DataFrame()
59
 
60
  api_url = DEFAULT_API_URL
61
  questions_url = f"{api_url}/questions"
62
  submit_url = f"{api_url}/submit"
63
 
64
- # --- Instantiate Agent ---
65
  try:
66
  agent = BasicAgent()
67
  except Exception as e:
68
- print(f"Error instantiating agent: {e}")
69
  return f"Error initializing agent: {e}", pd.DataFrame()
70
 
71
- # --- Agent Code URL ---
72
- if space_id:
73
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
74
- else:
75
- agent_code = "Agent code URL not available."
76
- print(agent_code)
77
 
78
  # --- Fetch Questions ---
79
- print(f"Fetching questions from: {questions_url}")
80
  try:
81
  response = requests.get(questions_url, timeout=15)
82
  response.raise_for_status()
83
  questions_data = response.json()
84
  if not questions_data:
85
- print("Fetched questions list is empty.")
86
  return "Fetched questions list is empty or invalid format.", pd.DataFrame()
87
- print(f"Fetched {len(questions_data)} questions.")
88
- except requests.exceptions.RequestException as e:
89
- print(f"Error fetching questions: {e}")
90
- return f"Error fetching questions: {e}", pd.DataFrame()
91
- except requests.exceptions.JSONDecodeError as e:
92
- print(f"Error decoding JSON response: {e}")
93
- print(f"Response text: {response.text[:500]}")
94
- return f"Error decoding server response for questions: {e}", pd.DataFrame()
95
  except Exception as e:
96
- print(f"Unexpected error fetching questions: {e}")
97
- return f"Unexpected error fetching questions: {e}", pd.DataFrame()
98
 
99
- # --- Run Agent ---
100
  results_log = []
101
  answers_payload = []
102
- print(f"Running agent on {len(questions_data)} questions...")
103
  for item in questions_data:
104
  task_id = item.get("task_id")
105
  question_text = item.get("question")
106
  if not task_id or question_text is None:
107
- print(f"Skipping item with missing task_id or question: {item}")
108
  continue
109
  try:
110
  submitted_answer = agent(question_text)
@@ -115,7 +115,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
115
  "Submitted Answer": submitted_answer
116
  })
117
  except Exception as e:
118
- print(f"Error running agent on task {task_id}: {e}")
119
  results_log.append({
120
  "Task ID": task_id,
121
  "Question": question_text,
@@ -123,18 +122,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
123
  })
124
 
125
  if not answers_payload:
126
- print("Agent did not produce any answers to submit.")
127
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
128
 
129
- # --- Prepare Submission ---
130
- submission_data = {
131
- "username": username.strip(),
132
- "agent_code": agent_code,
133
- "answers": answers_payload
134
- }
135
- print(f"Submitting {len(answers_payload)} answers for user '{username}'...")
136
 
137
- # --- Submit Answers ---
138
  try:
139
  response = requests.post(submit_url, json=submission_data, timeout=60)
140
  response.raise_for_status()
@@ -146,65 +137,22 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
146
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
147
  f"Message: {result_data.get('message', 'No message received.')}"
148
  )
149
- print("Submission successful.")
150
  results_df = pd.DataFrame(results_log)
151
  return final_status, results_df
152
- except requests.exceptions.RequestException as e:
153
- status_message = f"Submission Failed: {e}"
154
- print(status_message)
155
- results_df = pd.DataFrame(results_log)
156
- return status_message, results_df
157
  except Exception as e:
158
- status_message = f"Unexpected error during submission: {e}"
159
- print(status_message)
160
- results_df = pd.DataFrame(results_log)
161
- return status_message, results_df
162
 
163
 
164
  # --- Gradio Interface ---
165
  with gr.Blocks() as demo:
166
- gr.Markdown("# Basic Agent Evaluation Runner")
167
- gr.Markdown(
168
- """
169
- **Instructions:**
170
-
171
- 1. Clone this space and modify the code to define your agent's logic.
172
- 2. Log in to your Hugging Face account using the button below.
173
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
174
- """
175
- )
176
-
177
  gr.LoginButton()
178
  run_button = gr.Button("Run Evaluation & Submit All Answers")
179
-
180
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
181
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
182
 
183
- run_button.click(
184
- fn=run_and_submit_all,
185
- outputs=[status_output, results_table]
186
- )
187
 
188
 
189
- # --- Startup Logs ---
190
  if __name__ == "__main__":
191
- print("\n" + "-"*30 + " App Starting " + "-"*30)
192
- space_host_startup = os.getenv("SPACE_HOST")
193
- space_id_startup = os.getenv("SPACE_ID")
194
-
195
- if space_host_startup:
196
- print(f"✅ SPACE_HOST found: {space_host_startup}")
197
- print(f" Runtime URL: https://{space_host_startup}.hf.space")
198
- else:
199
- print("ℹ️ SPACE_HOST not found (running locally?)")
200
-
201
- if space_id_startup:
202
- print(f"✅ SPACE_ID found: {space_id_startup}")
203
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
204
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
205
- else:
206
- print("ℹ️ SPACE_ID not found (running locally?)")
207
-
208
- print("-"*(60 + len(" App Starting ")) + "\n")
209
- print("Launching Gradio Interface for Basic Agent Evaluation...")
210
  demo.launch(debug=True, share=False)
 
3
  import requests
4
  import re
5
  import pandas as pd
6
+ from transformers import pipeline, set_seed
7
 
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
+ # --- Smarter Basic Agent ---
 
 
12
  class BasicAgent:
13
  def __init__(self):
14
  print("BasicAgent initialized.")
15
+ set_seed(42)
16
  self.generator = pipeline(
17
  "text2text-generation",
18
  model="google/flan-t5-large",
 
20
  )
21
 
22
  def __call__(self, question: str) -> str:
23
+ # --- Detect question type ---
24
+ q_lower = question.lower()
25
+
26
+ # Boolean detection
27
+ if any(x in q_lower for x in ["true or false", "yes or no", "is it", "does it", "can it"]):
28
+ prompt = (
29
+ "Answer only True or False exactly. "
30
+ "No explanations or extra words.\n"
31
+ f"Q: {question}\nA:"
32
+ )
33
+ # Multiple choice detection
34
+ elif any(x in question for x in ["A)", "B)", "C)", "D)", "choose", "options"]):
35
+ prompt = (
36
+ "Answer only with one option exactly: A, B, C, or D. "
37
+ "No explanations or extra words.\n"
38
+ f"Q: {question}\nA:"
39
+ )
40
+ # Numeric detection (digits in question or ask for a number)
41
+ elif re.search(r"number|digits|how many|count|what is \d", q_lower):
42
+ prompt = (
43
+ "Answer only with the number, no words or punctuation.\n"
44
+ f"Q: {question}\nA:"
45
+ )
46
+ # Default: let model generate
47
+ else:
48
+ prompt = (
49
+ "Give ONLY the exact answer. "
50
+ "No explanation or extra words.\n"
51
+ f"Q: {question}\nA:"
52
+ )
53
+
54
+ # --- Generate answer ---
55
  result = self.generator(prompt)[0]["generated_text"]
56
 
57
+ # --- Post-process answer ---
58
  answer = result.strip()
59
+ answer = re.sub(r"(?i)^(the answer is|answer:)\s*", "", answer) # remove prefixes
60
+ answer = re.split(r"[.\n]", answer)[0].strip() # first line or sentence
61
+ answer = answer.rstrip(".,;:") # remove trailing punctuation
62
+
63
+ # Enforce boolean formatting
64
+ if answer.lower() in ["true", "false"]:
65
+ answer = answer.capitalize()
66
+
67
+ print(f"Question: {question}\nFinal answer: {answer}\n")
68
  return answer
69
 
70
 
71
  # --- Run and Submit Function ---
72
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
 
 
 
73
  space_id = os.getenv("SPACE_ID")
74
 
75
  if profile:
76
+ username = profile.username
 
77
  else:
 
78
  return "Please Login to Hugging Face with the button.", pd.DataFrame()
79
 
80
  api_url = DEFAULT_API_URL
81
  questions_url = f"{api_url}/questions"
82
  submit_url = f"{api_url}/submit"
83
 
 
84
  try:
85
  agent = BasicAgent()
86
  except Exception as e:
 
87
  return f"Error initializing agent: {e}", pd.DataFrame()
88
 
89
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Agent code URL not available."
 
 
 
 
 
90
 
91
  # --- Fetch Questions ---
 
92
  try:
93
  response = requests.get(questions_url, timeout=15)
94
  response.raise_for_status()
95
  questions_data = response.json()
96
  if not questions_data:
 
97
  return "Fetched questions list is empty or invalid format.", pd.DataFrame()
 
 
 
 
 
 
 
 
98
  except Exception as e:
99
+ return f"Error fetching questions: {e}", pd.DataFrame()
 
100
 
 
101
  results_log = []
102
  answers_payload = []
103
+
104
  for item in questions_data:
105
  task_id = item.get("task_id")
106
  question_text = item.get("question")
107
  if not task_id or question_text is None:
 
108
  continue
109
  try:
110
  submitted_answer = agent(question_text)
 
115
  "Submitted Answer": submitted_answer
116
  })
117
  except Exception as e:
 
118
  results_log.append({
119
  "Task ID": task_id,
120
  "Question": question_text,
 
122
  })
123
 
124
  if not answers_payload:
 
125
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
126
 
127
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
 
 
128
 
 
129
  try:
130
  response = requests.post(submit_url, json=submission_data, timeout=60)
131
  response.raise_for_status()
 
137
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
138
  f"Message: {result_data.get('message', 'No message received.')}"
139
  )
 
140
  results_df = pd.DataFrame(results_log)
141
  return final_status, results_df
 
 
 
 
 
142
  except Exception as e:
143
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
 
 
 
144
 
145
 
146
  # --- Gradio Interface ---
147
  with gr.Blocks() as demo:
148
+ gr.Markdown("# GAIA Smarter Agent Runner")
 
 
 
 
 
 
 
 
 
 
149
  gr.LoginButton()
150
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
151
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
152
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
153
 
154
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
155
 
156
 
 
157
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  demo.launch(debug=True, share=False)