-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamlit_demo.py
39 lines (29 loc) · 1.15 KB
/
streamlit_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import sys
import streamlit as st
from allennlp.predictors import Predictor
from spacy import displacy
import defx
from defx.util.displacy_formatter import DisplacyFormatter
model_name = sys.argv[1] or 'data/runs/joint_bert_classifier/model.tar.gz'
print('Loading model from', model_name)
def setup_predictor():
pred = Predictor.from_path(model_name, 'joint-classifier')
return pred
predictor = st.cache(
setup_predictor,
allow_output_mutation=True # the Predictor is not hashable
)()
HTML_WRAPPER = """
<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem">{}</div>
"""
st.title('Defx demo')
text = st.text_input("Model input", "A neural network is a network composed of artificial neurons or nodes")
result = predictor.predict_json({'sentence': text})
displacy_format = DisplacyFormatter().format(result)
html = displacy.render(displacy_format,
style="dep",
manual=True,
options={'compact': True})
# Double newlines seem to mess with the rendering
html = html.replace("\n\n", "\n")
st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)