pkduongsu commited on
Commit
27f0d16
·
verified ·
1 Parent(s): ae7a494

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +75 -68
  2. get_pl_data.py +231 -0
app.py CHANGED
@@ -1,69 +1,76 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
- import datetime
3
- import requests
4
- import pytz
5
- import yaml
6
- 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:
23
- """A tool that fetches the current local time in a specified timezone.
24
- Args:
25
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
- """
27
- try:
28
- # Create timezone object
29
- tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
- return f"The current local time in {timezone} is: {local_time}"
33
- except Exception as e:
34
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
-
36
-
37
- final_answer = FinalAnswerTool()
38
-
39
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
-
42
- model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
- )
48
-
49
-
50
- # Import tool from Hub
51
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
-
53
- with open("prompts.yaml", 'r') as stream:
54
- prompt_templates = yaml.safe_load(stream)
55
-
56
- agent = CodeAgent(
57
- model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
- max_steps=6,
60
- verbosity_level=1,
61
- grammar=None,
62
- planning_interval=None,
63
- name=None,
64
- description=None,
65
- prompt_templates=prompt_templates
66
- )
67
-
68
-
 
 
 
 
 
 
 
69
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ import datetime
3
+ import requests
4
+ import pytz
5
+ import yaml
6
+ from tools.final_answer import FinalAnswerTool
7
+ from tools.visit_webpage import VisitWebpageTool
8
+ from tools.get_pl_data import get_pl_standings, get_matches_by_date, get_head_to_head, get_all_teams, get_all_players_by_team, get_player_details_by_id, get_home_lineup, get_away_lineup, get_top_goalscorers, get_top_assisters, get_top_rated_players
9
+ import os
10
+ from Gradio_UI import GradioUI
11
+
12
+ # Below is an example of a tool that does nothing. Amaze us with your creativity !
13
+
14
+ @tool
15
+ def web_search(query: str) -> str:
16
+ """A tool that performs a duckduckgo web search based on your query (think a Google search) then returns the top search results.
17
+ Args:
18
+ query: The search query to perform.
19
+ """
20
+ try:
21
+ # Perform the search
22
+ search_results = DuckDuckGoSearchTool().forward(query)
23
+ return search_results
24
+ except Exception as e:
25
+ return f"Error performing search for query '{query}': {str(e)}"
26
+
27
+ @tool
28
+ def get_current_time_in_timezone(timezone: str) -> str:
29
+ """A tool that fetches the current local time in a specified timezone.
30
+ Args:
31
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
32
+ """
33
+ try:
34
+ # Create timezone object
35
+ tz = pytz.timezone(timezone)
36
+ # Get current time in that timezone
37
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
38
+ return f"The current local time in {timezone} is: {local_time}"
39
+ except Exception as e:
40
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
41
+
42
+
43
+ final_answer = FinalAnswerTool()
44
+ visit_webpage = VisitWebpageTool()
45
+
46
+ # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
47
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
48
+
49
+ model = HfApiModel(
50
+ max_tokens=2096,
51
+ temperature=0.5,
52
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
53
+ custom_role_conversions=None,
54
+ )
55
+
56
+
57
+ # Import tool from Hub
58
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
59
+
60
+ with open("prompts.yaml", 'r') as stream:
61
+ prompt_templates = yaml.safe_load(stream)
62
+
63
+ agent = CodeAgent(
64
+ model=model,
65
+ tools=[get_current_time_in_timezone, web_search, image_generation_tool, visit_webpage,get_pl_standings, get_matches_by_date, get_head_to_head, get_all_teams, get_all_players_by_team, get_player_details_by_id, get_home_lineup, get_away_lineup, get_top_goalscorers, get_top_assisters, get_top_rated_players, final_answer], ## add your tools here (don't remove final answer)
66
+ max_steps=6,
67
+ verbosity_level=1,
68
+ grammar=None,
69
+ planning_interval=None,
70
+ name=None,
71
+ description=None,
72
+ prompt_templates=prompt_templates
73
+ )
74
+
75
+
76
  GradioUI(agent).launch()
