File size: 2,489 Bytes
423352a
4a33edc
c19d193
3a5cef6
423352a
9b5b26a
 
e758f44
fd1cfb7
423352a
e758f44
423352a
2a158a3
423352a
 
 
2a158a3
423352a
 
 
2a158a3
423352a
 
 
 
 
 
 
 
 
 
2a158a3
423352a
 
 
2a158a3
423352a
 
 
 
 
 
2a158a3
423352a
 
 
 
 
fd1cfb7
e758f44
fd1cfb7
423352a
8707d1e
 
e121372
ae96930
 
32d0520
 
13d500a
8c01ffb
861422e
 
ae96930
8c01ffb
8fe992b
423352a
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
8c01ffb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from smolagents import CodeAgent, HfApiModel, load_tool, tool
import requests
import yaml
from bs4 import BeautifulSoup  # for web scraping
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI

# Fetch marathons in Europe for 2025
@tool
def european_marathons_2025() -> str:
    """Fetches a list of marathons in Europe in 2025 from MarathonRunnersDiary."""
    
    # Fetch the webpage
    URL = "http://www.marathonrunnersdiary.com/races/europe-marathons-list.php"
    page = requests.get(URL)
    soup = BeautifulSoup(page.content, "html.parser")

    # Find the section containing marathon events
    results = soup.find(id="holder")
    marathons = results.find_all(itemtype="http://schema.org/Event")

    # Extract relevant details
    marathon_list = []
    for marathon in marathons:
        # Extract name
        name_tag = marathon.find(itemprop="name")
        name = name_tag.text.strip() if name_tag else "N/A"
        
        # Extract date
        date_meta = marathon.find("meta", itemprop="startDate")
        date = date_meta["content"] if date_meta else marathon.find("div", class_="leftdate").text.strip()

        # Extract city and country
        city_tag = marathon.find("div", class_="leftcity")
        city = city_tag.text.strip() if city_tag else "N/A"

        country_tag = marathon.find("div", class_="leftregion")
        country = country_tag.text.strip() if country_tag else "N/A"
        
        # Extract URL
        link_tag = marathon.find("a")
        link = link_tag["href"] if link_tag else "N/A"

        marathon_list.append(f"{name} - {date} - {city}, {country} ({link})")

    # Return results as a formatted string
    if marathon_list:
        return "\n".join(marathon_list)
    else:
        return "No marathons found for 2025."

# Initialize the final answer tool
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, 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()