Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,55 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
-
import datetime
|
| 3 |
import requests
|
| 4 |
-
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
from bs4 import BeautifulSoup # for web scraping
|
| 8 |
-
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
# Fetch marathons in Europe for 2025
|
| 12 |
@tool
|
| 13 |
-
def
|
| 14 |
"""Fetches a list of marathons in Europe in 2025 from MarathonRunnersDiary."""
|
|
|
|
| 15 |
# Fetch the webpage
|
| 16 |
-
URL = "http://www.marathonrunnersdiary.com/races/europe-marathons-list.php"
|
| 17 |
-
page = requests.get(URL)
|
| 18 |
-
soup = BeautifulSoup(page.content, "html.parser")
|
| 19 |
|
| 20 |
-
# Find the section containing marathon events
|
| 21 |
-
results = soup.find(id="holder")
|
| 22 |
-
marathons = results.find_all(itemtype="http://schema.org/Event")
|
| 23 |
|
| 24 |
-
# Extract relevant details
|
| 25 |
-
marathon_list = []
|
| 26 |
-
for marathon in marathons:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
else:
|
| 51 |
return "No marathons found for 2025."
|
| 52 |
|
|
|
|
| 53 |
final_answer = FinalAnswerTool()
|
| 54 |
|
| 55 |
model = HfApiModel(
|
|
@@ -64,7 +64,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 64 |
|
| 65 |
agent = CodeAgent(
|
| 66 |
model=model,
|
| 67 |
-
tools=[final_answer,
|
| 68 |
max_steps=6,
|
| 69 |
verbosity_level=1,
|
| 70 |
grammar=None,
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
|
|
|
| 2 |
import requests
|
|
|
|
| 3 |
import yaml
|
|
|
|
| 4 |
from bs4 import BeautifulSoup # for web scraping
|
| 5 |
+
from tools.final_answer import FinalAnswerTool
|
| 6 |
from Gradio_UI import GradioUI
|
| 7 |
|
| 8 |
# Fetch marathons in Europe for 2025
|
| 9 |
@tool
|
| 10 |
+
def european_marathons_2025() -> str:
|
| 11 |
"""Fetches a list of marathons in Europe in 2025 from MarathonRunnersDiary."""
|
| 12 |
+
|
| 13 |
# Fetch the webpage
|
| 14 |
+
URL = "http://www.marathonrunnersdiary.com/races/europe-marathons-list.php"
|
| 15 |
+
page = requests.get(URL)
|
| 16 |
+
soup = BeautifulSoup(page.content, "html.parser")
|
| 17 |
|
| 18 |
+
# Find the section containing marathon events
|
| 19 |
+
results = soup.find(id="holder")
|
| 20 |
+
marathons = results.find_all(itemtype="http://schema.org/Event")
|
| 21 |
|
| 22 |
+
# Extract relevant details
|
| 23 |
+
marathon_list = []
|
| 24 |
+
for marathon in marathons:
|
| 25 |
+
# Extract name
|
| 26 |
+
name_tag = marathon.find(itemprop="name")
|
| 27 |
+
name = name_tag.text.strip() if name_tag else "N/A"
|
| 28 |
+
|
| 29 |
+
# Extract date
|
| 30 |
+
date_meta = marathon.find("meta", itemprop="startDate")
|
| 31 |
+
date = date_meta["content"] if date_meta else marathon.find("div", class_="leftdate").text.strip()
|
| 32 |
|
| 33 |
+
# Extract city and country
|
| 34 |
+
city_tag = marathon.find("div", class_="leftcity")
|
| 35 |
+
city = city_tag.text.strip() if city_tag else "N/A"
|
| 36 |
|
| 37 |
+
country_tag = marathon.find("div", class_="leftregion")
|
| 38 |
+
country = country_tag.text.strip() if country_tag else "N/A"
|
| 39 |
+
|
| 40 |
+
# Extract URL
|
| 41 |
+
link_tag = marathon.find("a")
|
| 42 |
+
link = link_tag["href"] if link_tag else "N/A"
|
| 43 |
|
| 44 |
+
marathon_list.append(f"{name} - {date} - {city}, {country} ({link})")
|
| 45 |
+
|
| 46 |
+
# Return results as a formatted string
|
| 47 |
+
if marathon_list:
|
| 48 |
+
return "\n".join(marathon_list)
|
| 49 |
else:
|
| 50 |
return "No marathons found for 2025."
|
| 51 |
|
| 52 |
+
# Initialize the final answer tool
|
| 53 |
final_answer = FinalAnswerTool()
|
| 54 |
|
| 55 |
model = HfApiModel(
|
|
|
|
| 64 |
|
| 65 |
agent = CodeAgent(
|
| 66 |
model=model,
|
| 67 |
+
tools=[final_answer, european_marathons_2025],
|
| 68 |
max_steps=6,
|
| 69 |
verbosity_level=1,
|
| 70 |
grammar=None,
|