rs-liu commited on
Commit
ebbd1dc
·
verified ·
1 Parent(s): 9d1afda

Fetch pl team match statics

Browse files
Files changed (1) hide show
  1. app.py +113 -8
app.py CHANGED
@@ -7,16 +7,121 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
 
 
 
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
 
 
 
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,7 +160,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer, image_generation_tool], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
7
 
8
  from Gradio_UI import GradioUI
9
 
 
10
  @tool
11
+ def get_pl_team_stats(pl_team_name: str = "Liverpool", season: str = "2023-24") -> dict:
12
+ """Fetches PL team latest Premier League statistics including:
13
+ - Average points
14
+ - Home/Away performance
15
+ - Performance against different table tiers
16
+
17
  Args:
18
+ pl_team_name: The Premier League football club's team name
19
+ season: The Premier League season to analyze (format: YYYY-YY)
20
+
21
+ Returns:
22
+ Dictionary containing comprehensive PL team statistics
23
  """
24
+ try:
25
+ # First fetch current standings to determine team tiers
26
+ standings_url = "https://fantasy.premierleague.com/api/bootstrap-static/"
27
+ response = requests.get(standings_url)
28
+ response.raise_for_status()
29
+ data = response.json()
30
+
31
+ # Get team IDs and positions
32
+ teams = {team['id']: team['name'] for team in data['teams']}
33
+ standings = {team['name']: team['position'] for team in data['teams']}
34
+
35
+ # Determine table tiers
36
+ top6 = [name for name, pos in standings.items() if pos <= 6]
37
+ middle = [name for name, pos in standings.items() if 7 <= pos <= 14]
38
+ bottom6 = [name for name, pos in standings.items() if pos >= 15]
39
+
40
+ # Now fetch team's fixtures
41
+ team_id = [id for id, name in teams.items() if "pl_team_name" in name][0]
42
+ fixtures_url = f"https://fantasy.premierleague.com/api/team/{team_id}/fixtures/"
43
+ response = requests.get(fixtures_url)
44
+ response.raise_for_status()
45
+ fixtures = response.json()
46
+
47
+ # Filter for completed matches in specified season
48
+ completed_matches = [
49
+ f for f in fixtures
50
+ if f['finished'] and f['season_name'] == season
51
+ ]
52
+
53
+ # Initialize stats
54
+ stats = {
55
+ 'total_matches': len(completed_matches),
56
+ 'total_points': 0,
57
+ 'home': {'matches': 0, 'points': 0},
58
+ 'away': {'matches': 0, 'points': 0},
59
+ 'vs_top6': {'matches': 0, 'points': 0},
60
+ 'vs_middle': {'matches': 0, 'points': 0},
61
+ 'vs_bottom6': {'matches': 0, 'points': 0}
62
+ }
63
+
64
+ # Process each match
65
+ for match in completed_matches:
66
+ # Calculate points (3 for win, 1 for draw)
67
+ if match['team_h_score'] > match['team_a_score']:
68
+ points = 3 if match['team_h'] == team_id else 0
69
+ elif match['team_h_score'] == match['team_a_score']:
70
+ points = 1
71
+ else:
72
+ points = 3 if match['team_a'] == team_id else 0
73
+
74
+ stats['total_points'] += points
75
+
76
+ # Home/away stats
77
+ if match['team_h'] == team_id:
78
+ stats['home']['matches'] += 1
79
+ stats['home']['points'] += points
80
+ opponent = teams[match['team_a']]
81
+ else:
82
+ stats['away']['matches'] += 1
83
+ stats['away']['points'] += points
84
+ opponent = teams[match['team_h']]
85
+
86
+ # Opposition tier stats
87
+ if opponent in top6:
88
+ stats['vs_top6']['matches'] += 1
89
+ stats['vs_top6']['points'] += points
90
+ elif opponent in middle:
91
+ stats['vs_middle']['matches'] += 1
92
+ stats['vs_middle']['points'] += points
93
+ elif opponent in bottom6:
94
+ stats['vs_bottom6']['matches'] += 1
95
+ stats['vs_bottom6']['points'] += points
96
+
97
+ # Calculate averages
98
+ def safe_divide(a, b):
99
+ return round(a/b, 2) if b > 0 else 0.0
100
+
101
+ stats['avg_points'] = safe_divide(stats['total_points'], stats['total_matches'])
102
+ stats['home']['avg_points'] = safe_divide(stats['home']['points'], stats['home']['matches'])
103
+ stats['away']['avg_points'] = safe_divide(stats['away']['points'], stats['away']['matches'])
104
+ stats['vs_top6']['avg_points'] = safe_divide(stats['vs_top6']['points'], stats['vs_top6']['matches'])
105
+ stats['vs_middle']['avg_points'] = safe_divide(stats['vs_middle']['points'], stats['vs_middle']['matches'])
106
+ stats['vs_bottom6']['avg_points'] = safe_divide(stats['vs_bottom6']['points'], stats['vs_bottom6']['matches'])
107
+
108
+ return stats
109
+
110
+ except Exception as e:
111
+ return {"error": f"Failed to fetch team stats: {str(e)}"}
112
+
113
+ # # Add the tool to your agent
114
+ # agent = CodeAgent(
115
+ # model=model,
116
+ # tools=[final_answer, image_generation_tool, get_pl_team_stats], # Added our new tool
117
+ # max_steps=6,
118
+ # verbosity_level=1,
119
+ # grammar=None,
120
+ # planning_interval=None,
121
+ # name=None,
122
+ # description=None,
123
+ # prompt_templates=prompt_templates
124
+ # )
125
 
126
  @tool
127
  def get_current_time_in_timezone(timezone: str) -> str:
 
160
 
161
  agent = CodeAgent(
162
  model=model,
163
+ tools=[final_answer, image_generation_tool, get_pl_team_stats], ## add your tools here (don't remove final answer)
164
  max_steps=6,
165
  verbosity_level=1,
166
  grammar=None,