leavoigt commited on
Commit
1fc7d6a
1 Parent(s): b90fe6b

Update appStore/target.py

Browse files
Files changed (1) hide show
  1. appStore/target.py +221 -9
appStore/target.py CHANGED
@@ -8,19 +8,231 @@ import matplotlib.pyplot as plt
8
  import numpy as np
9
  import pandas as pd
10
  import streamlit as st
11
- from utils.target_classifier import load_targetClassifier, target_classification
 
 
12
  from utils.config import get_classifier_params
 
 
 
 
 
13
 
14
  # Declare all the necessary variables
15
- classifier_identifier = 'target'
16
  params = get_classifier_params(classifier_identifier)
17
 
18
- ## Labels dictionary ###
19
- _lab_dict = {
20
- 'NEGATIVE':'NO TARGET INFO',
21
- 'TARGET':'TARGET',
22
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Download model from HF Hub
26
- vg_model = SetFitModel.from_pretrained("leavoigt/vulnerable_groups")
 
8
  import numpy as np
9
  import pandas as pd
10
  import streamlit as st
11
+ from utils.group_classifier import load_policyactionClassifier, policyaction_classification
12
+ import logging
13
+ logger = logging.getLogger(__name__)
14
  from utils.config import get_classifier_params
15
+ from utils.preprocessing import paraLengthCheck
16
+ from io import BytesIO
17
+ import xlsxwriter
18
+ import plotly.express as px
19
+
20
 
21
  # Declare all the necessary variables
22
+ classifier_identifier = 'policyaction'
23
  params = get_classifier_params(classifier_identifier)
24
 
25
+ @st.cache_data
26
+ def to_excel(df):
27
+ df['Target Validation'] = 'No'
28
+ df['Netzero Validation'] = 'No'
29
+ df['GHG Validation'] = 'No'
30
+ df['Adapt-Mitig Validation'] = 'No'
31
+ df['Sector'] = 'No'
32
+ len_df = len(df)
33
+ output = BytesIO()
34
+ writer = pd.ExcelWriter(output, engine='xlsxwriter')
35
+ df.to_excel(writer, index=False, sheet_name='Sheet1')
36
+ workbook = writer.book
37
+ worksheet = writer.sheets['Sheet1']
38
+ worksheet.data_validation('L2:L{}'.format(len_df),
39
+ {'validate': 'list',
40
+ 'source': ['No', 'Yes', 'Discard']})
41
+ worksheet.data_validation('M2:L{}'.format(len_df),
42
+ {'validate': 'list',
43
+ 'source': ['No', 'Yes', 'Discard']})
44
+ worksheet.data_validation('N2:L{}'.format(len_df),
45
+ {'validate': 'list',
46
+ 'source': ['No', 'Yes', 'Discard']})
47
+ worksheet.data_validation('O2:L{}'.format(len_df),
48
+ {'validate': 'list',
49
+ 'source': ['No', 'Yes', 'Discard']})
50
+ worksheet.data_validation('P2:L{}'.format(len_df),
51
+ {'validate': 'list',
52
+ 'source': ['No', 'Yes', 'Discard']})
53
+ writer.save()
54
+ processed_data = output.getvalue()
55
+ return processed_data
56
+
57
+ def app():
58
+
59
+ ### Main app code ###
60
+ with st.container():
61
+
62
+ if 'key1' in st.session_state:
63
+ df = st.session_state.key1
64
+ classifier = load_policyactionClassifier(classifier_name=params['model_name'])
65
+ st.session_state['{}_classifier'.format(classifier_identifier)] = classifier
66
+
67
+ if sum(df['Target Label'] == 'TARGET') > 100:
68
+ warning_msg = ": This might take sometime, please sit back and relax."
69
+ else:
70
+ warning_msg = ""
71
+
72
+ df = policyaction_classification(haystack_doc=df,
73
+ threshold= params['threshold'])
74
+
75
+ st.session_state.key1 = df
76
+
77
+
78
+
79
+ def action_display():
80
+ if 'key1' in st.session_state:
81
+ df = st.session_state.key1
82
+
83
+
84
+ df['Action_check'] = df['Policy-Action Label'].apply(lambda x: True if 'Action' in x else False)
85
+ hits = df[df['Action_check'] == True]
86
+ # hits['GHG Label'] = hits['GHG Label'].apply(lambda i: _lab_dict[i])
87
+ range_val = min(5,len(hits))
88
+ if range_val !=0:
89
+ count_action = len(hits)
90
+ #count_netzero = sum(hits['Netzero Label'] == 'NETZERO')
91
+ #count_ghg = sum(hits['GHG Label'] == 'GHG')
92
+ #count_economy = sum([True if 'Economy-wide' in x else False
93
+ # for x in hits['Sector Label']])
94
+
95
+ # count_df = df['Target Label'].value_counts()
96
+ # count_df = count_df.rename('count')
97
+ # count_df = count_df.rename_axis('Target Label').reset_index()
98
+ # count_df['Label_def'] = count_df['Target Label'].apply(lambda x: _lab_dict[x])
99
+
100
+ # fig = px.bar(count_df, y="Label_def", x="count", orientation='h', height=200)
101
+ # c1, c2 = st.columns([1,1])
102
+ # with c1:
103
+ # st.write('**Target Paragraphs**: `{}`'.format(count_target))
104
+ # st.write('**NetZero Related Paragraphs**: `{}`'.format(count_netzero))
105
+ #
106
+ # # st.plotly_chart(fig,use_container_width= True)
107
+ #
108
+ # count_netzero = sum(hits['Netzero Label'] == 'NETZERO')
109
+ # count_ghg = sum(hits['GHG Label'] == 'LABEL_2')
110
+ # count_economy = sum([True if 'Economy-wide' in x else False
111
+ # for x in hits['Sector Label']])
112
+ # with c2:
113
+ # st.write('**GHG Related Paragraphs**: `{}`'.format(count_ghg))
114
+ # st.write('**Economy-wide Related Paragraphs**: `{}`'.format(count_economy))
115
+ # st.write('-------------------')
116
+ # hits = hits.sort_values(by=['Relevancy'], ascending=False)
117
+ # netzerohit = hits[hits['Netzero Label'] == 'NETZERO']
118
+ # if not netzerohit.empty:
119
+ # netzerohit = netzerohit.sort_values(by = ['Netzero Score'], ascending = False)
120
+ # # st.write('-------------------')
121
+ # st.markdown("###### Netzero paragraph ######")
122
+ # st.write('**Netzero paragraph** `page {}`: {}'.format(netzerohit.iloc[0]['page'],
123
+ # netzerohit.iloc[0]['text'].replace("\n", " ")))
124
+ # st.write("")
125
+ # else:
126
+ # st.info("🤔 No Netzero paragraph found")
127
+
128
+ # st.write("**Result {}** `page {}` (Relevancy Score: {:.2f})'".format(i+1,hits.iloc[i]['page'],hits.iloc[i]['Relevancy'])")
129
+ # st.write('-------------------')
130
+ st.write("")
131
+ st.markdown("###### Top few Action Classified paragraph/text results from list of {} classified paragraphs ######".format(count_action))
132
+ st.markdown("""<hr style="height:10px;border:none;color:#097969;background-color:#097969;" /> """, unsafe_allow_html=True)
133
+ range_val = min(5,len(hits))
134
+ for i in range(range_val):
135
+ # the page number reflects the page that contains the main paragraph
136
+ # according to split limit, the overlapping part can be on a separate page
137
+ st.write('**Result {}** : `page {}`, `Sector: {}`,\
138
+ `Indicators: {}`, `Adapt-Mitig :{}`'\
139
+ .format(i+1,
140
+ hits.iloc[i]['page'], hits.iloc[i]['Sector Label'],
141
+ hits.iloc[i]['Indicator Label'],hits.iloc[i]['Adapt-Mitig Label']))
142
+ st.write("\t Text: \t{}".format(hits.iloc[i]['text'].replace("\n", " ")))
143
+ hits = hits.reset_index(drop =True)
144
+ st.write('----------------')
145
+ st.write('Explore the data')
146
+ st.write(hits)
147
+ df.drop(columns = ['Action_check'],inplace=True)
148
+ df_xlsx = to_excel(df)
149
+
150
+ with st.sidebar:
151
+ st.write('-------------')
152
+ st.download_button(label='📥 Download Result',
153
+ data=df_xlsx ,
154
+ file_name= 'cpu_analysis.xlsx')
155
+
156
+ else:
157
+ st.info("🤔 No Actions found")
158
+
159
+
160
+ def policy_display():
161
+ if 'key1' in st.session_state:
162
+ df = st.session_state.key1
163
+
164
+
165
+ df['Policy_check'] = df['Policy-Action Label'].apply(lambda x: True if 'Policies & Plans' in x else False)
166
+ hits = df[df['Policy_check'] == True]
167
+ # hits['GHG Label'] = hits['GHG Label'].apply(lambda i: _lab_dict[i])
168
+ range_val = min(5,len(hits))
169
+ if range_val !=0:
170
+ count_policy = len(hits)
171
+ #count_netzero = sum(hits['Netzero Label'] == 'NETZERO')
172
+ #count_ghg = sum(hits['GHG Label'] == 'GHG')
173
+ #count_economy = sum([True if 'Economy-wide' in x else False
174
+ # for x in hits['Sector Label']])
175
+
176
+ # count_df = df['Target Label'].value_counts()
177
+ # count_df = count_df.rename('count')
178
+ # count_df = count_df.rename_axis('Target Label').reset_index()
179
+ # count_df['Label_def'] = count_df['Target Label'].apply(lambda x: _lab_dict[x])
180
+
181
+ # fig = px.bar(count_df, y="Label_def", x="count", orientation='h', height=200)
182
+ # c1, c2 = st.columns([1,1])
183
+ # with c1:
184
+ # st.write('**Target Paragraphs**: `{}`'.format(count_target))
185
+ # st.write('**NetZero Related Paragraphs**: `{}`'.format(count_netzero))
186
+ #
187
+ # # st.plotly_chart(fig,use_container_width= True)
188
+ #
189
+ # count_netzero = sum(hits['Netzero Label'] == 'NETZERO')
190
+ # count_ghg = sum(hits['GHG Label'] == 'LABEL_2')
191
+ # count_economy = sum([True if 'Economy-wide' in x else False
192
+ # for x in hits['Sector Label']])
193
+ # with c2:
194
+ # st.write('**GHG Related Paragraphs**: `{}`'.format(count_ghg))
195
+ # st.write('**Economy-wide Related Paragraphs**: `{}`'.format(count_economy))
196
+ # st.write('-------------------')
197
+ # hits = hits.sort_values(by=['Relevancy'], ascending=False)
198
+ # netzerohit = hits[hits['Netzero Label'] == 'NETZERO']
199
+ # if not netzerohit.empty:
200
+ # netzerohit = netzerohit.sort_values(by = ['Netzero Score'], ascending = False)
201
+ # # st.write('-------------------')
202
+ # st.markdown("###### Netzero paragraph ######")
203
+ # st.write('**Netzero paragraph** `page {}`: {}'.format(netzerohit.iloc[0]['page'],
204
+ # netzerohit.iloc[0]['text'].replace("\n", " ")))
205
+ # st.write("")
206
+ # else:
207
+ # st.info("🤔 No Netzero paragraph found")
208
 
209
+ # st.write("**Result {}** `page {}` (Relevancy Score: {:.2f})'".format(i+1,hits.iloc[i]['page'],hits.iloc[i]['Relevancy'])")
210
+ # st.write('-------------------')
211
+ st.write("")
212
+ st.markdown("###### Top few Policy/Plans Classified paragraph/text results from list of {} classified paragraphs ######".format(count_policy))
213
+ st.markdown("""<hr style="height:10px;border:none;color:#097969;background-color:#097969;" /> """, unsafe_allow_html=True)
214
+ range_val = min(5,len(hits))
215
+ for i in range(range_val):
216
+ # the page number reflects the page that contains the main paragraph
217
+ # according to split limit, the overlapping part can be on a separate page
218
+ st.write('**Result {}** : `page {}`, `Sector: {}`,\
219
+ `Indicators: {}`, `Adapt-Mitig :{}`'\
220
+ .format(i+1,
221
+ hits.iloc[i]['page'], hits.iloc[i]['Sector Label'],
222
+ hits.iloc[i]['Indicator Label'],hits.iloc[i]['Adapt-Mitig Label']))
223
+ st.write("\t Text: \t{}".format(hits.iloc[i]['text'].replace("\n", " ")))
224
+ hits = hits.reset_index(drop =True)
225
+ st.write('----------------')
226
+ st.write('Explore the data')
227
+ st.write(hits)
228
+ df.drop(columns = ['Policy_check'],inplace=True)
229
+ df_xlsx = to_excel(df)
230
+
231
+ with st.sidebar:
232
+ st.write('-------------')
233
+ st.download_button(label='📥 Download Result',
234
+ data=df_xlsx ,
235
+ file_name= 'cpu_analysis.xlsx')
236
 
237
+ else:
238
+ st.info("🤔 No Policy/Plans found")