Update app.py
Browse files
app.py
CHANGED
|
@@ -2,58 +2,90 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# --- Constants ---
|
| 8 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
from smolagents import (
|
| 11 |
-
CodeAgent,
|
| 12 |
-
DuckDuckGoSearchTool,
|
| 13 |
-
VisitWebpageTool,
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
"""Initialize and return the Hugging Face model."""
|
| 20 |
-
|
| 21 |
-
# Initialize model with specific configuration
|
| 22 |
-
return OpenAIServerModel("gpt-4.1-mini", max_tokens=8096, api_key=os.getenv("OPENAI_API_KEY"))
|
| 23 |
-
|
| 24 |
-
class MultiAgent:
|
| 25 |
-
def __init__(self):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Create the web agent
|
| 29 |
-
web_agent = ToolCallingAgent(
|
| 30 |
-
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
| 31 |
-
model=get_model(),
|
| 32 |
-
max_steps=10,
|
| 33 |
-
name="web_agent",
|
| 34 |
-
description="Browses the web to find information, it can also visit webpages.",
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
# Create the code agent
|
| 38 |
-
self.agent = CodeAgent(
|
| 39 |
-
tools=[],
|
| 40 |
-
managed_agents=[web_agent],
|
| 41 |
-
model=get_model(),
|
| 42 |
-
max_steps=20,
|
| 43 |
-
additional_authorized_imports=["os", "shutil", "PyPDF2", "docx", "bs4", "requests", "numpy", "pandas"],
|
| 44 |
-
)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 59 |
"""
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
import yaml
|
| 6 |
+
import importlib
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from smolagents import (
|
| 9 |
+
CodeAgent,
|
| 10 |
+
DuckDuckGoSearchTool,
|
| 11 |
+
VisitWebpageTool,
|
| 12 |
+
WikipediaSearchTool,
|
| 13 |
+
Tool,
|
| 14 |
+
OpenAIServerModel,
|
| 15 |
+
SpeechToTextTool,
|
| 16 |
)
|
| 17 |
|
| 18 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
class GetTaskFileTool(Tool):
|
| 21 |
+
name = "get_task_file_tool"
|
| 22 |
+
description = """This tool downloads the file content associated with the given task_id if exists. Returns absolute file path"""
|
| 23 |
+
inputs = {
|
| 24 |
+
"task_id": {"type": "string", "description": "Task id"},
|
| 25 |
+
"file_name": {"type": "string", "description": "File name"},
|
| 26 |
+
}
|
| 27 |
+
output_type = "string"
|
| 28 |
+
|
| 29 |
+
def forward(self, task_id: str, file_name: str) -> str:
|
| 30 |
+
response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=15)
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
with open(file_name, 'wb') as file:
|
| 33 |
+
file.write(response.content)
|
| 34 |
+
return os.path.abspath(file_name)
|
| 35 |
|
| 36 |
+
class LoadXlsxFileTool(Tool):
|
| 37 |
+
name = "load_xlsx_file_tool"
|
| 38 |
+
description = """This tool loads xlsx file into pandas and returns it"""
|
| 39 |
+
inputs = {
|
| 40 |
+
"file_path": {"type": "string", "description": "File path"}
|
| 41 |
+
}
|
| 42 |
+
output_type = "object"
|
| 43 |
+
|
| 44 |
+
def forward(self, file_path: str) -> object:
|
| 45 |
+
return pd.read_excel(file_path)
|
| 46 |
+
|
| 47 |
+
class LoadTextFileTool(Tool):
|
| 48 |
+
name = "load_text_file_tool"
|
| 49 |
+
description = """This tool loads any text file"""
|
| 50 |
+
inputs = {
|
| 51 |
+
"file_path": {"type": "string", "description": "File path"}
|
| 52 |
+
}
|
| 53 |
+
output_type = "string"
|
| 54 |
+
|
| 55 |
+
def forward(self, file_path: str) -> object:
|
| 56 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 57 |
+
return file.read()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
prompts = yaml.safe_load(
|
| 61 |
+
importlib.resources.files("smolagents.prompts").joinpath("code_agent.yaml").read_text()
|
| 62 |
+
)
|
| 63 |
+
prompts["system_prompt"] = ("You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be 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. "
|
| 64 |
+
+ prompts["system_prompt"])
|
| 65 |
+
|
| 66 |
+
def init_agent():
|
| 67 |
+
gemini_model = OpenAIServerModel(
|
| 68 |
+
model_id="gpt-4.1-mini",
|
| 69 |
+
api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 70 |
+
api_key=os.getenv("OPENAI_API_KEY"),
|
| 71 |
+
temperature=0.7
|
| 72 |
+
)
|
| 73 |
+
agent = CodeAgent(
|
| 74 |
+
tools=[
|
| 75 |
+
DuckDuckGoSearchTool(),
|
| 76 |
+
VisitWebpageTool(),
|
| 77 |
+
WikipediaSearchTool(),
|
| 78 |
+
GetTaskFileTool(),
|
| 79 |
+
SpeechToTextTool(),
|
| 80 |
+
LoadXlsxFileTool(),
|
| 81 |
+
LoadTextFileTool()
|
| 82 |
+
],
|
| 83 |
+
model=gemini_model,
|
| 84 |
+
prompt_templates=prompts,
|
| 85 |
+
max_steps=15,
|
| 86 |
+
additional_authorized_imports = ["pandas"]
|
| 87 |
+
)
|
| 88 |
+
return agent
|
| 89 |
|
| 90 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 91 |
"""
|