nlp-qual-space / app.py
maxspad's picture
finally figured out altair
f58c9c5
raw
history blame
3.12 kB
import streamlit as st
import transformers as tf
import pandas as pd
from overview import NQDOverview
from fullreport import NQDFullReport
# Function to load and cache models
@st.experimental_singleton(show_spinner=False)
def load_model(username, prefix, model_name):
p = tf.pipeline('text-classification', f'{username}/{prefix}-{model_name}', return_all_scores=True)
return p
@st.experimental_singleton(show_spinner=False)
def load_pickle(f):
return pd.read_pickle(f)
def get_results(model, c):
res = model(c)[0]
scores = [r['score'] for r in res]
label = max(range(len(scores)), key=lambda i: scores[i])
# label = float(res['label'].split('_')[1])
# scores = res['score']
return {'label': label, 'scores': scores}
def run_models(model_names, models, c):
results = {}
for mn in model_names:
results[mn] = get_results(models[mn], c)
return results
st.title('Assess the *QuAL*ity of your feedback')
st.caption(
"""Medical education *requires* high-quality feedback, but evaluating feedback
is difficult and time-consuming. This tool uses NLP/ML to predict a validated
feedback quality metric known as the QuAL Score. *Try it for yourself!*
""")
### Load models
# Specify which models to load
USERNAME = 'maxspad'
PREFIX = 'nlp-qual'
models_to_load = ['qual', 'q1', 'q2i', 'q3i']
n_models = float(len(models_to_load))
models = {}
# Show a progress bar while models are downloading,
# then hide it when done
lc_placeholder = st.empty()
loader_container = lc_placeholder.container()
loader_container.caption('Loading models... please wait...')
pbar = loader_container.progress(0.0)
for i, mn in enumerate(models_to_load):
pbar.progress((i+1.0) / n_models)
models[mn] = load_model(USERNAME, PREFIX, mn)
lc_placeholder.empty()
### Load example data
examples = load_pickle('test.pkl')
### Process input
ex = examples['comment'].sample(1).tolist()[0]
try:
ex = ex.strip().replace('_x000D_', '').replace('nan', 'blank')
except:
ex = 'blank'
if 'comment' not in st.session_state:
st.session_state['comment'] = ex
with st.form('comment_form'):
comment = st.text_area('Try a comment:', value=st.session_state['comment'])
left_col, right_col = st.columns([1,9], gap='medium')
submitted = left_col.form_submit_button('Submit')
trying_example = right_col.form_submit_button('Try an example!')
if submitted:
st.session_state['button_clicked'] = 'submit'
st.session_state['comment'] = comment
st.experimental_rerun()
elif trying_example:
st.session_state['button_clicked'] = 'example'
st.session_state['comment'] = ex
st.experimental_rerun()
results = run_models(models_to_load, models, st.session_state['comment'])
st.write(results)
# tab_titles = ['Overview', 'Q1 - Level of Detail', 'Q2 - Suggestion Given', 'Q3 - Suggestion Linked', 'About']
tab_titles = ['Overview', 'Full Report']
tabs = st.tabs(tab_titles)
with tabs[0]:
overview = NQDOverview(st, results)
overview.draw()
with tabs[1]:
fullrep = NQDFullReport(st, results)
fullrep.draw()