SantoshKumar1310 commited on
Commit
52e9388
Β·
verified Β·
1 Parent(s): f3ff481

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -17
app.py CHANGED
@@ -2,10 +2,10 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
 
5
 
6
  # --- Constants ---
7
- DEFAULT_API_URL = "https://agents-course-unit4-scoring-api.hf.space"
8
-
9
 
10
  # --- Basic Agent Definition ---
11
  # πŸ‘‰ You can customize this class with your own logic or tools
@@ -21,6 +21,23 @@ class BasicAgent:
21
  return fixed_answer
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def run_and_submit_all(profile: gr.OAuthProfile | None):
25
  """
26
  Fetch all questions, run the agent, submit answers, and show results.
@@ -32,17 +49,33 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
32
  print(f"πŸ‘€ User logged in: {username}")
33
  else:
34
  print("❌ User not logged in.")
35
- return "Please login to Hugging Face first.", None
36
 
37
  api_url = DEFAULT_API_URL
38
  questions_url = f"{api_url}/questions"
39
  submit_url = f"{api_url}/submit"
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # 1️⃣ Create Agent
42
  try:
43
  agent = BasicAgent()
44
  except Exception as e:
45
- return f"Agent initialization failed: {e}", None
46
 
47
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Local_Run"
48
  print(f"πŸ“ Agent code link: {agent_code}")
@@ -51,39 +84,72 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
51
  try:
52
  print("πŸ“‘ Fetching questions...")
53
  response = requests.get(questions_url, timeout=15)
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  response.raise_for_status()
55
  questions_data = response.json()
 
56
  if not questions_data:
57
- return "Fetched question list is empty or invalid.", None
 
58
  print(f"βœ… Retrieved {len(questions_data)} questions.")
 
 
 
 
 
 
 
 
 
59
  except Exception as e:
60
- return f"Error fetching questions: {e}", None
61
 
62
  # 3️⃣ Run Agent
63
  results_log = []
64
  answers_payload = []
65
- for item in questions_data:
 
 
 
66
  task_id = item.get("task_id")
67
  question_text = item.get("question")
 
68
  if not task_id or question_text is None:
 
69
  continue
 
70
  try:
 
71
  submitted_answer = agent(question_text)
72
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
73
  results_log.append({
74
  "Task ID": task_id,
75
- "Question": question_text,
76
- "Submitted Answer": submitted_answer
77
  })
78
  except Exception as e:
 
 
79
  results_log.append({
80
  "Task ID": task_id,
81
- "Question": question_text,
82
- "Submitted Answer": f"AGENT ERROR: {e}"
83
  })
84
 
85
  if not answers_payload:
86
- return "No answers generated by the agent.", pd.DataFrame(results_log)
87
 
88
  # 4️⃣ Submit Answers
89
  submission_data = {
@@ -95,8 +161,19 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
95
  try:
96
  print("πŸ“€ Submitting answers...")
97
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
 
 
 
 
 
 
 
 
 
98
  response.raise_for_status()
99
  result_data = response.json()
 
100
  final_status = (
101
  f"βœ… Submission Successful!\n"
102
  f"πŸ‘€ User: {result_data.get('username')}\n"
@@ -106,12 +183,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
106
  )
107
  results_df = pd.DataFrame(results_log)
108
  return final_status, results_df
 
 
 
 
 
109
  except Exception as e:
110
- return f"Submission failed: {e}", pd.DataFrame(results_log)
111
 
112
 
113
  # --- Gradio Interface ---
114
- with gr.Blocks() as demo:
115
  gr.Markdown("# πŸ€– Basic Agent Evaluation Runner")
116
  gr.Markdown(
117
  """
@@ -122,13 +204,16 @@ with gr.Blocks() as demo:
122
  ---
123
  The process may take time (the agent answers all questions).
124
  You can customize the agent with reasoning, search tools, or memory.
 
 
 
