nismamjad commited on
Commit
da58ac4
·
verified ·
1 Parent(s): 0de4662

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -8,7 +8,8 @@ from huggingface_hub import login
8
  import requests
9
  import json
10
  import base64 # <-- ADDED IMPORT for image handling
11
-
 
12
  # Attempt to login using environment token
13
  try:
14
  HF_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
@@ -197,6 +198,30 @@ def get_gaia_file_data_for_task(task_id_for_file_fetch, associated_file_metadata
197
  print(f"Unexpected error fetching file for task {task_id_for_file_fetch}: {e_gen}")
198
  return None, None, None
199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  def my_agent_logic(task_id: str, question: str, files_metadata: list = None): # files_metadata is the list from task.get("files")
201
  """
202
  Uses the Gemini API, with GAIA-specific prompting and basic file handling,
@@ -291,17 +316,10 @@ def my_agent_logic(task_id: str, question: str, files_metadata: list = None): #
291
  result["candidates"][0].get("content") and
292
  result["candidates"][0]["content"].get("parts") and
293
  result["candidates"][0]["content"]["parts"][0].get("text")):
294
- raw_answer = result["candidates"][0]["content"]["parts"][0]["text"].strip()
295
 
296
- # Remove the "FINAL ANSWER:" prefix if the LLM included it, despite instructions
297
- if raw_answer.upper().startswith("FINAL ANSWER:"):
298
- agent_computed_answer = raw_answer[len("FINAL ANSWER:"):].strip()
299
- else:
300
- agent_computed_answer = raw_answer
301
- # Further cleaning: sometimes LLMs might still add subtle quotes if the answer is a simple string
302
- if len(agent_computed_answer) > 1 and ((agent_computed_answer.startswith('"') and agent_computed_answer.endswith('"')) or \
303
- (agent_computed_answer.startswith("'") and agent_computed_answer.endswith("'"))):
304
- agent_computed_answer = agent_computed_answer[1:-1]
305
 
306
  else:
307
  print(f"Warning: Unexpected response structure from Gemini API for task {task_id}: {result}")
@@ -364,6 +382,7 @@ def run_agent_on_gaia(profile: gr.OAuthProfile, run_all_questions: bool = True):
364
  submitted_answer = my_agent_logic(task_id, question, associated_files_metadata)
365
  log_messages.append(f"Agent's Answer: {submitted_answer}")
366
  answers_to_submit.append({"task_id": task_id, "submitted_answer": submitted_answer})
 
367
  else:
368
  log_messages.append(f"Skipping malformed task: {task}")
369
  if not answers_to_submit:
 
8
  import requests
9
  import json
10
  import base64 # <-- ADDED IMPORT for image handling
11
+ import re
12
+ import time
13
  # Attempt to login using environment token
14
  try:
15
  HF_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
 
198
  print(f"Unexpected error fetching file for task {task_id_for_file_fetch}: {e_gen}")
199
  return None, None, None
200
 
201
+
202
+ def clean_final_answer(raw_text):
203
+ # Step 1: Extract FINAL ANSWER section if present
204
+ if "FINAL ANSWER:" in raw_text.upper():
205
+ match = re.search(r"FINAL ANSWER:\s*(.*)", raw_text, re.IGNORECASE | re.DOTALL)
206
+ if match:
207
+ answer = match.group(1).strip()
208
+ else:
209
+ answer = raw_text.strip()
210
+ else:
211
+ answer = raw_text.strip()
212
+
213
+ # Step 2: Remove wrapping quotes if any
214
+ if (answer.startswith('"') and answer.endswith('"')) or (answer.startswith("'") and answer.endswith("'")):
215
+ answer = answer[1:-1].strip()
216
+
217
+ # Step 3: Remove unwanted units unless explicitly required
218
+ # You may adjust this depending on the task
219
+ answer = answer.replace('%', '').replace('$', '').strip()
220
+
221
+ # Step 4: Normalize spaces (e.g., for comma-separated answers)
222
+ answer = re.sub(r'\s*,\s*', ',', answer)
223
+
224
+ return answer
225
  def my_agent_logic(task_id: str, question: str, files_metadata: list = None): # files_metadata is the list from task.get("files")
226
  """
227
  Uses the Gemini API, with GAIA-specific prompting and basic file handling,
 
316
  result["candidates"][0].get("content") and
317
  result["candidates"][0]["content"].get("parts") and
318
  result["candidates"][0]["content"]["parts"][0].get("text")):
 
319
 
320
+ raw_answer = result["candidates"][0]["content"]["parts"][0]["text"].strip()
321
+ print(f"Raw Gemini output: {raw_answer}") # Debugging log
322
+ agent_computed_answer = clean_final_answer(raw_answer)
 
 
 
 
 
 
323
 
324
  else:
325
  print(f"Warning: Unexpected response structure from Gemini API for task {task_id}: {result}")
 
382
  submitted_answer = my_agent_logic(task_id, question, associated_files_metadata)
383
  log_messages.append(f"Agent's Answer: {submitted_answer}")
384
  answers_to_submit.append({"task_id": task_id, "submitted_answer": submitted_answer})
385
+ time.sleep(2)
386
  else:
387
  log_messages.append(f"Skipping malformed task: {task}")
388
  if not answers_to_submit: