Skip to content

Instantly share code, notes, and snippets.

@treuille
Created September 21, 2019 21:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save treuille/ecbd75552faca66a4dad1119587bcd5a to your computer and use it in GitHub Desktop.
Save treuille/ecbd75552faca66a4dad1119587bcd5a to your computer and use it in GitHub Desktop.
Provisional solution to @ines's Streamlit caching problem with Spacy
import streamlit as st
import spacy
from spacy import displacy
SPACY_MODEL_NAMES = ["en_core_web_sm", "de_core_news_sm"]
DEFAULT_TEXT = "Mark Zuckerberg is the CEO of Facebook."
HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem">{}</div>"""
@st.cache(ignore_hash=True)
def load_model(name):
return spacy.load(name)
@st.cache(ignore_hash=True)
def process_text(text):
return nlp(text)
st.title("Interactive spaCy visualizer")
st.markdown(
"""
A little Streamlit app that lets you process text with [spaCy](https://spacy.io)
models and visualize named entities and dependencies, or get the `Doc` object's
JSON representation. Uses spaCy's built-in
[displaCy](http://spacy.io/usage/visualizers) visualizer under the hood.
"""
)
spacy_model = st.selectbox("Model name", SPACY_MODEL_NAMES)
model_load_state = st.info(f"Loading model '{spacy_model}'...")
nlp = load_model(spacy_model)
model_load_state.success(f"✅ Loaded model '{spacy_model}'.")
text = st.text_input("Text to analyze", DEFAULT_TEXT)
button_deps = st.button("Analyze dependencies")
button_ents = st.button("Analyze named entities")
button_json = st.button("Get JSON")
doc = process_text(text)
if button_deps:
html = displacy.render(doc)
# Double newlines seem to mess with the rendering
html = html.replace("\n\n", "\n")
st.write(HTML_WRAPPER.format(html))
if button_ents:
html = displacy.render(doc, style="ent")
st.write(HTML_WRAPPER.format(html))
if button_json:
st.write(doc.to_json())
@mzeidhassan
Copy link

To update this snippet for latest Streamlit release, we need to replace "ignore_hash=True" with "allow_output_mutation=True".
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment