|
|
|
import glob, os, sys; |
|
sys.path.append('../utils') |
|
|
|
|
|
import seaborn as sns |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
import pandas as pd |
|
import streamlit as st |
|
from utils.policyaction_classifier import load_policyactionClassifier, policyaction_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 |
|
|
|
|
|
|
|
classifier_identifier = 'policyaction' |
|
params = get_classifier_params(classifier_identifier) |
|
|
|
@st.cache_data |
|
def to_excel(df): |
|
df['Target Validation'] = 'No' |
|
df['Netzero Validation'] = 'No' |
|
df['GHG Validation'] = 'No' |
|
df['Adapt-Mitig Validation'] = 'No' |
|
df['Sector'] = 'No' |
|
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('L2:L{}'.format(len_df), |
|
{'validate': 'list', |
|
'source': ['No', 'Yes', 'Discard']}) |
|
worksheet.data_validation('M2:L{}'.format(len_df), |
|
{'validate': 'list', |
|
'source': ['No', 'Yes', 'Discard']}) |
|
worksheet.data_validation('N2:L{}'.format(len_df), |
|
{'validate': 'list', |
|
'source': ['No', 'Yes', 'Discard']}) |
|
worksheet.data_validation('O2:L{}'.format(len_df), |
|
{'validate': 'list', |
|
'source': ['No', 'Yes', 'Discard']}) |
|
worksheet.data_validation('P2:L{}'.format(len_df), |
|
{'validate': 'list', |
|
'source': ['No', 'Yes', 'Discard']}) |
|
writer.save() |
|
processed_data = output.getvalue() |
|
return processed_data |
|
|
|
def app(): |
|
|
|
|
|
with st.container(): |
|
|
|
if 'key1' in st.session_state: |
|
df = st.session_state.key1 |
|
classifier = load_policyactionClassifier(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 = policyaction_classification(haystack_doc=df, |
|
threshold= params['threshold']) |
|
|
|
st.session_state.key1 = df |
|
|
|
|
|
|
|
def action_display(): |
|
if 'key1' in st.session_state: |
|
df = st.session_state.key1 |
|
|
|
|
|
df['Action_check'] = df['Policy-Action Label'].apply(lambda x: True if 'Action' in x else False) |
|
hits = df[df['Action_check'] == True] |
|
|
|
range_val = min(5,len(hits)) |
|
if range_val !=0: |
|
count_action = len(hits) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.write("") |
|
st.markdown("###### Top few Action Classified paragraph/text results from list of {} classified paragraphs ######".format(count_action)) |
|
st.markdown("""<hr style="height:10px;border:none;color:#097969;background-color:#097969;" /> """, unsafe_allow_html=True) |
|
range_val = min(5,len(hits)) |
|
for i in range(range_val): |
|
|
|
|
|
st.write('**Result {}** : `page {}`, `Sector: {}`,\ |
|
`Indicators: {}`, `Adapt-Mitig :{}`'\ |
|
.format(i+1, |
|
hits.iloc[i]['page'], hits.iloc[i]['Sector Label'], |
|
hits.iloc[i]['Indicator Label'],hits.iloc[i]['Adapt-Mitig Label'])) |
|
st.write("\t Text: \t{}".format(hits.iloc[i]['text'].replace("\n", " "))) |
|
hits = hits.reset_index(drop =True) |
|
st.write('----------------') |
|
st.write('Explore the data') |
|
st.write(hits) |
|
df.drop(columns = ['Action_check'],inplace=True) |
|
df_xlsx = to_excel(df) |
|
|
|
with st.sidebar: |
|
st.write('-------------') |
|
st.download_button(label='π₯ Download Result', |
|
data=df_xlsx , |
|
file_name= 'cpu_analysis.xlsx') |
|
|
|
else: |
|
st.info("π€ No Actions found") |
|
|
|
|
|
def policy_display(): |
|
if 'key1' in st.session_state: |
|
df = st.session_state.key1 |
|
|
|
|
|
df['Policy_check'] = df['Policy-Action Label'].apply(lambda x: True if 'Policies & Plans' in x else False) |
|
hits = df[df['Policy_check'] == True] |
|
|
|
range_val = min(5,len(hits)) |
|
if range_val !=0: |
|
count_policy = len(hits) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.write("") |
|
st.markdown("###### Top few Policy/Plans Classified paragraph/text results from list of {} classified paragraphs ######".format(count_policy)) |
|
st.markdown("""<hr style="height:10px;border:none;color:#097969;background-color:#097969;" /> """, unsafe_allow_html=True) |
|
range_val = min(5,len(hits)) |
|
for i in range(range_val): |
|
|
|
|
|
st.write('**Result {}** : `page {}`, `Sector: {}`,\ |
|
`Indicators: {}`, `Adapt-Mitig :{}`'\ |
|
.format(i+1, |
|
hits.iloc[i]['page'], hits.iloc[i]['Sector Label'], |
|
hits.iloc[i]['Indicator Label'],hits.iloc[i]['Adapt-Mitig Label'])) |
|
st.write("\t Text: \t{}".format(hits.iloc[i]['text'].replace("\n", " "))) |
|
hits = hits.reset_index(drop =True) |
|
st.write('----------------') |
|
st.write('Explore the data') |
|
st.write(hits) |
|
df.drop(columns = ['Policy_check'],inplace=True) |
|
df_xlsx = to_excel(df) |
|
|
|
with st.sidebar: |
|
st.write('-------------') |
|
st.download_button(label='π₯ Download Result', |
|
data=df_xlsx , |
|
file_name= 'cpu_analysis.xlsx') |
|
|
|
else: |
|
st.info("π€ No Policy/Plans found") |