""" Create a text summarization app using stremlit with a GUI """ import streamlit as st import spacy from spacy.lang.en.stop_words import STOP_WORDS from string import punctuation from heapq import nlargest # load the model nlp = spacy.load('en_core_web_sm') # add the stop words stopwords = list(STOP_WORDS) # add punctuation to stop words stopwords = stopwords + list(punctuation) # add words that aren't in the NLTK stopwords list other_exclusions = ["'s", "n't", "'m", "'re", "'ve", "'d", "'ll"] stopwords = stopwords + other_exclusions # function to get the keywords def get_summary(text): doc = nlp(text) tokens = [token.text for token in doc] word_frequencies = {} for word in doc: if word.text.lower() not in stopwords: if word.text.lower() not in word_frequencies.keys(): word_frequencies[word.text] = 1 else: word_frequencies[word.text] += 1 # get the weighted frequencies max_frequency = max(word_frequencies.values()) for word in word_frequencies.keys(): word_frequencies[word] = word_frequencies[word]/max_frequency # get the sentences sentence_tokens = [sent for sent in doc.sents] sentence_scores = {} for sent in sentence_tokens: for word in sent: if word.text.lower() in word_frequencies.keys(): if sent not in sentence_scores.keys(): sentence_scores[sent] = word_frequencies[word.text.lower()] else: sentence_scores[sent] += word_frequencies[word.text.lower()] # get the summary summary_sentences = nlargest(7, sentence_scores, key=sentence_scores.get) final_sentences = [w.text for w in summary_sentences] summary = ' '.join(final_sentences) return summary # main function def main(): st.title('Text Summarizer') st.subheader('Summarize your text') message = st.text_area('Enter your text here', 'Type here') summary_options = st.selectbox('Choose the summarizer', ['Gensim', 'Spacy']) if st.button('Summarize'): if summary_options == 'Gensim': summary_result = get_summary(message) elif summary_options == 'Spacy': summary_result = nlp(message) summary_result = ' '.join([sent.text for sent in summary_result.sents]) else: st.write('Select a summarizer') st.success(summary_result) if __name__ == '__main__': main()