File size: 4,125 Bytes
06b4325
1d4fc05
 
06b4325
 
1d4fc05
06b4325
 
 
1d4fc05
06b4325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# external libraries
import streamlit as st
from transformers import pipeline
    
import pandas as pd

# internal libraries
from config import config
import pipeline


def main():

    st.set_page_config(
        layout="centered",  # Can be "centered" or "wide". In the future also "dashboard", etc.
        initial_sidebar_state="auto",  # Can be "auto", "expanded", "collapsed"
        page_title=config.main_title,  # String or None. Strings get appended with "• Streamlit". 
        page_icon=config.logo_path,  # String, anything supported by st.image, or None.
    )

    if "output" not in st.session_state:
        st.session_state['data'] = pd.read_csv(config.sample_texts_path)
        st.session_state['sample_text'] = None
        generate_text()
        st.session_state["output"] = False
        st.session_state["output_text"] = ""
        st.session_state['inputs'] = {}

    col1, col2, col3 = st.columns(3)
    col1.write(' ')
    col2.image(config.logo_path)
    col3.write(' ')

    st.markdown(f"<h1 style='text-align: center;'>{config.main_title}</h1>", unsafe_allow_html=True)
    st.markdown(f"<h3 style='text-align: center;'>{config.lecture_title}</h3>", unsafe_allow_html=True)

    # topic modelling radio bar
    input_topic_modelling = st.radio(
                        config.topic_modelling_title,
                        config.topic_modelling_answers,
                        horizontal=True)
    st.session_state['inputs']['input_topic_modelling'] = input_topic_modelling

    # input text area
    input_text = st.text_area(config.input_text, st.session_state['sample_text'], height=300)
    st.session_state['inputs']['input_text'] = input_text

    # generate sample text button
    st.button(config.button_text, on_click=generate_text)
    
    # choosing segmenter radio bar
    input_segmenter = st.radio(
                        config.segmenter_title,
                        config.segmenter_answers,
                        horizontal=True)
    st.session_state['inputs']['input_segmenter'] = input_segmenter
    
    # choosing summarizer algorithm radio bar
    input_summarizer = st.radio(
                        config.summarizer_title,
                        config.summarizer_answers,
                        horizontal=True)
    st.session_state['inputs']['input_summarizer'] = input_summarizer
    
    # generating summary button
    col1, col2, col3 = st.columns(3)
    col1.header(' ')
    col2.button(config.generate_text, on_click=generate_summary)
    col3.header(' ')

    if st.session_state["output"]:
        
        TOPICS = [key for key, value in st.session_state["output_text"].items() if key != '#']

        if config.filter_threshold_summaries:
            TOPICS = [key for key in TOPICS if st.session_state["output_text"][key]['summary'] != config.threshold_error]

        st.write(config.output_title)
        options = {}
        for topic in TOPICS:
            option = st.checkbox(topic)
            options[topic] = option

        if len(options) == 0:
            st.warning(config.warning_len_input_text, icon="⚠️")

        for topic, option in options.items():
            if option == True:
                st.text_area(topic, 
                            st.session_state["output_text"][topic]['summary'],
                            disabled=True)

def generate_text():
    df = st.session_state['data']
    df = df[~df['data'].isnull()]
    df = df[df['data'].str.len().gt(100)]
    st.session_state['sample_text'] = df.sample(1)['data'].values[0]

def generate_summary():
    st.session_state["output"] = True

    MODELS = {
        'summarizer':st.session_state['inputs']['input_summarizer'],
        'topic_modelling':st.session_state['inputs']['input_topic_modelling'],
        'segmentizer':st.session_state['inputs']['input_segmenter']
    }

    with st.spinner('Generating the output of Topic Modeling for Summarization...'):
            OUTPUT = pipeline.run(st.session_state['inputs']['input_text'], MODELS)

    st.session_state["output_text"] = OUTPUT
    st.success('Done!')


if __name__ == "__main__":
    main()