tojonatolotra commited on
Commit
030fabd
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -6
app.py CHANGED
@@ -8,16 +8,76 @@ import pandas as pd
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
13
  class BasicAgent:
14
  def __init__(self):
15
- print("BasicAgent initialized.")
16
- def __call__(self, question: str) -> str:
 
 
17
  print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
@@ -80,7 +140,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
80
  print(f"Skipping item with missing task_id or question: {item}")
81
  continue
82
  try:
83
- submitted_answer = agent(question_text)
 
84
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
85
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
86
  except Exception as e:
 
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
+ def fetch_task_file(task_id: str, file_name: str | None = None) -> str | None:
12
+ """Download the file attached to a GAIA task, if any.
13
+
14
+ Args:
15
+ task_id: Unique identifier of the GAIA task.
16
+ file_name: Optional name of the file.
17
+
18
+ Returns:
19
+ Local path of the downloaded file, or None if no file is attached.
20
+ """
21
+ import os
22
+ # Try downloading from Hugging Face dataset if file_name is provided
23
+ if file_name:
24
+ url = f"https://huggingface.co/datasets/gaia-benchmark/GAIA/resolve/main/2023/validation/{file_name}"
25
+ token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
26
+ headers = {"Authorization": f"Bearer {token}"} if token else {}
27
+ try:
28
+ response = requests.get(url, headers=headers, timeout=30)
29
+ if response.status_code == 200 and len(response.content) > 0:
30
+ filename = f"task_{task_id}_{file_name}"
31
+ with open(filename, "wb") as f:
32
+ f.write(response.content)
33
+ print(f" [file] Downloaded attachment from HF GAIA repo: {filename} ({len(response.content)} bytes)")
34
+ return filename
35
+ except Exception as e:
36
+ print(f" [warn] Direct HF GAIA download failed for {file_name}: {e}")
37
+
38
+ # Fallback to course scoring API
39
+ url = f"{DEFAULT_API_URL}/files/{task_id}"
40
+ try:
41
+ response = requests.get(url, timeout=30)
42
+ if response.status_code == 200 and len(response.content) > 0:
43
+ # Detect file extension from Content-Type header
44
+ content_type = response.headers.get("Content-Type", "")
45
+ ext = ".bin"
46
+ if "pdf" in content_type:
47
+ ext = ".pdf"
48
+ elif "image" in content_type:
49
+ ext = ".png"
50
+ elif "text/csv" in content_type:
51
+ ext = ".csv"
52
+ elif "audio" in content_type:
53
+ ext = ".mp3"
54
+
55
+ filename = f"task_{task_id}{ext}"
56
+ with open(filename, "wb") as f:
57
+ f.write(response.content)
58
+ print(f" [file] Downloaded attachment from scoring server: {filename} ({len(response.content)} bytes)")
59
+ return filename
60
+ except Exception as e:
61
+ print(f" [warn] Could not fetch file from scoring server for task {task_id}: {e}")
62
+ return None
63
+
64
+
65
  # --- Basic Agent Definition ---
66
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
67
+ from agent import GAIAAgent
68
+
69
  class BasicAgent:
70
  def __init__(self):
71
+ self.agent = GAIAAgent()
72
+ print("BasicAgent (GAIAAgent wrapper) initialized.")
73
+
74
+ def __call__(self, question: str, task_id: str | None = None, file_name: str | None = None) -> str:
75
  print(f"Agent received question (first 50 chars): {question[:50]}...")
76
+ if task_id:
77
+ filepath = fetch_task_file(task_id, file_name)
78
+ if filepath:
79
+ question = f"{question}\n\n[Attached file available locally: {filepath}]"
80
+ return self.agent(question)
81
 
82
  def run_and_submit_all( profile: gr.OAuthProfile | None):
83
  """
 
140
  print(f"Skipping item with missing task_id or question: {item}")
141
  continue
142
  try:
143
+ file_name = item.get("file_name", "")
144
+ submitted_answer = agent(question_text, task_id=task_id, file_name=file_name)
145
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
146
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
147
  except Exception as e: