Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

spacy-streamlit

Package Overview
Dependencies
Maintainers
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

spacy-streamlit

Visualize spaCy with streamlit

  • 1.0.6
  • PyPI
  • Socket score

Maintainers
2

spacy-streamlit: spaCy building blocks for Streamlit apps

This package contains utilities for visualizing spaCy models and building interactive spaCy-powered apps with Streamlit. It includes various building blocks you can use in your own Streamlit app, like visualizers for syntactic dependencies, named entities, text classification, semantic similarity via word vectors, token attributes, and more.

Current Release Version pypi Version

🚀 Quickstart

You can install spacy-streamlit from pip:

pip install spacy-streamlit

The package includes building blocks that call into Streamlit and set up all the required elements for you. You can either use the individual components directly and combine them with other elements in your app, or call the visualize function to embed the whole visualizer.

Download the English model from spaCy to get started.

python -m spacy download en_core_web_sm

Then put the following example code in a file.

# streamlit_app.py
import spacy_streamlit

models = ["en_core_web_sm", "en_core_web_md"]
default_text = "Sundar Pichai is the CEO of Google."
spacy_streamlit.visualize(models, default_text)

You can then run your app with streamlit run streamlit_app.py. The app should pop up in your web browser. 😀

📦 Example: 01_out-of-the-box.py

Use the embedded visualizer with custom settings out-of-the-box.

streamlit run https://raw.githubusercontent.com/explosion/spacy-streamlit/master/examples/01_out-of-the-box.py
👑 Example: 02_custom.py

Use individual components in your existing app.

streamlit run https://raw.githubusercontent.com/explosion/spacy-streamlit/master/examples/02_custom.py

🎛 API

Visualizer components

These functions can be used in your Streamlit app. They call into streamlit under the hood and set up the required elements.

function visualize

Embed the full visualizer with selected components.

import spacy_streamlit

models = ["en_core_web_sm", "/path/to/model"]
default_text = "Sundar Pichai is the CEO of Google."
visualizers = ["ner", "textcat"]
spacy_streamlit.visualize(models, default_text, visualizers)
ArgumentTypeDescription
modelsList[str] / Dict[str, str]Names of loadable spaCy models (paths or package names). The models become selectable via a dropdown. Can either be a list of names or the names mapped to descriptions to display in the dropdown.
default_textstrDefault text to analyze on load. Defaults to "".
default_modelOptional[str]Optional name of default model. If not set, the first model in the list of models is used.
visualizersList[str]Names of visualizers to show. Defaults to ["parser", "ner", "textcat", "similarity", "tokens"].
ner_labelsOptional[List[str]]NER labels to include. If not set, all labels present in the "ner" pipeline component will be used.
ner_attrsList[str]Span attributes shown in table of named entities. See visualizer.py for defaults.
token_attrsList[str]Token attributes to show in token visualizer. See visualizer.py for defaults.
similarity_textsTuple[str, str]The default texts to compare in the similarity visualizer. Defaults to ("apple", "orange").
show_json_docboolShow button to toggle JSON representation of the Doc. Defaults to True.
show_metaboolShow button to toggle meta.json of the current pipeline. Defaults to True.
show_configboolShow button to toggle config.cfg of the current pipeline. Defaults to True.
show_visualizer_selectboolShow sidebar dropdown to select visualizers to display (based on enabled visualizers). Defaults to False.
sidebar_titleOptional[str]Title shown in the sidebar. Defaults to None.
sidebar_descriptionOptional[str]Description shown in the sidebar. Accepts Markdown-formatted text.
show_logoboolShow the spaCy logo in the sidebar. Defaults to True.
colorOptional[str]Experimental: Primary color to use for some of the main UI elements (None to disable hack). Defaults to "#09A3D5".
get_default_textCallable[[Language], str]Optional callable that takes the currently loaded nlp object and returns the default text. Can be used to provide language-specific default texts. If the function returns None, the value of default_text is used, if available. Defaults to None.
function visualize_parser

Visualize the dependency parse and part-of-speech tags using spaCy's displacy visualizer.

import spacy
from spacy_streamlit import visualize_parser

nlp = spacy.load("en_core_web_sm")
doc = nlp("This is a text")
visualize_parser(doc)
ArgumentTypeDescription
docDocThe spaCy Doc object to visualize.
keyword-only
titleOptional[str]Title of the visualizer block.
keyOptional[str]Key used for the streamlit component for selecting labels.
manualboolFlag signifying whether the doc argument is a Doc object or a List of Dicts containing parse information.
displacy_optoinsOptional[Dict]Dictionary of options to be passed to the displacy render method for generating the HTML to be rendered. See: https://spacy.io/api/top-level#options-dep
function visualize_ner

Visualize the named entities in a Doc using spaCy's displacy visualizer.

import spacy
from spacy_streamlit import visualize_ner

