Spaces:
Runtime error
Runtime error
# import | |
from tensorflow.python.keras.utils.generic_utils import default | |
import streamlit as st | |
from newspaper import Article | |
from transformers import pipeline | |
# set config | |
st.set_page_config(layout="wide", page_title="SummarizeLink") | |
# load the summarization model (cache for faster loading) | |
def load_summarize_model(): | |
# model = pipeline("summarization", model='sshleifer/distilbart-cnn-12-6') | |
model = pipeline("summarization") | |
return model | |
# loading the model | |
summ = load_summarize_model() | |
# define the down functions | |
def download_and_parse_article(url): | |
"""Downloads and parses an article from a URL. | |
Parameters | |
---------- | |
url : str | |
The URL of the article to download and parse. | |
Returns | |
------- | |
article : newspaper.Article | |
The article downloaded and parsed. | |
""" | |
# define the article | |
article = Article(url) | |
# download and parse the article | |
article.download() | |
article.parse() | |
# return the article | |
return article.text | |
# APP | |
# set title and subtitle | |
st.title("SummarizeLink") | |
st.markdown("Paste any article link below and click on the 'Summarize' button.") | |
st.markdown("*Note:* We truncate the text incase the article is lengthy! π") | |
# create the input text box and setting panel | |
link = st.text_area('Paste your link here...', "https://towardsdatascience.com/a-guide-to-the-knowledge-graphs-bfb5c40272f1", height=50) | |
button = st.button("Summarize") | |
min_length = st.sidebar.slider('Min summary length', min_value=10, max_value=100, value=50, step=10) | |
max_length = st.sidebar.slider('Max summary length', min_value=30, max_value=700, value=100, step=10) | |
num_beams = st.sidebar.slider('Beam length', min_value=1, max_value=10, value=5, step=1) | |
# if button is clicked | |
with st.spinner("Parsing article and Summarizing..."): | |
if button and link: | |
# get the text | |
text = download_and_parse_article(link) | |
# summarize the text | |
summary = summ(text, | |
truncation=True, | |
max_length = max_length, | |
min_length = min_length, | |
num_beams=num_beams, | |
do_sample=True, | |
early_stopping=True, | |
repetition_penalty=1.5, | |
length_penalty=1.5)[0] | |
# display the summary | |
st.markdown("**Summary:**") | |
st.write(summary['summary_text']) | |