wlchee commited on
Commit
53c8dc9
·
verified ·
1 Parent(s): 557287d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -58
app.py CHANGED
@@ -7,6 +7,9 @@ import random
7
  from transformers import Tool
8
  from transformers.agents import Agent
9
 
 
 
 
10
  # --- Enhanced Agent Definition ---
11
  class EnhancedAgent:
12
  def __init__(self):
@@ -61,84 +64,166 @@ class EnhancedAgent:
61
  print(f"Agent error: {e}")
62
  return "I couldn't find an answer to that question."
63
 
64
- # --- Gradio App ---
65
  def run_and_submit_all(profile: gr.OAuthProfile | None):
66
- """Main function to run evaluation"""
67
- if not profile:
68
- return "Please login with your Hugging Face account", None
69
-
70
- username = profile.username
71
- api_url = os.getenv("API_URL", "https://agents-course-unit4-scoring.hf.space")
 
 
 
 
 
 
 
 
 
72
  questions_url = f"{api_url}/questions"
73
  submit_url = f"{api_url}/submit"
74
- space_id = os.getenv("SPACE_ID", "your-space-id")
75
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
76
 
 
77
  try:
78
  agent = EnhancedAgent()
79
-
80
- # Fetch questions
 
 
 
 
 
 
 
 
81
  response = requests.get(questions_url, timeout=15)
82
  response.raise_for_status()
83
  questions_data = response.json()
84
-
85
  if not questions_data:
