Update app.py
Browse filestesting the gemini audio understanding
app.py
CHANGED
@@ -4,6 +4,7 @@ import pytz
|
|
4 |
import time
|
5 |
import wikipediaapi
|
6 |
import yaml
|
|
|
7 |
from tools.final_answer import FinalAnswerTool
|
8 |
|
9 |
import os
|
@@ -158,14 +159,25 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
158 |
answers_payload = []
|
159 |
print(f"Running agent on {len(questions_data)} questions...")
|
160 |
for item in questions_data:
|
|
|
|
|
|
|
161 |
task_id = item.get("task_id")
|
162 |
-
question_text = item.get("question") + " think hard to answer."
|
163 |
if not task_id or question_text is None:
|
164 |
print(f"Skipping item with missing task_id or question: {item}")
|
165 |
continue
|
166 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
# check if the file_name is not empty
|
168 |
-
|
169 |
question_text = f"{question_text} Here is the file: https://agents-course-unit4-scoring.hf.space/files/{item.get('task_id')}"
|
170 |
submitted_answer = agent(question_text)
|
171 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
@@ -180,55 +192,55 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
180 |
print("Agent did not produce any answers to submit.")
|
181 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
182 |
|
183 |
-
|
184 |
|
185 |
# 4. Prepare Submission
|
186 |
-
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
187 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
188 |
-
print(status_update)
|
189 |
|
190 |
# 5. Submit
|
191 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
192 |
-
try:
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
except requests.exceptions.HTTPError as e:
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
except requests.exceptions.Timeout:
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
except requests.exceptions.RequestException as e:
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
except Exception as e:
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
|
233 |
|
234 |
# --- Build Gradio Interface using Blocks ---
|
|
|
4 |
import time
|
5 |
import wikipediaapi
|
6 |
import yaml
|
7 |
+
from google import genai
|
8 |
from tools.final_answer import FinalAnswerTool
|
9 |
|
10 |
import os
|
|
|
159 |
answers_payload = []
|
160 |
print(f"Running agent on {len(questions_data)} questions...")
|
161 |
for item in questions_data:
|
162 |
+
if !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 |
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}
|
199 |
+
# status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
200 |
+
# print(status_update)
|
201 |
|
202 |
# 5. Submit
|
203 |
+
# print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
204 |
+
# try:
|
205 |
+
# response = requests.post(submit_url, json=submission_data, timeout=60)
|
206 |
+
# response.raise_for_status()
|
207 |
+
# result_data = response.json()
|
208 |
+
# final_status = (
|
209 |
+
# f"Submission Successful!\n"
|
210 |
+
# f"User: {result_data.get('username')}\n"
|
211 |
+
# f"Overall Score: {result_data.get('score', 'N/A')}% "
|
212 |
+
# f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
213 |
+
# f"Message: {result_data.get('message', 'No message received.')}"
|
214 |
+
# )
|
215 |
+
# print("Submission successful.")
|
216 |
+
# results_df = pd.DataFrame(results_log)
|
217 |
+
# return final_status, results_df
|
218 |
+
# except requests.exceptions.HTTPError as e:
|
219 |
+
# error_detail = f"Server responded with status {e.response.status_code}."
|
220 |
+
# try:
|
221 |
+
# error_json = e.response.json()
|
222 |
+
# error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
223 |
+
# except requests.exceptions.JSONDecodeError:
|
224 |
+
# error_detail += f" Response: {e.response.text[:500]}"
|
225 |
+
# status_message = f"Submission Failed: {error_detail}"
|
226 |
+
# print(status_message)
|
227 |
+
# results_df = pd.DataFrame(results_log)
|
228 |
+
# return status_message, results_df
|
229 |
+
# except requests.exceptions.Timeout:
|
230 |
+
# status_message = "Submission Failed: The request timed out."
|
231 |
+
# print(status_message)
|
232 |
+
# results_df = pd.DataFrame(results_log)
|
233 |
+
# return status_message, results_df
|
234 |
+
# except requests.exceptions.RequestException as e:
|
235 |
+
# status_message = f"Submission Failed: Network error - {e}"
|
236 |
+
# print(status_message)
|
237 |
+
# results_df = pd.DataFrame(results_log)
|
238 |
+
# return status_message, results_df
|
239 |
+
# except Exception as e:
|
240 |
+
# status_message = f"An unexpected error occurred during submission: {e}"
|
241 |
+
# print(status_message)
|
242 |
+
# results_df = pd.DataFrame(results_log)
|
243 |
+
# return status_message, results_df
|
244 |
|
245 |
|
246 |
# --- Build Gradio Interface using Blocks ---
|