Spaces:
Runtime error
Runtime error
Create tools.py
Browse files
tools.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import DuckDuckGoSearchTool
|
2 |
+
|
3 |
+
# Initialize the DuckDuckGo search tool
|
4 |
+
search_tool = DuckDuckGoSearchTool()
|
5 |
+
|
6 |
+
# Example usage
|
7 |
+
results = search_tool("Who's the current President of France?")
|
8 |
+
print(results)
|
9 |
+
|
10 |
+
from smolagents import Tool
|
11 |
+
import random
|
12 |
+
|
13 |
+
class WeatherInfoTool(Tool):
|
14 |
+
name = "weather_info"
|
15 |
+
description = "Fetches dummy weather information for a given location."
|
16 |
+
inputs = {
|
17 |
+
"location": {
|
18 |
+
"type": "string",
|
19 |
+
"description": "The location to get weather information for."
|
20 |
+
}
|
21 |
+
}
|
22 |
+
output_type = "string"
|
23 |
+
|
24 |
+
def forward(self, location: str):
|
25 |
+
# Dummy weather data
|
26 |
+
weather_conditions = [
|
27 |
+
{"condition": "Rainy", "temp_c": 15},
|
28 |
+
{"condition": "Clear", "temp_c": 25},
|
29 |
+
{"condition": "Windy", "temp_c": 20}
|
30 |
+
]
|
31 |
+
# Randomly select a weather condition
|
32 |
+
data = random.choice(weather_conditions)
|
33 |
+
return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
|
34 |
+
|
35 |
+
# Initialize the tool
|
36 |
+
weather_info_tool = WeatherInfoTool()
|
37 |
+
|
38 |
+
from smolagents import Tool
|
39 |
+
from huggingface_hub import list_models
|
40 |
+
|
41 |
+
class HubStatsTool(Tool):
|
42 |
+
name = "hub_stats"
|
43 |
+
description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
44 |
+
inputs = {
|
45 |
+
"author": {
|
46 |
+
"type": "string",
|
47 |
+
"description": "The username of the model author/organization to find models from."
|
48 |
+
}
|
49 |
+
}
|
50 |
+
output_type = "string"
|
51 |
+
|
52 |
+
def forward(self, author: str):
|
53 |
+
try:
|
54 |
+
# List models from the specified author, sorted by downloads
|
55 |
+
models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
|
56 |
+
|
57 |
+
if models:
|
58 |
+
model = models[0]
|
59 |
+
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
|
60 |
+
else:
|
61 |
+
return f"No models found for author {author}."
|
62 |
+
except Exception as e:
|
63 |
+
return f"Error fetching models for {author}: {str(e)}"
|
64 |
+
|
65 |
+
# Initialize the tool
|
66 |
+
hub_stats_tool = HubStatsTool()
|
67 |
+
|
68 |
+
# Example usage
|
69 |
+
print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Facebook
|
70 |
+
|
71 |
+
from smolagents import CodeAgent, InferenceClientModel
|
72 |
+
|
73 |
+
# Initialize the Hugging Face model
|
74 |
+
model = InferenceClientModel()
|
75 |
+
|
76 |
+
# Create Alfred with all the tools
|
77 |
+
alfred = CodeAgent(
|
78 |
+
tools=[search_tool, weather_info_tool, hub_stats_tool],
|
79 |
+
model=model
|
80 |
+
)
|
81 |
+
|
82 |
+
# Example query Alfred might receive during the gala
|
83 |
+
response = alfred.run("What is Facebook and what's their most popular model?")
|
84 |
+
|
85 |
+
print("🎩 Alfred's Response:")
|
86 |
+
print(response)
|
87 |
+
|