Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,45 @@
|
|
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
|
17 |
-
"""A tool that
|
18 |
|
19 |
Args:
|
20 |
-
query: The
|
21 |
-
|
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 |
-
#
|
26 |
-
|
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 |
-
|
50 |
-
|
51 |
|
52 |
# Format the results
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
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 |
-
|
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
|
83 |
-
|
84 |
@tool
|
85 |
def get_current_time_in_timezone(timezone: str) -> str:
|
86 |
"""A tool that fetches the current local time in a specified timezone.
|
@@ -118,7 +77,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
118 |
|
119 |
agent = CodeAgent(
|
120 |
model=model,
|
121 |
-
tools=[final_answer,
|
122 |
max_steps=6,
|
123 |
verbosity_level=1,
|
124 |
grammar=None,
|
|
|
1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
+
from duckduckgo_search import ddg
|
3 |
+
from typing import List
|
4 |
+
import time
|
5 |
import requests
|
6 |
import pytz
|
7 |
import yaml
|
8 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
9 |
import pandas as pd
|
10 |
from datetime import datetime, timedelta
|
|
|
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 |
+
|
16 |
@tool
|
17 |
+
def search_internet(query: str, max_results: int = 3) -> str:
|
18 |
+
"""A tool that searches the internet for information about a given query.
|
19 |
|
20 |
Args:
|
21 |
+
query: The search query (e.g., 'latest AI news', 'climate change updates')
|
22 |
+
max_results: Maximum number of results to return (default: 3)
|
|
|
23 |
"""
|
24 |
try:
|
25 |
+
# Perform the search
|
26 |
+
results = ddg(query, max_results=max_results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
if not results:
|
29 |
+
return f"No results found for '{query}'"
|
30 |
|
31 |
# Format the results
|
32 |
+
formatted_results = f"Search results for '{query}':\n\n"
|
33 |
|
34 |
+
for i, result in enumerate(results, 1):
|
35 |
+
formatted_results += f"{i}. {result['title']}\n"
|
36 |
+
formatted_results += f" {result['link']}\n"
|
37 |
+
formatted_results += f" {result['body']}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
return formatted_results
|
|
|
|
|
|
|
|
|
40 |
|
41 |
except Exception as e:
|
42 |
+
return f"Error performing search: {str(e)}"
|
|
|
43 |
@tool
|
44 |
def get_current_time_in_timezone(timezone: str) -> str:
|
45 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
77 |
|
78 |
agent = CodeAgent(
|
79 |
model=model,
|
80 |
+
tools=[final_answer, search_internet, get_current_time_in_timezone,], ## add your tools here (don't remove final answer)
|
81 |
max_steps=6,
|
82 |
verbosity_level=1,
|
83 |
grammar=None,
|