Spaces:
Sleeping
Sleeping
update --modified code
Browse files
app.py
CHANGED
|
@@ -1,30 +1,85 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
-
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from pytrends.request import TrendReq
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
@tool
|
| 13 |
-
def
|
| 14 |
-
|
| 15 |
-
|
| 16 |
Args:
|
| 17 |
-
topic:
|
| 18 |
-
location:
|
|
|
|
| 19 |
"""
|
| 20 |
try:
|
|
|
|
| 21 |
pytrends = TrendReq(hl='en-US', tz=360)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
-
return f"Error fetching information
|
| 28 |
|
| 29 |
@tool
|
| 30 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
| 2 |
import requests
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
from pytrends.request import TrendReq
|
| 7 |
+
from typing import Dict, List, Optional
|
| 8 |
+
import pandas as pd
|
| 9 |
+
from datetime import datetime, timedelta
|
| 10 |
+
import pycountry
|
| 11 |
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
| 14 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 15 |
@tool
|
| 16 |
+
def get_trending_topics(topic: str, location: str = "US", timeframe: str = "today 1-m") -> str:
|
| 17 |
+
"""A tool that fetches trending topics and related queries for a specific topic in a given location.
|
| 18 |
+
|
| 19 |
Args:
|
| 20 |
+
topic: The topic to search for (e.g., 'Artificial Intelligence', 'Climate Change')
|
| 21 |
+
location: Country or region code (e.g., 'US', 'IN', 'GB'). Defaults to 'US'
|
| 22 |
+
timeframe: Time range for the trends (e.g., 'today 1-m', 'today 3-m', 'today 12-m'). Defaults to 'today 1-m'
|
| 23 |
"""
|
| 24 |
try:
|
| 25 |
+
# Initialize pytrends
|
| 26 |
pytrends = TrendReq(hl='en-US', tz=360)
|
| 27 |
+
|
| 28 |
+
# Try to convert full country name to code if provided
|
| 29 |
+
try:
|
| 30 |
+
country = pycountry.countries.search_fuzzy(location)[0]
|
| 31 |
+
location = country.alpha_2
|
| 32 |
+
except:
|
| 33 |
+
# If conversion fails, use the provided location as-is
|
| 34 |
+
pass
|
| 35 |
+
|
| 36 |
+
# Build the payload
|
| 37 |
+
kw_list = [query]
|
| 38 |
+
pytrends.build_payload(
|
| 39 |
+
kw_list,
|
| 40 |
+
cat=0,
|
| 41 |
+
timeframe=timeframe,
|
| 42 |
+
geo=location,
|
| 43 |
+
gprop=''
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Get related queries
|
| 47 |
+
related_queries = pytrends.related_queries()
|
| 48 |
+
|
| 49 |
+
# Get interest over time
|
| 50 |
+
interest_df = pytrends.interest_over_time()
|
| 51 |
+
|
| 52 |
+
# Format the results
|
| 53 |
+
result = f"Trending information for '{query}' in {location}:\n\n"
|
| 54 |
+
|
| 55 |
+
# Add top related queries if available
|
| 56 |
+
if query in related_queries and related_queries[query]['top'] is not None:
|
| 57 |
+
top_queries = related_queries[query]['top'].head(5)
|
| 58 |
+
result += "Top Related Queries:\n"
|
| 59 |
+
for idx, row in top_queries.iterrows():
|
| 60 |
+
result += f"- {row['query']} (Score: {row['value']})\n"
|
| 61 |
+
|
| 62 |
+
# Add rising queries if available
|
| 63 |
+
if query in related_queries and related_queries[query]['rising'] is not None:
|
| 64 |
+
rising_queries = related_queries[query]['rising'].head(5)
|
| 65 |
+
result += "\nRising Queries:\n"
|
| 66 |
+
for idx, row in rising_queries.iterrows():
|
| 67 |
+
result += f"- {row['query']} (Score: {row['value']}%)\n"
|
| 68 |
+
|
| 69 |
+
# Add interest over time summary if available
|
| 70 |
+
if not interest_df.empty:
|
| 71 |
+
avg_interest = interest_df[query].mean()
|
| 72 |
+
max_interest = interest_df[query].max()
|
| 73 |
+
max_date = interest_df[query].idxmax().strftime('%Y-%m-%d')
|
| 74 |
+
|
| 75 |
+
result += f"\nInterest Summary:\n"
|
| 76 |
+
result += f"- Average interest: {avg_interest:.1f}/100\n"
|
| 77 |
+
result += f"- Peak interest: {max_interest:.1f}/100 (on {max_date})\n"
|
| 78 |
+
|
| 79 |
+
return result
|
| 80 |
+
|
| 81 |
except Exception as e:
|
| 82 |
+
return f"Error fetching trending information: {str(e)}\nPlease ensure you've provided a valid topic and location."
|
| 83 |
|
| 84 |
@tool
|
| 85 |
def get_current_time_in_timezone(timezone: str) -> str:
|