• Stars
    star
    787
  • Rank 57,828 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ‘‘ spaCy building blocks and visualizers for Streamlit apps

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)
Argument Type Description
models List[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_text str Default text to analyze on load. Defaults to "".
default_model Optional[str] Optional name of default model. If not set, the first model in the list of models is used.
visualizers List[str] Names of visualizers to show. Defaults to ["parser", "ner", "textcat", "similarity", "tokens"].
ner_labels Optional[List[str]] NER labels to include. If not set, all labels present in the "ner" pipeline component will be used.
ner_attrs List[str] Span attributes shown in table of named entities. See visualizer.py for defaults.
token_attrs List[str] Token attributes to show in token visualizer. See visualizer.py for defaults.
similarity_texts Tuple[str, str] The default texts to compare in the similarity visualizer. Defaults to ("apple", "orange").
show_json_doc bool Show button to toggle JSON representation of the Doc. Defaults to True.
show_meta bool Show button to toggle meta.json of the current pipeline. Defaults to True.
show_config bool Show button to toggle config.cfg of the current pipeline. Defaults to True.
show_visualizer_select bool Show sidebar dropdown to select visualizers to display (based on enabled visualizers). Defaults to False.
sidebar_title Optional[str] Title shown in the sidebar. Defaults to None.
sidebar_description Optional[str] Description shown in the sidebar. Accepts Markdown-formatted text.
show_logo bool Show the spaCy logo in the sidebar. Defaults to True.
color Optional[str] Experimental: Primary color to use for some of the main UI elements (None to disable hack). Defaults to "#09A3D5".
get_default_text Callable[[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)
Argument Type Description
doc Doc The spaCy Doc object to visualize.
keyword-only
title Optional[str] Title of the visualizer block.
key Optional[str] Key used for the streamlit component for selecting labels.
manual bool Flag signifying whether the doc argument is a Doc object or a List of Dicts containing parse information.
displacy_options Optional[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)
Argument Type Description
doc Doc The spaCy Doc object to visualize.
keyword-only
labels Sequence[str] The labels to show in the labels dropdown.
attrs List[str] The span attributes to show in entity table.
show_table bool Whether to show a table of entities and their attributes. Defaults to True.
title Optional[str] Title of the visualizer block.
colors Dict[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".)
key Optional[str] Key used for the streamlit component for selecting labels.
manual bool Flag signifying whether the doc argument is a Doc object or a List of Dicts containing entity span
information.
displacy_options Optional[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"}})
Argument Type Description
doc Doc The spaCy Doc object to visualize.
keyword-only
spans_key Sequence[str] Which spans key to render spans from. Default is "sc".
attrs List[str] The attributes on the entity Span to be labeled. Attributes are displayed only when the show_table argument is True.
show_table bool Whether to show a table of spans and their attributes. Defaults to True.
title Optional[str] Title of the visualizer block.
manual bool Flag signifying whether the doc argument is a Doc object or a List of Dicts containing entity span information.
displacy_options Optional[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)
Argument Type Description
doc Doc The spaCy Doc object to visualize.
keyword-only
title Optional[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"))
Argument Type Description
nlp Language The loaded nlp object with vectors.
default_texts Tuple[str, str] The default texts to compare on load. Defaults to ("apple", "orange").
keyword-only
threshold float Threshold for what's considered "similar". If the similarity score is greater than the threshold, the result is shown as similar. Defaults to 0.5.
title Optional[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_"])
Argument Type Description
doc Doc The spaCy Doc object to visualize.
keyword-only
attrs List[str] The names of token attributes to use. See visualizer.py for defaults.
title Optional[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)
Argument Type Description
model_name str Loadable spaCy model name. Can be path or package name.
text str The text to process.
RETURNS Doc The 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)
Argument Type Description
name str Loadable spaCy model name. Can be path or package name.
RETURNS Language The loaded nlp object.

More Repositories

1

spaCy

๐Ÿ’ซ Industrial-strength Natural Language Processing (NLP) in Python
Python
29,546
star
2

thinc

๐Ÿ”ฎ A refreshing functional take on deep learning, compatible with your favorite libraries
Python
2,813
star
3

spacy-course

๐Ÿ‘ฉโ€๐Ÿซ Advanced NLP with spaCy: A free online course
Python
2,299
star
4

sense2vec

๐Ÿฆ† Contextually-keyed word vectors
Python
1,615
star
5

spacy-models

๐Ÿ’ซ Models for the spaCy Natural Language Processing (NLP) library
Python
1,589
star
6

spacy-transformers

๐Ÿ›ธ Use pretrained transformers like BERT, XLNet and GPT-2 in spaCy
Python
1,334
star
7

projects

๐Ÿช End-to-end NLP workflows from prototype to production
Python
1,285
star
8

spacy-llm

๐Ÿฆ™ Integrating LLMs into structured NLP pipelines
Python
1,049
star
9

curated-transformers

๐Ÿค– A PyTorch library of curated Transformer models and their composable components
Python
858
star
10

spacy-stanza

๐Ÿ’ฅ Use the latest Stanza (StanfordNLP) research models directly in spaCy
Python
722
star
11

prodigy-recipes

๐Ÿณ Recipes for the Prodigy, our fully scriptable annotation tool
Jupyter Notebook
477
star
12

wasabi

๐Ÿฃ A lightweight console printing and formatting toolkit
Python
444
star
13

cymem

๐Ÿ’ฅ Cython memory pool for RAII-style memory management
Cython
436
star
14

srsly

๐Ÿฆ‰ Modern high-performance serialization utilities for Python (JSON, MessagePack, Pickle)
Python
422
star
15

displacy

๐Ÿ’ฅ displaCy.js: An open-source NLP visualiser for the modern web
JavaScript
343
star
16

lightnet

๐ŸŒ“ Bringing pjreddie's DarkNet out of the shadows #yolo
C
319
star
17

prodigy-openai-recipes

โœจ Bootstrap annotation with zero- & few-shot learning via OpenAI GPT-3
Python
318
star
18

spacy-notebooks

๐Ÿ’ซ Jupyter notebooks for spaCy examples and tutorials
Jupyter Notebook
285
star
19

spacy-services

๐Ÿ’ซ REST microservices for various spaCy-related tasks
Python
240
star
20

cython-blis

๐Ÿ’ฅ Fast matrix-multiplication as a self-contained Python library โ€“ no system dependencies!
C
215
star
21

displacy-ent

๐Ÿ’ฅ displaCy-ent.js: An open-source named entity visualiser for the modern web
CSS
197
star
22

jupyterlab-prodigy

๐Ÿงฌ A JupyterLab extension for annotating data with Prodigy
TypeScript
188
star
23

spacymoji

๐Ÿ’™ Emoji handling and meta data for spaCy with custom extension attributes
Python
180
star
24

tokenizations

Robust and Fast tokenizations alignment library for Rust and Python https://tamuhey.github.io/tokenizations/
Rust
180
star
25

wheelwright

๐ŸŽก Automated build repo for Python wheels and source packages
Python
174
star
26

catalogue

Super lightweight function registries for your library
Python
171
star
27

confection

๐Ÿฌ Confection: the sweetest config system for Python
Python
169
star
28

spacy-dev-resources

๐Ÿ’ซ Scripts, tools and resources for developing spaCy
Python
125
star
29

radicli

๐Ÿ•Š๏ธ Radically lightweight command-line interfaces
Python
100
star
30

spacy-lookups-data

๐Ÿ“‚ Additional lookup tables and data resources for spaCy
Python
98
star
31

spacy-experimental

๐Ÿงช Cutting-edge experimental spaCy components and features
Python
94
star
32

talks

๐Ÿ’ฅ Browser-based slides or PDFs of our talks and presentations
JavaScript
94
star
33

thinc-apple-ops

๐Ÿ Make Thinc faster on macOS by calling into Apple's native Accelerate library
Cython
90
star
34

healthsea

Healthsea is a spaCy pipeline for analyzing user reviews of supplementary products for their effects on health.
Python
87
star
35

preshed

๐Ÿ’ฅ Cython hash tables that assume keys are pre-hashed
Cython
82
star
36

weasel

๐Ÿฆฆ weasel: A small and easy workflow system
Python
62
star
37

spacy-huggingface-pipelines

๐Ÿ’ฅ Use Hugging Face text and token classification pipelines directly in spaCy
Python
61
star
38

spacy-ray

โ˜„๏ธ Parallel and distributed training with spaCy and Ray
Python
54
star
39

ml-datasets

๐ŸŒŠ Machine learning dataset loaders for testing and example scripts
Python
45
star
40

murmurhash

๐Ÿ’ฅ Cython bindings for MurmurHash2
C++
44
star
41

assets

๐Ÿ’ฅ Explosion Assets
43
star
42

spacy-huggingface-hub

๐Ÿค— Push your spaCy pipelines to the Hugging Face Hub
Python
42
star
43

wikid

Generate a SQLite database from Wikipedia & Wikidata dumps.
Python
30
star
44

vscode-prodigy

๐Ÿงฌ A VS Code extension for annotating data with Prodigy
TypeScript
30
star
45

spacy-alignments

๐Ÿ’ซ A spaCy package for Yohei Tamura's Rust tokenizations library
Python
26
star
46

spacy-vscode

spaCy extension for Visual Studio Code
Python
24
star
47

spacy-curated-transformers

spaCy entry points for Curated Transformers
Python
22
star
48

spacy-benchmarks

๐Ÿ’ซ Runtime performance comparison of spaCy against other NLP libraries
Python
20
star
49

prodigy-hf

Train huggingface models on top of Prodigy annotations
Python
19
star
50

prodigy-pdf

A Prodigy plugin for PDF annotation
Python
18
star
51

spacy-vectors-builder

๐ŸŒธ Train floret vectors
Python
17
star
52

os-signpost

Wrapper for the macOS signpost API
Cython
12
star
53

spacy-loggers

๐Ÿ“Ÿ Logging utilities for spaCy
Python
12
star
54

prodigy-evaluate

๐Ÿ”Ž A Prodigy plugin for evaluating spaCy pipelines
Python
12
star
55

prodigy-segment

Select pixels in Prodigy via Facebook's Segment-Anything model.
Python
11
star
56

curated-tokenizers

Lightweight piece tokenization library
Cython
11
star
57

conll-2012

A slightly cleaned up version of the scripts & data for the CoNLL 2012 Coreference task.
Python
10
star
58

thinc_gpu_ops

๐Ÿ”ฎ GPU kernels for Thinc
C++
9
star
59

prodigy-ann

A Prodigy pluging for ANN techniques
Python
4
star
60

prodigy-whisper

Audio transcription with OpenAI's whisper model in the loop.
Python
4
star
61

princetondh

Code for our presentation in Princeton DH 2023 April.
Jupyter Notebook
4
star
62

spacy-legacy

๐Ÿ•ธ๏ธ Legacy architectures and other registered spaCy v3.x functions for backwards-compatibility
Python
4
star
63

ec2buildwheel

Python
2
star
64

aiGrunn-2023

Materials for the aiGrunn 2023 talk on spaCy Transformer pipelines
Python
1
star
65

spacy-io-binder

๐Ÿ“’ Repository used to build Binder images for the interactive spaCy code examples
Jupyter Notebook
1
star
66

prodigy-lunr

A Prodigy plugin for document search via LUNR
Python
1
star
67

.github

:octocat: GitHub settings
1
star
68

span-labeling-datasets

Loaders for various span labeling datasets
Python
1
star
69

spacy-biaffine-parser

Python
1
star