Spaces:
Sleeping
Sleeping
Update tools.py
Browse filesAdded the web search tool, weather tool, hub stat tool
tools.py
CHANGED
|
@@ -30,4 +30,83 @@ class GuestInfoRetrieverTool(Tool):
|
|
| 30 |
return "No matching guest information found."
|
| 31 |
|
| 32 |
# Initialize the tool
|
| 33 |
-
guest_info_tool = GuestInfoRetrieverTool(docs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return "No matching guest information found."
|
| 31 |
|
| 32 |
# Initialize the tool
|
| 33 |
+
guest_info_tool = GuestInfoRetrieverTool(docs)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Give Your Agent Access to the Web
|
| 38 |
+
from smolagents import DuckDuckGoSearchTool
|
| 39 |
+
|
| 40 |
+
# Initialize the DuckDuckGo search tool
|
| 41 |
+
search_tool = DuckDuckGoSearchTool()
|
| 42 |
+
|
| 43 |
+
# Example usage
|
| 44 |
+
results = search_tool("Who's the current President of France?")
|
| 45 |
+
print(results)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# Creating a Custom Tool for Weather Information to Schedule the Fireworks
|
| 49 |
+
from smolagents import Tool
|
| 50 |
+
import random
|
| 51 |
+
|
| 52 |
+
class WeatherInfoTool(Tool):
|
| 53 |
+
name = "weather_info"
|
| 54 |
+
description = "Fetches dummy weather information for a given location."
|
| 55 |
+
inputs = {
|
| 56 |
+
"location": {
|
| 57 |
+
"type": "string",
|
| 58 |
+
"description": "The location to get weather information for."
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
output_type = "string"
|
| 62 |
+
|
| 63 |
+
def forward(self, location: str):
|
| 64 |
+
# Dummy weather data
|
| 65 |
+
weather_conditions = [
|
| 66 |
+
{"condition": "Rainy", "temp_c": 15},
|
| 67 |
+
{"condition": "Clear", "temp_c": 25},
|
| 68 |
+
{"condition": "Windy", "temp_c": 20}
|
| 69 |
+
]
|
| 70 |
+
# Randomly select a weather condition
|
| 71 |
+
data = random.choice(weather_conditions)
|
| 72 |
+
return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
|
| 73 |
+
|
| 74 |
+
# Initialize the tool
|
| 75 |
+
weather_info_tool = WeatherInfoTool()
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# Creating a Hub Stats Tool for Influential AI Builders
|
| 81 |
+
from smolagents import Tool
|
| 82 |
+
from huggingface_hub import list_models
|
| 83 |
+
|
| 84 |
+
class HubStatsTool(Tool):
|
| 85 |
+
name = "hub_stats"
|
| 86 |
+
description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
| 87 |
+
inputs = {
|
| 88 |
+
"author": {
|
| 89 |
+
"type": "string",
|
| 90 |
+
"description": "The username of the model author/organization to find models from."
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
output_type = "string"
|
| 94 |
+
|
| 95 |
+
def forward(self, author: str):
|
| 96 |
+
try:
|
| 97 |
+
# List models from the specified author, sorted by downloads
|
| 98 |
+
models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
|
| 99 |
+
|
| 100 |
+
if models:
|
| 101 |
+
model = models[0]
|
| 102 |
+
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
|
| 103 |
+
else:
|
| 104 |
+
return f"No models found for author {author}."
|
| 105 |
+
except Exception as e:
|
| 106 |
+
return f"Error fetching models for {author}: {str(e)}"
|
| 107 |
+
|
| 108 |
+
# Initialize the tool
|
| 109 |
+
hub_stats_tool = HubStatsTool()
|
| 110 |
+
|
| 111 |
+
# Example usage
|
| 112 |
+
print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Facebook
|