wlchee commited on
Commit
51b3197
·
verified ·
1 Parent(s): 78615d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -184
app.py CHANGED
@@ -2,33 +2,20 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- #from transformers import Tool, HfAgent
6
  from datetime import datetime
7
  import random
8
- import wikipediaapi
9
- #import wolframalpha
10
- from transformers import Tool
11
- from transformers.agents import HfAgent
12
-
13
- # --- Constants ---
14
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
16
  # --- Enhanced Agent Definition ---
17
  class EnhancedAgent:
18
  def __init__(self):
19
  print("EnhancedAgent initialized with tools.")
20
- # Initialize tools
21
  self.tools = {
22
  "calculator": self.calculator,
23
  "time": self.get_current_time,
24
- "wikipedia": self.wikipedia_search,
25
  "random_choice": self.random_choice
26
  }
27
 
28
- # Initialize external APIs (would need proper API keys in production)
29
- self.wiki_wiki = wikipediaapi.Wikipedia('en')
30
- # self.wolfram_client = wolframalpha.Client('YOUR_WOLFRAM_APP_ID') # Replace with actual ID
31
-
32
  def calculator(self, expression: str) -> str:
33
  """Evaluate mathematical expressions"""
34
  try:
@@ -40,15 +27,8 @@ class EnhancedAgent:
40
  """Get current UTC time"""
41
  return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
42
 
43
- def wikipedia_search(self, term: str) -> str:
44
- """Search Wikipedia for information"""
45
- page = self.wiki_wiki.page(term)
46
- if page.exists():
47
- return page.summary[:500] # Return first 500 chars of summary
48
- return f"No Wikipedia page found for '{term}'"
49
-
50
  def random_choice(self, items: str) -> str:
51
- """Randomly select from a list of comma-separated items"""
52
  try:
53
  options = [x.strip() for x in items.split(",")]
54
  return f"I choose: {random.choice(options)}"
@@ -56,30 +36,18 @@ class EnhancedAgent:
56
  return "Error: Please provide comma-separated options"
57
 
58
  def __call__(self, question: str) -> str:
59
- print(f"Agent processing question: {question[:100]}...")
60
-
61
- # Simple question classification and routing
62
  question_lower = question.lower()
63
 
64
  # Math questions
65
  if any(word in question_lower for word in ["calculate", "what is", "how much is", "+", "-", "*", "/"]):
66
- try:
67
- # Extract math expression
68
- expr = question.replace("?", "").replace("what is", "").replace("calculate", "").strip()
69
- return self.tools["calculator"](expr)
70
- except:
71
- pass
72
 
73
  # Time questions
74
  if any(word in question_lower for word in ["time", "current time", "what time is it"]):
75
  return self.tools["time"]()
76
 
77
- # Wikipedia questions
78
- if any(word in question_lower for word in ["who is", "what is a", "tell me about", "explain"]):
79
- # Extract search term
80
- term = question.replace("?", "").replace("who is", "").replace("what is a", "").replace("tell me about", "").strip()
81
- return self.tools["wikipedia"](term)
82
-
83
  # Random choice questions
84
  if " or " in question_lower and not any(word in question_lower for word in ["who", "what", "when", "where", "why", "how"]):
85
  return self.tools["random_choice"](question.replace("?", "").replace(" or ", ","))
@@ -87,179 +55,93 @@ class EnhancedAgent:
87
  # Fallback to HF Agent for complex questions
88
  try:
89
  agent = HfAgent(
90
- "https://api-inference.huggingface.co/models/bigcode/starcoder",
91
  max_new_tokens=150,
92
  temperature=0.5
93
  )
94
  return agent.run(question)
95
- except:
 
96
  return "I couldn't find an answer to that question."
97
 
 
98
  def run_and_submit_all(profile: gr.OAuthProfile | None):
99
- """
100
- Fetches all questions, runs the EnhancedAgent on them, submits all answers,
101
- and displays the results.
102
- """
103
- # --- Determine HF Space Runtime URL and Repo URL ---
104
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
105
-
106
- if profile:
107
- username= f"{profile.username}"
108
- print(f"User logged in: {username}")
109
- else:
110
- print("User not logged in.")
111
- return "Please Login to Hugging Face with the button.", None
112
-
113
- api_url = DEFAULT_API_URL
114
  questions_url = f"{api_url}/questions"
115
  submit_url = f"{api_url}/submit"
116
-
117
- # 1. Instantiate Agent
118
- try:
119
- agent = EnhancedAgent()
120
- except Exception as e:
121
- print(f"Error instantiating agent: {e}")
122
- return f"Error initializing agent: {e}", None
123
-
124
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
125
- print(agent_code)
126
 
127
- # 2. Fetch Questions
128
- print(f"Fetching questions from: {questions_url}")
129
  try:
 
 
 
130
  response = requests.get(questions_url, timeout=15)
131
  response.raise_for_status()
132
  questions_data = response.json()
 
133
  if not questions_data:
