Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,96 @@ from tools.final_answer import FinalAnswerTool
|
|
| 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 best_nba_team(arg1:str)-> 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
|
|
@@ -73,7 +163,9 @@ agent = CodeAgent(
|
|
| 73 |
model=model,
|
| 74 |
tools=[
|
| 75 |
final_answer,
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
],
|
| 78 |
max_steps=6,
|
| 79 |
verbosity_level=1,
|
|
|
|
| 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 get_weather(city: str) -> str:
|
| 13 |
+
"""Gets the current weather for a city.
|
| 14 |
+
Args:
|
| 15 |
+
city: City and state/country, for example 'Phoenix, AZ' or 'Flippin, AR'
|
| 16 |
+
"""
|
| 17 |
+
try:
|
| 18 |
+
# Step 1: geocode city into latitude/longitude
|
| 19 |
+
geo_url = "https://geocoding-api.open-meteo.com/v1/search"
|
| 20 |
+
geo_params = {"name": city, "count": 1, "language": "en", "format": "json"}
|
| 21 |
+
geo_response = requests.get(geo_url, params=geo_params, timeout=10)
|
| 22 |
+
geo_response.raise_for_status()
|
| 23 |
+
geo_data = geo_response.json()
|
| 24 |
+
|
| 25 |
+
if "results" not in geo_data or len(geo_data["results"]) == 0:
|
| 26 |
+
return f"I could not find weather coordinates for {city}."
|
| 27 |
+
|
| 28 |
+
location = geo_data["results"][0]
|
| 29 |
+
latitude = location["latitude"]
|
| 30 |
+
longitude = location["longitude"]
|
| 31 |
+
location_name = location.get("name", city)
|
| 32 |
+
state = location.get("admin1", "")
|
| 33 |
+
country = location.get("country", "")
|
| 34 |
+
|
| 35 |
+
# Step 2: get current weather
|
| 36 |
+
weather_url = "https://api.open-meteo.com/v1/forecast"
|
| 37 |
+
weather_params = {
|
| 38 |
+
"latitude": latitude,
|
| 39 |
+
"longitude": longitude,
|
| 40 |
+
"current": "temperature_2m,relative_humidity_2m,wind_speed_10m,weather_code",
|
| 41 |
+
"temperature_unit": "fahrenheit",
|
| 42 |
+
"wind_speed_unit": "mph",
|
| 43 |
+
"timezone": "auto",
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
weather_response = requests.get(weather_url, params=weather_params, timeout=10)
|
| 47 |
+
weather_response.raise_for_status()
|
| 48 |
+
weather_data = weather_response.json()
|
| 49 |
+
current = weather_data["current"]
|
| 50 |
+
|
| 51 |
+
return (
|
| 52 |
+
f"Current weather for {location_name}, {state}, {country}: "
|
| 53 |
+
f"{current['temperature_2m']}°F, "
|
| 54 |
+
f"humidity {current['relative_humidity_2m']}%, "
|
| 55 |
+
f"wind {current['wind_speed_10m']} mph."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error getting weather for {city}: {str(e)}"
|
| 60 |
+
|
| 61 |
+
@tool
|
| 62 |
+
def get_nba_scores(date: str = "") -> str:
|
| 63 |
+
"""Gets current NBA scores from ESPN.
|
| 64 |
+
Args:
|
| 65 |
+
date: Optional date in YYYYMMDD format. Leave blank for today's games.
|
| 66 |
+
"""
|
| 67 |
+
try:
|
| 68 |
+
url = "https://site.api.espn.com/apis/site/v2/sports/basketball/nba/scoreboard"
|
| 69 |
+
params = {}
|
| 70 |
+
if date:
|
| 71 |
+
params["dates"] = date
|
| 72 |
+
|
| 73 |
+
response = requests.get(url, params=params, timeout=10)
|
| 74 |
+
response.raise_for_status()
|
| 75 |
+
data = response.json()
|
| 76 |
+
|
| 77 |
+
events = data.get("events", [])
|
| 78 |
+
if not events:
|
| 79 |
+
return "No NBA games found for that date."
|
| 80 |
+
|
| 81 |
+
results = []
|
| 82 |
+
for event in events:
|
| 83 |
+
name = event.get("name", "Unknown matchup")
|
| 84 |
+
status = event.get("status", {}).get("type", {}).get("description", "")
|
| 85 |
+
competitors = event["competitions"][0]["competitors"]
|
| 86 |
+
|
| 87 |
+
teams = []
|
| 88 |
+
for c in competitors:
|
| 89 |
+
team_name = c["team"]["displayName"]
|
| 90 |
+
score = c.get("score", "0")
|
| 91 |
+
teams.append(f"{team_name}: {score}")
|
| 92 |
+
|
| 93 |
+
results.append(f"{name} — {status} — " + " | ".join(teams))
|
| 94 |
+
|
| 95 |
+
return "\n".join(results)
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
return f"Error getting NBA scores: {str(e)}"
|
| 99 |
+
|
| 100 |
+
|
| 101 |
@tool
|
| 102 |
def best_nba_team(arg1:str)-> str: #it's import to specify the return type
|
| 103 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
|
|
|
| 163 |
model=model,
|
| 164 |
tools=[
|
| 165 |
final_answer,
|
| 166 |
+
get_current_time_in_timezone,
|
| 167 |
+
get_weather,
|
| 168 |
+
get_nba_scores,
|
| 169 |
],
|
| 170 |
max_steps=6,
|
| 171 |
verbosity_level=1,
|