harryab commited on
Commit
2a158a3
·
verified ·
1 Parent(s): 4a33edc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -40
app.py CHANGED
@@ -8,51 +8,42 @@ from bs4 import BeautifulSoup # for web scraping
8
 
9
  from Gradio_UI import GradioUI
10
 
11
-
12
- @tool
13
- def fetch_marathon_plan(goal_time: str) -> str:
14
- """Fetches a marathon training plan based on a goal time.
15
-
16
- Args:
17
- goal_time: Desired marathon completion time (e.g., '4:00', '3:30').
18
-
19
- Returns:
20
- A link to a training plan or summary of key details.
21
- """
22
- search_tool = DuckDuckGoSearchTool()
23
- query = f"marathon training plan {goal_time} site:runnersworld.com"
24
- results = search_tool.search(query)
25
-
26
- if results:
27
- return f"Here is a recommended training plan for {goal_time}: {results[0]['url']}"
28
- else:
29
- return f"Could not find a specific plan for {goal_time}, but you can check Runner's World for more details."
30
-
31
  # Fetch marathons in Europe for 2025
32
  @tool
33
  def fetch_european_marathons_2025() -> str:
34
  """Fetches a list of marathons in Europe in 2025 from MarathonRunnersDiary."""
35
- url = "http://www.marathonrunnersdiary.com/races/europe-marathons-list.php"
36
- response = requests.get(url)
37
-
38
- if response.status_code != 200:
39
- return "Failed to fetch marathon data."
40
-
41
- soup = BeautifulSoup(response.text, 'html.parser')
 
 
 
 
 
 
 
 
42
 
43
- # Locate the marathon table (this is based on the structure of the website)
44
- marathons = []
45
- table = soup.find('table', {'class': 'racedatatable'}) # Adjust the class if necessary
 
 
 
 
 
 
 
46
 
47
- if table:
48
- rows = table.find_all('tr')[1:] # Skip the header row
49
- for row in rows:
50
- columns = row.find_all('td')
51
- if len(columns) >= 4:
52
- marathon_name = columns[0].get_text(strip=True)
53
- marathon_date = columns[1].get_text(strip=True)
54
- marathon_location = columns[2].get_text(strip=True)
55
- marathons.append(f"{marathon_name} on {marathon_date} in {marathon_location}")
56
 
57
  if marathons:
58
  return "\n".join(marathons)
@@ -73,7 +64,7 @@ with open("prompts.yaml", 'r') as stream:
73
 
74
  agent = CodeAgent(
75
  model=model,
76
- tools=[final_answer, fetch_marathon_plan, fetch_european_marathons_2025],
77
  max_steps=6,
78
  verbosity_level=1,
79
  grammar=None,
 
8
 
9
  from Gradio_UI import GradioUI
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # Fetch marathons in Europe for 2025
12
  @tool
13
  def fetch_european_marathons_2025() -> str:
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
+ # Extract name
28
+ name_tag = marathon.find(itemprop="name")
29
+ name = name_tag.text.strip() if name_tag else "N/A"
30
 
31
+ # Extract date
32
+ date_meta = marathon.find("meta", itemprop="startDate")
33
+ date = date_meta["content"] if date_meta else marathon.find("div", class_="leftdate").text.strip()
34
+
35
+ # Extract city and country
36
+ city_tag = marathon.find("div", class_="leftcity")
37
+ city = city_tag.text.strip() if city_tag else "N/A"
38
+
39
+ country_tag = marathon.find("div", class_="leftregion")
40
+ country = country_tag.text.strip() if country_tag else "N/A"
41
 
42
+ # Extract URL
43
+ link_tag = marathon.find("a")
44
+ link = link_tag["href"] if link_tag else "N/A"
45
+
46
+ marathon_list.append([name, date, city, country, link])
 
 
 
 
47
 
48
  if marathons:
49
  return "\n".join(marathons)
 
64
 
65
  agent = CodeAgent(
66
  model=model,
67
+ tools=[final_answer, fetch_european_marathons_2025],
68
  max_steps=6,
69
  verbosity_level=1,
70
  grammar=None,