SmolNews / app.py
jairo's picture
Modify prompts.yaml and app.py to focus on news and time information
3a2c59e
import datetime
import pytz
import yaml
from smolagents import CodeAgent, HfApiModel, tool
from Gradio_UI import GradioUI
from tools.final_answer import FinalAnswerTool
from tools.visit_webpage import VisitWebpageTool
@tool
def get_news_from_place(place: str) -> list[str]:
"""Fetches the an rss feed for a given place from google news in xml format and returns the titles of the news.
Args:
place: The place to search news for (e.g., 'London').
"""
url = f"https://news.google.com/rss/search?q={place}&hl=en-US&gl=US&ceid=US:en"
visit_webpage_tool = VisitWebpageTool()
titles = visit_webpage_tool.forward(url=url)
top_news = titles[:5]
formatted_news = [f"• {title}" for title in top_news]
return formatted_news
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""A tool that fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
"""
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current local time in {timezone} is: {local_time}"
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.666,
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
custom_role_conversions=None,
)
with open("prompts.yaml", "r") as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[final_answer, get_current_time_in_timezone, get_news_from_place],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates,
additional_authorized_imports=["pytz"],
)
GradioUI(agent).launch()