Spaces:
Build error
Build error
Commit
·
6d648e6
1
Parent(s):
a90b609
added news tool
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
-
from smolagents import GradioUI, CodeAgent, HfApiModel, DuckDuckGoSearchTool
|
| 4 |
# Import our custom tools from their modules
|
| 5 |
from tools import WeatherInfoTool, HubStatsTool
|
| 6 |
from retriever import load_guest_dataset
|
|
@@ -10,7 +10,8 @@ model = HfApiModel()
|
|
| 10 |
|
| 11 |
# Initialize the DuckDuckGo search tool
|
| 12 |
search_tool = DuckDuckGoSearchTool()
|
| 13 |
-
|
|
|
|
| 14 |
# Initialize the weather tool
|
| 15 |
weather_info_tool = WeatherInfoTool()
|
| 16 |
|
|
@@ -23,12 +24,11 @@ hub_stats_tool = HubStatsTool()
|
|
| 23 |
|
| 24 |
# Create Alfred with all the tools
|
| 25 |
agent = CodeAgent(
|
| 26 |
-
tools=[guest_info_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 |
-
if __name__ == "__main__":
|
| 34 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
+
from smolagents import GradioUI, CodeAgent, HfApiModel, DuckDuckGoSearchTool, LatestNewsTool
|
| 4 |
# Import our custom tools from their modules
|
| 5 |
from tools import WeatherInfoTool, HubStatsTool
|
| 6 |
from retriever import load_guest_dataset
|
|
|
|
| 10 |
|
| 11 |
# Initialize the DuckDuckGo search tool
|
| 12 |
search_tool = DuckDuckGoSearchTool()
|
| 13 |
+
# Initialize the News tool
|
| 14 |
+
latest_news_tool = LatestNewsTool()
|
| 15 |
# Initialize the weather tool
|
| 16 |
weather_info_tool = WeatherInfoTool()
|
| 17 |
|
|
|
|
| 24 |
|
| 25 |
# Create Alfred with all the tools
|
| 26 |
agent = CodeAgent(
|
| 27 |
+
tools=[guest_info_tool, latest_news_tool, weather_info_tool, hub_stats_tool, search_tool],
|
| 28 |
model=model,
|
| 29 |
add_base_tools=True, # Add any additional base tools
|
| 30 |
+
# planning_interval=3, # Enable planning every 3 steps
|
| 31 |
)
|
| 32 |
#
|
| 33 |
if __name__ == "__main__":
|
|
|
|
| 34 |
GradioUI(agent).launch()
|
tools.py
CHANGED
|
@@ -2,7 +2,9 @@ from smolagents import DuckDuckGoSearchTool
|
|
| 2 |
from smolagents import Tool
|
| 3 |
import requests
|
| 4 |
from huggingface_hub import list_models
|
|
|
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
class WeatherInfoTool(Tool):
|
| 8 |
name = "weather_info"
|
|
@@ -23,6 +25,7 @@ class WeatherInfoTool(Tool):
|
|
| 23 |
|
| 24 |
def forward(self, location:str, units:str="imperial")->str:
|
| 25 |
# Weather from openweather api
|
|
|
|
| 26 |
weather_api_key = os.getenv("WEATHER_API")
|
| 27 |
url = f"http://api.openweathermap.org/geo/1.0/direct?limit=5"
|
| 28 |
params = {
|
|
@@ -49,7 +52,36 @@ class WeatherInfoTool(Tool):
|
|
| 49 |
result = f"""The weather in {location} is {combined_dictionary['description']}. The temperature is {combined_dictionary['temp']}, feels like {combined_dictionary['feels_like']}"""
|
| 50 |
|
| 51 |
return result
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
class HubStatsTool(Tool):
|
| 54 |
name = "hub_stats"
|
| 55 |
description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
|
|
|
| 2 |
from smolagents import Tool
|
| 3 |
import requests
|
| 4 |
from huggingface_hub import list_models
|
| 5 |
+
from dotenv import find_dotenv, load_dotenv
|
| 6 |
import os
|
| 7 |
+
from datetime import datetime, timedelta
|
| 8 |
|
| 9 |
class WeatherInfoTool(Tool):
|
| 10 |
name = "weather_info"
|
|
|
|
| 25 |
|
| 26 |
def forward(self, location:str, units:str="imperial")->str:
|
| 27 |
# Weather from openweather api
|
| 28 |
+
#load_dotenv(find_dotenv())
|
| 29 |
weather_api_key = os.getenv("WEATHER_API")
|
| 30 |
url = f"http://api.openweathermap.org/geo/1.0/direct?limit=5"
|
| 31 |
params = {
|
|
|
|
| 52 |
result = f"""The weather in {location} is {combined_dictionary['description']}. The temperature is {combined_dictionary['temp']}, feels like {combined_dictionary['feels_like']}"""
|
| 53 |
|
| 54 |
return result
|
| 55 |
+
|
| 56 |
+
class LatestNewsTool(Tool):
|
| 57 |
+
name = "latest_news"
|
| 58 |
+
description = "Fetches latest news on a specified topic."
|
| 59 |
+
inputs = {
|
| 60 |
+
"topic": {
|
| 61 |
+
"type": "string",
|
| 62 |
+
"description": "Topic for which to get the latest news."
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
output_type = "string"
|
| 66 |
+
|
| 67 |
+
def forward(self, topic:str)->str:
|
| 68 |
+
# news from newsapi
|
| 69 |
+
load_dotenv(find_dotenv())
|
| 70 |
+
news_api_key = os.getenv("NEWS_API")
|
| 71 |
+
today = datetime.now()
|
| 72 |
+
yesterday = today - timedelta(days=1)
|
| 73 |
+
yesterday = str(yesterday.date())
|
| 74 |
+
url = ('https://newsapi.org/v2/everything?'
|
| 75 |
+
f'q={topic}&'
|
| 76 |
+
f'from={yesterday}&'
|
| 77 |
+
'sortBy=popularity&'
|
| 78 |
+
f'apiKey={news_api_key}')
|
| 79 |
+
response = requests.get(url)
|
| 80 |
+
resp = response.json()
|
| 81 |
+
results = [f'Title: {el["title"]}. Description: {el["description"]}' for el in resp["articles"]][:10]
|
| 82 |
+
results_str = ' '.join(el for el in results)
|
| 83 |
+
return results_str
|
| 84 |
+
|
| 85 |
class HubStatsTool(Tool):
|
| 86 |
name = "hub_stats"
|
| 87 |
description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|