File size: 9,288 Bytes
21b78eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import streamlit as st
import streamlit.components.v1 as components
# from PIL import Image
import requests
import pickle as pkl
import re
import pandas as pd
import json
import PyPDF2
import os

from examples import EXAMPLES
from ner_examples import NER_EXAMPLES
from annotated_text import annotated_text
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
import numpy as np

try:
    import StringIO
except ImportError:
    from io import StringIO

def get_topics(url, data):
    r = requests.post(url, json = data)
    print(r.status_code)
    if r.status_code != 200:
        return None
    else:
        return r.json()

def get_ner(url, data):
    r_ner = requests.post(url, json = data)
    print(r_ner.status_code)
    if r_ner.status_code != 200:
        return None
    else:
        return r_ner.json()
# def download_link(
#     content, label="Download", filename="file.txt", mimetype="text/plain"


# a fuction used for word cloud
def plot_wordcloud(list_topic:list) -> None:
    '''
    Plot a wordcloud of top 20 words from the input text
    masked by world logo
    '''
    text = " ".join(word for word in list_topic)
    # Create stopword
    stopwords = set(STOPWORDS)

    # mask = np.array(Image.open('/home/ked/client/img/worldmap1.png')) 
    wordcloud = WordCloud( 
        background_color='white', 
        stopwords=stopwords,
        random_state=42,
        max_words=20, 
        max_font_size=80).generate(text)
    # plt.figure(figsize=(10,10))
    # plt.imshow(wordcloud, interpolation="bilinear")
    # plt.axis("off")
    # plt.show()
    fig, ax = plt.subplots(figsize = (10, 10))
    ax.imshow(wordcloud, interpolation = "bilinear")
    plt.axis("off")
    st.pyplot(fig)



url = 'http://backendtopic:8000/topic/predict'
url_ner = 'http://backendner:9000/ner/predict'


st.set_page_config(page_title="GlgCapstone-Demo", page_icon=":star:", layout="wide")

st.subheader("GLG Topic Modeling and Named Entity Recognation")

tab1, tab2 = st.tabs(["Topic Analysis", "Named Entity Recognition"])

with tab1: 
    with st.expander("ℹ️ About Topic Model", expanded=True):

        st.write(
            """     
        -   Topic model is a type of statistical model for discovering the abstract "topics" that occur in a collection of documents. 
        -   Topic models can help to organize and offer insights for us to understand large collections of unstructured text bodies.
            """
        )

    st.markdown("")

    
# st.set_page_config(layout="wide")

# st.markdown("""
# <style>
# .big-font {
#     font-size:300px !important;
# }
# </style>
# """, unsafe_allow_html=True)