125
  """
126
  )
127
 
128
  gr.LoginButton()
129
- run_button = gr.Button("πŸš€ Run Evaluation & Submit All Answers")
130
 
131
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=6, interactive=False)
132
  results_table = gr.DataFrame(label="Questions and Agent Answers")
133
 
134
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
@@ -136,4 +221,4 @@ with gr.Blocks() as demo:
136
 
137
  if __name__ == "__main__":
138
  print("πŸš€ Launching Gradio Interface...")
139
- demo.launch(debug=True, share=False)
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ import time
6
 
7
  # --- Constants ---
8
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
9
 
10
  # --- Basic Agent Definition ---
11
  # πŸ‘‰ You can customize this class with your own logic or tools
 
21
  return fixed_answer
22
 
23
 
24
+ def check_api_status(api_url: str) -> tuple[bool, str]:
25
+ """Check if the API is accessible"""
26
+ try:
27
+ # Try to access the base URL or a health check endpoint
28
+ response = requests.get(api_url, timeout=10)
29
+ if response.status_code == 200:
30
+ return True, "API is accessible"
31
+ else:
32
+ return False, f"API returned status code: {response.status_code}"
33
+ except requests.exceptions.Timeout:
34
+ return False, "API request timed out"
35
+ except requests.exceptions.ConnectionError:
36
+ return False, "Cannot connect to API (connection error)"
37
+ except Exception as e:
38
+ return False, f"API check failed: {str(e)}"
39
+
40
+
41
  def run_and_submit_all(profile: gr.OAuthProfile | None):
42
  """
43
  Fetch all questions, run the agent, submit answers, and show results.
 
49
  print(f"πŸ‘€ User logged in: {username}")
50
  else:
51
  print("❌ User not logged in.")
52
+ return "❌ Please login to Hugging Face first.", None
53
 
54
  api_url = DEFAULT_API_URL
55
  questions_url = f"{api_url}/questions"
56
  submit_url = f"{api_url}/submit"
57
 
58
+ # Check API status first
59
+ print("πŸ” Checking API status...")
60
+ api_ok, api_msg = check_api_status(api_url)
61
+ if not api_ok:
62
+ error_msg = (
63
+ f"⚠️ API Status Check Failed\n\n"
64
+ f"Issue: {api_msg}\n\n"
65
+ f"Possible solutions:\n"
66
+ f"1. The scoring API at {api_url} may be down or moved\n"
67
+ f"2. Check if the API URL is correct\n"
68
+ f"3. The Hugging Face Space might need to be restarted\n"
69
+ f"4. Try again in a few minutes\n\n"
70
+ f"You can verify the API status by visiting: {api_url}"
71
+ )
72
+ return error_msg, None
73
+
74
  # 1️⃣ Create Agent
75
  try:
76
  agent = BasicAgent()
77
  except Exception as e:
78
+ return f"❌ Agent initialization failed: {e}", None
79
 
80
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Local_Run"
81
  print(f"πŸ“ Agent code link: {agent_code}")
 
84
  try:
85
  print("πŸ“‘ Fetching questions...")
86
  response = requests.get(questions_url, timeout=15)
87
+
88
+ if response.status_code == 404:
89
+ error_msg = (
90
+ f"⚠️ Questions endpoint not found (404)\n\n"
91
+ f"The endpoint {questions_url} is not available.\n\n"
92
+ f"Possible issues:\n"
93
+ f"1. The API structure may have changed\n"
94
+ f"2. The scoring service might be under maintenance\n"
95
+ f"3. You may need to update the API URL\n\n"
96
+ f"Please check the course materials or contact the instructor for the correct API endpoint."
97
+ )
98
+ return error_msg, None
99
+
100
  response.raise_for_status()
101
  questions_data = response.json()
102
+
103
  if not questions_data:
104
+ return "⚠️ Fetched question list is empty or invalid.", None
105
+
106
  print(f"βœ… Retrieved {len(questions_data)} questions.")
107
+
108
+ except requests.exceptions.Timeout:
109
+ return f"⏱️ Request timed out while fetching questions from {questions_url}", None
110
+ except requests.exceptions.ConnectionError:
111
+ return f"πŸ”Œ Connection error: Cannot reach {questions_url}", None
112
+ except requests.exceptions.HTTPError as e:
113
+ return f"❌ HTTP Error fetching questions: {e}", None
114
+ except ValueError as e:
115
+ return f"❌ Invalid JSON response from questions endpoint: {e}", None
116
  except Exception as e:
