Update agent.py
Browse files
agent.py
CHANGED
@@ -2,6 +2,8 @@ import os
|
|
2 |
import requests
|
3 |
import re
|
4 |
import base64
|
|
|
|
|
5 |
from openai import OpenAI
|
6 |
|
7 |
class GaiaAgent:
|
@@ -39,11 +41,22 @@ class GaiaAgent:
|
|
39 |
return {
|
40 |
"role": "user",
|
41 |
"content": [
|
42 |
-
{"type": "text", "text": "Please analyze the image and answer the
|
43 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_b64}"}}
|
44 |
]
|
45 |
}
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
def __call__(self, question: str, task_id: str = None) -> str:
|
48 |
messages = [{"role": "system", "content": self.instructions}]
|
49 |
|
@@ -73,6 +86,9 @@ class GaiaAgent:
|
|
73 |
elif isinstance(content_type, str) and "audio" in content_type:
|
74 |
messages.append({"role": "user", "content": f"[Audio content detected]\n\nQuestion: {question}"})
|
75 |
|
|
|
|
|
|
|
76 |
video_context = self.extract_youtube_context(question)
|
77 |
if video_context:
|
78 |
messages.append({"role": "user", "content": f"{video_context}\n\nQuestion: {question}"})
|
@@ -88,4 +104,3 @@ class GaiaAgent:
|
|
88 |
return response.choices[0].message.content.strip()
|
89 |
except Exception as e:
|
90 |
return f"[Answer error: {e}]"
|
91 |
-
|
|
|
2 |
import requests
|
3 |
import re
|
4 |
import base64
|
5 |
+
import pandas as pd
|
6 |
+
import io
|
7 |
from openai import OpenAI
|
8 |
|
9 |
class GaiaAgent:
|
|
|
41 |
return {
|
42 |
"role": "user",
|
43 |
"content": [
|
44 |
+
{"type": "text", "text": "Please analyze the image and answer the question accurately."},
|
45 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_b64}"}}
|
46 |
]
|
47 |
}
|
48 |
|
49 |
+
def handle_excel_sales_question(self, excel_bytes: bytes, question: str) -> str:
|
50 |
+
try:
|
51 |
+
df = pd.read_excel(io.BytesIO(excel_bytes))
|
52 |
+
if 'category' in df.columns and 'sales' in df.columns:
|
53 |
+
food_only = df[df['category'].str.lower() == 'food']
|
54 |
+
total = food_only['sales'].sum()
|
55 |
+
return f"${total:.2f}"
|
56 |
+
return "[SKIPPED: Required columns not found in Excel]"
|
57 |
+
except Exception as e:
|
58 |
+
return f"[Excel processing error: {e}]"
|
59 |
+
|
60 |
def __call__(self, question: str, task_id: str = None) -> str:
|
61 |
messages = [{"role": "system", "content": self.instructions}]
|
62 |
|
|
|
86 |
elif isinstance(content_type, str) and "audio" in content_type:
|
87 |
messages.append({"role": "user", "content": f"[Audio content detected]\n\nQuestion: {question}"})
|
88 |
|
89 |
+
elif isinstance(content_type, str) and "spreadsheet" in content_type or content_type.endswith("excel") or content_type.endswith("xlsx"):
|
90 |
+
return self.handle_excel_sales_question(file_data, question)
|
91 |
+
|
92 |
video_context = self.extract_youtube_context(question)
|
93 |
if video_context:
|
94 |
messages.append({"role": "user", "content": f"{video_context}\n\nQuestion: {question}"})
|
|
|
104 |
return response.choices[0].message.content.strip()
|
105 |
except Exception as e:
|
106 |
return f"[Answer error: {e}]"
|
|