# st.markdown('<p class="big-font">Hello World !!</p>', unsafe_allow_html=True)


    # with st.form(key="my_form"):
    col1, col2, col3 = st.columns([3,1,3])

    with col1:
        prompts = list(EXAMPLES.keys()) + ["Select a document"]
        prompt = st.selectbox(
            'Example Inputs',
            prompts,
            index=2
        )

        if prompt == "Select a document":
            prompt_box = ""
        else:
            prompt_box = EXAMPLES[prompt]


    with col3:

        uploaded_file = col3.file_uploader("Upload pdf document", type=".pdf")
        if uploaded_file:
            # creating a pdf file object
            # pdfFileObj = StringIO(uploaded_file.getvalue().decode("utf-8"))
                
            # creating a pdf reader object
            pdfReader = PyPDF2.PdfFileReader(uploaded_file)
                
            # printing number of pages in pdf file
            print(pdfReader.numPages)
                
            # creating a page object
            pageObj = pdfReader.getPage(0)
            prompt_box = pageObj.extractText()
            # closing the pdf file object
            # pdfFileObj.close()

    doc_txt = st.text_area(
            "Document:",
            prompt_box, height=200
        )
    submit_button = st.button(label="Generate topics")


    # if not doc_txt:
    #     st.stop() # pop up message


    if submit_button:
        if doc_txt != "" and len(doc_txt.split(" ")) > 12:
            with st.spinner("Generating topics..."):
                data = {"document": {"0": doc_txt}}
                topics = get_topics(url, data)

                st.markdown("Model Output")

                tab1_result, tab2_result = st.tabs(["Result Tables", "Result Wordcloud" ])

                st.header("")
                df_global = pd.DataFrame(topics['topics']['0']['global'].items())
                df_global['label'], df_global['topics'] = df_global[1].apply(lambda x: x['labels']), df_global[1].apply(lambda x: x['topics'])
                df_global = df_global.set_index(df_global[0])
                df_global.drop(1, axis=1, inplace=True)

                df_local = pd.DataFrame(topics['topics']['0']['local'].items())
                df_local['label'], df_local['topics'] = df_local[1].apply(lambda x: x['labels']), df_local[1].apply(lambda x: x['topics'])
                df_local = df_local.set_index(df_local[0])
                df_local.drop(1, axis=1, inplace=True)

                with tab1_result:

                    st.header("Global Topics")

                    st.table(df_global)

                    st.header("Local Topics")

                    st.table(df_local)

                with tab2_result:
                    global_topics = df_global['topics'].tolist()
                    global_labels = df_global['label'].tolist()
                    local_topics  = df_local['topics'].tolist()
                    local_labels  = df_local['label'].tolist()
                    global_topic_label = global_topics + global_labels
                    local_topic_label = local_topics + local_labels
                    col4, col_, col5 = st.columns([2,1,2])

                    with col4:
                        st.header("Global Topics as a wordcloud")
                        plot_wordcloud(global_topic_label)


                    with col5:

                        st.header("Local Topics as a wordcloud")
                        plot_wordcloud(local_topic_label)
        else:
            st.warning('Please insert a document', icon="⚠️")
with tab2: 
    with st.expander("ℹ️ Named Entity Recognition", expanded=True):

        st.write(
            """     
            Named Entity Recognition is the task of identifying named entities (people, locations, organizations, etc.) in the input text.

            """
        )

    tab3, tab4 = st.tabs(["Demo", "Model Info"])

    with tab3:
        prompts_ner = list(NER_EXAMPLES.keys()) + ["Select a Sentence"]
        prompt_ner = st.selectbox(
            'Example Document',
            prompts_ner,
            index=3
        )

        if prompt_ner == "Select a Sentence":
            prompt_box = ""
        else:
            prompt_box = NER_EXAMPLES[prompt_ner]

        sent_txt = st.text_area(
        "Sentence:",
        prompt_box, height=100)
        submit_button_ner = st.button(label="Run Model")
        if submit_button_ner:
            if sent_txt != "":
                with st.spinner("Generating entities..."):
                    sent_data = {"sentence": sent_txt}
                    ner_output = get_ner(url_ner, sent_data)

                    st.markdown("Model Output")
                    st.markdown("Entities")
                    tokens_ner = ner_output['ner_tags']['tokens'][1:-1]
                    labels_ner = ner_output['ner_tags']['labels'][1:-1]

                    print(zip(tokens_ner, labels_ner))
                    annotated_list = []
                    ner_entities = ['per','gpe','geo','art','eve','org','tim','nat']

                    for i,token_label in enumerate(zip(tokens_ner, labels_ner)):
                        token, label = token_label[0], token_label[1]
                        if label.lower() not in ['o', 'pad']:
                            tag = label.split("-")
                            if tag[0] == "B":
                                collector = token
                                flag = True
                                j = i+1
                                while flag:
                                    if labels_ner[j].lower() not in ['o', 'pad']:
                                        if labels_ner[j].split("-")[1] != tag[1]:
                                            flag = False
                                        else:
                                            collector = collector + " " +tokens_ner[j]
                                            j += 1
                                    else:
                                        flag = False
                                annotated_list.append((collector, tag[1]))
                        else:
                            annotated_list.append(token+" ")
                    print(annotated_list)
                    # st.write(annotated_list)
                    annotated_text(*annotated_list)
    with tab4:
        data_path = os.getcwd()
        os.path.join(data_path,"data/modelcard.csv")
        df = pd.read_csv(os.path.join(data_path,"data/modelcard.csv"), sep=',')
        # df = df.rename(columns={'0':'','1':''})

        st.table(df)