117
+ return f"❌ Error fetching questions: {e}", None
118
 
119
  # 3️⃣ Run Agent
120
  results_log = []
121
  answers_payload = []
122
+
123
+ print(f"πŸ€– Running agent on {len(questions_data)} questions...")
124
+
125
+ for i, item in enumerate(questions_data):
126
  task_id = item.get("task_id")
127
  question_text = item.get("question")
128
+
129
  if not task_id or question_text is None:
130
+ print(f"⚠️ Skipping invalid question item: {item}")
131
  continue
132
+
133
  try:
134
+ print(f"Processing question {i+1}/{len(questions_data)}: {task_id}")
135
  submitted_answer = agent(question_text)
136
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
137
  results_log.append({
138
  "Task ID": task_id,
139
+ "Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
140
+ "Submitted Answer": submitted_answer[:100] + "..." if len(str(submitted_answer)) > 100 else submitted_answer
141
  })
142
  except Exception as e:
143
+ error_msg = f"AGENT ERROR: {e}"
144
+ print(f"❌ {error_msg} for task {task_id}")
145
  results_log.append({
146
  "Task ID": task_id,
147
+ "Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
148
+ "Submitted Answer": error_msg
149
  })
150
 
151
  if not answers_payload:
152
+ return "⚠️ No answers generated by the agent.", pd.DataFrame(results_log)
153
 
154
  # 4️⃣ Submit Answers
155
  submission_data = {
 
161
  try:
162
  print("πŸ“€ Submitting answers...")
163
  response = requests.post(submit_url, json=submission_data, timeout=60)
164
+
165
+ if response.status_code == 404:
166
+ error_msg = (
167
+ f"⚠️ Submit endpoint not found (404)\n\n"
168
+ f"Generated {len(answers_payload)} answers but cannot submit them.\n"
169
+ f"The endpoint {submit_url} is not available.\n\n"
170
+ f"Your answers are shown in the table below for reference."
171
+ )
172
+ return error_msg, pd.DataFrame(results_log)
173
+
174
  response.raise_for_status()
175
  result_data = response.json()
176
+
177
  final_status = (
178
  f"βœ… Submission Successful!\n"
179
  f"πŸ‘€ User: {result_data.get('username')}\n"
 
183
  )
184
  results_df = pd.DataFrame(results_log)
185
  return final_status, results_df
186
+
187
+ except requests.exceptions.Timeout:
188
+ return f"⏱️ Submission timed out. Your answers:\n{len(answers_payload)} answers generated but not submitted.", pd.DataFrame(results_log)
189
+ except requests.exceptions.HTTPError as e:
190
+ return f"❌ HTTP Error during submission: {e}\nYour answers are shown below.", pd.DataFrame(results_log)
191
  except Exception as e:
192
+ return f"❌ Submission failed: {e}\nYour answers are shown below.", pd.DataFrame(results_log)
193
 
194
 
195
  # --- Gradio Interface ---
196
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
197
  gr.Markdown("# πŸ€– Basic Agent Evaluation Runner")
198
  gr.Markdown(
199
  """
 
204
  ---
205
  The process may take time (the agent answers all questions).
206
  You can customize the agent with reasoning, search tools, or memory.
207
+
208
+ ⚠️ **Note**: If you see a 404 error, the scoring API may be temporarily unavailable.
209
+ Check with your instructor for the correct API endpoint.
210
  """
211
  )
212
 
213
  gr.LoginButton()
214
+ run_button = gr.Button("πŸš€ Run Evaluation & Submit All Answers", variant="primary")
215
 
216
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=8, interactive=False)
217
  results_table = gr.DataFrame(label="Questions and Agent Answers")
218
 
219
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
221
 
222
  if __name__ == "__main__":
223
  print("πŸš€ Launching Gradio Interface...")
224
+ demo.launch(debug=True, share=False)