Spaces:
Sleeping
Sleeping
web search test
Browse files- app.py +6 -3
- app_agents/manager_agent.py +56 -0
- app_agents/web_agent.py +13 -0
- {tools → app_tools}/tools.py +27 -1
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from huggingface_hub import login
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
import agent
|
|
|
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
|
@@ -135,14 +136,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 135 |
def test_init_agent_for_chat(text_input, history):
|
| 136 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 137 |
try:
|
| 138 |
-
|
|
|
|
| 139 |
except Exception as e:
|
| 140 |
print(f"Error instantiating agent: {e}")
|
| 141 |
return f"Error initializing agent: {e}", None
|
| 142 |
|
| 143 |
-
|
|
|
|
| 144 |
|
| 145 |
-
return submitted_answer
|
| 146 |
|
| 147 |
# --- Build Gradio Interface using Blocks ---
|
| 148 |
with gr.Blocks() as demo:
|
|
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
import agent
|
| 10 |
+
import app_agents
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
|
|
|
| 136 |
def test_init_agent_for_chat(text_input, history):
|
| 137 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 138 |
try:
|
| 139 |
+
managerAgent = agents.manager_agent
|
| 140 |
+
# basicAgent = agent.BasicAgent()
|
| 141 |
except Exception as e:
|
| 142 |
print(f"Error instantiating agent: {e}")
|
| 143 |
return f"Error initializing agent: {e}", None
|
| 144 |
|
| 145 |
+
manager_agent.visualize()
|
| 146 |
+
# submitted_answer = basicAgent(text_input)
|
| 147 |
|
| 148 |
+
# return submitted_answer
|
| 149 |
|
| 150 |
# --- Build Gradio Interface using Blocks ---
|
| 151 |
with gr.Blocks() as demo:
|
app_agents/manager_agent.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from smolagents.utils import encode_image_base64, make_image_url
|
| 3 |
+
from smolagents import OpenAIServerModel
|
| 4 |
+
|
| 5 |
+
import app_agents.web_agent as web_agent
|
| 6 |
+
|
| 7 |
+
def check_reasoning_and_plot(final_answer, agent_memory):
|
| 8 |
+
multimodal_model = OpenAIServerModel("gpt-4o", max_tokens=8096)
|
| 9 |
+
filepath = "saved_map.png"
|
| 10 |
+
assert os.path.exists(filepath), "Make sure to save the plot under saved_map.png!"
|
| 11 |
+
image = Image.open(filepath)
|
| 12 |
+
prompt = (
|
| 13 |
+
f"Here is a user-given task and the agent steps: {agent_memory.get_succinct_steps()}. Now here is the plot that was made."
|
| 14 |
+
"Please check that the reasoning process and plot are correct: do they correctly answer the given task?"
|
| 15 |
+
"First list reasons why yes/no, then write your final decision: PASS in caps lock if it is satisfactory, FAIL if it is not."
|
| 16 |
+
"Don't be harsh: if the plot mostly solves the task, it should pass."
|
| 17 |
+
"To pass, a plot should be made using px.scatter_map and not any other method (scatter_map looks nicer)."
|
| 18 |
+
)
|
| 19 |
+
messages = [
|
| 20 |
+
{
|
| 21 |
+
"role": "user",
|
| 22 |
+
"content": [
|
| 23 |
+
{
|
| 24 |
+
"type": "text",
|
| 25 |
+
"text": prompt,
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"type": "image_url",
|
| 29 |
+
"image_url": {"url": make_image_url(encode_image_base64(image))},
|
| 30 |
+
},
|
| 31 |
+
],
|
| 32 |
+
}
|
| 33 |
+
]
|
| 34 |
+
output = multimodal_model(messages).content
|
| 35 |
+
print("Feedback: ", output)
|
| 36 |
+
if "FAIL" in output:
|
| 37 |
+
raise Exception(output)
|
| 38 |
+
return True
|
| 39 |
+
|
| 40 |
+
manager_agent = CodeAgent(
|
| 41 |
+
model=InferenceClientModel("deepseek-ai/DeepSeek-R1", provider="together", max_tokens=8096),
|
| 42 |
+
tools=[calculate_cargo_travel_time],
|
| 43 |
+
managed_agents=[web_agent.agent],
|
| 44 |
+
additional_authorized_imports=[
|
| 45 |
+
"geopandas",
|
| 46 |
+
"plotly",
|
| 47 |
+
"shapely",
|
| 48 |
+
"json",
|
| 49 |
+
"pandas",
|
| 50 |
+
"numpy",
|
| 51 |
+
],
|
| 52 |
+
planning_interval=5,
|
| 53 |
+
verbosity_level=2,
|
| 54 |
+
final_answer_checks=[check_reasoning_and_plot],
|
| 55 |
+
max_steps=15,
|
| 56 |
+
)
|
app_agents/web_agent.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, VisitWebpageTool
|
| 2 |
+
|
| 3 |
+
model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", provider="together")
|
| 4 |
+
|
| 5 |
+
agent = CodeAgent(
|
| 6 |
+
model=model,
|
| 7 |
+
tools=[GoogleSearchTool(), VisitWebpageTool()],
|
| 8 |
+
additional_authorized_imports=["pandas"],
|
| 9 |
+
name="web_agent",
|
| 10 |
+
description="Browses the web to find information",
|
| 11 |
+
verbosity_level=0,
|
| 12 |
+
max_steps=20,
|
| 13 |
+
)
|
{tools → app_tools}/tools.py
RENAMED
|
@@ -60,4 +60,30 @@ def calculate_cargo_travel_time(
|
|
| 60 |
return round(flight_time, 2)
|
| 61 |
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
return round(flight_time, 2)
|
| 61 |
|
| 62 |
|
| 63 |
+
@tool
|
| 64 |
+
def visit_webpage(url: str) -> str:
|
| 65 |
+
"""Visits a webpage at the given URL and returns its content as a markdown string.
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
url: The URL of the webpage to visit.
|
| 69 |
+
|
| 70 |
+
Returns:
|
| 71 |
+
The content of the webpage converted to Markdown, or an error message if the request fails.
|
| 72 |
+
"""
|
| 73 |
+
try:
|
| 74 |
+
# Send a GET request to the URL
|
| 75 |
+
response = requests.get(url)
|
| 76 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
| 77 |
+
|
| 78 |
+
# Convert the HTML content to Markdown
|
| 79 |
+
markdown_content = markdownify(response.text).strip()
|
| 80 |
+
|
| 81 |
+
# Remove multiple line breaks
|
| 82 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
| 83 |
+
|
| 84 |
+
return markdown_content
|
| 85 |
+
|
| 86 |
+
except RequestException as e:
|
| 87 |
+
return f"Error fetching the webpage: {str(e)}"
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"An unexpected error occurred: {str(e)}"
|