Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,51 +2,68 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import requests
|
|
|
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
#
|
| 8 |
summarizer = pipeline("summarization")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
iface = gr.Interface(
|
| 45 |
-
fn=
|
| 46 |
inputs="text",
|
| 47 |
outputs="text",
|
| 48 |
-
title="
|
| 49 |
-
description="Enter
|
| 50 |
)
|
| 51 |
|
| 52 |
iface.launch()
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import requests
|
| 5 |
+
from googlesearch import search
|
| 6 |
|
| 7 |
+
def summarize_news(query):
|
| 8 |
+
# Initialize summarization pipeline
|
| 9 |
summarizer = pipeline("summarization")
|
| 10 |
|
| 11 |
+
# Search for news articles
|
| 12 |
+
search_results = search(query, num_results=3)
|
| 13 |
+
articles = []
|
| 14 |
+
|
| 15 |
+
for url in search_results:
|
| 16 |
+
try:
|
| 17 |
+
# Fetch the content of the news article
|
| 18 |
+
r = requests.get(url)
|
| 19 |
+
soup = BeautifulSoup(r.text, 'html.parser')
|
| 20 |
+
results = soup.find_all(['h1', 'p'])
|
| 21 |
+
text = [result.text for result in results]
|
| 22 |
+
ARTICLE = ' '.join(text)
|
| 23 |
+
|
| 24 |
+
# Chunk the article text
|
| 25 |
+
max_chunk = 500
|
| 26 |
+
ARTICLE = ARTICLE.replace('.', '.<eos>')
|
| 27 |
+
ARTICLE = ARTICLE.replace('?', '?<eos>')
|
| 28 |
+
ARTICLE = ARTICLE.replace('!', '!<eos>')
|
| 29 |
+
|
| 30 |
+
sentences = ARTICLE.split('<eos>')
|
| 31 |
+
current_chunk = 0
|
| 32 |
+
chunks = []
|
| 33 |
+
for sentence in sentences:
|
| 34 |
+
if len(chunks) == current_chunk + 1:
|
| 35 |
+
if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk:
|
| 36 |
+
chunks[current_chunk].extend(sentence.split(' '))
|
| 37 |
+
else:
|
| 38 |
+
current_chunk += 1
|
| 39 |
+
chunks.append(sentence.split(' '))
|
| 40 |
+
else:
|
| 41 |
+
chunks.append(sentence.split(' '))
|
| 42 |
+
|
| 43 |
+
for chunk_id in range(len(chunks)):
|
| 44 |
+
chunks[chunk_id] = ' '.join(chunks[chunk_id])
|
| 45 |
+
|
| 46 |
+
# Summarize the chunks
|
| 47 |
+
summaries = summarizer(chunks, max_length=120, min_length=30, do_sample=False)
|
| 48 |
+
summary_text = " ".join([summary['summary_text'] for summary in summaries])
|
| 49 |
+
articles.append((url, summary_text))
|
| 50 |
+
except Exception as e:
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
return articles
|
| 54 |
+
|
| 55 |
+
def format_output(articles):
|
| 56 |
+
formatted_text = ""
|
| 57 |
+
for url, summary in articles:
|
| 58 |
+
formatted_text += f"URL: {url}\nSummary: {summary}\n\n"
|
| 59 |
+
return formatted_text
|
| 60 |
|
| 61 |
iface = gr.Interface(
|
| 62 |
+
fn=lambda query: format_output(summarize_news(query)),
|
| 63 |
inputs="text",
|
| 64 |
outputs="text",
|
| 65 |
+
title="News Summarizer",
|
| 66 |
+
description="Enter a query to get summarized versions of the top news articles."
|
| 67 |
)
|
| 68 |
|
| 69 |
iface.launch()
|