santimber commited on
Commit
8790f29
·
1 Parent(s): 25c6986

trying with file handling

Browse files
Files changed (4) hide show
  1. __pycache__/tools.cpython-311.pyc +0 -0
  2. app.py +25 -8
  3. test_agent.py +0 -38
  4. tools.py +42 -0
__pycache__/tools.cpython-311.pyc CHANGED
Binary files a/__pycache__/tools.cpython-311.pyc and b/__pycache__/tools.cpython-311.pyc differ
 
app.py CHANGED
@@ -28,6 +28,7 @@ from tools import (
28
  analyze_csv_file_tool,
29
  analyze_excel_file_tool,
30
  handle_file_tool,
 
31
  download_file,
32
  )
33
  import re
@@ -57,6 +58,7 @@ tools = [
57
  analyze_csv_file_tool,
58
  analyze_excel_file_tool,
59
  handle_file_tool,
 
60
  ]
61
  chat_with_tools = llm.bind_tools(tools)
62
 
@@ -77,13 +79,18 @@ def process_question_with_files(question_data: dict) -> str:
77
  """
78
  question_text = question_data.get('question', '')
79
  file_name = question_data.get('file_name', '')
 
80
 
81
  if not file_name:
82
  return question_text
83
 
84
- # Simple approach: just tell the agent about the file
85
- # Let the agent use its tools to download and process the file
86
- file_url = f"{DEFAULT_API_URL}/files/{file_name}"
 
 
 
 
87
 
88
  return f"{question_text}\n\n[There is an attached file: {file_name}. You can download it from: {file_url}]"
89
 
@@ -142,8 +149,8 @@ def assistant(state: MyAgent):
142
  You are a helpful assistant tasked with answering questions using a set of tools.
143
 
144
  IMPORTANT: When a question mentions an attached file, follow this process:
145
- 1. Use download_file_from_url_tool to download the file from the provided URL
146
- (Alternative: use handle_file_tool with file_type="url" for more flexible file handling)
147
  2. Use the appropriate analysis tool based on file type:
148
  - For images: use image_recognition_tool or extract_text_from_image_tool
149
  - For audio: use audio_processing_tool
@@ -246,19 +253,29 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
246
  for item in questions_data:
247
  task_id = item.get("task_id")
248
  question_text = item.get("question")
 
 
249
  if not task_id or question_text is None:
250
  print(f"Skipping item with missing task_id or question: {item}")
251
  continue
 
 
 
 
 
 
 
 
252
  try:
253
- submitted_answer = agent(question_text)
254
  answers_payload.append(
255
  {"task_id": task_id, "submitted_answer": submitted_answer})
256
  results_log.append(
257
- {"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
258
  except Exception as e:
259
  print(f"Error running agent on task {task_id}: {e}")
260
  results_log.append(
261
- {"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
262
 
263
  if not answers_payload:
264
  print("Agent did not produce any answers to submit.")
 
28
  analyze_csv_file_tool,
29
  analyze_excel_file_tool,
30
  handle_file_tool,
31
+ download_gaia_file_tool,
32
  download_file,
33
  )
34
  import re
 
58
  analyze_csv_file_tool,
59
  analyze_excel_file_tool,
60
  handle_file_tool,
61
+ download_gaia_file_tool,
62
  ]
63
  chat_with_tools = llm.bind_tools(tools)
64
 
 
79
  """
80
  question_text = question_data.get('question', '')
81
  file_name = question_data.get('file_name', '')
82
+ task_id = question_data.get('task_id', '')
83
 
84
  if not file_name:
85
  return question_text
86
 
87
+ # Use the correct API endpoint based on the documentation
88
+ # Files are accessed via /files/{task_id} not /files/{file_name}
89
+ if task_id:
90
+ file_url = f"{DEFAULT_API_URL}/files/{task_id}"
91
+ else:
92
+ # Fallback to old method if task_id is not available
93
+ file_url = f"{DEFAULT_API_URL}/files/{file_name}"
94
 
95
  return f"{question_text}\n\n[There is an attached file: {file_name}. You can download it from: {file_url}]"
96
 
 
149
  You are a helpful assistant tasked with answering questions using a set of tools.
150
 
151
  IMPORTANT: When a question mentions an attached file, follow this process:
152
+ 1. For GAIA files: Use download_gaia_file_tool with the task_id to download the file
153
+ (Alternative: use download_file_from_url_tool for other URLs, or handle_file_tool for flexible handling)
154
  2. Use the appropriate analysis tool based on file type:
155
  - For images: use image_recognition_tool or extract_text_from_image_tool
