DrekFretson commited on
Commit
ab4b8ca
·
verified ·
1 Parent(s): 70afa82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -147
app.py CHANGED
@@ -1,85 +1,47 @@
 
1
  import os
 
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
6
- from agent import SmolAgent
7
- import json
 
8
 
 
 
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
- USE_CACHE = False # Set to False to disable caching
13
- CACHE_FILE = "cache/answers_cache.json"
14
- QUESTIONS_CACHE_FILE = "cache/questions_cache.json"
15
 
16
 
17
- def load_cache(filename):
18
- if not USE_CACHE:
19
- print(f"Caching is disabled. Not loading from {filename}.")
20
- return None
21
- else:
22
- if os.path.exists(filename):
23
- try:
24
- with open(filename, "r") as f:
25
- return json.load(f)
26
- except Exception as e:
27
- print(f"Failed to load cache from {filename}: {e}")
28
- return None
29
 
30
-
31
- def save_cache(filename, data):
32
- if not USE_CACHE:
33
- print(f"Caching is disabled. Not loading from {filename}.")
34
- return None
35
- else:
36
- try:
37
- with open(filename, "w") as f:
38
- json.dump(data, f)
39
- except Exception as e:
40
- print(f"Failed to save cache to {filename}: {e}")
41
-
42
-
43
- def download_file(task_id):
44
- """
45
- Downloads a file for the given task_id from the /files/{task_id} endpoint,
46
- extracts the filename from the Content-Disposition header, and saves it to disk.
47
- Returns the local file path if successful, else None.
48
- """
49
- api_url = DEFAULT_API_URL
50
- file_url = f"{api_url}/files/{task_id}"
51
- try:
52
- response = requests.get(file_url, stream=True, timeout=30)
53
- response.raise_for_status()
54
- content_disp = response.headers.get("content-disposition", "")
55
- filename = None
56
- if "filename=" in content_disp:
57
- filename = content_disp.split("filename=")[-1].strip('"; ')
58
- if not filename:
59
- filename = f"{task_id}.txt"
60
- os.makedirs("cache", exist_ok=True)
61
- file_path = os.path.join("cache", filename)
62
- with open(file_path, "wb") as f:
63
- for chunk in response.iter_content(chunk_size=8192):
64
- if chunk:
65
- f.write(chunk)
66
- print(f"Downloaded file for task {task_id} to {file_path}")
67
- return file_path
68
- except Exception as e:
69
- print(f"Failed to download file for task {task_id}: {e}")
70
- return None
71
 
72
 
73
- def run_and_submit_all(profile: gr.OAuthProfile | None):
74
  """
75
  Fetches all questions, runs the BasicAgent on them, submits all answers,
76
  and displays the results.
77
  """
78
  # --- Determine HF Space Runtime URL and Repo URL ---
79
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
80
 
81
  if profile:
82
- username = f"{profile.username}"
83
  print(f"User logged in: {username}")
84
  else:
85
  print("User not logged in.")
@@ -91,7 +53,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
91
 
92
  # 1. Instantiate Agent ( modify this part to create your agent)
93
  try:
94
- agent = SmolAgent()
95
  except Exception as e:
96
  print(f"Error instantiating agent: {e}")
97
  return f"Error initializing agent: {e}", None
@@ -99,36 +61,30 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
99
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
100
  print(agent_code)
101
 
102
- # 2. Fetch Questions (with caching)
103
- questions_data = load_cache(QUESTIONS_CACHE_FILE)
104
- if questions_data:
105
- print(f"Loaded {len(questions_data)} questions from cache.")
106
- else:
107
- print(f"Fetching questions from: {questions_url}")
108
- try:
109
- response = requests.get(questions_url, timeout=15)
110
- response.raise_for_status()
111
- questions_data = response.json()
112
- if not questions_data:
113
- print("Fetched questions list is empty.")
114
- return "Fetched questions list is empty or invalid format.", None
115
- print(f"Fetched {len(questions_data)} questions.")
116
- save_cache(QUESTIONS_CACHE_FILE, questions_data)
117
- except requests.exceptions.RequestException as e:
118
- print(f"Error fetching questions: {e}")
119
- return f"Error fetching questions: {e}", None
120
- except requests.exceptions.JSONDecodeError as e:
121
- print(f"Error decoding JSON response from questions endpoint: {e}")
122
- print(f"Response text: {response.text[:500]}")
123
- return f"Error decoding server response for questions: {e}", None
124
- except Exception as e:
125
- print(f"An unexpected error occurred fetching questions: {e}")
126
- return f"An unexpected error occurred fetching questions: {e}", None
127
 
