|
import os |
|
import gradio as gr |
|
|
|
from duckduckgo_search import DDGS |
|
from urllib.request import urlopen |
|
from bs4 import BeautifulSoup |
|
|
|
def fn(text): |
|
with DDGS() as ddgs: |
|
results = [r for r in ddgs.text(text, max_results=5)] |
|
|
|
url = results[0]['href'] |
|
|
|
html = urlopen(url).read() |
|
soup = BeautifulSoup(html, features="html.parser") |
|
|
|
|
|
for script in soup(["script", "style"]): |
|
script.extract() |
|
|
|
|
|
text = soup.get_text() |
|
|
|
|
|
lines = (line.strip() for line in text.splitlines()) |
|
|
|
chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) |
|
|
|
text = '\n'.join(chunk for chunk in chunks if chunk) |
|
|
|
return text, results |
|
|
|
gr.Interface( |
|
fn=fn, |
|
inputs="text", |
|
outputs=["text","text"]).launch() |
|
|