File size: 8,245 Bytes
031e5e2 6d737a4 031e5e2 6d737a4 031e5e2 6d737a4 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 170 171 172 173 174 175 |
# 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.adapmit_classifier import load_adapmitClassifier,adapmit_classification
# from utils.keyword_extraction import textrank
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 = 'adapmit'
params = get_classifier_params(classifier_identifier)
@st.cache_data
def to_excel(df):
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('E2:E{}'.format(len_df),
{'validate': 'list',
'source': ['No', 'Yes', 'Discard']})
worksheet.data_validation('F2:F{}'.format(len_df),
{'validate': 'list',
'source': ['No', 'Yes', 'Discard']})
worksheet.data_validation('G2:G{}'.format(len_df),
{'validate': 'list',
'source': ['No', 'Yes', 'Discard']})
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_adapmitClassifier(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 = adapmit_classification(haystack_doc=df,
threshold= params['threshold'])
st.session_state.key1 = 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, y='category', x='count',
# color='truth_value',orientation='h', height =200)
# 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)
# st.markdown("###### Top few 'Mitigation' related paragraph/text ######")
# df = df.sort_values(by = ['Mitigation'], ascending=False)
# for i in range(3):
# if df.iloc[i]['Mitigation'] >= 0.50:
# st.write('**Result {}** (Relevancy Score: {:.2f})'.format(i+1,df.iloc[i]['Mitigation']))
# st.write("\t Text: \t{}".format(df.iloc[i]['text'].replace("\n", " ")))
# st.markdown("###### Top few 'Adaptation' related paragraph/text ######")
# df = df.sort_values(by = ['Adaptation'], ascending=False)
# for i in range(3):
# if df.iloc[i]['Adaptation'] > 0.5:
# st.write('**Result {}** (Relevancy Score: {:.2f})'.format(i+1,df.iloc[i]['Adaptation']))
# st.write("\t Text: \t{}".format(df.iloc[i]['text'].replace("\n", " ")))
# # st.write(df[['text','labels']])
# df['Validation'] = 'No'
# df['Val-Mitigation'] = 'No'
# df['Val-Adaptation'] = 'No'
# df_xlsx = to_excel(df)
# st.download_button(label='π₯ Download Current Result',
# data=df_xlsx ,
# file_name= 'file_adaptation-mitigation.xlsx')
# # st.session_state.key4 =
# # category =set(df.columns)
# # removecols = {'Validation','Val-Adaptation','Val-Mitigation','text'}
# # category = list(category - removecols)
# 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 'key4' in st.session_state:
# if st.session_state.key4 is not None:
# df = st.session_state.key4
# 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" )
# category =set(df.columns)
# removecols = {'Validation','Val-Adaptation','Val-Mitigation','text'}
# category = list(category - removecols)
# placeholder = {}
# for val in category:
# 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 = ['category','count','truth_value'])
# fig = px.bar(count_df, x='category', y='count',
# color='truth_value',
# height=400)
# st.write("")
# st.plotly_chart(fig)
# df['Validation'] = 'No'
# df['Val-Mitigation'] = 'No'
# df['Val-Adaptation'] = 'No'
# df_xlsx = to_excel(df)
# st.download_button(label='π₯ Download Current Result',
# data=df_xlsx ,
# file_name= 'file_adaptation-mitigation.xlsx')
|