File size: 7,779 Bytes
031e5e2
 
 
 
 
 
 
 
 
 
6d737a4
031e5e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d737a4
 
031e5e2
 
6d737a4
 
031e5e2
 
 
 
6d737a4
031e5e2
 
6d737a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
031e5e2
6d737a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
031e5e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# set path
import glob, os, sys; 
sys.path.append('../utils')

#import needed libraries
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import streamlit as st
from utils.sector_classifier import load_sectorClassifier, sector_classification
import logging
logger = logging.getLogger(__name__)
from utils.config import get_classifier_params
from utils.preprocessing import paraLengthCheck
from io import BytesIO
import xlsxwriter
import plotly.express as px


# Declare all the necessary variables
classifier_identifier = 'sector'
params  = get_classifier_params(classifier_identifier)

@st.cache_data
def to_excel(df,sectorlist):
    len_df = len(df)
    output = BytesIO()
    writer = pd.ExcelWriter(output, engine='xlsxwriter')
    df.to_excel(writer, index=False, sheet_name='Sheet1')
    workbook = writer.book
    worksheet = writer.sheets['Sheet1']
    worksheet.data_validation('S2:S{}'.format(len_df), 
                              {'validate': 'list', 
                               'source': ['No', 'Yes', 'Discard']})
    worksheet.data_validation('X2:X{}'.format(len_df), 
                              {'validate': 'list', 
                               'source': sectorlist + ['Blank']})
    worksheet.data_validation('T2:T{}'.format(len_df), 
                              {'validate': 'list', 
                               'source': sectorlist + ['Blank']})
    worksheet.data_validation('U2:U{}'.format(len_df), 
                              {'validate': 'list', 
                               'source': sectorlist + ['Blank']})                               
    worksheet.data_validation('V2:V{}'.format(len_df), 
                              {'validate': 'list', 
                               'source': sectorlist + ['Blank']})
    worksheet.data_validation('W2:U{}'.format(len_df), 
                              {'validate': 'list', 
                               'source': sectorlist + ['Blank']})                            
    writer.save()
    processed_data = output.getvalue()
    return processed_data

def app():

    ### Main app code ###
    with st.container():
                   
            if 'key1' in st.session_state:
                df = st.session_state.key1
                classifier = load_sectorClassifier(classifier_name=params['model_name'])
                st.session_state['{}_classifier'.format(classifier_identifier)] = classifier

                if sum(df['Target Label'] == 'TARGET') > 100:
                    warning_msg = ": This might take sometime, please sit back and relax."
                else:
                    warning_msg = ""
                    
                df = sector_classification(haystack_doc=df,
                                            threshold= params['threshold'])

                st.session_state.key1 = df


                # # st.write(df)
                # threshold= params['threshold']
                # truth_df = df.drop(['text'],axis=1)
                # truth_df = truth_df.astype(float) >= threshold
                # truth_df = truth_df.astype(str)
                # categories = list(truth_df.columns)

                # placeholder = {}
                # for val in categories:
                #     placeholder[val] = dict(truth_df[val].value_counts())
                # count_df = pd.DataFrame.from_dict(placeholder)
                # count_df = count_df.T
                # count_df = count_df.reset_index()
                # # st.write(count_df)
                # placeholder  = []
                # for i in range(len(count_df)):
                #     placeholder.append([count_df.iloc[i]['index'],count_df['True'][i],'Yes'])
                #     placeholder.append([count_df.iloc[i]['index'],count_df['False'][i],'No'])
                # count_df = pd.DataFrame(placeholder, columns = ['category','count','truth_value'])
                # # st.write("Total Paragraphs: {}".format(len(df)))
                # fig = px.bar(count_df, x='category', y='count',
                #             color='truth_value')
                # # c1, c2 = st.columns([1,1])
                # # with c1:
                # st.plotly_chart(fig,use_container_width= True)

                # truth_df['labels'] = truth_df.apply(lambda x: {i if x[i]=='True' else None for i in categories}, axis=1)
                # truth_df['labels'] = truth_df.apply(lambda x: list(x['labels'] -{None}),axis=1)
                # # st.write(truth_df)
                # df = pd.concat([df,truth_df['labels']],axis=1)
                # df['Validation'] =  'No'
                # df['Sector1'] = 'Blank'
                # df['Sector2'] = 'Blank'
                # df['Sector3'] = 'Blank'
                # df['Sector4'] = 'Blank'
                # df['Sector5'] = 'Blank'
                # df_xlsx = to_excel(df,categories)
                # st.download_button(label='πŸ“₯ Download Current Result',
                #                 data=df_xlsx ,
            #     #               file_name= 'file_sector.xlsx')
            # else:
            #     st.info("πŸ€” No document found, please try to upload it at the sidebar!")
            #     logging.warning("Terminated as no document provided")
        
        # # Creating truth value dataframe
        # if 'key' in st.session_state:
        #     if st.session_state.key is not None:
        #         df = st.session_state.key
        #         st.markdown("###### Select the threshold for classifier ######")
        #         c4, c5 = st.columns([1,1])

        #         with c4:                    
        #             threshold = st.slider("Threshold", min_value=0.00, max_value=1.0,
        #                                   step=0.01, value=0.5,
        #                 help = "Keep High Value if want refined result, low if dont want to miss anything" )
        #         sectors =set(df.columns)
        #         removecols = {'Validation','Sector1','Sector2','Sector3','Sector4',
        #                       'Sector5','text'}
        #         sectors  = list(sectors - removecols)

        #         placeholder = {}
        #         for val in sectors:
        #             temp = df[val].astype(float) > threshold
        #             temp = temp.astype(str)
        #             placeholder[val] = dict(temp.value_counts())
                    
        #         count_df = pd.DataFrame.from_dict(placeholder)
        #         count_df = count_df.T
        #         count_df = count_df.reset_index()
        #         placeholder  = []
        #         for i in range(len(count_df)):
        #             placeholder.append([count_df.iloc[i]['index'],count_df['False'][i],'False'])
        #             placeholder.append([count_df.iloc[i]['index'],count_df['True'][i],'True'])

        #         count_df = pd.DataFrame(placeholder, columns = ['sector','count','truth_value'])
        #         fig = px.bar(count_df, x='sector', y='count',
        #                     color='truth_value',
        #                     height=400)
        #         st.write("")
        #         st.plotly_chart(fig)

                # df['Validation'] =  'No'
                # df['Sector1'] = 'Blank'
                # df['Sector2'] = 'Blank'
                # df['Sector3'] = 'Blank'
                # df['Sector4'] = 'Blank'
                # df['Sector5'] = 'Blank'
                # df_xlsx = to_excel(df,sectors)
                # st.download_button(label='πŸ“₯ Download Current Result',
                #                 data=df_xlsx ,
                #               file_name= 'file_sector.xlsx')