Kunal commited on
Commit
e2ae217
·
1 Parent(s): 0149305

added tools.py and app.py

Browse files
Files changed (3) hide show
  1. app.py +30 -4
  2. requirements.txt +5 -4
  3. tools.py +56 -0
app.py CHANGED
@@ -1,7 +1,33 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import random
3
+ from smolagents import GradioUI, CodeAgent, HfApiModel
4
 
5
+ # Import our custom tools from their modules
6
+ from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
7
+ from retriever import load_guest_dataset
8
 
9
+ # Initialize the Hugging Face model
10
+ model = HfApiModel()
11
+
12
+ # Initialize the web search tool
13
+ search_tool = DuckDuckGoSearchTool()
14
+
15
+ # Initialize the weather tool
16
+ weather_info_tool = WeatherInfoTool()
17
+
18
+ # Initialize the Hub stats tool
19
+ hub_stats_tool = HubStatsTool()
20
+
21
+ # Load the guest dataset and initialize the guest info tool
22
+ guest_info_tool = load_guest_dataset()
23
+
24
+ # Create Alfred with all the tools
25
+ alfred = CodeAgent(
26
+ tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
27
+ model=model,
28
+ add_base_tools=True, # Add any additional base tools
29
+ planning_interval=3 # Enable planning every 3 steps
30
+ )
31
+
32
+ if __name__ == "__main__":
33
+ GradioUI(alfred).launch()
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
- gradio
2
- langchain
3
- langchain-community
4
- smolagents
 
 
1
+ datasets
2
+ smolagents
3
+ langchain-community
4
+ rank_bm25
5
+ duckduckgo-search
tools.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import DuckDuckGoSearchTool, Tool
2
+ import random
3
+ from huggingface_hub import list_models
4
+
5
+ # Initialize the DuckDuckGo search tool
6
+ search_tool = DuckDuckGoSearchTool()
7
+
8
+ # Example usage
9
+ # results = search_tool("Who's the current President of France?")
10
+ # print(results)
11
+
12
+ class WeatherInfoTool(Tool):
13
+ name = "weather_info"
14
+ description = "Fetches dummy weather information for a given location."
15
+ inputs = {
16
+ "location": {
17
+ "type": "string",
18
+ "description": "The location to get weather information for."
19
+ }
20
+ }
21
+ output_type = "string"
22
+
23
+ def forward(self, location: str):
24
+ # Dummy weather data
25
+ weather_conditions = [
26
+ {"condition": "Rainy", "temp_c": 15},
27
+ {"condition": "Clear", "temp_c": 25},
28
+ {"condition": "Windy", "temp_c": 20}
29
+ ]
30
+ # Randomly select a weather condition
31
+ data = random.choice(weather_conditions)
32
+ return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
33
+
34
+ class HubStatsTool(Tool):
35
+ name = "hub_stats"
36
+ description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
37
+ inputs = {
38
+ "author": {
39
+ "type": "string",
40
+ "description": "The username of the model author/organization to find models from."
41
+ }
42
+ }
43
+ output_type = "string"
44
+
45
+ def forward(self, author: str):
46
+ try:
47
+ # List models from the specified author, sorted by downloads
48
+ models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
49
+
50
+ if models:
51
+ model = models[0]
52
+ return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
53
+ else:
54
+ return f"No models found for author {author}."
55
+ except Exception as e:
56
+ return f"Error fetching models for {author}: {str(e)}"