hjerpe commited on
Commit
691a653
·
1 Parent(s): 2b9baa7

WIP: Add latest trend functionality

Browse files
Files changed (1) hide show
  1. app.py +49 -8
app.py CHANGED
@@ -1,22 +1,63 @@
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
 
8
  from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
 
 
11
  @tool
12
- def parse_search_results(query_results: str)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """This tool parses the search results for a given search query result. This tool is suggested to
15
- be used after the DuckDuckGoSearch tool.
16
  Args:
17
- query_results: A string representing the search results.
 
 
 
 
18
  """
19
- return query_results.lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,7 +96,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer, parse_search_results, DuckDuckGoSearchTool()], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
+ from typing import List
7
  from tools.final_answer import FinalAnswerTool
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
+ import re
13
+ from typing import Dict
14
+
15
  @tool
16
+ def parse_search_results(query_results: str) -> Dict[str, str]:
17
+ """
18
+ Parses search results text and extracts URLs along with their corresponding description.
19
+
20
  Args:
21
+ query_results: A string representing the search results. Each search result is expected to start with a URL,
22
+ followed by one or more lines of description until the next URL is encountered.
23
+
24
+ Returns:
25
+ A dictionary where each key is a URL and the corresponding value is the description text.
26
  """
27
+ results: Dict[str, str] = {}
28
+ current_url = None
29
+ current_desc = []
30
+
31
+ # Split the input into lines
32
+ lines = query_results.splitlines()
33
+
34
+ # Regex pattern to match a URL (starting with http:// or https://)
35
+ url_pattern = re.compile(r"^(https?://\S+)")
36
+
37
+ for line in lines:
38
+ stripped_line = line.strip()
39
+ if not stripped_line:
40
+ continue # Skip empty lines
41
+
42
+ # Check if the line starts with a URL
43
+ url_match = url_pattern.match(stripped_line)
44
+ if url_match:
45
+ # Save the previous URL and its description if available
46
+ if current_url is not None:
47
+ results[current_url] = " ".join(current_desc).strip()
48
+ # Set the new URL and reset description accumulator
49
+ current_url = url_match.group(1)
50
+ current_desc = []
51
+ else:
52
+ # Accumulate description lines
53
+ current_desc.append(stripped_line)
54
+
55
+ # Save the last URL and its description if any
56
+ if current_url is not None:
57
+ results[current_url] = " ".join(current_desc).strip()
58
+
59
+ return results
60
+
61
 
62
  @tool
63
  def get_current_time_in_timezone(timezone: str) -> str:
 
96
 
97
  agent = CodeAgent(
98
  model=model,
99
+ tools=[final_answer, parse_search_results, DuckDuckGoSearchTool(), VisitWebpageTool()], ## add your tools here (don't remove final answer)
100
  max_steps=6,
101
  verbosity_level=1,
102
  grammar=None,