Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,56 +1,51 @@
|
|
| 1 |
-
from smolagents import DuckDuckGoSearchTool
|
| 2 |
-
from smolagents import Tool
|
| 3 |
import random
|
| 4 |
-
from huggingface_hub import list_models
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Initialize the DuckDuckGo search tool
|
| 8 |
-
#search_tool = DuckDuckGoSearchTool()
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
class WeatherInfoTool(Tool):
|
| 12 |
-
name = "weather_info"
|
| 13 |
-
description = "Fetches dummy weather information for a given location."
|
| 14 |
-
inputs = {
|
| 15 |
-
"location": {
|
| 16 |
-
"type": "string",
|
| 17 |
-
"description": "The location to get weather information for."
|
| 18 |
-
}
|
| 19 |
-
}
|
| 20 |
-
output_type = "string"
|
| 21 |
-
|
| 22 |
-
def forward(self, location: str):
|
| 23 |
-
# Dummy weather data
|
| 24 |
-
weather_conditions = [
|
| 25 |
-
{"condition": "Rainy", "temp_c": 15},
|
| 26 |
-
{"condition": "Clear", "temp_c": 25},
|
| 27 |
-
{"condition": "Windy", "temp_c": 20}
|
| 28 |
-
]
|
| 29 |
-
# Randomly select a weather condition
|
| 30 |
-
data = random.choice(weather_conditions)
|
| 31 |
-
return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
|
| 32 |
-
|
| 33 |
-
class HubStatsTool(Tool):
|
| 34 |
-
name = "hub_stats"
|
| 35 |
-
description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
| 36 |
-
inputs = {
|
| 37 |
-
"author": {
|
| 38 |
-
"type": "string",
|
| 39 |
-
"description": "The username of the model author/organization to find models from."
|
| 40 |
-
}
|
| 41 |
-
}
|
| 42 |
-
output_type = "string"
|
| 43 |
-
|
| 44 |
-
def forward(self, author: str):
|
| 45 |
-
try:
|
| 46 |
-
# List models from the specified author, sorted by downloads
|
| 47 |
-
models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
|
| 48 |
-
|
| 49 |
-
if models:
|
| 50 |
-
model = models[0]
|
| 51 |
-
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
|
| 52 |
-
else:
|
| 53 |
-
return f"No models found for author {author}."
|
| 54 |
-
except Exception as e:
|
| 55 |
-
return f"Error fetching models for {author}: {str(e)}"
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
from huggingface_hub import list_models
|
| 4 |
+
from langchain.tools import Tool
|
| 5 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def get_weather_info(location: str) -> str:
|
| 9 |
+
"""Fetches dummy weather information for a given location."""
|
| 10 |
+
# 虚拟天气数据
|
| 11 |
+
weather_conditions = [
|
| 12 |
+
{"condition": "Rainy", "temp_c": 15},
|
| 13 |
+
{"condition": "Clear", "temp_c": 25},
|
| 14 |
+
{"condition": "Windy", "temp_c": 20}
|
| 15 |
+
]
|
| 16 |
+
# 随机选择一种天气状况
|
| 17 |
+
data = random.choice(weather_conditions)
|
| 18 |
+
return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_hub_stats(author: str) -> str:
|
| 22 |
+
"""Fetches the most downloaded model from a specific author on the Hugging Face Hub."""
|
| 23 |
+
try:
|
| 24 |
+
# 列出指定作者的模型,按下载次数排序
|
| 25 |
+
models = list(list_models(
|
| 26 |
+
author=author, sort="downloads", direction=-1, limit=1))
|
| 27 |
+
|
| 28 |
+
if models:
|
| 29 |
+
model = models[0]
|
| 30 |
+
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
|
| 31 |
+
else:
|
| 32 |
+
return f"No models found for author {author}."
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error fetching models for {author}: {str(e)}"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
search_tool = DuckDuckGoSearchRun()
|
| 38 |
+
|
| 39 |
+
# 初始化天气工具
|
| 40 |
+
weather_info_tool = Tool(
|
| 41 |
+
name="get_weather_info",
|
| 42 |
+
func=get_weather_info,
|
| 43 |
+
description="Fetches dummy weather information for a given location."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# 初始化hub统计工具
|
| 47 |
+
hub_stats_tool = Tool(
|
| 48 |
+
name="get_hub_stats",
|
| 49 |
+
func=get_hub_stats,
|
| 50 |
+
description="Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
| 51 |
+
)
|