leavoigt/vulnerability
2
1import streamlit as st2import os3import pkg_resources4 5# Using this wacky hack to get around the massively ridicolous managed env loading order6def is_installed(package_name, version):7 try:8 pkg = pkg_resources.get_distribution(package_name)9 return pkg.version == version10 except pkg_resources.DistributionNotFound:11 return False12 13# shifted from below - this must be the first streamlit call; otherwise: problems14st.set_page_config(page_title = 'Vulnerability Analysis', 15 initial_sidebar_state='expanded', layout="wide") 16 17@st.cache_resource # cache the function so it's not called every time app.py is triggered18def install_packages():19 install_commands = []20 21 if not is_installed("spaces", "0.12.0"):22 install_commands.append("pip install spaces==0.17.0")23 24 if not is_installed("pydantic", "1.8.2"):25 install_commands.append("pip install pydantic==1.8.2")26 27 if not is_installed("typer", "0.4.0"):28 install_commands.append("pip install typer==0.4.0")29 30 if install_commands:31 os.system(" && ".join(install_commands))32 33# install packages if necessary34install_packages()35 36import appStore.vulnerability_analysis as vulnerability_analysis37import appStore.target as target_analysis38import appStore.doc_processing as processing39from utils.uploadAndExample import add_upload40from utils.vulnerability_classifier import label_dict41import pandas as pd42import plotly.express as px43 44#st.set_page_config(page_title = 'Vulnerability Analysis', 45 # initial_sidebar_state='expanded', layout="wide") 46 47with st.sidebar:48 # upload and example doc49 choice = st.sidebar.radio(label = 'Select the Document',50 help = 'You can upload the document \51 or else you can try a example document', 52 options = ('Upload Document', 'Try Example'), 53 horizontal = True)54 add_upload(choice) 55 56with st.container():57 st.markdown("<h2 style='text-align: center; color: black;'> Vulnerability Analysis 2.0 </h2>", unsafe_allow_html=True)58 st.write(' ')59 60with st.expander("ℹ️ - About this app", expanded=False):61 st.write(62 """63 The Vulnerability Analysis App is an open-source\64 digital tool which aims to assist policy analysts and \65 other users in extracting and filtering references \66 to different groups in vulnerable situations from public documents. \67 We use Natural Language Processing (NLP), specifically deep \68 learning-based text representations to search context-sensitively \69 for mentions of the special needs of groups in vulnerable situations 70 to cluster them thematically. 71 """)72 73 st.write("""74 What Happens in background?75 76 - Step 1: Once the document is provided to app, it undergoes *Pre-processing*.\77 In this step the document is broken into smaller paragraphs \78 (based on word/sentence count).79 - Step 2: The paragraphs are then fed to the **Vulnerability Classifier** which detects if80 the paragraph contains any or multiple references to vulnerable groups.81 """)82 83 st.write("")84 85 86# Define the apps used87apps = [processing.app, vulnerability_analysis.app, target_analysis.app]88 89multiplier_val =1/len(apps)90if st.button("Analyze Document"):91 prg = st.progress(0.0)92 for i,func in enumerate(apps):93 func()94 prg.progress((i+1)*multiplier_val)95 96# If there is data stored97if 'key0' in st.session_state:98 99 vulnerability_analysis.vulnerability_display()100 target_analysis.target_display()101 102 103 # ###################################################################104 105 # #with st.sidebar:106 # # topic = st.radio(107 # # "Which category you want to explore?",108 # # (['Vulnerability', 'Concrete targets/actions/measures']))109 110 # #if topic == 'Vulnerability':111 112 # # Assign dataframe a name113 # df_vul = st.session_state['key0']114 # st.write(df_vul)115 116 # col1, col2 = st.columns([1,1])117 118 # with col1:119 120 # # Header121 # st.subheader("Explore references to vulnerable groups:")122 123 # # Text 124 # num_paragraphs = len(df_vul['Vulnerability Label'])125 # num_references = df_vul['Vulnerability Label'].apply(lambda x: 'Other' not in x).sum()126 127 # st.markdown(f"""<div style="text-align: justify;"> The document contains a128 # total of <span style="color: red;">{num_paragraphs}</span> paragraphs.129 # We identified <span style="color: red;">{num_references}</span>130 # references to vulnerable groups.</div>131 # <br>132 # In the pie chart on the right you can see the distribution of the different 133 # groups defined. For a more detailed view in the text, see the paragraphs and 134 # their respective labels in the table below.</div>""", unsafe_allow_html=True)135 136 # with col2:137 138 # ### Bar chart139 140 # # # Create a df that stores all the labels141 # df_labels = pd.DataFrame(list(label_dict.items()), columns=['Label ID', 'Label'])142 143 # # Count how often each label appears in the "Vulnerability Labels" column144 # group_counts = {}145 146 # # Iterate through each sublist147 # for index, row in df_vul.iterrows():148 149 # # Iterate through each group in the sublist150 # for sublist in row['Vulnerability Label']:151 152 # # Update the count in the dictionary153 # group_counts[sublist] = group_counts.get(sublist, 0) + 1154 155 # # Create a new dataframe from group_counts156 # df_label_count = pd.DataFrame(list(group_counts.items()), columns=['Label', 'Count'])157 158 # # Merge the label counts with the df_label DataFrame159 # df_label_count = df_labels.merge(df_label_count, on='Label', how='left')160 # st.write("df_label_count")161 162 # # # Configure graph163 # # fig = px.pie(df_labels,164 # # names="Label", 165 # # values="Count",166 # # title='Label Counts',167 # # hover_name="Count",168 # # color_discrete_sequence=px.colors.qualitative.Plotly169 # # )170 171 # # #Show plot172 # # st.plotly_chart(fig, use_container_width=True)173 174 # # ### Table 175 # st.table(df_vul[df_vul['Vulnerability Label'] != 'Other'])176 177 # vulnerability_analysis.vulnerability_display()178# elif topic == 'Action':179# policyaction.action_display()180# else: 181# policyaction.policy_display()182#st.write(st.session_state.key0)