Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, Tool
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
|
@@ -16,37 +16,81 @@ class BasicAgent:
|
|
| 16 |
def __init__(self):
|
| 17 |
print("Initializing Smolagent...")
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
resp.raise_for_status()
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
image_analysis_tool = Tool.from_space(
|
| 25 |
-
"nvidia/LocateAnything",
|
| 26 |
-
name
|
| 27 |
-
description
|
| 28 |
-
trust_remote_code
|
| 29 |
-
)
|
| 30 |
-
#you need something to pull the spreadhseetso write a custom function for that
|
| 31 |
-
|
| 32 |
-
spreadsheet_analyser = Tool.from_space(
|
| 33 |
-
"Abhi8720/spreadsheet-whisperer",
|
| 34 |
-
name="spreadsheet_analyser",
|
| 35 |
-
description="helps answer questions about spreadsheets",
|
| 36 |
-
trust_remote_code=True
|
| 37 |
)
|
| 38 |
-
|
| 39 |
self.agent = CodeAgent(
|
| 40 |
-
tools=[
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
#answering questions
|
| 51 |
def __call__(self, question: str) -> str:
|
| 52 |
print(f"Agent received question: {question[:50]}...")
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, Tool, tool
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
|
|
|
| 16 |
def __init__(self):
|
| 17 |
print("Initializing Smolagent...")
|
| 18 |
|
| 19 |
+
@tool
|
| 20 |
+
def fetch_task_file(task_id: str) -> str:
|
| 21 |
+
"""
|
| 22 |
+
Downloads the file attached to a GAIA task and saves it locally.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
task_id: The task_id of the current question.
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
The local file path where the file was saved.
|
| 29 |
+
"""
|
| 30 |
+
resp = requests.get(f"{DEFAULT_API_URL}/files/{task_id}")
|
| 31 |
resp.raise_for_status()
|
| 32 |
+
# Try to infer extension from content-type; default to .bin
|
| 33 |
+
ext = resp.headers.get("content-type", "").split("/")[-1].split(";")[0]
|
| 34 |
+
path = f"/tmp/{task_id}.{ext or 'bin'}"
|
| 35 |
+
with open(path, "wb") as f:
|
| 36 |
+
f.write(resp.content)
|
| 37 |
+
return path
|
| 38 |
+
|
| 39 |
+
@tool
|
| 40 |
+
def read_spreadsheet(file_path: str) -> str:
|
| 41 |
+
"""
|
| 42 |
+
Reads a CSV or Excel file and returns a text summary of its contents.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
file_path: Local path to the spreadsheet file.
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
A string representation of the dataframe.
|
| 49 |
+
"""
|
| 50 |
+
if file_path.endswith(".csv"):
|
| 51 |
+
df = pd.read_csv(file_path)
|
| 52 |
+
else:
|
| 53 |
+
df = pd.read_excel(file_path)
|
| 54 |
+
return df.to_string()
|
| 55 |
|
| 56 |
image_analysis_tool = Tool.from_space(
|
| 57 |
+
"nvidia/LocateAnything",
|
| 58 |
+
name="image_analyser",
|
| 59 |
+
description="Analyses images to locate and describe objects in them.",
|
| 60 |
+
trust_remote_code=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
+
|
| 63 |
self.agent = CodeAgent(
|
| 64 |
+
tools=[
|
| 65 |
+
DuckDuckGoSearchTool(),
|
| 66 |
+
fetch_task_file,
|
| 67 |
+
read_spreadsheet,
|
| 68 |
+
image_analysis_tool,
|
| 69 |
+
],
|
| 70 |
+
model=InferenceClientModel("Qwen/Qwen2.5-VL-72B-Instruct"),
|
| 71 |
+
instructions=(
|
| 72 |
+
"Answer with only the final answer value — no explanation, "
|
| 73 |
+
"no 'FINAL ANSWER' prefix, no extra formatting. "
|
| 74 |
+
"If the question references a file, first call fetch_task_file "
|
| 75 |
+
"with the given task_id to download it, then use the appropriate "
|
| 76 |
+
"tool (read_spreadsheet for tables, image_analyser for images) "
|
| 77 |
+
"to inspect it before answering. Use DuckDuckGoSearchTool only "
|
| 78 |
+
"when the question needs current or external information."
|
| 79 |
+
),
|
| 80 |
+
max_steps=10,
|
| 81 |
)
|
| 82 |
+
|
| 83 |
+
def __call__(self, question: str, task_id: str = None) -> str:
|
| 84 |
+
print(f"Agent received question: {question[:50]}...")
|
| 85 |
+
full_prompt = question
|
| 86 |
+
if task_id:
|
| 87 |
+
full_prompt = f"{question}\n\n(task_id for any file: {task_id})"
|
| 88 |
+
try:
|
| 89 |
+
answer = self.agent.run(full_prompt)
|
| 90 |
+
return str(answer).strip()
|
| 91 |
+
except Exception as e:
|
| 92 |
+
print(f"Error during agent run: {e}")
|
| 93 |
+
return "Error"
|
| 94 |
#answering questions
|
| 95 |
def __call__(self, question: str) -> str:
|
| 96 |
print(f"Agent received question: {question[:50]}...")
|