22Nikk0 commited on
Commit
9e17f9d
·
verified ·
1 Parent(s): 03444e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -59
app.py CHANGED
@@ -3,7 +3,9 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
- import json
 
 
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
@@ -11,23 +13,13 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  # --- Basic Agent Definition ---
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
- # class BasicAgent:
15
- # def __init__(self):
16
- # print("BasicAgent initialized.")
17
- # def __call__(self, question: str) -> str:
18
- # print(f"Agent received question (first 50 chars): {question[:50]}...")
19
- # fixed_answer = "This is a default answer."
20
- # print(f"Agent returning fixed answer: {fixed_answer}")
21
- # return fixed_answer
22
  class BasicAgent:
23
  def __init__(self):
24
  print("BasicAgent initialized.")
25
  self.agent_graph = build_graph()
26
  def __call__(self, question: str) -> str:
27
  print(f"Agent received question (first 50 chars): {question[:50]}...")
28
- # fixed_answer = "This is a default answer."
29
- # print(f"Agent returning fixed answer: {fixed_answer}")
30
- # return fixed_answer
31
  messages = [HumanMessage(content=f"Can you answer this question please ? {question}")]
32
 
33
  messages = self.agent_graph.invoke({"messages": messages, "question": question}, {"recursion_limit": 10})
@@ -41,6 +33,91 @@ class BasicAgent:
41
 
42
  return answer
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def run_and_submit_all( profile: gr.OAuthProfile | None):
45
  """
46
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -58,7 +135,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
58
 
59
  api_url = DEFAULT_API_URL
60
  questions_url = f"{api_url}/questions"
61
- one_question_url = f"{api_url}/random-question"
62
  submit_url = f"{api_url}/submit"
63
 
64
  # 1. Instantiate Agent ( modify this part to create your agent)
@@ -72,11 +148,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
72
  print(agent_code)
73
 
74
  # 2. Fetch Questions
75
- # print(f"Fetching questions from: {questions_url}")
76
- print(f"Fetching questions from: {one_question_url}")
77
  try:
78
- # response = requests.get(questions_url, timeout=15)
79
- response = requests.get(one_question_url, timeout=15)
80
  response.raise_for_status()
81
  questions_data = response.json()
82
  if not questions_data:
@@ -105,6 +179,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
105
  for item in questions_data:
106
  task_id = item.get("task_id")
107
  question_text = item.get("question")
 
108
  if not task_id or question_text is None:
109
  print(f"Skipping item with missing task_id or question: {item}")
110
  continue
@@ -125,48 +200,48 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
125
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
126
  print(status_update)
127
 
128
- # # 5. Submit
129
- # print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
130
- # try:
131
- # response = requests.post(submit_url, json=submission_data, timeout=60)
132
- # response.raise_for_status()
133
- # result_data = response.json()
134
- # final_status = (
135
- # f"Submission Successful!\n"
136
- # f"User: {result_data.get('username')}\n"
137
- # f"Overall Score: {result_data.get('score', 'N/A')}% "
138
- # f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
139
- # f"Message: {result_data.get('message', 'No message received.')}"
140
- # )
141
- # print("Submission successful.")
142
- # results_df = pd.DataFrame(results_log)
143
- # return final_status, results_df
144
- # except requests.exceptions.HTTPError as e:
145
- # error_detail = f"Server responded with status {e.response.status_code}."
146
- # try:
147
- # error_json = e.response.json()
148
- # error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
149
- # except requests.exceptions.JSONDecodeError:
150
- # error_detail += f" Response: {e.response.text[:500]}"
151
- # status_message = f"Submission Failed: {error_detail}"
152
- # print(status_message)
153
- # results_df = pd.DataFrame(results_log)
154
- # return status_message, results_df
155
- # except requests.exceptions.Timeout:
156
- # status_message = "Submission Failed: The request timed out."
157
- # print(status_message)
158
- # results_df = pd.DataFrame(results_log)
159
- # return status_message, results_df
160
- # except requests.exceptions.RequestException as e:
161
- # status_message = f"Submission Failed: Network error - {e}"
162
- # print(status_message)
163
- # results_df = pd.DataFrame(results_log)
164
- # return status_message, results_df
165
- # except Exception as e:
166
- # status_message = f"An unexpected error occurred during submission: {e}"
167
- # print(status_message)
168
- # results_df = pd.DataFrame(results_log)
169
- # return status_message, results_df
170
 
171
 
172
  # --- Build Gradio Interface using Blocks ---
@@ -190,11 +265,17 @@ with gr.Blocks() as demo:
190
  gr.LoginButton()
191
 
192
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
193
 
194
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
195
  # Removed max_rows=10 from DataFrame constructor
196
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
197
 
 
 
 
 
 
198
  run_button.click(
199
  fn=run_and_submit_all,
200
  outputs=[status_output, results_table]
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ import re
7
+ from agentic import build_graph
8
+ from langchain_core.messages import HumanMessage
9
 
10
  # (Keep Constants as is)
11
  # --- Constants ---
 
13
 
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
 
 
 
 
 
16
  class BasicAgent:
17
  def __init__(self):
18
  print("BasicAgent initialized.")
19
  self.agent_graph = build_graph()
20
  def __call__(self, question: str) -> str:
21
  print(f"Agent received question (first 50 chars): {question[:50]}...")
22
+
 
 
23
  messages = [HumanMessage(content=f"Can you answer this question please ? {question}")]
24
 
25
  messages = self.agent_graph.invoke({"messages": messages, "question": question}, {"recursion_limit": 10})
 
33
 
34
  return answer
35
 
36
+ def try_one_question( profile: gr.OAuthProfile | None):
37
+ """
38
+ Fetches one questions, runs the BasicAgent on it, does not submit
39
+ and displays the results.
40
+ """
41
+ # --- Determine HF Space Runtime URL and Repo URL ---
42
+ space_id = os.getenv("SPACE_ID", "local") # Get the SPACE_ID for sending link to the code
43
+
44
+ if profile:
45
+ username= f"{profile.username}"
46
+ print(f"User logged in: {username}")
47
+ else:
48
+ print("User not logged in.")
49
+ return "Please Login to Hugging Face with the button.", None
50
+
51
+ api_url = DEFAULT_API_URL
52
+ one_question_url = f"{api_url}/random-question"
53
+
54
+ # 1. Instantiate Agent ( modify this part to create your agent)
55
+ try:
56
+ agent = BasicAgent()
57
+ except Exception as e:
58
+ print(f"Error instantiating agent: {e}")
59
+ return f"Error initializing agent: {e}", None
60
+ # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
61
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
62
+ print(agent_code)
63
+
64
+ # 2. Fetch Questions
65
+ print(f"Fetching questions from: {one_question_url}")
66
+ try:
67
+ response = requests.get(one_question_url, timeout=15)
68
+ response.raise_for_status()
69
+ questions_data = response.json()
70
+ if not questions_data:
71
+ print("Fetched questions list is empty.")
72
+ return "Fetched questions list is empty or invalid format.", None
73
+ if isinstance(questions_data, dict) and "question" in questions_data:
74
+ print("Fetched a single question instead of a list.")
75
+ questions_data = [questions_data]
76
+ elif isinstance(questions_data, list):
77
+ print(f"Fetched {len(questions_data)} questions.")
78
+ except requests.exceptions.RequestException as e:
79
+ print(f"Error fetching questions: {e}")
80
+ return f"Error fetching questions: {e}", None
81
+ except requests.exceptions.JSONDecodeError as e:
82
+ print(f"Error decoding JSON response from questions endpoint: {e}")
83
+ print(f"Response text: {response.text[:500]}")
84
+ return f"Error decoding server response for questions: {e}", None
85
+ except Exception as e:
86
+ print(f"An unexpected error occurred fetching questions: {e}")
87
+ return f"An unexpected error occurred fetching questions: {e}", None
88
+
89
+ # 3. Run your Agent
90
+ results_log = []
91
+ answers_payload = []
92
+ print(f"Running agent on {len(questions_data)} questions...")
93
+ for item in questions_data:
94
+ task_id = item.get("task_id")
95
+ question_text = item.get("question")
96
+ print(f"question: {question_text}")
97
+ if not task_id or question_text is None:
98
+ print(f"Skipping item with missing task_id or question: {item}")
99
+ continue
100
+ try:
101
+ submitted_answer = agent(question_text)
102
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
103
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
104
+ except Exception as e:
105
+ print(f"Error running agent on task {task_id}: {e}")
106
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
107
+
108
+ if not answers_payload:
109
+ print("Agent did not produce any answers to submit.")
110
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
111
+
112
+ # 4. Prepare Submission
113
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
114
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
115
+ print(status_update)
116
+
117
+ results_df = pd.DataFrame(results_log)
118
+ return "test", results_df
119
+
120
+
121
  def run_and_submit_all( profile: gr.OAuthProfile | None):
122
  """
