|
"""Summary tab rendering functionality""" |
|
|
|
from config import app_config |
|
import utils |
|
import sys |
|
import streamlit as st |
|
from sumy.parsers.plaintext import PlaintextParser |
|
from sumy.nlp.tokenizers import Tokenizer |
|
from sumy.evaluation import rouge_n |
|
|
|
|
|
|
|
|
|
|
|
def __get_summarizer(summarizer_type): |
|
"""Helper to get summarizer object given its name as string""" |
|
summarizer_dict = app_config.summarizers.get(summarizer_type) |
|
module = sys.modules[summarizer_dict["module"]] |
|
summarizer = utils.get_class_from_name(module, summarizer_type) |
|
desc = summarizer_dict["desc"] |
|
return summarizer(), desc |
|
|
|
|
|
def __summarize(text, summarizer, n_sentences): |
|
|
|
parser = PlaintextParser.from_string(text, Tokenizer("english")) |
|
summary_tuple = summarizer(parser.document, n_sentences) |
|
summary_text = "" |
|
for sentence in summary_tuple: |
|
summary_text += str(sentence) |
|
|
|
rouge = rouge_n( |
|
evaluated_sentences=summary_tuple, |
|
reference_sentences=parser.document.sentences, |
|
n=2, |
|
) |
|
stats = f""" |
|
Number of sentences in original text: **{len(parser.document.sentences)}** |
|
Number of sentences in summary text: **{len(summary_tuple)}** |
|
ROUGE (bi-gram) score: **{rouge}** |
|
""" |
|
return summary_text, stats |
|
|
|
|
|
def __section(header): |
|
"""Build page UI elements""" |
|
st.header(header) |
|
st.write( |
|
"Choose the `Summarization Method`, `Enter Text` in the text " |
|
+ "area, choose the `Number Of Sentences` required in summary text " |
|
+ "and then click `Summarize`" |
|
) |
|
summarizer_type = st.radio( |
|
"Summarization Method:", |
|
options=[ |
|
|
|
"TextRankSummarizer", |
|
"LexRankSummarizer", |
|
"LsaSummarizer", |
|
], |
|
) |
|
|
|
summarizer, desc = __get_summarizer(summarizer_type) |
|
st.info(body=f"{desc}", icon=app_config.icon_info) |
|
text = st.text_area("Enter text:", height=300, key="summarization") |
|
n_sentences = st.slider( |
|
label="Number Of Sentences", min_value=1, max_value=10, value=3 |
|
) |
|
|
|
if st.button("Summarize"): |
|
summary, stats = __summarize(text, summarizer, n_sentences) |
|
st.divider() |
|
st.subheader("Summary") |
|
st.success(stats) |
|
st.write(summary) |
|
st.divider() |
|
|
|
|
|
|
|
|
|
|
|
def render(): |
|
__section("Text Summarization") |
|
|