134
- print("Fetched questions list is empty.")
135
- return "Fetched questions list is empty or invalid format.", None
136
- print(f"Fetched {len(questions_data)} questions.")
137
- except requests.exceptions.RequestException as e:
138
- print(f"Error fetching questions: {e}")
139
- return f"Error fetching questions: {e}", None
140
- except requests.exceptions.JSONDecodeError as e:
141
- print(f"Error decoding JSON response from questions endpoint: {e}")
142
- print(f"Response text: {response.text[:500]}")
143
- return f"Error decoding server response for questions: {e}", None
144
- except Exception as e:
145
- print(f"An unexpected error occurred fetching questions: {e}")
146
- return f"An unexpected error occurred fetching questions: {e}", None
147
-
148
- # 3. Run your Agent
149
- results_log = []
150
- answers_payload = []
151
- print(f"Running agent on {len(questions_data)} questions...")
152
- for item in questions_data:
153
- task_id = item.get("task_id")
154
- question_text = item.get("question")
155
- if not task_id or question_text is None:
156
- print(f"Skipping item with missing task_id or question: {item}")
157
- continue
158
- try:
159
- submitted_answer = agent(question_text)
160
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
161
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
162
- except Exception as e:
163
- print(f"Error running agent on task {task_id}: {e}")
164
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
165
-
166
- if not answers_payload:
167
- print("Agent did not produce any answers to submit.")
168
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
169
-
170
- # 4. Prepare Submission
171
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
172
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
173
- print(status_update)
174
-
175
- # 5. Submit
176
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
177
- try:
178
  response = requests.post(submit_url, json=submission_data, timeout=60)
179
  response.raise_for_status()
180
- result_data = response.json()
181
- final_status = (
 
182
  f"Submission Successful!\n"
183
- f"User: {result_data.get('username')}\n"
184
- f"Overall Score: {result_data.get('score', 'N/A')}% "
185
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
186
- f"Message: {result_data.get('message', 'No message received.')}"
187
  )
188
- print("Submission successful.")
189
- results_df = pd.DataFrame(results_log)
190
- return final_status, results_df
191
- except requests.exceptions.HTTPError as e:
192
- error_detail = f"Server responded with status {e.response.status_code}."
193
- try:
194
- error_json = e.response.json()
195
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
196
- except requests.exceptions.JSONDecodeError:
197
- error_detail += f" Response: {e.response.text[:500]}"
198
- status_message = f"Submission Failed: {error_detail}"
199
- print(status_message)
200
- results_df = pd.DataFrame(results_log)
201
- return status_message, results_df
202
- except requests.exceptions.Timeout:
203
- status_message = "Submission Failed: The request timed out."
204
- print(status_message)
205
- results_df = pd.DataFrame(results_log)
206
- return status_message, results_df
207
- except requests.exceptions.RequestException as e:
208
- status_message = f"Submission Failed: Network error - {e}"
209
- print(status_message)
210
- results_df = pd.DataFrame(results_log)
211
- return status_message, results_df
212
  except Exception as e:
213
- status_message = f"An unexpected error occurred during submission: {e}"
214
- print(status_message)
215
- results_df = pd.DataFrame(results_log)
216
- return status_message, results_df
217
 
218
-
219
- # --- Build Gradio Interface using Blocks ---
220
  with gr.Blocks() as demo:
221
- gr.Markdown("# Enhanced Agent Evaluation Runner")
222
- gr.Markdown(
223
- """
224
- **Instructions:**
225
-
226
- 1. Log in to your Hugging Face account using the button below.
227
- 2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run the enhanced agent, submit answers, and see the score.
228
-
229
- This agent includes:
230
- - Math calculation capabilities
231
- - Time lookup
232
- - Wikipedia integration
233
- - Random choice selection
234
- - Fallback to HF's StarCoder model for complex questions
235
- """
236
- )
237
-
238
  gr.LoginButton()
239
-
240
- run_button = gr.Button("Run Evaluation & Submit All Answers")
241
-
242
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
243
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
244
-
245
- run_button.click(
246
  fn=run_and_submit_all,
247
  outputs=[status_output, results_table]
248
  )
249
 
250
  if __name__ == "__main__":
251
- print("\n" + "-"*30 + " App Starting " + "-"*30)
252
- space_host_startup = os.getenv("SPACE_HOST")
253
- space_id_startup = os.getenv("SPACE_ID")
254
-
255
- if space_host_startup:
256
- print(f"✅ SPACE_HOST found: {space_host_startup}")
257
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
258
-
259
- if space_id_startup:
260
- print(f"✅ SPACE_ID found: {space_id_startup}")
261
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
262
-
263
- print("-"*(60 + len(" App Starting ")) + "\n")
264
- print("Launching Gradio Interface for Enhanced Agent Evaluation...")
265
- demo.launch(debug=True, share=False)
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
 
5
  from datetime import datetime
6
  import random
7
+ from smolagents import Tool, HfAgent
 
 
 
 
 
 
8
 
9
  # --- Enhanced Agent Definition ---
10
  class EnhancedAgent:
11
  def __init__(self):
12
  print("EnhancedAgent initialized with tools.")
 