nlp = spacy.load("en_core_web_sm")
doc = nlp("Sundar Pichai is the CEO of Google.")
visualize_ner(doc, labels=nlp.get_pipe("ner").labels)
ArgumentTypeDescription
docDocThe spaCy Doc object to visualize.
keyword-only
labelsSequence[str]The labels to show in the labels dropdown.
attrsList[str]The span attributes to show in entity table.
show_tableboolWhether to show a table of entities and their attributes. Defaults to True.
titleOptional[str]Title of the visualizer block.
colorsDict[str,str]Dictionary of colors for the entity spans to visualize, with keys as labels and corresponding colors as the values. This argument will be deprecated soon. In future the colors arg need to be passed in the displacy_options arg with the key "colors".)
keyOptional[str]Key used for the streamlit component for selecting labels.
manualboolFlag signifying whether the doc argument is a Doc object or a List of Dicts containing entity span
information.
displacy_optionsOptional[Dict]Dictionary of options to be passed to the displacy render method for generating the HTML to be rendered. See https://spacy.io/api/top-level#displacy_options-ent.
function visualize_spans

Visualize spans in a Doc using spaCy's displacy visualizer.

import spacy
from spacy_streamlit import visualize_spans

nlp = spacy.load("en_core_web_sm")
doc = nlp("Sundar Pichai is the CEO of Google.")
span = doc[4:7]  # CEO of Google
span.label_ = "CEO"
doc.spans["job_role"] = [span]
visualize_spans(doc, spans_key="job_role", displacy_options={"colors": {"CEO": "#09a3d5"}})
ArgumentTypeDescription
docDocThe spaCy Doc object to visualize.
keyword-only
spans_keySequence[str]Which spans key to render spans from. Default is "sc".
attrsList[str]The attributes on the entity Span to be labeled. Attributes are displayed only when the show_table argument is True.
show_tableboolWhether to show a table of spans and their attributes. Defaults to True.
titleOptional[str]Title of the visualizer block.
manualboolFlag signifying whether the doc argument is a Doc object or a List of Dicts containing entity span information.
displacy_optionsOptional[Dict]Dictionary of options to be passed to the displacy render method for generating the HTML to be rendered. See https://spacy.io/api/top-level#displacy_options-span.
function visualize_textcat

Visualize text categories predicted by a trained text classifier.

import spacy
from spacy_streamlit import visualize_textcat

nlp = spacy.load("./my_textcat_model")
doc = nlp("This is a text about a topic")
visualize_textcat(doc)
ArgumentTypeDescription
docDocThe spaCy Doc object to visualize.
keyword-only
titleOptional[str]Title of the visualizer block.
visualize_similarity

Visualize semantic similarity using the model's word vectors. Will show a warning if no vectors are present in the model.

import spacy
from spacy_streamlit import visualize_similarity

nlp = spacy.load("en_core_web_lg")
visualize_similarity(nlp, ("pizza", "fries"))
ArgumentTypeDescription
nlpLanguageThe loaded nlp object with vectors.
default_textsTuple[str, str]The default texts to compare on load. Defaults to ("apple", "orange").
keyword-only
thresholdfloatThreshold for what's considered "similar". If the similarity score is greater than the threshold, the result is shown as similar. Defaults to 0.5.
titleOptional[str]Title of the visualizer block.
function visualize_tokens

Visualize the tokens in a Doc and their attributes.

import spacy
from spacy_streamlit import visualize_tokens

nlp = spacy.load("en_core_web_sm")
doc = nlp("This is a text")
visualize_tokens(doc, attrs=["text", "pos_", "dep_", "ent_type_"])
ArgumentTypeDescription
docDocThe spaCy Doc object to visualize.
keyword-only
attrsList[str]The names of token attributes to use. See visualizer.py for defaults.
titleOptional[str]Title of the visualizer block.

Cached helpers

These helpers attempt to cache loaded models and created Doc objects.

function process_text

Process a text with a model of a given name and create a Doc object. Calls into the load_model helper to load the model.

import streamlit as st
from spacy_streamlit import process_text

spacy_model = st.sidebar.selectbox("Model name", ["en_core_web_sm", "en_core_web_md"])
text = st.text_area("Text to analyze", "This is a text")
doc = process_text(spacy_model, text)
ArgumentTypeDescription
model_namestrLoadable spaCy model name. Can be path or package name.
textstrThe text to process.
RETURNSDocThe processed document.
function load_model

Load a spaCy model from a path or installed package and return a loaded nlp object.

import streamlit as st
from spacy_streamlit import load_model

spacy_model = st.sidebar.selectbox("Model name", ["en_core_web_sm", "en_core_web_md"])
nlp = load_model(spacy_model)
ArgumentTypeDescription
namestrLoadable spaCy model name. Can be path or package name.
RETURNSLanguageThe loaded nlp object.

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc