Spaces:
Build error
Build error
Updated input file fetching and tool handing for images, audio and python code
Browse files- app.py +93 -41
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
|
|
|
| 4 |
import inspect
|
| 5 |
import yaml
|
| 6 |
import pandas as pd
|
|
|
|
| 7 |
from smolagents import (
|
| 8 |
OpenAIServerModel,
|
| 9 |
ToolCallingAgent,
|
|
@@ -12,7 +15,9 @@ from smolagents import (
|
|
| 12 |
DuckDuckGoSearchTool,
|
| 13 |
WebSearchTool,
|
| 14 |
VisitWebpageTool,
|
| 15 |
-
SpeechToTextTool
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
from dotenv import load_dotenv
|
| 18 |
# Load environment variables from .env file
|
|
@@ -44,20 +49,29 @@ class BasicAgent:
|
|
| 44 |
description="This agent can search the web and visit webpages to gather information.",
|
| 45 |
)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
manager_agent = CodeAgent(
|
| 57 |
tools=[],
|
| 58 |
model=model,
|
| 59 |
-
|
| 60 |
-
managed_agents=[web_agent],
|
| 61 |
additional_authorized_imports=["time", "numpy", "pandas"],
|
| 62 |
)
|
| 63 |
|
|
@@ -65,11 +79,27 @@ class BasicAgent:
|
|
| 65 |
print(f"Agent initialized with model ID: {os.environ['MODEL_ID']}")
|
| 66 |
print(f"Agent initialized with tools: {self.agent.tools}")
|
| 67 |
|
| 68 |
-
def __call__(self, question: str) -> str:
|
| 69 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 70 |
system_prompt = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer as a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. Here is the question: "
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
if answer:
|
| 74 |
print(f"Agent returning answer: {answer}")
|
| 75 |
return answer
|
|
@@ -137,39 +167,61 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 137 |
for item in questions_data:
|
| 138 |
task_id = item.get("task_id")
|
| 139 |
question_text = item.get("question")
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
if not task_id or question_text is None:
|
| 169 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 170 |
continue
|
| 171 |
try:
|
| 172 |
-
submitted_answer = agent(question_text)
|
| 173 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 174 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 175 |
except Exception as e:
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from PIL import Image
|
| 6 |
import inspect
|
| 7 |
import yaml
|
| 8 |
import pandas as pd
|
| 9 |
+
|
| 10 |
from smolagents import (
|
| 11 |
OpenAIServerModel,
|
| 12 |
ToolCallingAgent,
|
|
|
|
| 15 |
DuckDuckGoSearchTool,
|
| 16 |
WebSearchTool,
|
| 17 |
VisitWebpageTool,
|
| 18 |
+
SpeechToTextTool,
|
| 19 |
+
AgentAudio,
|
| 20 |
+
PythonInterpreterTool,
|
| 21 |
)
|
| 22 |
from dotenv import load_dotenv
|
| 23 |
# Load environment variables from .env file
|
|
|
|
| 49 |
description="This agent can search the web and visit webpages to gather information.",
|
| 50 |
)
|
| 51 |
|
| 52 |
+
python_agent = ToolCallingAgent(
|
| 53 |
+
verbosity_level=1,
|
| 54 |
+
tools=[PythonInterpreterTool()],
|
| 55 |
+
max_steps=5,
|
| 56 |
+
model=model,
|
| 57 |
+
name="python_agent",
|
| 58 |
+
description="This agent can run Python code snippets.",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
stt_agent = ToolCallingAgent(
|
| 62 |
+
verbosity_level=1,
|
| 63 |
+
tools=[SpeechToTextTool()],
|
| 64 |
+
max_steps=5,
|
| 65 |
+
model=model,
|
| 66 |
+
name="speech_to_text_agent",
|
| 67 |
+
description="This agent can transcribe audio files to text.",
|
| 68 |
+
)
|
| 69 |
|
| 70 |
manager_agent = CodeAgent(
|
| 71 |
tools=[],
|
| 72 |
model=model,
|
| 73 |
+
managed_agents=[web_agent, stt_agent, python_agent],
|
| 74 |
+
# managed_agents=[web_agent],
|
| 75 |
additional_authorized_imports=["time", "numpy", "pandas"],
|
| 76 |
)
|
| 77 |
|
|
|
|
| 79 |
print(f"Agent initialized with model ID: {os.environ['MODEL_ID']}")
|
| 80 |
print(f"Agent initialized with tools: {self.agent.tools}")
|
| 81 |
|
| 82 |
+
def __call__(self, question: str, file_name: str, file_type: str) -> str:
|
| 83 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 84 |
system_prompt = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer as a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. Here is the question: "
|
| 85 |
|
| 86 |
+
if file_type == "image":
|
| 87 |
+
# If the file is an image, read file_name and convert it to a PIL Image
|
| 88 |
+
image = Image.open(file_name)
|
| 89 |
+
image = image.convert("RGB")
|
| 90 |
+
# Convert the image to bytes
|
| 91 |
+
image_bytes = BytesIO()
|
| 92 |
+
answer = self.agent.run(system_prompt + question, images=[image_bytes])
|
| 93 |
+
elif file_type == "audio":
|
| 94 |
+
arguments = {"audio": file_name}
|
| 95 |
+
answer = self.agent.run(system_prompt + question, arguments=arguments)
|
| 96 |
+
elif file_type == "python":
|
| 97 |
+
with open(file_name, "r") as file:
|
| 98 |
+
python_code = file.read()
|
| 99 |
+
answer = self.agent.run(system_prompt + question, code=python_code)
|
| 100 |
+
else:
|
| 101 |
+
answer = self.agent.run(system_prompt + question)
|
| 102 |
+
|
| 103 |
if answer:
|
| 104 |
print(f"Agent returning answer: {answer}")
|
| 105 |
return answer
|
|
|
|
| 167 |
for item in questions_data:
|
| 168 |
task_id = item.get("task_id")
|
| 169 |
question_text = item.get("question")
|
| 170 |
+
file_name = item.get("file_name")
|
| 171 |
+
file_type = "unknown"
|
| 172 |
+
if file_name:
|
| 173 |
+
print(f"Fetching file content for task ID: {task_id}")
|
| 174 |
+
try:
|
| 175 |
+
file_url = f"{api_url}/files/{task_id}"
|
| 176 |
+
file_response = requests.get(file_url, timeout=15)
|
| 177 |
+
file_response.raise_for_status()
|
| 178 |
+
|
| 179 |
+
# parse the file extension for the file name to see if it is an image, audio, or python file
|
| 180 |
+
file_extension = os.path.splitext(file_name)[1].lower()
|
| 181 |
+
if file_extension in ['.jpg', '.jpeg', '.png', '.gif']:
|
| 182 |
+
# If the file is an image, convert it to a PIL Image
|
| 183 |
+
file_type = "image"
|
| 184 |
+
|
| 185 |
+
question_text = f"Here is an image: {file_name}. Please describe it."
|
| 186 |
+
# Save the image to a local file
|
| 187 |
+
with open(file_name, "wb") as image_file:
|
| 188 |
+
image_file.write(file_response.content)
|
| 189 |
+
print(f"Saved image file: {file_name}")
|
| 190 |
+
|
| 191 |
+
elif file_extension in ['.wav', '.mp3', '.ogg']:
|
| 192 |
+
# If the file is an audio file, convert it to text
|
| 193 |
+
file_type = "audio"
|
| 194 |
+
audio_data = file_response.content
|
| 195 |
+
question_text = f"Here is an audio file: {file_name}. Please transcribe it."
|
| 196 |
+
|
| 197 |
+
# Save the audio to a local file
|
| 198 |
+
with open(file_name, "wb") as audio_file:
|
| 199 |
+
audio_file.write(file_response.content)
|
| 200 |
+
print(f"Saved audio file: {file_name}")
|
| 201 |
+
|
| 202 |
+
elif file_extension in ['.py']:
|
| 203 |
+
# If the file is a Python file, you might want to run it or analyze it
|
| 204 |
+
file_type = "python"
|
| 205 |
+
question_text = f"Here is a Python file: {file_name}. Please analyze it."
|
| 206 |
+
|
| 207 |
+
# Save the Python file to a local file
|
| 208 |
+
with open(file_name, "wb") as python_file:
|
| 209 |
+
python_file.write(file_response.content)
|
| 210 |
+
print(f"Saved Python file: {file_name}")
|
| 211 |
+
|
| 212 |
+
except requests.exceptions.HTTPError as e:
|
| 213 |
+
print(f"Error fetching file for task ID {task_id}: {e}")
|
| 214 |
+
continue
|
| 215 |
+
except requests.exceptions.RequestException as e:
|
| 216 |
+
print(f"Error fetching file for task ID {task_id}: {e}")
|
| 217 |
+
continue
|
| 218 |
+
|
| 219 |
|
| 220 |
if not task_id or question_text is None:
|
| 221 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 222 |
continue
|
| 223 |
try:
|
| 224 |
+
submitted_answer = agent(question_text, file_name, file_type)
|
| 225 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 226 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 227 |
except Exception as e:
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
smolagents
|
| 4 |
-
smolagents[openai]
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
smolagents
|
| 4 |
+
smolagents[openai]
|
| 5 |
+
smolagents[audio]
|
| 6 |
+
transformers
|