rezzie-rich commited on
Commit
85fad21
·
verified ·
1 Parent(s): f76dc79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -61
app.py CHANGED
@@ -4,27 +4,11 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
- from bs4 import BeautifulSoup
8
- from duckduckgo_search import ddg
9
- import re
10
 
11
  from Gradio_UI import GradioUI
12
 
13
- def generate_summary(text: str, num_sentences: int = 5) -> str:
14
- """
15
- A simple summarization function that extracts the first few sentences from the text.
16
-
17
- Args:
18
- text: The full text to summarize.
19
- num_sentences: The number of sentences to include in the summary.
20
-
21
- Returns:
22
- A summary composed of the first few sentences.
23
- """
24
- # Split text into sentences based on punctuation
25
- sentences = re.split(r'(?<=[.!?])\s+', text)
26
- summary = " ".join(sentences[:num_sentences])
27
- return summary
28
 
29
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
30
  @tool
@@ -37,48 +21,6 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
37
  """
38
  return "What magic will you build ?"
39
 
40
- @tool
41
- def duckduck_tool(query: str, num_results: int = 5) -> str:
42
- """
43
- A tool that uses DuckDuckGo search to retrieve the latest information and webpage content for a given query.
44
-
45
- Args:
46
- query: The search query string.
47
- num_results: The number of search results to return (default is 5).
48
-
49
- Returns:
50
- A string summarizing the latest information and webpage content related to the query.
51
- """
52
- # Perform DuckDuckGo search
53
- results = ddg(query, max_results=num_results)
54
- if not results:
55
- return "No search results found."
56
-
57
- combined_content = ""
58
-
59
- # Iterate over search results to fetch content
60
- for result in results:
61
- url = result.get("href") or result.get("url")
62
- if not url:
63
- continue
64
- try:
65
- response = requests.get(url, timeout=5)
66
- if response.status_code == 200:
67
- soup = BeautifulSoup(response.text, "html.parser")
68
- # Extract text from paragraph tags
69
- paragraphs = soup.find_all("p")
70
- page_text = " ".join(p.get_text() for p in paragraphs)
71
- combined_content += page_text + "\n"
72
- except Exception:
73
- # Skip URLs that cause errors
74
- continue
75
-
76
- if not combined_content.strip():
77
- return "No content could be retrieved from the search results."
78
-
79
- # Generate a summary using our custom summarization function
80
- summary = generate_summary(combined_content, num_sentences=5)
81
- return summary
82
 
83
  @tool
84
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -97,6 +39,8 @@ def get_current_time_in_timezone(timezone: str) -> str:
97
 
98
 
99
  final_answer = FinalAnswerTool()
 
 
100
 
101
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
102
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
@@ -117,7 +61,7 @@ with open("prompts.yaml", 'r') as stream:
117
 
118
  agent = CodeAgent(
119
  model=model,
120
- tools=[final_answer, duckduck_tool], ## add your tools here (don't remove final answer)
121
  max_steps=6,
122
  verbosity_level=1,
123
  grammar=None,
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from tools.visit_webpage import VisitWebpageTool
8
+ from tools.web_search import DuckDuckGoSearchTool
 
9
 
10
  from Gradio_UI import GradioUI
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
14
  @tool
 
21
  """
22
  return "What magic will you build ?"
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  @tool
26
  def get_current_time_in_timezone(timezone: str) -> str:
 
39
 
40
 
41
  final_answer = FinalAnswerTool()
42
+ visit_webpage = VisitWebpageTool()
43
+ web_search = DuckDuckGoSearchTool()
44
 
45
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
46
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
61
 
62
  agent = CodeAgent(
63
  model=model,
64
+ tools=[final_answer, visit_webpage, web_search ], ## add your tools here (don't remove final answer)
65
  max_steps=6,
66
  verbosity_level=1,
67
  grammar=None,