dawid-lorek commited on
Commit
2693f75
·
verified ·
1 Parent(s): 2b6064d

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +26 -11
agent.py CHANGED
@@ -1,4 +1,6 @@
1
  import os
 
 
2
  import requests
3
  from openai import OpenAI
4
 
@@ -6,35 +8,48 @@ class GaiaAgent:
6
  def __init__(self):
7
  self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
8
  self.instructions = (
9
- "You are a highly skilled research assistant solving GAIA benchmark questions. "
10
- "You can analyze documents, perform reasoning, and answer with a single factual answer only."
11
  )
12
  self.api_url = "https://agents-course-unit4-scoring.hf.space"
13
 
14
- def fetch_file_content(self, task_id: str) -> str:
15
  try:
16
  url = f"{self.api_url}/files/{task_id}"
17
  response = requests.get(url, timeout=10)
18
  response.raise_for_status()
19
-
20
  content_type = response.headers.get("Content-Type", "")
21
- if "text" in content_type or "csv" in content_type or "json" in content_type:
22
- return response.text[:3000] # Truncate to fit token limit
 
 
 
 
 
 
 
 
 
23
  elif "application/pdf" in content_type:
24
- return "[PDF file content detected. Summarize manually or use tool.]"
 
 
 
 
25
  else:
26
  return f"[Unsupported file type: {content_type}]"
 
27
  except Exception as e:
28
- return f"[Error downloading or reading file: {e}]"
29
 
30
  def __call__(self, question: str, task_id: str = None) -> str:
31
  file_context = ""
32
  if task_id:
33
- file_context = self.fetch_file_content(task_id)
34
  if file_context:
35
- file_context = f"Here is the related file content:\n{file_context}\n"
36
 
37
- prompt = f"{self.instructions}\n\n{file_context}Question: {question}\nAnswer:"
38
 
39
  response = self.client.chat.completions.create(
40
  model="gpt-4-turbo",
 
1
  import os
2
+ import io
3
+ import pandas as pd
4
  import requests
5
  from openai import OpenAI
6
 
 
8
  def __init__(self):
9
  self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
10
  self.instructions = (
11
+ "You are an expert assistant solving GAIA benchmark questions. "
12
+ "You analyze file contents (like CSV), reason step-by-step, and respond with a single factual answer."
13
  )
14
  self.api_url = "https://agents-course-unit4-scoring.hf.space"
15
 
16
+ def fetch_file_context(self, task_id: str) -> str:
17
  try:
18
  url = f"{self.api_url}/files/{task_id}"
19
  response = requests.get(url, timeout=10)
20
  response.raise_for_status()
 
21
  content_type = response.headers.get("Content-Type", "")
22
+
23
+ if "text/csv" in content_type or url.endswith(".csv"):
24
+ df = pd.read_csv(io.StringIO(response.text))
25
+ if df.shape[1] <= 15 and df.shape[0] <= 30:
26
+ return f"CSV table preview:\n{df.to_markdown(index=False)}"
27
+ else:
28
+ return f"CSV summary: {df.shape[0]} rows, {df.shape[1]} columns.\nColumns: {', '.join(df.columns[:10])}"
29
+
30
+ elif "application/json" in content_type:
31
+ return f"JSON content:\n{response.text[:2000]}"
32
+
33
  elif "application/pdf" in content_type:
34
+ return "[PDF detected. You may need to request OCR summary.]"
35
+
36
+ elif "text/plain" in content_type:
37
+ return f"File preview:\n{response.text[:2000]}"
38
+
39
  else:
40
  return f"[Unsupported file type: {content_type}]"
41
+
42
  except Exception as e:
43
+ return f"[Error downloading or processing file: {e}]"
44
 
45
  def __call__(self, question: str, task_id: str = None) -> str:
46
  file_context = ""
47
  if task_id:
48
+ file_context = self.fetch_file_context(task_id)
49
  if file_context:
50
+ file_context = f"FILE CONTEXT:\n{file_context}\n"
51
 
52
+ prompt = f"{self.instructions}\n\n{file_context}QUESTION:\n{question}\nANSWER:"
53
 
54
  response = self.client.chat.completions.create(
55
  model="gpt-4-turbo",