Spaces:
Running
Running
Custom Tool to fetch football matches
Browse files
app.py
CHANGED
|
@@ -1,22 +1,45 @@
|
|
| 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
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -25,9 +48,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 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:
|
|
@@ -36,26 +57,26 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following
|
| 40 |
-
#
|
|
|
|
| 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
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
@@ -65,5 +86,4 @@ agent = CodeAgent(
|
|
| 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 Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
def get_latest_football_matches(competition: str = "PL") -> str:
|
| 13 |
+
"""A tool that fetches the latest football match results from the Football-Data.org API.
|
|
|
|
| 14 |
Args:
|
| 15 |
+
competition: The competition code to fetch matches for (e.g., 'PL' for Premier League,
|
| 16 |
+
'CL' for Champions League, 'BL1' for Bundesliga, 'SA' for Serie A,
|
| 17 |
+
'PD' for La Liga, 'FL1' for Ligue 1). Defaults to 'PL'.
|
| 18 |
"""
|
| 19 |
+
try:
|
| 20 |
+
url = f"https://api.football-data.org/v4/competitions/{competition}/matches?status=FINISHED"
|
| 21 |
+
headers = {"X-Auth-Token": "6b7b4c2b0b1d4996a8238ae546b3f049"}
|
| 22 |
+
response = requests.get(url, headers=headers)
|
| 23 |
+
|
| 24 |
+
if response.status_code == 200:
|
| 25 |
+
data = response.json()
|
| 26 |
+
matches = data.get("matches", [])[-5:] # Get last 5 matches
|
| 27 |
+
if not matches:
|
| 28 |
+
return "No recent matches found."
|
| 29 |
+
result = f"Latest {competition} matches:\n"
|
| 30 |
+
for match in matches:
|
| 31 |
+
home = match["homeTeam"]["name"]
|
| 32 |
+
away = match["awayTeam"]["name"]
|
| 33 |
+
home_score = match["score"]["fullTime"]["home"]
|
| 34 |
+
away_score = match["score"]["fullTime"]["away"]
|
| 35 |
+
date = match["utcDate"][:10]
|
| 36 |
+
result += f" {date}: {home} {home_score} - {away_score} {away}\n"
|
| 37 |
+
return result
|
| 38 |
+
else:
|
| 39 |
+
return f"Error fetching matches: {response.status_code} - {response.text}"
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Error fetching football matches: {str(e)}"
|
| 42 |
+
|
| 43 |
|
| 44 |
@tool
|
| 45 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 48 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 49 |
"""
|
| 50 |
try:
|
|
|
|
| 51 |
tz = pytz.timezone(timezone)
|
|
|
|
| 52 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 53 |
return f"The current local time in {timezone} is: {local_time}"
|
| 54 |
except Exception as e:
|
|
|
|
| 57 |
|
| 58 |
final_answer = FinalAnswerTool()
|
| 59 |
|
| 60 |
+
# If the agent does not answer, the model is overloaded, please use another model or the following
|
| 61 |
+
# Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 62 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 63 |
|
| 64 |
model = HfApiModel(
|
| 65 |
+
max_tokens=2096,
|
| 66 |
+
temperature=0.5,
|
| 67 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
| 68 |
+
custom_role_conversions=None,
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# Load image generation tool from Hub
|
|
|
|
| 72 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 73 |
|
| 74 |
with open("prompts.yaml", 'r') as stream:
|
| 75 |
prompt_templates = yaml.safe_load(stream)
|
| 76 |
+
|
| 77 |
agent = CodeAgent(
|
| 78 |
model=model,
|
| 79 |
+
tools=[final_answer, image_generation_tool, get_latest_football_matches], ## ✅ tools registered here
|
| 80 |
max_steps=6,
|
| 81 |
verbosity_level=1,
|
| 82 |
grammar=None,
|
|
|
|
| 86 |
prompt_templates=prompt_templates
|
| 87 |
)
|
| 88 |
|
|
|
|
| 89 |
GradioUI(agent).launch()
|