voidKaustubh commited on
Commit
b8a1072
·
verified ·
1 Parent(s): 926c024

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -61
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 get_trending_topics(query: 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
- query: 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:
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, get_trending_topics, get_current_time_in_timezone,], ## add your tools here (don't remove 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,