civerson916 commited on
Commit
4f6359a
·
verified ·
1 Parent(s): 8fce68c

Update app.py

Browse files

Added a FetchFileTool and an AudioDescriptionTool

Files changed (1) hide show
  1. app.py +67 -14
app.py CHANGED
@@ -30,6 +30,67 @@ model = LiteLLMModel(
30
  # Import tool from Hub
31
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  class WikipediaSearchTool(Tool):
34
  name = "wikipedia_search"
35
  description = "Fetches a summary of a Wikipedia page based on a given search query (only one word or group of words)."
@@ -85,7 +146,7 @@ class BasicAgent:
85
  prompt_templates = yaml.safe_load(stream)
86
  self.agent = CodeAgent(
87
  model=model,
88
- tools=[final_answer, search_tool, wiki_tool],
89
  max_steps=7,
90
  verbosity_level=1,
91
  grammar=None,
@@ -159,25 +220,17 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
159
  answers_payload = []
160
  print(f"Running agent on {len(questions_data)} questions...")
161
  for item in questions_data:
162
- if not item.get("file_name"):
163
- continue
164
-
165
  task_id = item.get("task_id")
166
- question_text = item.get("question") + " think hard to answer."
167
  if not task_id or question_text is None:
168
  print(f"Skipping item with missing task_id or question: {item}")
169
  continue
170
  try:
171
  # check if question_text contains an mp3 filename
172
- if item.get("file_name") and ".mp3" in question_text:
173
- client = genai.Client(api_key=os.environ.get("GEMINI_KEY"))
174
- mp3_file = client.files.upload(file=f"/files/{item.get('task_id')}")
175
- audio_description = client.models.generate_content(
176
- model="gemini-2.0-flash", contents=["Describe this audio clip", mp3_file]
177
- )
178
- question_text = f"{question_text} {audio_description.text}"
179
  # check if the file_name is not empty
180
- elif item.get("file_name"):
181
  question_text = f"{question_text} Here is the file: https://agents-course-unit4-scoring.hf.space/files/{item.get('task_id')}"
182
  submitted_answer = agent(question_text)
183
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
@@ -192,7 +245,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
192
  print("Agent did not produce any answers to submit.")
193
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
194
 
195
- return "Questions parsed.", pd.DataFrame(results_log)
196
 
197
  # 4. Prepare Submission
198
  # submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
30
  # Import tool from Hub
31
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
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",
39
+ "description": "The name of the local file to describe, for example: /files/99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3",
40
+ }
41
+ }
42
+ output_type = "string"
43
+ def __init__(self):
44
+ self.client = genai.Client(api_key=os.environ.get("GEMINI_KEY"))
45
+
46
+ def forward(self, file_name: str):
47
+ try:
48
+ mp3_file = self.client.files.upload(file=f"{file_name}")
49
+ audio_description = self.client.models.generate_content(
50
+ model="gemini-2.0-flash", contents=["Describe this audio clip", mp3_file]
51
+ )
52
+ return audio_description.text
53
+ except Exception as e:
54
+ print(f"Error getting audio description: {e}")
55
+ return False
56
+
57
+ audio_description_tool = AudioDescriptionTool()
58
+ class FetchFileTool(Tool):
59
+ name = "file_saver"
60
+ description = """
61
+ This is a tool that will fetch an internet file and save content locally.
62
+ It takes a location, file path, file name, and content to save."""
63
+ inputs = {
64
+ "location": {
65
+ "type": "string",
66
+ "description": "The internet location of the file to fetch",
67
+ },
68
+ "file_path": {
69
+ "type": "string",
70
+ "description": "The path to the directory where the file will be saved",
71
+ },
72
+ "file_name": {
73
+ "type": "string",
74
+ "description": "The name of the file where content will be saved",
75
+ },
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
+
92
+ fetch_file_tool = FetchFileTool()
93
+
94
  class WikipediaSearchTool(Tool):
95
  name = "wikipedia_search"
96
  description = "Fetches a summary of a Wikipedia page based on a given search query (only one word or group of words)."
 
146
  prompt_templates = yaml.safe_load(stream)
147
  self.agent = CodeAgent(
148
  model=model,
149
+ tools=[final_answer, search_tool, wiki_tool, fetch_file_tool, audio_description_tool],
150
  max_steps=7,
151
  verbosity_level=1,
152
  grammar=None,
 
220
  answers_payload = []
221
  print(f"Running agent on {len(questions_data)} questions...")
222
  for item in questions_data:
 
 
 
223
  task_id = item.get("task_id")
224
+ question_text = item.get("question") + " think hard to answer."
225
  if not task_id or question_text is None:
226
  print(f"Skipping item with missing task_id or question: {item}")
227
  continue
228
  try:
229
  # check if question_text contains an mp3 filename
230
+ if not item.get("file_name") and ".mp3" in question_text:
231
+ continue
 
 
 
 
 
232
  # check if the file_name is not empty
233
+ if item.get("file_name"):
234
  question_text = f"{question_text} Here is the file: https://agents-course-unit4-scoring.hf.space/files/{item.get('task_id')}"
235
  submitted_answer = agent(question_text)
236
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
 
245
  print("Agent did not produce any answers to submit.")
246
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
247
 
248
+ # return "Questions parsed.", pd.DataFrame(results_log)
249
 
250
  # 4. Prepare Submission
251
  # submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}