Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,39 @@
|
|
| 1 |
from smolagents import CodeAgent, HfApiModel, tool
|
| 2 |
import requests
|
| 3 |
-
from requests.exceptions import RequestException #
|
| 4 |
from tools.final_answer import FinalAnswerTool
|
| 5 |
from Gradio_UI import GradioUI
|
| 6 |
import markdownify
|
| 7 |
import re
|
|
|
|
| 8 |
|
| 9 |
@tool
|
| 10 |
-
def
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@tool
|
| 23 |
def visit_webpage(url: str) -> str:
|
| 24 |
"""Visits a webpage at the given URL and returns its content as a markdown string.
|
|
|
|
| 25 |
Args:
|
| 26 |
url: The URL of the webpage to visit.
|
|
|
|
| 27 |
Returns:
|
| 28 |
The content of the webpage converted to Markdown, or an error message if the request fails.
|
| 29 |
"""
|
| 30 |
try:
|
| 31 |
-
# Send a GET request to the URL
|
| 32 |
response = requests.get(url)
|
| 33 |
response.raise_for_status() # Raise an exception for bad status codes
|
| 34 |
|
|
@@ -40,7 +45,7 @@ def visit_webpage(url: str) -> str:
|
|
| 40 |
|
| 41 |
return markdown_content
|
| 42 |
|
| 43 |
-
except RequestException as e:
|
| 44 |
return f"Error fetching the webpage: {str(e)}"
|
| 45 |
except Exception as e:
|
| 46 |
return f"An unexpected error occurred: {str(e)}"
|
|
@@ -50,7 +55,7 @@ final_answer = FinalAnswerTool()
|
|
| 50 |
|
| 51 |
# Initialize the model
|
| 52 |
model = HfApiModel(
|
| 53 |
-
max_tokens=1500, #
|
| 54 |
temperature=0.5,
|
| 55 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 56 |
custom_role_conversions=None,
|
|
@@ -59,7 +64,7 @@ model = HfApiModel(
|
|
| 59 |
# Create the agent
|
| 60 |
agent = CodeAgent(
|
| 61 |
model=model,
|
| 62 |
-
tools=[
|
| 63 |
max_steps=6,
|
| 64 |
verbosity_level=1,
|
| 65 |
grammar=None,
|
|
|
|
| 1 |
from smolagents import CodeAgent, HfApiModel, tool
|
| 2 |
import requests
|
| 3 |
+
from requests.exceptions import RequestException # Import request exception handling
|
| 4 |
from tools.final_answer import FinalAnswerTool
|
| 5 |
from Gradio_UI import GradioUI
|
| 6 |
import markdownify
|
| 7 |
import re
|
| 8 |
+
from smolagents.tools.web import web_search as web_search_tool # Import web search from SmolAgents
|
| 9 |
|
| 10 |
@tool
|
| 11 |
+
def web_search(query: str) -> str:
|
| 12 |
+
"""Performs a web search and returns the top search results.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
query: The search query string.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
A formatted string with the top search results, or an error message if the request fails.
|
| 19 |
+
"""
|
| 20 |
+
try:
|
| 21 |
+
results = web_search_tool(query)
|
| 22 |
+
return results if results else "No relevant results found."
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Error performing web search: {str(e)}"
|
| 25 |
|
| 26 |
@tool
|
| 27 |
def visit_webpage(url: str) -> str:
|
| 28 |
"""Visits a webpage at the given URL and returns its content as a markdown string.
|
| 29 |
+
|
| 30 |
Args:
|
| 31 |
url: The URL of the webpage to visit.
|
| 32 |
+
|
| 33 |
Returns:
|
| 34 |
The content of the webpage converted to Markdown, or an error message if the request fails.
|
| 35 |
"""
|
| 36 |
try:
|
|
|
|
| 37 |
response = requests.get(url)
|
| 38 |
response.raise_for_status() # Raise an exception for bad status codes
|
| 39 |
|
|
|
|
| 45 |
|
| 46 |
return markdown_content
|
| 47 |
|
| 48 |
+
except RequestException as e:
|
| 49 |
return f"Error fetching the webpage: {str(e)}"
|
| 50 |
except Exception as e:
|
| 51 |
return f"An unexpected error occurred: {str(e)}"
|
|
|
|
| 55 |
|
| 56 |
# Initialize the model
|
| 57 |
model = HfApiModel(
|
| 58 |
+
max_tokens=1500, # Adjusted to avoid token limit errors
|
| 59 |
temperature=0.5,
|
| 60 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 61 |
custom_role_conversions=None,
|
|
|
|
| 64 |
# Create the agent
|
| 65 |
agent = CodeAgent(
|
| 66 |
model=model,
|
| 67 |
+
tools=[web_search, visit_webpage, final_answer], # Added web_search tool
|
| 68 |
max_steps=6,
|
| 69 |
verbosity_level=1,
|
| 70 |
grammar=None,
|