get_pl_data.py ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import tool
2
+ import requests
3
+ import os
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+
8
+ headers = {
9
+ "x-rapidapi-key": os.getenv("RAPID_API_KEY"),
10
+ "x-rapidapi-host": "free-api-live-football-data.p.rapidapi.com"
11
+ }
12
+
13
+ @tool
14
+ def get_pl_standings() -> dict: # return the current Premier League standings
15
+ """A tool that returns the current Premier League standings.
16
+ Args:
17
+ None
18
+ Returns:
19
+ The current Premier League standings in JSON.
20
+ """
21
+
22
+ url = "https://free-api-live-football-data.p.rapidapi.com/football-get-standing-all"
23
+
24
+ querystring = {"leagueid":"47"}
25
+
26
+ try:
27
+ response = requests.get(url, headers=headers, params=querystring)
28
+ response.raise_for_status()
29
+ return response.json()
30
+ except Exception as e:
31
+ return f"Error fetching Premier League standings: {str(e)}"
32
+
33
+ @tool
34
+ def get_matches_by_date(date:str) -> dict: # return all matches in the current Premier League season by date
35
+ """A tool that returns all matches in the current Premier League season by date.
36
+ Args:
37
+ date: The date in the format 'YYYYMMDD'.
38
+ Returns:
39
+ All matches in the current Premier League season by date in JSON.
40
+ """
41
+
42
+ url = "https://free-api-live-football-data.p.rapidapi.com/football-get-matches-by-date"
43
+
44
+ querystring = {"date": date}
45
+
46
+ try:
47
+ response = requests.get(url, headers=headers, params=querystring)
48
+ response.raise_for_status()
49
+ return response.json()
50
+ except Exception as e:
51
+ return f"Error fetching Premier League standings: {str(e)}"
52
+
53
+ @tool
54
+ def get_head_to_head(eventid: str) -> dict: #return head to head records by the event (match) id
55
+ """A tool that returns the head to head records by the event (match) id.
56
+ Args:
57
+ eventid: The event (match) id.
58
+ Returns:
59
+ The head to head records in JSON.
60
+ """
61
+
62
+ url = "https://free-api-live-football-data.p.rapidapi.com/football-get-head-to-head"
63
+
64
+ querystring = {"eventid": eventid}
65
+
66
+ try:
67
+ response = requests.get(url, headers=headers, params=querystring)
68
+ response.raise_for_status()
69
+ return response.json()
70
+ except Exception as e:
71
+ return f"Error fetching Premier League standings: {str(e)}"
72
+
73
+ @tool
74
+ def get_all_teams() -> dict:
75
+ """A tool that returns all teams in the Premier League.
76
+ Args:
77
+ None
78
+ Returns:
79
+ All teams in the Premier League in JSON.
80
+ """
81
+
82
+ url = "https://free-api-live-football-data.p.rapidapi.com/football-get-list-all-team"
83
+
84
+ querystring = {"leagueid":"47"}
85
+
86
+ try:
87
+ response = requests.get(url, headers=headers, params=querystring)
88
+ response.raise_for_status()
89
+ return response.json()
90
+ except Exception as e:
91
+ return f"Error fetching Premier League standings: {str(e)}"
92
+
93
+ @tool
94
+ def get_all_players_by_team(teamid: str) -> dict:
95
+ """A tool that returns all players by team.
96
+ Args:
97
+ teamid: The team id.
98
+ Returns:
99
+ All players by team in JSON.
100
+ """
101
+
102
+ url = "hhttps://free-api-live-football-data.p.rapidapi.com/football-get-list-player"
103
+
104
+ querystring = {"teamid": teamid}
105
+
106
+ try:
107
+ response = requests.get(url, headers=headers, params=querystring)
108
+ response.raise_for_status()
109
+ return response.json()
110
+ except Exception as e:
111
+ return f"Error fetching Premier League standings: {str(e)}"
112
+
113
+ @tool
114
+ def get_player_details_by_id(playerid: str) -> dict:
115
+ """A tool that returns player details by player id.
116
+ Args:
117
+ playerid: The player id.
118
+ Returns:
119
+ Player details in JSON.
120
+ """
121
+
122
+ url = "https://free-api-live-football-data.p.rapidapi.com/football-get-player-detail"
123
+
124
+ querystring = {"playerid": playerid}
125
+
126
+ try:
127
+ response = requests.get(url, headers=headers, params=querystring)
128
+ response.raise_for_status()
129
+ return response.json()
130
+ except Exception as e:
131
+ return f"Error fetching Premier League standings: {str(e)}"
132
+
133
+ @tool
134
+ def get_home_lineup(eventid: str) -> dict:
135
+ """A tool that returns the home team lineup by event (match) id.
136
+ Args:
137
+ eventid: The event (match) id.
138
+ Returns:
139
+ The home team lineup in JSON.
140
+ """
141
+
142
+ url = "https://free-api-live-football-data.p.rapidapi.com/football-get-hometeam-lineup"
143
+
144
+ querystring = {"eventid": eventid}
145
+
146
+ try:
147
+ response = requests.get(url, headers=headers, params=querystring)
148
+ response.raise_for_status()
149
+ return response.json()
150
+ except Exception as e:
151
+ return f"Error fetching Premier League standings: {str(e)}"
152
+
153
+ @tool
154
+ def get_away_lineup(eventid: str) -> dict:
155
+ """A tool that returns the away team lineup by event (match) id.
156
+ Args:
157
+ eventid: The event (match) id.
158
+ Returns:
159
+ The away team lineup in JSON.
160
+ """
161
+
162
+ url = "https://free-api-live-football-data.p.rapidapi.com/football-get-awayteam-lineup"
163
+
164
+ querystring = {"eventid": eventid}
165
+
166
+ try:
167
+ response = requests.get(url, headers=headers, params=querystring)
168
+ response.raise_for_status()
169
+ return response.json()
170
+ except Exception as e:
171
+ return f"Error fetching Premier League standings: {str(e)}"
172
+
173
+ @tool
174
+ def get_top_goalscorers() -> dict:
175
+ """A tool that returns the top goalscorers in the Premier League.
176
+ Args:
177
+ None
178
+ Returns:
179
+ The top goalscorers in JSON.
180
+ """
181
+
182
+ url = "https://free-api-live-football-data.p.rapidapi.com/football-get-top-players-by-goals"
183
+
184
+ querystring = {"leagueid":"47"}
185
+
186
+ try:
187
+ response = requests.get(url, headers=headers, params=querystring)
188
+ response.raise_for_status()
189
+ return response.json()
190
+ except Exception as e:
191
+ return f"Error fetching Premier League standings: {str(e)}"
192
+
193
+ @tool
194
+ def get_top_assisters() -> dict:
195
+ """A tool that returns the top assisters in the Premier League.
196
+ Args:
197
+ None
198
+ Returns:
199
+ The top assisters in JSON.
200
+ """
201
+
202
+ url = "https://free-api-live-football-data.p.rapidapi.com/football-get-top-players-by-assists"
203
+
204
+ querystring = {"leagueid":"47"}
205
+
206
+ try:
207
+ response = requests.get(url, headers=headers, params=querystring)
208
+ response.raise_for_status()
209
+ return response.json()
210
+ except Exception as e:
211
+ return f"Error fetching Premier League standings: {str(e)}"
212
+
213
+ @tool
214
+ def get_top_rated_players() -> dict:
215
+ """A tool that returns the top rated players in the Premier League.
216
+ Args:
217
+ None
218
+ Returns:
219
+ The top rated players in JSON.
220
+ """
221
+
222
+ url = "https://free-api-live-football-data.p.rapidapi.com/football-get-top-players-by-rating"
223
+
224
+ querystring = {"leagueid":"47"}
225
+
226
+ try:
227
+ response = requests.get(url, headers=headers, params=querystring)
228
+ response.raise_for_status()
229
+ return response.json()
230
+ except Exception as e:
231
+ return f"Error fetching Premier League standings: {str(e)}"