abtsousa commited on
Commit
194856c
·
unverified ·
1 Parent(s): ae7a494

Added the built-in tools and a Primeira Liga scores tool.

Browse files
Files changed (1) hide show
  1. app.py +109 -2
app.py CHANGED
@@ -4,6 +4,9 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
 
 
7
 
8
  from Gradio_UI import GradioUI
9
 
@@ -33,8 +36,112 @@ def get_current_time_in_timezone(timezone: str) -> str:
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'
@@ -55,7 +162,7 @@ with open("prompts.yaml", 'r') as 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,
@@ -66,4 +173,4 @@ agent = CodeAgent(
66
  )
67
 
68
 
69
- GradioUI(agent).launch()
 
4
  import pytz
5
  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
12
 
 
36
  except Exception as e:
37
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
38
 
39
+ @tool
40
+ def curiosity(input: str) -> str:
41
+ """This tool is designed to spark curiosity in the user. It takes a string as input and returns a message that encourages the user to explore further.
42
+ Args:
43
+ input: A string input from the user.
44
+ """
45
+ return "Wow, amazing!"
46
+
47
+ @tool
48
+ def get_primeira_liga_team_stats(team_name: str) -> Optional[Dict[str, Any]]:
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 None
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 {
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 None
139
+
140
+
141
 
142
  final_answer = FinalAnswerTool()
143
+ visit_webpage = VisitWebpageTool()
144
+ web_search = DuckDuckGoSearchTool()
145
 
146
  # 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:
147
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
162
 
163
  agent = CodeAgent(
164
  model=model,
165
+ tools=[final_answer, get_current_time_in_timezone, curiosity, get_primeira_liga_team_stats, visit_webpage, web_search, image_generation_tool], ## add your tools here (don't remove final answer)
166
  max_steps=6,
167
  verbosity_level=1,
168
  grammar=None,
 
173
  )
174
 
175
 
176
+ GradioUI(agent).launch()