Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -6,61 +6,80 @@ import yaml
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
-
#
|
|
|
|
| 10 |
@tool
|
| 11 |
-
def
|
| 12 |
-
"""
|
| 13 |
Args:
|
| 14 |
-
|
| 15 |
"""
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
-
return f"Error searching
|
| 23 |
|
| 24 |
-
# Custom tool for generating Ethiopian food images
|
| 25 |
@tool
|
| 26 |
-
def
|
| 27 |
-
"""
|
| 28 |
Args:
|
| 29 |
-
|
| 30 |
-
style: Style of image - "authentic", "restaurant", "home-cooked"
|
| 31 |
"""
|
| 32 |
try:
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
-
return f"Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
final_answer = FinalAnswerTool()
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Model configuration
|
| 41 |
model = HfApiModel(
|
| 42 |
max_tokens=2096,
|
| 43 |
temperature=0.5,
|
| 44 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 45 |
custom_role_conversions=None,
|
| 46 |
)
|
| 47 |
|
| 48 |
-
# Load prompt templates
|
| 49 |
with open("prompts.yaml", 'r') as stream:
|
| 50 |
prompt_templates = yaml.safe_load(stream)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
agent = CodeAgent(
|
| 54 |
model=model,
|
| 55 |
-
tools=[
|
| 56 |
max_steps=6,
|
| 57 |
verbosity_level=1,
|
| 58 |
grammar=None,
|
| 59 |
planning_interval=None,
|
| 60 |
-
name=
|
| 61 |
-
description=
|
| 62 |
prompt_templates=prompt_templates
|
| 63 |
)
|
| 64 |
|
| 65 |
-
# Launch the Gradio UI
|
| 66 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# Define custom tools
|
| 10 |
+
|
| 11 |
@tool
|
| 12 |
+
def search_images(query: str) -> str:
|
| 13 |
+
"""A tool that searches for images using DuckDuckGo and returns URLs of the top 3 results.
|
| 14 |
Args:
|
| 15 |
+
query: The search query for images (e.g., 'Ethiopian injera food').
|
| 16 |
"""
|
| 17 |
try:
|
| 18 |
+
from duckduckgo_search import DDGS
|
| 19 |
+
with DDGS() as ddgs:
|
| 20 |
+
results = ddgs.images(query, max_results=3)
|
| 21 |
+
image_urls = [result['image'] for result in results]
|
| 22 |
+
return "\n".join(image_urls)
|
| 23 |
except Exception as e:
|
| 24 |
+
return f"Error searching images: {str(e)}"
|
| 25 |
|
|
|
|
| 26 |
@tool
|
| 27 |
+
def visit_webpage(url: str) -> str:
|
| 28 |
+
"""A tool that visits a webpage and returns its content in markdown format.
|
| 29 |
Args:
|
| 30 |
+
url: The URL of the webpage to visit.
|
|
|
|
| 31 |
"""
|
| 32 |
try:
|
| 33 |
+
response = requests.get(url)
|
| 34 |
+
response.raise_for_status()
|
| 35 |
+
from markdownify import markdownify as md
|
| 36 |
+
return md(response.text)
|
| 37 |
except Exception as e:
|
| 38 |
+
return f"Error visiting webpage: {str(e)}"
|
| 39 |
+
|
| 40 |
+
@tool
|
| 41 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 42 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 43 |
+
Args:
|
| 44 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 45 |
+
"""
|
| 46 |
+
try:
|
| 47 |
+
tz = pytz.timezone(timezone)
|
| 48 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 49 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 52 |
|
| 53 |
final_answer = FinalAnswerTool()
|
| 54 |
|
| 55 |
+
# Load image generation tool from Hub
|
| 56 |
+
image_generator = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 57 |
+
|
| 58 |
+
# Instantiate web search tool
|
| 59 |
+
web_search = DuckDuckGoSearchTool()
|
| 60 |
+
|
| 61 |
# Model configuration
|
| 62 |
model = HfApiModel(
|
| 63 |
max_tokens=2096,
|
| 64 |
temperature=0.5,
|
| 65 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Use another endpoint if overloaded
|
| 66 |
custom_role_conversions=None,
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
with open("prompts.yaml", 'r') as stream:
|
| 70 |
prompt_templates = yaml.safe_load(stream)
|
| 71 |
|
| 72 |
+
# Initialize the agent with all tools
|
| 73 |
agent = CodeAgent(
|
| 74 |
model=model,
|
| 75 |
+
tools=[web_search, search_images, visit_webpage, image_generator, final_answer],
|
| 76 |
max_steps=6,
|
| 77 |
verbosity_level=1,
|
| 78 |
grammar=None,
|
| 79 |
planning_interval=None,
|
| 80 |
+
name=None,
|
| 81 |
+
description=None,
|
| 82 |
prompt_templates=prompt_templates
|
| 83 |
)
|
| 84 |
|
|
|
|
| 85 |
GradioUI(agent).launch()
|