128
- # 3. Run your Agent (with answer caching)
129
  results_log = []
130
  answers_payload = []
131
- answers_cache = load_cache(CACHE_FILE) or {}
132
  print(f"Running agent on {len(questions_data)} questions...")
133
  for item in questions_data:
134
  task_id = item.get("task_id")
@@ -136,50 +92,24 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
136
  if not task_id or question_text is None:
137
  print(f"Skipping item with missing task_id or question: {item}")
138
  continue
139
- file_name = item.get("file_name")
140
-
141
- # Check if answer is cached
142
- if task_id in answers_cache:
143
- submitted_answer = answers_cache[task_id]
144
- print(f"Loaded cached answer for task {task_id}")
145
- else:
146
- try:
147
- if len(file_name) > 0:
148
- file = download_file(task_id)
149
- print(f"File for task {file_name} downloaded: {file}")
150
- submitted_answer = agent.run(question_text, file_path=file)
151
- else:
152
- submitted_answer = agent.run(question_text)
153
- answers_cache[task_id] = submitted_answer
154
- save_cache(CACHE_FILE, answers_cache)
155
- except Exception as e:
156
- print(f"Error running agent on task {task_id}: {e}")
157
- submitted_answer = f"AGENT ERROR: {e}"
158
- answers_payload.append(
159
- {"task_id": task_id, "submitted_answer": submitted_answer}
160
- )
161
- results_log.append(
162
- {
163
- "Task ID": task_id,
164
- "Question": question_text,
165
- "Submitted Answer": submitted_answer,
166
- }
167
- )
168
 
169
  if not answers_payload:
170
  print("Agent did not produce any answers to submit.")
171
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
172
 
173
- # 4. Prepare Submission
174
- submission_data = {
175
- "username": username.strip(),
176
- "agent_code": agent_code,
177
- "answers": answers_payload,
178
- }
179
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
180
  print(status_update)
181
 
182
- # # 5. Submit
183
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
184
  try:
185
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -193,7 +123,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
193
  f"Message: {result_data.get('message', 'No message received.')}"
194
  )
195
  print("Submission successful.")
196
- print(final_status)
197
  results_df = pd.DataFrame(results_log)
198
  return final_status, results_df
199
  except requests.exceptions.HTTPError as e:
@@ -226,14 +155,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
226
 
227
  # --- Build Gradio Interface using Blocks ---
228
  with gr.Blocks() as demo:
229
- gr.Markdown("# Robin's Basic Agent Evaluation Space")
230
  gr.Markdown(
231
  """
232
- ### Description
233
- My submission for the final assigment of the huggingface agent course.
234
- I chose to use the smolagents library to create a simple agent and stay away from the bigger multimodel models.
235
- By providing specifc tools to handle image and audio files.
236
- ### Original Instructions
237
  **Instructions:**
238
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
239
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
@@ -249,19 +173,20 @@ with gr.Blocks() as demo:
249
 
250
  run_button = gr.Button("Run Evaluation & Submit All Answers")
251
 
252
- status_output = gr.Textbox(
253
- label="Run Status / Submission Result", lines=5, interactive=False
254
- )
255
  # Removed max_rows=10 from DataFrame constructor
256
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
257
 
258
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
259
 
260
  if __name__ == "__main__":
261
- print("\n" + "-" * 30 + " App Starting " + "-" * 30)
262
  # Check for SPACE_HOST and SPACE_ID at startup for information
263
  space_host_startup = os.getenv("SPACE_HOST")
264
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
265
 
266
  if space_host_startup:
267
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -269,18 +194,14 @@ if __name__ == "__main__":
269
  else:
270
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
271
 
272
- if space_id_startup: # Print repo URLs if SPACE_ID is found
273
  print(f"✅ SPACE_ID found: {space_id_startup}")
274
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
275
- print(
276
- f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
277
- )
278
  else:
279
- print(
280
- "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
281
- )
282
 
283
- print("-" * (60 + len(" App Starting ")) + "\n")
284
 
285
  print("Launching Gradio Interface for Basic Agent Evaluation...")
286
  demo.launch(debug=True, share=False)
 
1
+ """ Basic Agent Evaluation Runner"""
2
  import os
3
+ import inspect
4
  import gradio as gr
5
  import requests
 
6
  import pandas as pd
7
+ from langchain_core.messages import HumanMessage
8
+ from agent import build_graph
9
+
10
 
11
+
12
+ # (Keep Constants as is)
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
16
+ # --- Basic Agent Definition ---
17
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
18
 
19
 
20
+ class BasicAgent:
21
+ """A langgraph agent."""
22
+ def __init__(self):
23
+ print("BasicAgent initialized.")
24
+ self.graph = build_graph()
 
 
 
 
 
 
 
25
 
26
+ def __call__(self, question: str) -> str:
27
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
28
+ # Wrap the question in a HumanMessage from langchain_core
29
+ messages = [HumanMessage(content=question)]
30
+ messages = self.graph.invoke({"messages": messages})
31
+ answer = messages['messages'][-1].content
32
+ return answer[14:]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
 
35
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
36
  """
37
  Fetches all questions, runs the BasicAgent on them, submits all answers,
38
  and displays the results.
39
  """
40
  # --- Determine HF Space Runtime URL and Repo URL ---
41
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
42
 
43
  if profile:
44
+ username= f"{profile.username}"
45
  print(f"User logged in: {username}")
46
  else:
47
  print("User not logged in.")
 
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
 
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: {questions_url}")
66
+ try:
67
+ response = requests.get(questions_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
+ print(f"Fetched {len(questions_data)} questions.")
74
+ except requests.exceptions.RequestException as e:
75
+ print(f"Error fetching questions: {e}")
76
+ return f"Error fetching questions: {e}", None
77
+ except requests.exceptions.JSONDecodeError as e:
78
+ print(f"Error decoding JSON response from questions endpoint: {e}")
79
+ print(f"Response text: {response.text[:500]}")
80
+ return f"Error decoding server response for questions: {e}", None
81
+ except Exception as e:
82
+ print(f"An unexpected error occurred fetching questions: {e}")
83
+ return f"An unexpected error occurred fetching questions: {e}", None
 
 
 
 
 
84
 
85
+ # 3. Run your Agent
86
  results_log = []
87
  answers_payload = []
 
88
  print(f"Running agent on {len(questions_data)} questions...")
89
  for item in questions_data:
90
  task_id = item.get("task_id")
 
92
  if not task_id or question_text is None:
93
  print(f"Skipping item with missing task_id or question: {item}")
94
  continue
95
+ try:
96
+ submitted_answer = agent(question_text)
97
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
98
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
99
+ except Exception as e:
100
+ print(f"Error running agent on task {task_id}: {e}")
101
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  if not answers_payload:
104
  print("Agent did not produce any answers to submit.")
105
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
106
 
107
+ # 4. Prepare Submission
108
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
109
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
110
  print(status_update)
111
 
112
+ # 5. Submit
113
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
114
  try:
115
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
123
  f"Message: {result_data.get('message', 'No message received.')}"
124
  )
125
  print("Submission successful.")
 
126
  results_df = pd.DataFrame(results_log)
127
  return final_status, results_df
128
  except requests.exceptions.HTTPError as e:
 
155
 
156
  # --- Build Gradio Interface using Blocks ---
157
  with gr.Blocks() as demo:
158
+ gr.Markdown("# Basic Agent Evaluation Runner")
159
  gr.Markdown(
160
  """
 
 
 
 
 
161
  **Instructions:**
162
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
163
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
 
173
 
174
  run_button = gr.Button("Run Evaluation & Submit All Answers")
175
 
176
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
 
177
  # Removed max_rows=10 from DataFrame constructor
178
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
179
 
180
+ run_button.click(
181
+ fn=run_and_submit_all,
182
+ outputs=[status_output, results_table]
183
+ )
184
 
185
  if __name__ == "__main__":
186
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
187
  # Check for SPACE_HOST and SPACE_ID at startup for information
188
  space_host_startup = os.getenv("SPACE_HOST")
189
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
190
 
191
  if space_host_startup:
192
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
194
  else:
195
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
196
 
197
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
198
  print(f"✅ SPACE_ID found: {space_id_startup}")
199
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
200
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
 
 
201
  else:
202
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
 
 
203
 
204
+ print("-"*(60 + len(" App Starting ")) + "\n")
205
 
206
  print("Launching Gradio Interface for Basic Agent Evaluation...")
207
  demo.launch(debug=True, share=False)