Gh / app.py
Ashrafb's picture
Update app.py
06ef4fc
import streamlit as st
from sumy.parsers.plaintext import PlaintextParser
from sumy.parsers.html import HtmlParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.nlp.stemmers import Stemmer
from sumy.utils import get_stop_words
import nltk
nltk.download('punkt')
def summarize(method, language, sentence_count, input_type, input_):
if method== 'LSA':
from sumy.summarizers.lsa import LsaSummarizer as Summarizer
if method=='text-rank':
from sumy.summarizers.text_rank import TextRankSummarizer as Summarizer
if method=='lex-rank':
from sumy.summarizers.lex_rank import LexRankSummarizer as Summarizer
if method=='edmundson':
from sumy.summarizers.edmundson import EdmundsonSummarizer as Summarizer
if method=='luhn':
from sumy.summarizers.luhn import LuhnSummarizer as Summarizer
if method=='kl-sum':
from sumy.summarizers.kl import KLSummarizer as Summarizer
if method=='random':
from sumy.summarizers.random import RandomSummarizer as Summarizer
if method=='reduction':
from sumy.summarizers.reduction import ReductionSummarizer as Summarizer
if input_type=="URL":
parser = HtmlParser.from_url(input_, Tokenizer(language))
else:
parser = PlaintextParser.from_string(input_, Tokenizer(language))
stemmer = Stemmer(language)
summarizer = Summarizer(stemmer)
stop_words = get_stop_words(language)
if method=='edmundson':
summarizer.null_words = stop_words
summarizer.bonus_words = parser.significant_words
summarizer.stigma_words = parser.stigma_words
else:
summarizer.stop_words = stop_words
summary_sentences = summarizer(parser.document, sentence_count)
summary = ' '.join([str(sentence) for sentence in summary_sentences])
return summary
title = "AIconvert AI text summarization"
description = """
The summary can be extracted either from url link or plain text. .
"""
methods = ["LSA", "luhn", "edmundson", "text-rank", "lex-rank", "random", "reduction", "kl-sum"]
supported_languages = ["english", "french", "arabic", "chinese", "czech", "german", "italian", "hebrew",
"japanese", "portuguese", "slovak", "spanish", "ukrainian", "greek"]
# Streamlit UI
st.title("AIconvert AI text summarization")
st.markdown('<style>h1{color: Crimson; text-align: center;}</style>', unsafe_allow_html=True)
st.markdown(description)
method = st.selectbox("Select Summarization Method", methods)
language = st.selectbox("Select Language", supported_languages)
sentence_count = st.number_input("Number of Sentences", min_value=1, value=7)
input_type = st.radio("Input Type", ["URL", "Text"])
if input_type == "URL":
input_ = st.text_input("Enter URL")
else:
input_ = st.text_area("Enter Text", height=200)
if st.button("Summarize"):
summary = summarize(method, language, sentence_count, input_type, input_)
st.subheader("Summary")
st.write(summary)