123
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
135
 
136
  api_url = DEFAULT_API_URL
137
  questions_url = f"{api_url}/questions"
 
138
  submit_url = f"{api_url}/submit"
139
 
140
  # 1. Instantiate Agent ( modify this part to create your agent)
 
148
  print(agent_code)
149
 
150
  # 2. Fetch Questions
151
+ print(f"Fetching questions from: {questions_url}")
 
152
  try:
153
+ response = requests.get(questions_url, timeout=15)
 
154
  response.raise_for_status()
155
  questions_data = response.json()
156
  if not questions_data:
 
179
  for item in questions_data:
180
  task_id = item.get("task_id")
181
  question_text = item.get("question")
182
+ print(f"question: {question_text}")
183
  if not task_id or question_text is None:
184
  print(f"Skipping item with missing task_id or question: {item}")
185
  continue
 
200
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
201
  print(status_update)
202
 
203
+ # 5. Submit
204
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
205
+ try:
206
+ response = requests.post(submit_url, json=submission_data, timeout=60)
207
+ response.raise_for_status()
208
+ result_data = response.json()
209
+ final_status = (
210
+ f"Submission Successful!\n"
211
+ f"User: {result_data.get('username')}\n"
212
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
213
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
214
+ f"Message: {result_data.get('message', 'No message received.')}"
215
+ )
216
+ print("Submission successful.")
217
+ results_df = pd.DataFrame(results_log)
218
+ return final_status, results_df
219
+ except requests.exceptions.HTTPError as e:
220
+ error_detail = f"Server responded with status {e.response.status_code}."
221
+ try:
222
+ error_json = e.response.json()
223
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
224
+ except requests.exceptions.JSONDecodeError:
225
+ error_detail += f" Response: {e.response.text[:500]}"
226
+ status_message = f"Submission Failed: {error_detail}"
227
+ print(status_message)
228
+ results_df = pd.DataFrame(results_log)
229
+ return status_message, results_df
230
+ except requests.exceptions.Timeout:
231
+ status_message = "Submission Failed: The request timed out."
232
+ print(status_message)
233
+ results_df = pd.DataFrame(results_log)
234
+ return status_message, results_df
235
+ except requests.exceptions.RequestException as e:
236
+ status_message = f"Submission Failed: Network error - {e}"
237
+ print(status_message)
238
+ results_df = pd.DataFrame(results_log)
239
+ return status_message, results_df
240
+ except Exception as e:
241
+ status_message = f"An unexpected error occurred during submission: {e}"
242
+ print(status_message)
243
+ results_df = pd.DataFrame(results_log)
244
+ return status_message, results_df
245
 
246
 
247
  # --- Build Gradio Interface using Blocks ---
 
265
  gr.LoginButton()
266
 
267
  run_button = gr.Button("Run Evaluation & Submit All Answers")
268
+ run_button_one_question = gr.Button("Run One Question")
269
 
270
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
271
  # Removed max_rows=10 from DataFrame constructor
272
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
273
 
274
+ run_button_one_question.click(
275
+ fn=try_one_question,
276
+ outputs=[status_output, results_table]
277
+ )
278
+
279
  run_button.click(
280
  fn=run_and_submit_all,
281
  outputs=[status_output, results_table]