civerson916 commited on
Commit
77ccfad
·
verified ·
1 Parent(s): 18e5479

Update app.py

Browse files

Getting files locally as fallback for 429 errors, also added testing condition

Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -1,6 +1,7 @@
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, Tool, HfApiModel, LiteLLMModel, load_tool, tool
2
  import datetime
3
  import pytz
 
4
  import time
5
  import wikipediaapi
6
  import yaml
@@ -32,7 +33,7 @@ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_co
32
 
33
  class AudioDescriptionTool(Tool):
34
  name = "audio_description"
35
- description = "This is a tool that will describe an audio clip."
36
  inputs = {
37
  "file_name": {
38
  "type": "string",
@@ -55,6 +56,7 @@ class AudioDescriptionTool(Tool):
55
  return False
56
 
57
  audio_description_tool = AudioDescriptionTool()
 
58
  class FetchFileTool(Tool):
59
  name = "file_saver"
60
  description = """
@@ -76,16 +78,26 @@ class FetchFileTool(Tool):
76
  }
77
  output_type = "boolean"
78
 
79
- def forward(self, location: str, file_path: str, file_name: str):
80
- try:
81
- response = requests.get(location)
82
- response.raise_for_status()
83
- content = response.content
 
 
 
 
 
 
 
 
 
 
 
84
  full_path = os.path.join(file_path, file_name)
85
- with open(full_path, 'wb') as out_file:
86
- out_file.write(content)
87
  return True
88
- except Exception as e:
89
  print(f"Error saving content to file: {e}")
90
  return False
91
 
@@ -241,7 +253,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
241
  continue
242
  try:
243
  # check if the file_name is not empty
244
- if item.get("file_name"):
 
 
245
  question_text = f"{question_text} Here is the file: https://agents-course-unit4-scoring.hf.space/files/{item.get('task_id')}"
246
  else:
247
  continue
@@ -258,7 +272,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
258
  print("Agent did not produce any answers to submit.")
259
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
260
 
261
- # return "Questions parsed.", pd.DataFrame(results_log)
262
 
263
  # 4. Prepare Submission
264
  # submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, Tool, HfApiModel, LiteLLMModel, load_tool, tool
2
  import datetime
3
  import pytz
4
+ import shutil
5
  import time
6
  import wikipediaapi
7
  import yaml
 
33
 
34
  class AudioDescriptionTool(Tool):
35
  name = "audio_description"
36
+ description = "This is a tool that will describe a local audio clip."
37
  inputs = {
38
  "file_name": {
39
  "type": "string",
 
56
  return False
57
 
58
  audio_description_tool = AudioDescriptionTool()
59
+
60
  class FetchFileTool(Tool):
61
  name = "file_saver"
62
  description = """
 
78
  }
79
  output_type = "boolean"
80
 
81
+ def forward(self, location: str, file_path: str, file_name: str):
82
+ try:
83
+ response = requests.get(location)
84
+ response.raise_for_status()
85
+ content = response.content
86
+ full_path = os.path.join(file_path, file_name)
87
+ with open(full_path, 'wb') as out_file:
88
+ out_file.write(content)
89
+ return True
90
+
91
+ except Exception as e:
92
+ # if error is HTTP 429 then copy from /files
93
+ if response.status_code == 429:
94
+ # get last part of location separated by slash
95
+ file_id = location.split('/')[-1]
96
+ local_path = f"files/{file_id}"
97
  full_path = os.path.join(file_path, file_name)
98
+ shutil.copy(local_path, full_path)
 
99
  return True
100
+ else:
101
  print(f"Error saving content to file: {e}")
102
  return False
103
 
 
253
  continue
254
  try:
255
  # check if the file_name is not empty
256
+ # 99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3"
257
+ if item.get("task_id") == "99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3":
258
+ # if item.get("file_name"):
259
  question_text = f"{question_text} Here is the file: https://agents-course-unit4-scoring.hf.space/files/{item.get('task_id')}"
260
  else:
261
  continue
 
272
  print("Agent did not produce any answers to submit.")
273
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
274
 
275
+ return "Questions parsed.", pd.DataFrame(results_log)
276
 
277
  # 4. Prepare Submission
278
  # submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}