Spaces:
Sleeping
Sleeping
Adding tools part 1
Browse files
app.py
CHANGED
|
@@ -7,16 +7,72 @@ 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:
|
|
@@ -43,15 +99,21 @@ custom_role_conversions=None,
|
|
| 43 |
)
|
| 44 |
|
| 45 |
|
| 46 |
-
# Import tool from Hub
|
| 47 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 48 |
|
| 49 |
with open("prompts.yaml", 'r') as stream:
|
| 50 |
prompt_templates = yaml.safe_load(stream)
|
| 51 |
|
| 52 |
agent = CodeAgent(
|
| 53 |
model=model,
|
| 54 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
max_steps=6,
|
| 56 |
verbosity_level=1,
|
| 57 |
grammar=None,
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
| 10 |
@tool
|
| 11 |
+
def get_weather_tool(location: str) -> dict:
|
| 12 |
+
"""Tool that fetches the current weather for a given location using DuckDuckGo search.
|
|
|
|
| 13 |
Args:
|
| 14 |
+
location: The name of the city (e.g., 'New York').
|
| 15 |
+
Returns:
|
| 16 |
+
A dictionary containing temperature (°C) and weather condition.
|
| 17 |
"""
|
| 18 |
+
search = DuckDuckGoSearchTool()
|
| 19 |
+
query = f"current weather in {location}"
|
| 20 |
+
search_results = search.run(query)
|
| 21 |
+
|
| 22 |
+
# Try to extract temperature and condition from search results
|
| 23 |
+
match = re.search(r"(-?\d+°C).*?\b(?:clear|cloudy|rain|snow|storm|sunny|drizzle|mist)\b", search_results, re.IGNORECASE)
|
| 24 |
+
|
| 25 |
+
if match:
|
| 26 |
+
temp = match.group(1) # Extract temperature
|
| 27 |
+
condition = match.group(0).split()[-1] # Extract last weather-related word
|
| 28 |
+
|
| 29 |
+
return {"temperature": temp, "condition": condition, "location": location}
|
| 30 |
+
|
| 31 |
+
return {"error": f"Could not extract weather data for {location}. Try again later."}
|
| 32 |
+
|
| 33 |
+
@tool
|
| 34 |
+
def suggest_outfit_tool(weather_data: dict) -> str:
|
| 35 |
+
"""Tool that suggests an outfit based on weather fetched from get_weather_from_duckduckgo tool.
|
| 36 |
+
Args:
|
| 37 |
+
weather_data: A dictionary containing temperature (°C) and weather condition.
|
| 38 |
+
Returns:
|
| 39 |
+
A clothing suggestion based on the weather.
|
| 40 |
+
"""
|
| 41 |
+
if "error" in weather_data:
|
| 42 |
+
return weather_data["error"]
|
| 43 |
+
|
| 44 |
+
temp = int(re.search(r"-?\d+", weather_data["temperature"]).group()) # Extract numeric value
|
| 45 |
+
condition = weather_data["condition"]
|
| 46 |
+
location = weather_data["location"]
|
| 47 |
+
|
| 48 |
+
# Outfit recommendations based on temperature
|
| 49 |
+
if temp > 25:
|
| 50 |
+
outfit = "It's hot outside! Wear light clothing, sunglasses, and a hat."
|
| 51 |
+
elif 15 <= temp <= 25:
|
| 52 |
+
outfit = "Mild weather. A t-shirt and jeans should be fine, but carry a light jacket."
|
| 53 |
+
elif 5 <= temp < 15:
|
| 54 |
+
outfit = "Chilly weather. Wear a sweater and a jacket."
|
| 55 |
+
else:
|
| 56 |
+
outfit = "It's cold! Wear a heavy coat, scarf, gloves, and boots."
|
| 57 |
+
|
| 58 |
+
# Adjust for specific weather conditions
|
| 59 |
+
if "rain" in condition:
|
| 60 |
+
outfit += " Also, carry an umbrella or wear a raincoat."
|
| 61 |
+
elif "snow" in condition:
|
| 62 |
+
outfit += " Make sure to wear waterproof boots and layer up!"
|
| 63 |
+
|
| 64 |
+
return f"Weather in {location}: {condition}, {temp}°C. {outfit}"
|
| 65 |
+
|
| 66 |
+
# @tool
|
| 67 |
+
# def my_custom_tooltom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 68 |
+
# #Keep this format for the description / args / args description but feel free to modify the tool
|
| 69 |
+
# """A tool that does nothing yet
|
| 70 |
+
# Args:
|
| 71 |
+
# arg1: the first argument
|
| 72 |
+
# arg2: the second argument
|
| 73 |
+
# """
|
| 74 |
+
# return "What magic will you build ?"
|
| 75 |
+
|
| 76 |
|
| 77 |
@tool
|
| 78 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
|
| 102 |
+
# # Import tool from Hub
|
| 103 |
+
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 104 |
|
| 105 |
with open("prompts.yaml", 'r') as stream:
|
| 106 |
prompt_templates = yaml.safe_load(stream)
|
| 107 |
|
| 108 |
agent = CodeAgent(
|
| 109 |
model=model,
|
| 110 |
+
tools=[
|
| 111 |
+
final_answer,
|
| 112 |
+
get_current_time_in_timezone,
|
| 113 |
+
get_weather_tool,
|
| 114 |
+
suggest_outfit_tool,
|
| 115 |
+
DuckDuckGoSearchTool(),
|
| 116 |
+
],
|
| 117 |
max_steps=6,
|
| 118 |
verbosity_level=1,
|
| 119 |
grammar=None,
|