harryab's picture
Update app.py
3a5cef6 verified
raw
history blame
2.83 kB
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from bs4 import BeautifulSoup # for web scraping
from Gradio_UI import GradioUI
@tool
def fetch_marathon_plan(goal_time: str) -> str:
"""Fetches a marathon training plan based on a goal time.
Args:
goal_time: Desired marathon completion time (e.g., '4:00', '3:30').
Returns:
A link to a training plan or summary of key details.
"""
search_tool = DuckDuckGoSearchTool()
query = f"marathon training plan {goal_time} site:runnersworld.com"
results = search_tool.search(query)
if results:
return f"Here is a recommended training plan for {goal_time}: {results[0]['url']}"
else:
return f"Could not find a specific plan for {goal_time}, but you can check Runner's World for more details."
# Fetch marathons in Europe for 2025
@tool
def fetch_european_marathons_2025() -> str:
"""Fetches a list of marathons in Europe in 2025 from MarathonRunnersDiary."""
url = "http://www.marathonrunnersdiary.com/races/europe-marathons-list.php"
response = requests.get(url)
if response.status_code != 200:
return "Failed to fetch marathon data."
soup = BeautifulSoup(response.text, 'html.parser')
# Locate the marathon table (this is based on the structure of the website)
marathons = []
table = soup.find('table', {'class': 'racedatatable'}) # Adjust the class if necessary
if table:
rows = table.find_all('tr')[1:] # Skip the header row
for row in rows:
columns = row.find_all('td')
if len(columns) >= 4:
marathon_name = columns[0].get_text(strip=True)
marathon_date = columns[1].get_text(strip=True)
marathon_location = columns[2].get_text(strip=True)
marathons.append(f"{marathon_name} on {marathon_date} in {marathon_location}")
if marathons:
return "\n".join(marathons)
else:
return "No marathons found for 2025."
final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
#model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[final_answer, fetch_marathon_plan, fetch_european_marathons_2025],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch()