dlaima commited on
Commit
1a790ff
·
verified ·
1 Parent(s): bb4dc1a

Create tools.py

Browse files
Files changed (1) hide show
  1. tools.py +102 -0
tools.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # --------- Weather Info Tool ---------
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
+
34
+
35
+ # --------- Hugging Face Hub Stats Tool ---------
36
+ class HubStatsTool(Tool):
37
+ name = "hub_stats"
38
+ description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
39
+ inputs = {
40
+ "author": {
41
+ "type": "string",
42
+ "description": "The username of the model author/organization to find models from."
43
+ }
44
+ }
45
+ output_type = "string"
46
+
47
+ def forward(self, author: str):
48
+ try:
49
+ # List models from the specified author, sorted by downloads
50
+ models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
51
+
52
+ if models:
53
+ model = models[0]
54
+ return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
55
+ else:
56
+ return f"No models found for author {author}."
57
+ except Exception as e:
58
+ return f"Error fetching models for {author}: {str(e)}"
59
+
60
+
61
+ # --------- News Search Tool using ContextualWeb API ---------
62
+ class NewsSearchTool(Tool):
63
+ name = "news_search"
64
+ description = "Fetches the latest news headlines about a specific topic using the ContextualWeb API."
65
+ inputs = {
66
+ "topic": {
67
+ "type": "string",
68
+ "description": "The topic to search for in recent news."
69
+ }
70
+ }
71
+ output_type = "string"
72
+
73
+ def __init__(self, api_key: str):
74
+ self.api_key = api_key
75
+
76
+ def forward(self, topic: str):
77
+ url = "https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/NewsSearchAPI"
78
+ params = {
79
+ "q": topic,
80
+ "pageNumber": "1",
81
+ "pageSize": "3",
82
+ "autoCorrect": "true"
83
+ }
84
+ headers = {
85
+ "X-RapidAPI-Key": self.api_key,
86
+ "X-RapidAPI-Host": "contextualwebsearch-websearch-v1.p.rapidapi.com"
87
+ }
88
+
89
+ try:
90
+ response = requests.get(url, headers=headers, params=params)
91
+ response.raise_for_status()
92
+ articles = response.json().get("value", [])
93
+
94
+ if not articles:
95
+ return f"No news articles found for topic '{topic}'."
96
+
97
+ return "\n\n".join(
98
+ f"📰 {a['title']}\n{a['description'] or ''}\nSource: {a['provider']['name']}\nLink: {a['url']}"
99
+ for a in articles
100
+ )
101
+ except Exception as e:
102
+ return f"Error retrieving news for topic '{topic}': {str(e)}"