H commited on
Commit
f3a1380
·
1 Parent(s): 67a5f12

Duckduckgosearch (#1388)

Browse files

### What problem does this PR solve?

#918

Add components: Baidu, Duckduckgo

### Type of change
- [x] New Feature (non-breaking change which adds functionality)

graph/component/__init__.py CHANGED
@@ -10,6 +10,7 @@ from .message import Message, MessageParam
10
  from .rewrite import RewriteQuestion, RewriteQuestionParam
11
  from .keyword import KeywordExtract, KeywordExtractParam
12
  from .baidu import Baidu, BaiduParam
 
13
 
14
 
15
  def component_class(class_name):
 
10
  from .rewrite import RewriteQuestion, RewriteQuestionParam
11
  from .keyword import KeywordExtract, KeywordExtractParam
12
  from .baidu import Baidu, BaiduParam
13
+ from .duckduckgosearch import DuckDuckGoSearch, DuckDuckGoSearchParam
14
 
15
 
16
  def component_class(class_name):
graph/component/baidu.py CHANGED
@@ -50,12 +50,12 @@ class Baidu(ComponentBase, ABC):
50
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'}
51
  response = requests.get(url=url, headers=headers)
52
 
53
- baidu_res = re.findall(r'"contentText":"(.*?)"', response.text)
54
  url_res = re.findall(r"'url': \\\"(.*?)\\\"}", response.text)
55
- for i in range(min(len(baidu_res), len(url_res))):
56
- baidu_res[i] += '<a>' + url_res[i] + '</a>'
57
-
58
- del url_res
 
59
 
60
  br = pd.DataFrame(baidu_res, columns=['content'])
61
  print(">>>>>>>>>>>>>>>>>>>>>>>>>>\n", br)
 
50
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'}
51
  response = requests.get(url=url, headers=headers)
52
 
 
53
  url_res = re.findall(r"'url': \\\"(.*?)\\\"}", response.text)
54
+ title_res = re.findall(r"'title': \\\"(.*?)\\\",\\n", response.text)
55
+ body_res = re.findall(r"\"contentText\":\"(.*?)\"", response.text)
56
+ baidu_res = [re.sub('<em>|</em>', '', '<a href="' + url + '">' + title + '</a> ' + body) for url, title, body
57
+ in zip(url_res, title_res, body_res)]
58
+ del body_res, url_res, title_res
59
 
60
  br = pd.DataFrame(baidu_res, columns=['content'])
61
  print(">>>>>>>>>>>>>>>>>>>>>>>>>>\n", br)
graph/component/duckduckgosearch.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ import random
17
+ from abc import ABC
18
+ from functools import partial
19
+ from duckduckgosearch import DDGS
20
+ import pandas as pd
21
+
22
+ from graph.component.base import ComponentBase, ComponentParamBase
23
+
24
+
25
+ class DuckDuckGoSearchParam(ComponentParamBase):
26
+ """
27
+ Define the DuckDuckGoSearch component parameters.
28
+ """
29
+
30
+ def __init__(self):
31
+ super().__init__()
32
+ self.top_n = 10
33
+ self.channel = "text"
34
+
35
+ def check(self):
36
+ self.check_positive_integer(self.top_n, "Top N")
37
+ self.check_valid_value(self.channel, "Web Search or News", ["text", "news"])
38
+
39
+
40
+ class DuckDuckGoSearch(ComponentBase, ABC):
41
+ component_name = "DuckDuckGoSearch"
42
+
43
+ def _run(self, history, **kwargs):
44
+ ans = self.get_input()
45
+ ans = " - ".join(ans["content"]) if "content" in ans else ""
46
+ if not ans:
47
+ return Baidu.be_output(self._param.no)
48
+
49
+ if self.channel == "text":
50
+ with DDGS() as ddgs:
51
+ # {'title': '', 'href': '', 'body': ''}
52
+ duck_res = ['<a href="' + i["href"] + '">' + i["title"] + '</a> ' + i["body"] for i in
53
+ ddgs.text(ans, max_results=self._param.top_n)]
54
+ elif self.channel == "news":
55
+ with DDGS() as ddgs:
56
+ # {'date': '', 'title': '', 'body': '', 'url': '', 'image': '', 'source': ''}
57
+ duck_res = ['<a href="' + i["url"] + '">' + i["title"] + '</a> ' + i["body"] for i in
58
+ ddgs.news(ans, max_results=self._param.top_n)]
59
+
60
+ dr = pd.DataFrame(duck_res, columns=['content'])
61
+ print(">>>>>>>>>>>>>>>>>>>>>>>>>>\n", dr)
62
+ return dr