Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool,Tool,tool,VisitWebpageTool,FinalAnswerTool
|
| 7 |
-
|
| 8 |
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
|
@@ -79,7 +79,7 @@ class BasicAgent:
|
|
| 79 |
df = pd.read_excel(file_path)
|
| 80 |
return df.to_string()
|
| 81 |
|
| 82 |
-
|
| 83 |
name = "read_pdf"
|
| 84 |
description = "Extracts text from a PDF file."
|
| 85 |
inputs = {
|
|
@@ -93,7 +93,56 @@ class BasicAgent:
|
|
| 93 |
def forward(self, file_path: str) -> str:
|
| 94 |
from pypdf import PdfReader
|
| 95 |
reader = PdfReader(file_path)
|
| 96 |
-
return "\n".join(page.extract_text() or "" for page in reader.pages)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
#pdf_tool = load_tool(
|
| 98 |
#"matterattetatte/pdf-upload-extractor-tool",
|
| 99 |
# trust_remote_code = True
|
|
@@ -110,11 +159,15 @@ class BasicAgent:
|
|
| 110 |
#read_pdf,
|
| 111 |
read_spreadsheet,
|
| 112 |
get_youtube_transcript,
|
|
|
|
|
|
|
| 113 |
],
|
| 114 |
model= InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct") ,
|
| 115 |
-
|
| 116 |
instructions = ("You are an advanced CodeAgent that will show your capabilities to work in the real world by being tested in GAIA, the agent testing platform. If the question includes a task_id and mentions a file, call fetch_task_file first; route YouTube URLs to get_youtube_transcript, other URLs to visit_webpage, PDFs to pdf_tool, and spreadsheets to read_spreadsheet, using web_search only when no URL or file is given,then respond with only the exact final answer value, no explanation, no prefix."
|
| 117 |
-
"Use the Thought Action observation to produce high quality results and only answer when you are sure you have performed the necessary steps for the task and question"
|
|
|
|
|
|
|
| 118 |
max_steps=20,
|
| 119 |
)
|
| 120 |
#answering questions
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool,Tool,tool,VisitWebpageTool,FinalAnswerTool
|
| 7 |
+
import base64
|
| 8 |
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
|
|
|
| 79 |
df = pd.read_excel(file_path)
|
| 80 |
return df.to_string()
|
| 81 |
|
| 82 |
+
class ReadPDFTool(Tool):
|
| 83 |
name = "read_pdf"
|
| 84 |
description = "Extracts text from a PDF file."
|
| 85 |
inputs = {
|
|
|
|
| 93 |
def forward(self, file_path: str) -> str:
|
| 94 |
from pypdf import PdfReader
|
| 95 |
reader = PdfReader(file_path)
|
| 96 |
+
return "\n".join(page.extract_text() or "" for page in reader.pages)
|
| 97 |
+
|
| 98 |
+
@tool
|
| 99 |
+
def analyze_image(file_path: str, question: str) -> str:
|
| 100 |
+
"""
|
| 101 |
+
Analyzes an image and answers a question about its contents.
|
| 102 |
+
|
| 103 |
+
Args:
|
| 104 |
+
file_path: Local path to the image file.
|
| 105 |
+
question: What to look for or answer about the image.
|
| 106 |
+
|
| 107 |
+
Returns:
|
| 108 |
+
A text answer describing what's found in the image.
|
| 109 |
+
"""
|
| 110 |
+
client = InferenceClient(token=os.environ["HF_TOKEN"])
|
| 111 |
+
|
| 112 |
+
with open(file_path, "rb") as f:
|
| 113 |
+
image_bytes = f.read()
|
| 114 |
+
image_b64 = base64.b64encode(image_bytes).decode("utf-8")
|
| 115 |
+
|
| 116 |
+
result = client.chat_completion(
|
| 117 |
+
model="Qwen/Qwen2.5-VL-72B-Instruct",
|
| 118 |
+
messages=[
|
| 119 |
+
{
|
| 120 |
+
"role": "user",
|
| 121 |
+
"content": [
|
| 122 |
+
{"type": "text", "text": question},
|
| 123 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}},
|
| 124 |
+
]
|
| 125 |
+
}
|
| 126 |
+
]
|
| 127 |
+
)
|
| 128 |
+
return result.choices[0].message.content
|
| 129 |
+
@tool
|
| 130 |
+
def transcribe_audio(file_path: str) -> str:
|
| 131 |
+
"""
|
| 132 |
+
Transcribes speech from an audio file to text.
|
| 133 |
+
|
| 134 |
+
Args:
|
| 135 |
+
file_path: Local path to the audio file.
|
| 136 |
+
|
| 137 |
+
Returns:
|
| 138 |
+
The transcribed text.
|
| 139 |
+
"""
|
| 140 |
+
client = InferenceClient(token=os.environ["HF_TOKEN"])
|
| 141 |
+
result = client.automatic_speech_recognition(
|
| 142 |
+
file_path,
|
| 143 |
+
model="openai/whisper-large-v3",
|
| 144 |
+
)
|
| 145 |
+
return result.text
|
| 146 |
#pdf_tool = load_tool(
|
| 147 |
#"matterattetatte/pdf-upload-extractor-tool",
|
| 148 |
# trust_remote_code = True
|
|
|
|
| 159 |
#read_pdf,
|
| 160 |
read_spreadsheet,
|
| 161 |
get_youtube_transcript,
|
| 162 |
+
analyze_image,
|
| 163 |
+
transcribe_audio,
|
| 164 |
],
|
| 165 |
model= InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct") ,
|
| 166 |
+
additional_authorized_imports=["pandas", "requests", "re"],
|
| 167 |
instructions = ("You are an advanced CodeAgent that will show your capabilities to work in the real world by being tested in GAIA, the agent testing platform. If the question includes a task_id and mentions a file, call fetch_task_file first; route YouTube URLs to get_youtube_transcript, other URLs to visit_webpage, PDFs to pdf_tool, and spreadsheets to read_spreadsheet, using web_search only when no URL or file is given,then respond with only the exact final answer value, no explanation, no prefix."
|
| 168 |
+
"Use the Thought Action observation to produce high quality results and only answer when you are sure you have performed the necessary steps for the task and question"
|
| 169 |
+
"If the file is an image, use analyze_image with a specific question about "
|
| 170 |
+
"what to find. If the file is audio, use transcribe_audio first, then reason over the transcribed text "),
|
| 171 |
max_steps=20,
|
| 172 |
)
|
| 173 |
#answering questions
|