13
  self.tools = {
14
  "calculator": self.calculator,
15
  "time": self.get_current_time,
 
16
  "random_choice": self.random_choice
17
  }
18
 
 
 
 
 
19
  def calculator(self, expression: str) -> str:
20
  """Evaluate mathematical expressions"""
21
  try:
 
27
  """Get current UTC time"""
28
  return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
29
 
 
 
 
 
 
 
 
30
  def random_choice(self, items: str) -> str:
31
+ """Randomly select from comma-separated items"""
32
  try:
33
  options = [x.strip() for x in items.split(",")]
34
  return f"I choose: {random.choice(options)}"
 
36
  return "Error: Please provide comma-separated options"
37
 
38
  def __call__(self, question: str) -> str:
39
+ print(f"Processing question: {question[:100]}...")
 
 
40
  question_lower = question.lower()
41
 
42
  # Math questions
43
  if any(word in question_lower for word in ["calculate", "what is", "how much is", "+", "-", "*", "/"]):
44
+ expr = question.replace("?", "").replace("what is", "").replace("calculate", "").strip()
45
+ return self.tools["calculator"](expr)
 
 
 
 
46
 
47
  # Time questions
48
  if any(word in question_lower for word in ["time", "current time", "what time is it"]):
49
  return self.tools["time"]()
50
 
 
 
 
 
 
 
51
  # Random choice questions
52
  if " or " in question_lower and not any(word in question_lower for word in ["who", "what", "when", "where", "why", "how"]):
53
  return self.tools["random_choice"](question.replace("?", "").replace(" or ", ","))
 
55
  # Fallback to HF Agent for complex questions
56
  try:
57
  agent = HfAgent(
58
+ "bigcode/starcoder",
59
  max_new_tokens=150,
60
  temperature=0.5
61
  )
62
  return agent.run(question)
63
+ except Exception as e:
64
+ print(f"Agent error: {e}")
65
  return "I couldn't find an answer to that question."
66
 
67
+ # --- Gradio App ---
68
  def run_and_submit_all(profile: gr.OAuthProfile | None):
69
+ """Main function to run evaluation"""
70
+ if not profile:
71
+ return "Please login with your Hugging Face account", None
72
+
73
+ username = profile.username
74
+ api_url = os.getenv("API_URL", "https://agents-course-unit4-scoring.hf.space")
 
 
 
 
 
 
 
 
 
75
  questions_url = f"{api_url}/questions"
76
  submit_url = f"{api_url}/submit"
77
+ space_id = os.getenv("SPACE_ID", "your-space-id")
 
 
 
 
 
 
 
78
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
79
 
 
 
80
  try:
81
+ agent = EnhancedAgent()
82
+
83
+ # Fetch questions
84
  response = requests.get(questions_url, timeout=15)
85
  response.raise_for_status()
86
  questions_data = response.json()
87
+
88
  if not questions_data:
89
+ return "No questions received from server", None
90
+
91
+ # Process questions
92
+ results_log = []
93
+ answers_payload = []
94
+ for item in questions_data:
95
+ task_id = item.get("task_id")
96
+ question = item.get("question")
97
+ if not task_id or not question:
98
+ continue
99
+
100
+ try:
101
+ answer = agent(question)
102
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
103
+ results_log.append({"Task ID": task_id, "Question": question, "Answer": answer})
104
+ except Exception as e:
105
+ results_log.append({"Task ID": task_id, "Question": question, "Answer": f"Error: {str(e)}"})
106
+
107
+ # Submit answers
108
+ submission_data = {
109
+ "username": username,
110
+ "agent_code": agent_code,
111
+ "answers": answers_payload
112
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  response = requests.post(submit_url, json=submission_data, timeout=60)
114
  response.raise_for_status()
115
+ result = response.json()
116
+
117
+ return (
118
  f"Submission Successful!\n"
119
+ f"Score: {result.get('score', 'N/A')}%\n"
120
+ f"Correct: {result.get('correct_count', '?')}/{result.get('total_attempted', '?')}\n"
121
+ f"Message: {result.get('message', '')}",
122
+ pd.DataFrame(results_log)
123
  )
124
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  except Exception as e:
126
+ return f"Error: {str(e)}", None
 
 
 
127
 
128
+ # --- Gradio Interface ---
 
129
  with gr.Blocks() as demo:
130
+ gr.Markdown("""
131
+ # Hugging Face Agent Evaluation
132
+ Submit your agent to be evaluated against the benchmark questions.
133
+ """)
134
+
 
 
 
 
 
 
 
 
 
 
 
 
135
  gr.LoginButton()
136
+ run_btn = gr.Button("Run Evaluation & Submit Answers")
137
+
138
+ status_output = gr.Textbox(label="Results", interactive=False)
139
+ results_table = gr.DataFrame(label="Question Log", wrap=True)
140
+
141
+ run_btn.click(
 
142
  fn=run_and_submit_all,
143
  outputs=[status_output, results_table]
144
  )
145
 
146
  if __name__ == "__main__":
147
+ demo.launch()