|
| 1 | +import streamlit as st |
| 2 | +st.set_page_config( |
| 3 | + layout="centered", # Can be "centered" or "wide". In the future also "dashboard", etc. |
| 4 | + initial_sidebar_state="auto", # Can be "auto", "expanded", "collapsed" |
| 5 | + page_title='Detect tumor characteristics', # String or None. Strings get appended with "• Streamlit". |
| 6 | + page_icon='../../resources/favicon.png', # String, anything supported by st.image, or None. |
| 7 | +) |
| 8 | +import pandas as pd |
| 9 | +import numpy as np |
| 10 | +import os |
| 11 | +os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64" |
| 12 | +import sys |
| 13 | +sys.path.append(os.path.abspath('./NER_COLAB')) |
| 14 | +#sys.path.append(os.path.abspath('../../')) |
| 15 | +import streamlit_apps_config as config |
| 16 | +from streamlit_ner_output import show_html2, jsl_display_annotations, get_color |
| 17 | + |
| 18 | +## Marking down NER Style |
| 19 | +st.markdown(config.STYLE_CONFIG, unsafe_allow_html=True) |
| 20 | + |
| 21 | +root_path = config.project_path |
| 22 | + |
| 23 | + |
| 24 | +########## To Remove the Main Menu Hamburger ######## |
| 25 | + |
| 26 | +hide_menu_style = """ |
| 27 | + <style> |
| 28 | + #MainMenu {visibility: hidden;} |
| 29 | + </style> |
| 30 | + """ |
| 31 | +st.markdown(hide_menu_style, unsafe_allow_html=True) |
| 32 | + |
| 33 | +########## Side Bar ######## |
| 34 | + |
| 35 | +## loading logo(newer version with href) |
| 36 | +import base64 |
| 37 | +@st.cache(allow_output_mutation=True) |
| 38 | +def get_base64_of_bin_file(bin_file): |
| 39 | + with open(bin_file, 'rb') as f: |
| 40 | + data = f.read() |
| 41 | + return base64.b64encode(data).decode() |
| 42 | + |
| 43 | +@st.cache(allow_output_mutation=True) |
| 44 | +def get_img_with_href(local_img_path, target_url): |
| 45 | + img_format = os.path.splitext(local_img_path)[-1].replace('.', '') |
| 46 | + bin_str = get_base64_of_bin_file(local_img_path) |
| 47 | + html_code = f''' |
| 48 | + <a href="{target_url}"> |
| 49 | + <img height="90%" width="90%" src="data:image/{img_format};base64,{bin_str}" /> |
| 50 | + </a>''' |
| 51 | + return html_code |
| 52 | + |
| 53 | +logo_html = get_img_with_href('./NER_COLAB/resources/jsl-logo.png', 'https://www.johnsnowlabs.com/') |
| 54 | +st.sidebar.markdown(logo_html, unsafe_allow_html=True) |
| 55 | + |
| 56 | + |
| 57 | +## loading logo (older version without href) |
| 58 | +# st.sidebar.image('../../resources/jsl-logo.png', use_column_width=True) |
| 59 | + |
| 60 | +### loading description file |
| 61 | +descriptions = pd.read_json('./NER_COLAB/streamlit_apps_descriptions.json') |
| 62 | + |
| 63 | +## Getting NER model list |
| 64 | +descriptions = descriptions[descriptions['app'] == 'NER_TUMOR'] |
| 65 | +model_names = descriptions['model_name'].values |
| 66 | + |
| 67 | +## Displaying Availabe models |
| 68 | +st.sidebar.title("Choose the pretrained model to test") |
| 69 | +test_data = pd.read_json('./test.json') |
| 70 | + |
| 71 | +selected_model = st.sidebar.selectbox("", test_data.columns[1:]) |
| 72 | + |
| 73 | + |
| 74 | +######## Main Page ######### |
| 75 | + |
| 76 | +#### displaying selected model's output |
| 77 | +#app_title = descriptions[descriptions['model_name'] == selected_model]['title'].iloc[0] |
| 78 | +#app_description = descriptions[descriptions['model_name'] == selected_model]['description'].iloc[0] |
| 79 | +#st.title('Spark NLP NER') |
| 80 | +#st.title(app_title) |
| 81 | +#st.markdown("<h2>"+app_description+"</h2>", unsafe_allow_html=True) |
| 82 | +#st.subheader("") |
| 83 | +#### Loading Files |
| 84 | + |
| 85 | +#TEXT_FILE_PATH = os.path.join(os.getcwd(),'inputs/'+selected_model) |
| 86 | +#RESULT_FILE_PATH = os.path.join(os.getcwd(),'outputs/'+selected_model) |
| 87 | + |
| 88 | +#input_files = sorted(os.listdir(TEXT_FILE_PATH)) |
| 89 | +#input_files_paths = [os.path.join(TEXT_FILE_PATH, fname) for fname in input_files] |
| 90 | +#result_files_paths = [os.path.join(RESULT_FILE_PATH, fname.split('.')[0]+'.json') for fname in input_files] |
| 91 | + |
| 92 | +#title_text_mapping_dict={} |
| 93 | +#title_json_mapping_dict={} |
| 94 | +#for index, file_path in enumerate(input_files_paths): |
| 95 | +# lines = open(file_path, 'r', encoding='utf-8').readlines() |
| 96 | +# title = lines[0] |
| 97 | +# text = ''.join(lines[1:]) |
| 98 | +# title_text_mapping_dict[title] = text |
| 99 | +# title_json_mapping_dict[title] = result_files_paths[index] |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +#### Displaying Available examples |
| 104 | + |
| 105 | + |
| 106 | +import SessionState |
| 107 | + |
| 108 | +ss = SessionState.get(x=1) |
| 109 | + |
| 110 | +selected_title = list(zip(test_data.index ,test_data.Insight))[ss.x] |
| 111 | + |
| 112 | +c1, c2 = st.columns(2) |
| 113 | + |
| 114 | +with c1: |
| 115 | + if st.button("Previous Insight"): |
| 116 | + ss.x = ss.x - 1 |
| 117 | + selected_title = list(zip(test_data.index ,test_data.Insight))[ss.x] |
| 118 | + |
| 119 | +with c2: |
| 120 | + if st.button("Next Insight"): |
| 121 | + ss.x = ss.x + 1 |
| 122 | + selected_title = list(zip(test_data.index ,test_data.Insight))[ss.x] |
| 123 | + |
| 124 | +#if st.button("Next Insight"): |
| 125 | +# ss.x = ss.x + 1 |
| 126 | +# selected_title = list(zip(test_data.index ,test_data.Insight))[ss.x] |
| 127 | + |
| 128 | +#if st.button("Previous Insight"): |
| 129 | +# ss.x = ss.x - 1 |
| 130 | +# selected_title = list(zip(test_data.index ,test_data.Insight))[ss.x] |
| 131 | + |
| 132 | +widget = st.empty() |
| 133 | +ss.x = widget.slider('Position', 0, max(test_data.index), ss.x) |
| 134 | + |
| 135 | +#slider = st.slider("Select Insight", 0, max(test_data.index)) |
| 136 | + |
| 137 | +#title = st.selectbox("Choose Sample Text", zip(test_data.index ,test_data.Insight))#input_files_list) |
| 138 | +#selected = st.button('Go to selected text') |
| 139 | + |
| 140 | +#if selected: |
| 141 | +# selected_title = title |
| 142 | +# df = pd.DataFrame({"ner_chunk" : test_data[selected_model][selected_title[0]]}) |
| 143 | + |
| 144 | +## selected example |
| 145 | + |
| 146 | +labels_set = set() |
| 147 | + |
| 148 | +df = pd.DataFrame({"ner_chunk" : test_data[selected_model][selected_title[0]]}) |
| 149 | + |
| 150 | +for i in test_data[selected_model][selected_title[0]]: |
| 151 | + i[4]['entity'] = i[4]['entity'].strip().upper() |
| 152 | + labels_set.add(i[4]['entity']) |
| 153 | + |
| 154 | +labels_set = list(labels_set) |
| 155 | + |
| 156 | +labels = st.sidebar.multiselect( |
| 157 | + "Entity labels", options=labels_set, default=list(labels_set) |
| 158 | + ) |
| 159 | + |
| 160 | +### Show Entities |
| 161 | +#selected_text = title_text_mapping_dict[selected_title] |
| 162 | +show_html2(test_data.Insight[ss.x], df, labels, "Text annotated with identified Named Entities") |
0 commit comments