Update agent.py
Browse files
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
|
10 |
-
"You
|
11 |
)
|
12 |
self.api_url = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
14 |
-
def
|
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 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
elif "application/pdf" in content_type:
|
24 |
-
return "[PDF
|
|
|
|
|
|
|
|
|
25 |
else:
|
26 |
return f"[Unsupported file type: {content_type}]"
|
|
|
27 |
except Exception as e:
|
28 |
-
return f"[Error downloading or
|
29 |
|
30 |
def __call__(self, question: str, task_id: str = None) -> str:
|
31 |
file_context = ""
|
32 |
if task_id:
|
33 |
-
file_context = self.
|
34 |
if file_context:
|
35 |
-
file_context = f"
|
36 |
|
37 |
-
prompt = f"{self.instructions}\n\n{file_context}
|
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",
|