Spaces:
Sleeping
Sleeping
abtsousa
commited on
Refactored the primeira liga tool.
Browse files- app.py +2 -95
- tools/primeira_liga.py +94 -0
app.py
CHANGED
@@ -6,6 +6,7 @@ import yaml
|
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
from tools.visit_webpage import VisitWebpageTool
|
8 |
from tools.web_search import DuckDuckGoSearchTool
|
|
|
9 |
from typing import Any, Dict, Optional
|
10 |
|
11 |
from Gradio_UI import GradioUI
|
@@ -44,101 +45,7 @@ def curiosity(input: str) -> str:
|
|
44 |
"""
|
45 |
return "Wow, amazing!"
|
46 |
|
47 |
-
|
48 |
-
def get_primeira_liga_team_stats(team_name: str) -> str:
|
49 |
-
"""
|
50 |
-
Retrieve standing and last 3 match scores for a given Primeira Liga team using TheSportsDB API.
|
51 |
-
|
52 |
-
Args:
|
53 |
-
team_name (str): Name of the team to search for
|
54 |
-
|
55 |
-
Returns:
|
56 |
-
Dict containing team statistics or None if team not found
|
57 |
-
"""
|
58 |
-
|
59 |
-
def _determine_match_outcome(home_team: str, away_team: str,
|
60 |
-
home_score: int, away_score: int,
|
61 |
-
target_team: str) -> str:
|
62 |
-
"""
|
63 |
-
Determine match outcome (W/D/L) from the perspective of the target team.
|
64 |
-
"""
|
65 |
-
|
66 |
-
if home_team == target_team:
|
67 |
-
if home_score > away_score:
|
68 |
-
return 'W'
|
69 |
-
elif home_score < away_score:
|
70 |
-
return 'L'
|
71 |
-
else:
|
72 |
-
return 'D'
|
73 |
-
else:
|
74 |
-
if away_score > home_score:
|
75 |
-
return 'W'
|
76 |
-
elif away_score < home_score:
|
77 |
-
return 'L'
|
78 |
-
else:
|
79 |
-
return 'D'
|
80 |
-
|
81 |
-
# API Base URL
|
82 |
-
BASE_URL = "https://www.thesportsdb.com/api/v1/json/3"
|
83 |
-
|
84 |
-
# Hardcoded Primeira Liga League ID
|
85 |
-
PRIMEIRA_LIGA_ID = 4344
|
86 |
-
|
87 |
-
try:
|
88 |
-
# Step 1: Search for the team to get its ID
|
89 |
-
team_search_url = f"{BASE_URL}/searchteams.php?t={team_name.replace(' ', '%20')}"
|
90 |
-
team_search_response = requests.get(team_search_url)
|
91 |
-
team_search_data = team_search_response.json()
|
92 |
-
|
93 |
-
if not team_search_data.get('teams'):
|
94 |
-
return "Error: Team not found"
|
95 |
-
|
96 |
-
# Get the first matching team's ID
|
97 |
-
team_id = team_search_data['teams'][0]['idTeam']
|
98 |
-
|
99 |
-
# Step 2: Get league table
|
100 |
-
table_url = f"{BASE_URL}/lookuptable.php?l={PRIMEIRA_LIGA_ID}"
|
101 |
-
table_response = requests.get(table_url)
|
102 |
-
table_data = table_response.json()
|
103 |
-
|
104 |
-
# Find team's standing
|
105 |
-
standing = next(
|
106 |
-
(int(entry['intRank']) for entry in table_data.get('table', [])
|
107 |
-
if entry['idTeam'] == team_id),
|
108 |
-
None
|
109 |
-
)
|
110 |
-
|
111 |
-
# Step 3: Get last 5 events for the team
|
112 |
-
last_events_url = f"{BASE_URL}/eventslast.php?id={team_id}"
|
113 |
-
last_events_response = requests.get(last_events_url)
|
114 |
-
last_events_data = last_events_response.json()
|
115 |
-
|
116 |
-
# Process last 3 matches
|
117 |
-
last_3_matches = []
|
118 |
-
for event in last_events_data.get('results', [])[:3]:
|
119 |
-
match = {
|
120 |
-
'opponent': event['strAwayTeam'] if event['strHomeTeam'] == team_name else event['strHomeTeam'],
|
121 |
-
'result': f"{event['intHomeScore']} - {event['intAwayScore']}",
|
122 |
-
'outcome': _determine_match_outcome(
|
123 |
-
event['strHomeTeam'], event['strAwayTeam'],
|
124 |
-
event['intHomeScore'], event['intAwayScore'],
|
125 |
-
team_name
|
126 |
-
)
|
127 |
-
}
|
128 |
-
last_3_matches.append(match)
|
129 |
-
|
130 |
-
return str({
|
131 |
-
"team": team_name,
|
132 |
-
"standing": standing,
|
133 |
-
"last_3_matches": last_3_matches
|
134 |
-
})
|
135 |
-
|
136 |
-
except Exception as e:
|
137 |
-
print(f"Error retrieving team stats: {e}")
|
138 |
-
return str("Error retrieving team stats")
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
final_answer = FinalAnswerTool()
|
143 |
visit_webpage = VisitWebpageTool()
|
144 |
web_search = DuckDuckGoSearchTool()
|
|
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
from tools.visit_webpage import VisitWebpageTool
|
8 |
from tools.web_search import DuckDuckGoSearchTool
|
9 |
+
from tools.primeira_liga import PrimeiraLigaTeamStatsTool
|
10 |
from typing import Any, Dict, Optional
|
11 |
|
12 |
from Gradio_UI import GradioUI
|
|
|
45 |
"""
|
46 |
return "Wow, amazing!"
|
47 |
|
48 |
+
get_primeira_liga_team_stats = PrimeiraLigaTeamStatsTool()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
final_answer = FinalAnswerTool()
|
50 |
visit_webpage = VisitWebpageTool()
|
51 |
web_search = DuckDuckGoSearchTool()
|
tools/primeira_liga.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents.tools import Tool
|
2 |
+
import requests
|
3 |
+
|
4 |
+
class PrimeiraLigaTeamStatsTool(Tool):
|
5 |
+
name = "get_primeira_liga_team_stats"
|
6 |
+
description = "Retrieve standing and last 3 match scores for a given Primeira Liga team."
|
7 |
+
inputs = {'team_name': {'type': 'string', 'description': 'Name of the team to search for'}}
|
8 |
+
output_type = "string"
|
9 |
+
|
10 |
+
def forward(self, team_name: str) -> str:
|
11 |
+
def _determine_match_outcome(home_team: str, away_team: str,
|
12 |
+
home_score: int, away_score: int,
|
13 |
+
target_team: str) -> str:
|
14 |
+
"""
|
15 |
+
Determine match outcome (W/D/L) from the perspective of the target team.
|
16 |
+
"""
|
17 |
+
|
18 |
+
if home_team == target_team:
|
19 |
+
if home_score > away_score:
|
20 |
+
return 'W'
|
21 |
+
elif home_score < away_score:
|
22 |
+
return 'L'
|
23 |
+
else:
|
24 |
+
return 'D'
|
25 |
+
else:
|
26 |
+
if away_score > home_score:
|
27 |
+
return 'W'
|
28 |
+
elif away_score < home_score:
|
29 |
+
return 'L'
|
30 |
+
else:
|
31 |
+
return 'D'
|
32 |
+
|
33 |
+
# API Base URL
|
34 |
+
BASE_URL = "https://www.thesportsdb.com/api/v1/json/3"
|
35 |
+
|
36 |
+
# Hardcoded Primeira Liga League ID
|
37 |
+
PRIMEIRA_LIGA_ID = 4344
|
38 |
+
|
39 |
+
try:
|
40 |
+
# Step 1: Search for the team to get its ID
|
41 |
+
team_search_url = f"{BASE_URL}/searchteams.php?t={team_name.replace(' ', '%20')}"
|
42 |
+
team_search_response = requests.get(team_search_url)
|
43 |
+
team_search_data = team_search_response.json()
|
44 |
+
|
45 |
+
if not team_search_data.get('teams'):
|
46 |
+
return "Error: Team not found"
|
47 |
+
|
48 |
+
# Get the first matching team's ID
|
49 |
+
team_id = team_search_data['teams'][0]['idTeam']
|
50 |
+
|
51 |
+
# Step 2: Get league table
|
52 |
+
table_url = f"{BASE_URL}/lookuptable.php?l={PRIMEIRA_LIGA_ID}"
|
53 |
+
table_response = requests.get(table_url)
|
54 |
+
table_data = table_response.json()
|
55 |
+
|
56 |
+
# Find team's standing
|
57 |
+
standing = next(
|
58 |
+
(int(entry['intRank']) for entry in table_data.get('table', [])
|
59 |
+
if entry['idTeam'] == team_id),
|
60 |
+
None
|
61 |
+
)
|
62 |
+
|
63 |
+
# Step 3: Get last 5 events for the team
|
64 |
+
last_events_url = f"{BASE_URL}/eventslast.php?id={team_id}"
|
65 |
+
last_events_response = requests.get(last_events_url)
|
66 |
+
last_events_data = last_events_response.json()
|
67 |
+
|
68 |
+
# Process last 3 matches
|
69 |
+
last_3_matches = []
|
70 |
+
for event in last_events_data.get('results', [])[:3]:
|
71 |
+
match = {
|
72 |
+
'opponent': event['strAwayTeam'] if event['strHomeTeam'] == team_name else event['strHomeTeam'],
|
73 |
+
'result': f"{event['intHomeScore']} - {event['intAwayScore']}",
|
74 |
+
'outcome': _determine_match_outcome(
|
75 |
+
event['strHomeTeam'], event['strAwayTeam'],
|
76 |
+
event['intHomeScore'], event['intAwayScore'],
|
77 |
+
team_name
|
78 |
+
)
|
79 |
+
}
|
80 |
+
last_3_matches.append(match)
|
81 |
+
|
82 |
+
return str({
|
83 |
+
"team": team_name,
|
84 |
+
"standing": standing,
|
85 |
+
"last_3_matches": last_3_matches
|
86 |
+
})
|
87 |
+
|
88 |
+
except Exception as e:
|
89 |
+
print(f"Error retrieving team stats: {e}")
|
90 |
+
return str("Error retrieving team stats")
|
91 |
+
|
92 |
+
def __init__(self, *args, **kwargs):
|
93 |
+
self.is_initialized = False
|
94 |
+
|