wlchee commited on
Commit
a003b2e
·
verified ·
1 Parent(s): ac19de6

Upload app.py

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