biaotang commited on
Commit
307ffe3
·
1 Parent(s): 2637699

Add hf hub stats and weather info tools

Browse files
Files changed (2) hide show
  1. app.py +7 -3
  2. tools.py +58 -0
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import os
3
  from smolagents import CodeAgent, HfApiModel, LiteLLMModel
4
  from retriever import load_guest_dataset
 
5
 
6
  is_running_on_space = os.environ.get('SPACE_ID') is not None
7
  if is_running_on_space:
@@ -12,15 +13,18 @@ else:
12
  api_base="http://127.0.0.1:11434",
13
  num_ctx=8192,)
14
 
15
- guest_info_tool = load_guest_dataset()
16
 
17
  # Create Alfred, our gala agent, with the guest info tool
18
- alfred = CodeAgent(tools=[guest_info_tool], model=model)
19
 
20
 
21
  def query_guest(guest_name):
22
  return alfred.run(f"Tell me about our guest named '{guest_name}'.")
23
 
 
 
24
 
25
- demo = gr.Interface(fn=query_guest, inputs="text", outputs="text")
 
26
  demo.launch()
 
2
  import os
3
  from smolagents import CodeAgent, HfApiModel, LiteLLMModel
4
  from retriever import load_guest_dataset
5
+ from tools import *
6
 
7
  is_running_on_space = os.environ.get('SPACE_ID') is not None
8
  if is_running_on_space:
 
13
  api_base="http://127.0.0.1:11434",
14
  num_ctx=8192,)
15
 
16
+ # guest_info_tool = load_guest_dataset()
17
 
18
  # Create Alfred, our gala agent, with the guest info tool
19
+ alfred = CodeAgent(tools=[hub_stats_tool, search_tool, weather_info_tool], model=model)
20
 
21
 
22
  def query_guest(guest_name):
23
  return alfred.run(f"Tell me about our guest named '{guest_name}'.")
24
 
25
+ def ai_builder(builder):
26
+ return alfred.run(f"What is {builder} most downloaded model on huggingface hub?")
27
 
28
+
29
+ demo = gr.Interface(fn=ai_builder, inputs="text", outputs="text")
30
  demo.launch()
tools.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import list_models
2
+ from smolagents import DuckDuckGoSearchTool, Tool
3
+ import random
4
+
5
+ # Initialize the DuckDuckGo search tool
6
+ search_tool = DuckDuckGoSearchTool()
7
+
8
+ class WeatherInfoTool(Tool):
9
+ name = "weather_info"
10
+ description = "Fetches dummy weather information for a given location."
11
+ inputs = {
12
+ "location": {
13
+ "type": "string",
14
+ "description": "The location to get weather information for."
15
+ }
16
+ }
17
+ output_type = "string"
18
+
19
+ def forward(self, location: str):
20
+ # Dummy weather data
21
+ weather_conditions = [
22
+ {"condition": "Rainy", "temp_c": 15},
23
+ {"condition": "Clear", "temp_c": 25},
24
+ {"condition": "Windy", "temp_c": 20}
25
+ ]
26
+ # Randomly select a weather condition
27
+ data = random.choice(weather_conditions)
28
+ return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
29
+
30
+ # Initialize the tool
31
+ weather_info_tool = WeatherInfoTool()
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
+
57
+ # Initialize the tool
58
+ hub_stats_tool = HubStatsTool()