86
- return "No questions received from server", None
87
-
88
- # Process questions
89
- results_log = []
90
- answers_payload = []
91
- for item in questions_data:
92
- task_id = item.get("task_id")
93
- question = item.get("question")
94
- if not task_id or not question:
95
- continue
96
-
97
- try:
98
- answer = agent(question)
99
- answers_payload.append({"task_id": task_id, "submitted_answer": answer})
100
- results_log.append({"Task ID": task_id, "Question": question, "Answer": answer})
101
- except Exception as e:
102
- results_log.append({"Task ID": task_id, "Question": question, "Answer": f"Error: {str(e)}"})
103
-
104
- # Submit answers
105
- submission_data = {
106
- "username": username,
107
- "agent_code": agent_code,
108
- "answers": answers_payload
109
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  response = requests.post(submit_url, json=submission_data, timeout=60)
111
  response.raise_for_status()
112
- result = response.json()
113
-
114
- return (
115
  f"Submission Successful!\n"
116
- f"Score: {result.get('score', 'N/A')}%\n"
117
- f"Correct: {result.get('correct_count', '?')}/{result.get('total_attempted', '?')}\n"
118
- f"Message: {result.get('message', '')}",
119
- pd.DataFrame(results_log)
120
  )
121
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  except Exception as e:
123
- return f"Error: {str(e)}", None
 
 
 
124
 
125
- # --- Gradio Interface ---
126
  with gr.Blocks() as demo:
127
- gr.Markdown("""
128
- # Hugging Face Agent Evaluation
129
- Submit your agent to be evaluated against the benchmark questions.
130
- """)
131
-
 
 
 
 
 
 
 
 
 
 
132
  gr.LoginButton()
133
- run_btn = gr.Button("Run Evaluation & Submit Answers")
134
-
135
- status_output = gr.Textbox(label="Results", interactive=False)
136
- results_table = gr.DataFrame(label="Question Log", wrap=True)
137
-
138
- run_btn.click(
139
  fn=run_and_submit_all,
140
  outputs=[status_output, results_table]
141
  )
142
 
143
  if __name__ == "__main__":
144
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  from transformers import Tool
8
  from transformers.agents import Agent
9
 
10
+ # --- Constants ---
11
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
+
13
  # --- Enhanced Agent Definition ---
14
  class EnhancedAgent:
15
  def __init__(self):
 
64
  print(f"Agent error: {e}")
65
  return "I couldn't find an answer to that question."
66
 
 
67
  def run_and_submit_all(profile: gr.OAuthProfile | None):
68
+ """
69
+ Fetches all questions, runs the EnhancedAgent on them, submits all answers,
70
+ and displays the results.
71
+ """
72
+ # --- Determine HF Space Runtime URL and Repo URL ---
73
+ space_id = os.getenv("SPACE_ID")
74
+
75
+ if profile:
76
+ username = f"{profile.username}"
77
+ print(f"User logged in: {username}")
78
+ else:
79
+ print("User not logged in.")
80
+ return "Please Login to Hugging Face with the button.", None
81
+
82
+ api_url = DEFAULT_API_URL
83
  questions_url = f"{api_url}/questions"
84
  submit_url = f"{api_url}/submit"
 
 
85
 
86
+ # 1. Instantiate Agent
87
  try:
88
  agent = EnhancedAgent()
89
+ except Exception as e:
90
+ print(f"Error instantiating agent: {e}")
91
+ return f"Error initializing agent: {e}", None
92
+
93
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
94
+ print(agent_code)
95
+
96
+ # 2. Fetch Questions
97
+ print(f"Fetching questions from: {questions_url}")
98
+ try:
99
  response = requests.get(questions_url, timeout=15)
100
  response.raise_for_status()
101
  questions_data = response.json()
 
102
  if not questions_data:
103
+ print("Fetched questions list is empty.")
104
+ return "Fetched questions list is empty or invalid format.", None
105
+ print(f"Fetched {len(questions_data)} questions.")
106
+ except requests.exceptions.RequestException as e:
107
+ print(f"Error fetching questions: {e}")
108
+ return f"Error fetching questions: {e}", None
109
+ except requests.exceptions.JSONDecodeError as e:
110
+ print(f"Error decoding JSON response from questions endpoint: {e}")
111
+ print(f"Response text: {response.text[:500]}")
112
+ return f"Error decoding server response for questions: {e}", None
113
+ except Exception as e:
114
+ print(f"An unexpected error occurred fetching questions: {e}")
115
+ return f"An unexpected error occurred fetching questions: {e}", None
116
+
117
+ # 3. Run your Agent
118
+ results_log = []
119
+ answers_payload = []
120
+ print(f"Running agent on {len(questions_data)} questions...")
121
+ for item in questions_data:
122
+ task_id = item.get("task_id")
123
+ question_text = item.get("question")
124
+ if not task_id or question_text is None:
125
+ print(f"Skipping item with missing task_id or question: {item}")
126
+ continue
127
+ try:
128
+ submitted_answer = agent(question_text)
129
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
130
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
131
+ except Exception as e:
132
+ print(f"Error running agent on task {task_id}: {e}")
133
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
134
+
135
+ if not answers_payload:
136
+ print("Agent did not produce any answers to submit.")
137
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
138
+
139
+ # 4. Prepare Submission
140
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
141
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
142
+ print(status_update)
143
+
144
+ # 5. Submit
145
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
146
+ try:
147
  response = requests.post(submit_url, json=submission_data, timeout=60)
148
  response.raise_for_status()
149
+ result_data = response.json()
150
+ final_status = (
 
151
  f"Submission Successful!\n"
152
+ f"User: {result_data.get('username')}\n"
153
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
154
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
155
+ f"Message: {result_data.get('message', 'No message received.')}"
156
  )
157
+ print("Submission successful.")
158
+ results_df = pd.DataFrame(results_log)
159
+ return final_status, results_df
160
+ except requests.exceptions.HTTPError as e:
161
+ error_detail = f"Server responded with status {e.response.status_code}."
162
+ try:
163
+ error_json = e.response.json()
164
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
165
+ except requests.exceptions.JSONDecodeError:
166
+ error_detail += f" Response: {e.response.text[:500]}"
167
+ status_message = f"Submission Failed: {error_detail}"
168
+ print(status_message)
169
+ results_df = pd.DataFrame(results_log)
170
+ return status_message, results_df
171
+ except requests.exceptions.Timeout:
172
+ status_message = "Submission Failed: The request timed out."
173
+ print(status_message)
174
+ results_df = pd.DataFrame(results_log)
175
+ return status_message, results_df
176
+ except requests.exceptions.RequestException as e:
177
+ status_message = f"Submission Failed: Network error - {e}"
178
+ print(status_message)
179
+ results_df = pd.DataFrame(results_log)
180
+ return status_message, results_df
181
  except Exception as e:
182
+ status_message = f"An unexpected error occurred during submission: {e}"
183
+ print(status_message)
184
+ results_df = pd.DataFrame(results_log)
185
+ return status_message, results_df
186
 
187
+ # --- Build Gradio Interface using Blocks ---
188
  with gr.Blocks() as demo:
189
+ gr.Markdown("# Enhanced Agent Evaluation Runner")
190
+ gr.Markdown(
191
+ """
192
+ **Instructions:**
193
+ 1. Log in to your Hugging Face account using the button below.
194
+ 2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run the enhanced agent, submit answers, and see the score.
195
+
196
+ **Agent Capabilities:**
197
+ - Math calculations
198
+ - Current time lookup
199
+ - Random choice selection
200
+ - Complex question handling via AI
201
+ """
202
+ )
203
+
204
  gr.LoginButton()
205
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
206
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
207
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
208
+
209
+ run_button.click(
 
210
  fn=run_and_submit_all,
211
  outputs=[status_output, results_table]
212
  )
213
 
214
  if __name__ == "__main__":
215
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
216
+ space_host_startup = os.getenv("SPACE_HOST")
217
+ space_id_startup = os.getenv("SPACE_ID")
218
+
219
+ if space_host_startup:
220
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
221
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
222
+
223
+ if space_id_startup:
224
+ print(f"✅ SPACE_ID found: {space_id_startup}")
225
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
226
+
227
+ print("-"*(60 + len(" App Starting ")) + "\n")
228
+ print("Launching Gradio Interface for Enhanced Agent Evaluation...")
229
+ demo.launch(debug=True, share=False)