Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,34 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 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 |
-
"""
|
|
|
|
| 24 |
Args:
|
| 25 |
-
timezone: A
|
| 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 |
@tool
|
| 38 |
def get_nba_matches() -> str:
|
| 39 |
"""
|
| 40 |
A tool that retrieves upcoming NBA matches using TheRundown API.
|
|
|
|
| 41 |
|
| 42 |
Returns:
|
| 43 |
A human-readable string listing the upcoming NBA matches.
|
| 44 |
"""
|
| 45 |
-
import requests
|
| 46 |
-
# Replace the endpoint below with the correct NBA events endpoint if available.
|
| 47 |
-
# Here we assume sport_id 4 corresponds to NBA and that the '/events' endpoint returns the matches.
|
| 48 |
url = "https://api.apilayer.com/therundown/sports/4/events"
|
| 49 |
headers = {
|
| 50 |
"apikey": "7k4hKSUeWkbigKxZiNV5CQ8RSlEd72Cj"
|
|
@@ -54,8 +38,7 @@ def get_nba_matches() -> str:
|
|
| 54 |
if response.status_code == 200:
|
| 55 |
data = response.json()
|
| 56 |
matches = []
|
| 57 |
-
#
|
| 58 |
-
# (Adjust the keys below according to the actual response format.)
|
| 59 |
for event in data.get("events", []):
|
| 60 |
start_time = event.get("start_time", "Unknown time")
|
| 61 |
home_team = event.get("home_team", "Home team not available")
|
|
@@ -77,7 +60,7 @@ def predict_nba_match(match_info: str) -> str:
|
|
| 77 |
match_info: A string containing match details in the format "TeamA vs TeamB".
|
| 78 |
|
| 79 |
Returns:
|
| 80 |
-
A string with the prediction
|
| 81 |
"""
|
| 82 |
import random
|
| 83 |
teams = match_info.split(" vs ")
|
|
@@ -85,31 +68,27 @@ def predict_nba_match(match_info: str) -> str:
|
|
| 85 |
prediction = random.choice(teams)
|
| 86 |
return f"The prediction is that {prediction} will win."
|
| 87 |
else:
|
| 88 |
-
return "Invalid match format. Please provide
|
| 89 |
-
|
| 90 |
|
|
|
|
| 91 |
final_answer = FinalAnswerTool()
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 95 |
-
|
| 96 |
model = HfApiModel(
|
| 97 |
-
max_tokens=2096,
|
| 98 |
-
temperature=0.5,
|
| 99 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 100 |
-
custom_role_conversions=None,
|
| 101 |
)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
# Import tool from Hub
|
| 105 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 106 |
-
|
| 107 |
with open("prompts.yaml", 'r') as stream:
|
| 108 |
prompt_templates = yaml.safe_load(stream)
|
| 109 |
-
|
|
|
|
| 110 |
agent = CodeAgent(
|
| 111 |
model=model,
|
| 112 |
-
tools=[final_answer
|
| 113 |
max_steps=6,
|
| 114 |
verbosity_level=1,
|
| 115 |
grammar=None,
|
|
@@ -119,5 +98,4 @@ agent = CodeAgent(
|
|
| 119 |
prompt_templates=prompt_templates
|
| 120 |
)
|
| 121 |
|
| 122 |
-
|
| 123 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, 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 |
@tool
|
| 10 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 11 |
+
"""
|
| 12 |
+
A tool that fetches the current local time in a specified timezone.
|
| 13 |
Args:
|
| 14 |
+
timezone: A valid timezone string (e.g., 'America/New_York').
|
| 15 |
"""
|
| 16 |
try:
|
|
|
|
| 17 |
tz = pytz.timezone(timezone)
|
|
|
|
| 18 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 19 |
return f"The current local time in {timezone} is: {local_time}"
|
| 20 |
except Exception as e:
|
| 21 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 22 |
|
|
|
|
| 23 |
@tool
|
| 24 |
def get_nba_matches() -> str:
|
| 25 |
"""
|
| 26 |
A tool that retrieves upcoming NBA matches using TheRundown API.
|
| 27 |
+
(Assumes that sport_id 4 corresponds to the NBA.)
|
| 28 |
|
| 29 |
Returns:
|
| 30 |
A human-readable string listing the upcoming NBA matches.
|
| 31 |
"""
|
|
|
|
|
|
|
|
|
|
| 32 |
url = "https://api.apilayer.com/therundown/sports/4/events"
|
| 33 |
headers = {
|
| 34 |
"apikey": "7k4hKSUeWkbigKxZiNV5CQ8RSlEd72Cj"
|
|
|
|
| 38 |
if response.status_code == 200:
|
| 39 |
data = response.json()
|
| 40 |
matches = []
|
| 41 |
+
# Adjust the parsing keys based on the actual API response.
|
|
|
|
| 42 |
for event in data.get("events", []):
|
| 43 |
start_time = event.get("start_time", "Unknown time")
|
| 44 |
home_team = event.get("home_team", "Home team not available")
|
|
|
|
| 60 |
match_info: A string containing match details in the format "TeamA vs TeamB".
|
| 61 |
|
| 62 |
Returns:
|
| 63 |
+
A string with the prediction (e.g., "The prediction is that TeamA will win.").
|
| 64 |
"""
|
| 65 |
import random
|
| 66 |
teams = match_info.split(" vs ")
|
|
|
|
| 68 |
prediction = random.choice(teams)
|
| 69 |
return f"The prediction is that {prediction} will win."
|
| 70 |
else:
|
| 71 |
+
return "Invalid match format. Please provide details in the format 'TeamA vs TeamB'."
|
|
|
|
| 72 |
|
| 73 |
+
# Instantiate the final_answer tool.
|
| 74 |
final_answer = FinalAnswerTool()
|
| 75 |
|
| 76 |
+
# Configure the model.
|
|
|
|
|
|
|
| 77 |
model = HfApiModel(
|
| 78 |
+
max_tokens=2096,
|
| 79 |
+
temperature=0.5,
|
| 80 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 81 |
+
custom_role_conversions=None,
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Load prompt templates.
|
|
|
|
|
|
|
|
|
|
| 85 |
with open("prompts.yaml", 'r') as stream:
|
| 86 |
prompt_templates = yaml.safe_load(stream)
|
| 87 |
+
|
| 88 |
+
# Initialize the agent with all necessary tools.
|
| 89 |
agent = CodeAgent(
|
| 90 |
model=model,
|
| 91 |
+
tools=[final_answer, get_current_time_in_timezone, get_nba_matches, predict_nba_match],
|
| 92 |
max_steps=6,
|
| 93 |
verbosity_level=1,
|
| 94 |
grammar=None,
|
|
|
|
| 98 |
prompt_templates=prompt_templates
|
| 99 |
)
|
| 100 |
|
|
|
|
| 101 |
GradioUI(agent).launch()
|