Spaces:
Sleeping
Sleeping
Adding new tools : get_current_weather and recommend_activities to app.py
Browse files
app.py
CHANGED
|
@@ -9,14 +9,32 @@ 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 |
-
|
|
|
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -33,6 +51,53 @@ 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 |
|
|
@@ -55,7 +120,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,
|
|
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
def get_current_weather(location: str, date_time: str) -> str:
|
| 13 |
+
"""
|
| 14 |
+
Returns the weather report.
|
| 15 |
+
|
| 16 |
Args:
|
| 17 |
+
location: the name of the place that you want the weather for. Should be a place name, followed by possibly a city name, then a country, like "Anchor Point, Taghazout, Morocco".
|
| 18 |
+
date_time: the date and time for which you want the report, formatted as '%m/%d/%y %H:%M:%S'.
|
| 19 |
"""
|
| 20 |
+
try:
|
| 21 |
+
lon, lat = convert_location_to_coordinates(location)
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Failed to convert location '{location}' to coordinates. Error: {str(e)}"
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
date_time_obj = datetime.strptime(date_time, "%m/%d/%y %H:%M:%S")
|
| 27 |
+
except ValueError as e:
|
| 28 |
+
return f"Invalid `date_time` format. Expected '%m/%d/%y %H:%M:%S'. Error: {str(e)}"
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
temperature_celsius, risk_of_rain, wave_height = get_weather_report_at_coordinates((lon, lat), date_time_obj)
|
| 32 |
+
return (f"Weather report for {location} on {date_time}: "
|
| 33 |
+
f"Temperature: {temperature_celsius}°C, "
|
| 34 |
+
f"Risk of rain: {risk_of_rain * 100:.0f}%, "
|
| 35 |
+
f"Wave height: {wave_height}m.")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Failed to fetch weather data for {location}. Error: {str(e)}"
|
| 38 |
|
| 39 |
@tool
|
| 40 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 53 |
|
| 54 |
+
@tool
|
| 55 |
+
def recommend_activities(temperature: float, risk_of_rain: float, time_of_day: str) -> str:
|
| 56 |
+
"""
|
| 57 |
+
Suggests a friendly activity recommendation based on temperature, risk of rain, and time of day.
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
temperature: The temperature in Celsius.
|
| 61 |
+
risk_of_rain: The probability of rain (as a decimal between 0 and 1).
|
| 62 |
+
time_of_day: Part of the day (morning, afternoon, evening, night).
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
A friendly activity suggestion as a sentence.
|
| 66 |
+
"""
|
| 67 |
+
time_of_day = time_of_day.lower()
|
| 68 |
+
|
| 69 |
+
if time_of_day not in ["morning", "afternoon", "evening", "night"]:
|
| 70 |
+
return "Hmm, I don’t recognize that time of day. Try 'morning', 'afternoon', 'evening', or 'night'."
|
| 71 |
+
|
| 72 |
+
cold = temperature < 10
|
| 73 |
+
warm = 10 <= temperature <= 25
|
| 74 |
+
hot = temperature > 25
|
| 75 |
+
rainy = risk_of_rain > 0.5
|
| 76 |
+
|
| 77 |
+
if time_of_day == "morning":
|
| 78 |
+
if rainy or cold:
|
| 79 |
+
return "It's the perfect time to stay in, enjoy a warm cup of coffee, and start your day with a good book or a cozy movie."
|
| 80 |
+
return "A fresh morning awaits! Go for a peaceful walk, grab a nice breakfast, or enjoy some outdoor yoga."
|
| 81 |
+
|
| 82 |
+
elif time_of_day == "afternoon":
|
| 83 |
+
if rainy:
|
| 84 |
+
return "Rainy afternoon? How about visiting a museum, watching a movie, or enjoying a relaxed café break?"
|
| 85 |
+
if hot:
|
| 86 |
+
return "It's warm outside! A swim, a cold drink, or a relaxing afternoon indoors sounds perfect."
|
| 87 |
+
return "Great time to explore! A bike ride, a picnic, or a casual city stroll could be just what you need."
|
| 88 |
+
|
| 89 |
+
elif time_of_day == "evening":
|
| 90 |
+
if rainy or cold:
|
| 91 |
+
return "A cozy dinner indoors, a movie night, or a relaxed evening at home sounds just right."
|
| 92 |
+
return "How about a sunset walk, a rooftop café visit, or a fun evening with friends?"
|
| 93 |
+
|
| 94 |
+
elif time_of_day == "night":
|
| 95 |
+
if rainy or cold:
|
| 96 |
+
return "Snuggle up with a blanket, put on your favorite series, or enjoy a warm drink before bed."
|
| 97 |
+
return "A quiet night walk, some stargazing, or a laid-back evening out could be a great way to end the day."
|
| 98 |
+
|
| 99 |
+
return "Not sure what to recommend, but whatever you do, enjoy your time!"
|
| 100 |
+
|
| 101 |
|
| 102 |
final_answer = FinalAnswerTool()
|
| 103 |
|
|
|
|
| 120 |
|
| 121 |
agent = CodeAgent(
|
| 122 |
model=model,
|
| 123 |
+
tools=[final_answer, get_current_time_in_timezone, get_current_weather, recommend_activities], ## add your tools here (don't remove final answer)
|
| 124 |
max_steps=6,
|
| 125 |
verbosity_level=1,
|
| 126 |
grammar=None,
|