156
  - For audio: use audio_processing_tool
 
253
  for item in questions_data:
254
  task_id = item.get("task_id")
255
  question_text = item.get("question")
256
+ file_name = item.get("file_name", "")
257
+
258
  if not task_id or question_text is None:
259
  print(f"Skipping item with missing task_id or question: {item}")
260
  continue
261
+
262
+ # Create complete question data for the agent
263
+ question_data = {
264
+ "task_id": task_id,
265
+ "question": question_text,
266
+ "file_name": file_name
267
+ }
268
+
269
  try:
270
+ submitted_answer = agent(question_data)
271
  answers_payload.append(
272
  {"task_id": task_id, "submitted_answer": submitted_answer})
273
  results_log.append(
274
+ {"Task ID": task_id, "Question": question_text, "File": file_name, "Submitted Answer": submitted_answer})
275
  except Exception as e:
276
  print(f"Error running agent on task {task_id}: {e}")
277
  results_log.append(
278
+ {"Task ID": task_id, "Question": question_text, "File": file_name, "Submitted Answer": f"AGENT ERROR: {e}"})
279
 
280
  if not answers_payload:
281
  print("Agent did not produce any answers to submit.")
test_agent.py DELETED
@@ -1,38 +0,0 @@
1
- import os
2
- import requests
3
-
4
-
5
- def fetch_questions():
6
- api_url = os.getenv("DEFAULT_API_URL",
7
- "https://agents-course-unit4-scoring.hf.space")
8
- questions_url = f"{api_url}/questions"
9
- print(f"Fetching questions from: {questions_url}")
10
- try:
11
- response = requests.get(questions_url, timeout=15)
12
- response.raise_for_status()
13
- questions_data = response.json()
14
- if not questions_data:
15
- print("Fetched questions list is empty.")
16
- return []
17
- print(f"Fetched {len(questions_data)} questions.")
18
- return questions_data
19
- except Exception as e:
20
- print(f"Error fetching questions: {e}")
21
- return []
22
-
23
-
24
- def main():
25
- questions = fetch_questions()
26
- if not questions:
27
- print("No questions to display.")
28
- return
29
- print("\nFirst 20 questions from the API:")
30
- for idx, q in enumerate(questions[:20], 1):
31
- print(f"\n[{idx}] Task ID: {q.get('task_id')}")
32
- print(f"Question: {q.get('question')}")
33
- if q.get('file_name'):
34
- print(f"Attached file: {q['file_name']}")
35
-
36
-
37
- if __name__ == "__main__":
38
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tools.py CHANGED
@@ -659,6 +659,48 @@ analyze_excel_file_tool = Tool(
659
  description="Analyze an Excel file using pandas and answer a question about it."
660
  )
661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662
  # =========================
663
  # Comprehensive File Handling Tool
664
  # =========================
 
659
  description="Analyze an Excel file using pandas and answer a question about it."
660
  )
661
 
662
+ # =========================
663
+ # GAIA-Specific File Download Tool
664
+ # =========================
665
+
666
+
667
+ def download_gaia_file(task_id: str, filename: str = None) -> str:
668
+ """
669
+ Download a file from the GAIA API using the task_id.
670
+
671
+ Args:
672
+ task_id: The task ID from the GAIA API
673
+ filename: Optional filename to save as (defaults to task_id)
674
+ """
675
+ try:
676
+ if not filename:
677
+ filename = f"gaia_file_{task_id}"
678
+
679
+ # Use the correct GAIA API endpoint
680
+ api_url = "https://agents-course-unit4-scoring.hf.space"
681
+ file_url = f"{api_url}/files/{task_id}"
682
+
683
+ temp_dir = tempfile.gettempdir()
684
+ filepath = os.path.join(temp_dir, filename)
685
+
686
+ response = requests.get(file_url, stream=True, timeout=15)
687
+ response.raise_for_status()
688
+
689
+ with open(filepath, "wb") as f:
690
+ for chunk in response.iter_content(chunk_size=8192):
691
+ f.write(chunk)
692
+
693
+ return f"GAIA file downloaded to {filepath}. You can read this file to process its contents."
694
+ except Exception as e:
695
+ return f"Error downloading GAIA file: {str(e)}"
696
+
697
+
698
+ download_gaia_file_tool = Tool(
699
+ name="download_gaia_file_tool",
700
+ func=download_gaia_file,
701
+ description="Download a file from the GAIA API using task_id. Use this specifically for GAIA benchmark files."
702
+ )
703
+
704
  # =========================
705
  # Comprehensive File Handling Tool
706
  # =========================