Code Monkey home page Code Monkey logo

spacy-streamlit's Introduction

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.

spacy-streamlit's People

Contributors

adrianeboyd avatar callistachang avatar discdiver avatar ehom avatar ines avatar jcharis avatar jette16 avatar narayanacharya6 avatar pmbaumgartner avatar svlandeg avatar tvst avatar victorialslocum avatar yagays avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spacy-streamlit's Issues

Requesting new release that includes fixes for issue #40.

Apologies if this is not the right way to request a release. The last release I see is 1.0.4 in June 2022. I added a minor fix for issue #40 in August and was hoping it be part of the next release. I have a hacky way to work around the issue in my projects but was hoping to resolve it for once with an updated release version of spacy-streamlit.

Thanks!

OSError: [E050] Can't find model 'en_core_web_sm'. It doesn't seem to be a Python package or a valid path to a data directory.

Hi. πŸ‘‹
This is my first time ever submitting an issue on Github.
I resolved the issue somehow myself but I thought it's important to report and share the possible bug anyway. I'll try my best to follow the github issue submit best practices, I hope I'm doing it correctly.

My code

import spacy
nlp = spacy.load('en')

Errors I ran into

Error 1

OSError: [E050] Can't find model 'en_core_web_sm'. It doesn't seem to be a Python package or a valid path to a data directory.

Error 2

[E053] Could not read config.cfg from c:\users\Pycharmprojects\my_project_foler\venv\lib\site-packages\en_core_web_sm\en_core_web_sm-2.2.0\config.cfg

After Error 1, I tried the following link.
explosion/spaCy#4577

Then the first error resolved and pycharm threw the error 2 right after.
I tried all the pip install github_links but didn't work.
explosion/spaCy#7453

Unsuccessful attempts in order

  1. download
    python3 -m spacy download en_core_web_sm

  2. Upgrade
    pip3 install -U spacy

  3. Github link 1
    pip3 install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.2.0/en_core_web_sm-2.2.0.tar.gz

  4. Github link 2
    pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.0.0/en_core_web_sm-3.0.0.tar.gz Failed to build spacy

  5. validate
    python -m spacy validate

  6. install
    pip install --no-cache-dir spacy

What worked

python -m spacy download en

As of spaCy v3.0, shortcuts like 'en' are deprecated. Please use the full
pipeline package name 'en_core_web_sm' instead.
Collecting en-core-web-sm==3.2.0

The terminal automatically downloaded en_core_web_sm and it worked.
But this was the first thing I had tried. Is it because I used python3 instead of python in the command...?

Thank you!

AttributeError: module 'spacy_streamlit' has no attribute 'visualizer'

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)

gives the error:

AttributeError: module 'spacy_streamlit' has no attribute 'visualizer'
Traceback:

File "e:\wpy-3710\python-3.7.1.amd64\lib\site-packages\streamlit\ScriptRunner.py", line 322, in _run_script
    exec(code, module.__dict__)
File "e:\work\Python\spacy_streamlit.py", line 1, in <module>
    import spacy_streamlit
File "e:\work\Python\spacy_streamlit.py", line 5, in <module>
    spacy_streamlit.visualizer(models, default_text)

I am using Windows 10 WinPython 3.7.1

Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32

Removing extra space when visualizing Named Entities

Recently, I was working on a side project with some colleagues, playing with Named Entity Recognition, Streamlit, and Spacy.

One of my friends manages to use displacy to generate the html necessary to show the named entities, pretty much like the implementation of visualize_ner does.

However, this friend removed the line break character \n from the text before processing it with spacy and turning it into a Doc object. Like the 30 line from the utils.py file.

As you might know, this approach shows the texts all clumped together without the line breaks.

We came up with a better solution by replacing all the extras spaces from the generated html like this:

html = re.sub("\s{2,}", " ", html)

And we were wondering if you guys would like to know that or let the users know it.

That’s all folk πŸ˜„

Support for extra options to be passed to `visualize_ner`

This issue is mainly inspired by work in this PR - explosion/spaCy#9748. Unable to set newly introduced options with the current visualize_ner API. Allowing extra options to be passed to the method will be helpful.

I am guessing allowing to pass extra options for other visualize methods will be helpful too? If that is the case the scope of this issue can be expanded.

valueError E030 sentence boundaries unset

I am following the guidance on the spacy website using the following code, but i get the error below the code:

models = ["modelout\model-best"]
default_text = "this sentence is a test"
spacy_streamlit.visualize(models, default_text)

ValueError: [E030] Sentence boundaries unset. You can add the 'sentencizer' component to the pipeline with: nlp.add_pipe('sentencizer'). Alternatively, add the dependency parser or sentence recognizer, or set sentence boundaries by setting doc[i].is_sent_start.

Be able to load spacy.lang.en.English model (and more)

I have a pipeline that builds on spacy.lang.en.English. I replace the tokenizer and add some custom components. Now spacy_streamlit uses spacy.load to load models. Is it possible to register my pipeline and be loadable via spacy.load?

I am aware that I can do nlp.to_disk on spacy.lang.en.English with my replaced tokenizer and that I can register my components using entry_points but I'd rather not have to do nlp.to_disk (e.g. shouldn't keep that in my git repo and it seems uneccesary!?).

Another alternative is to make spacy.lang.en.English with my replaced tokenizer as its own language and add that to entry_points but it feels kinda wrong and then I wouldn't be able to get the lexeme normalization table from spacy-lookups-data.

I hope it makes sense.

Text from a scanned document.

Many research institutes in humanities scan documents and books and get text via OCR. We can analyze the text, but is there a standard way to connect the scan and the text when you know the coefficient of where the word or phrase is on the image.

Be able to specify port & host

I'm currently running the spacy-streamlit app on a server. I prefer to have this on my server on my local network because otherwise my laptop gets very hot during training. In order to properly host it though I need to be able to customise the port-number as well as the host. It seems like these settings are currently missing. Would @ines you be open to these commands? I wouldn't mind picking this up.

Cannot use `visualize` multiple times in the same script

When using visualize methods multiple times, we get into a DuplicateWidgetID error.

import spacy_streamlit

models = ["en_core_web_sm", "en_core_web_md"]
text1 = "Sundar Pichai is the CEO of Google."
text2 = "Randy Zwitch is Head of Developer Relations at Streamlit"
spacy_streamlit.visualize(models, text1)
spacy_streamlit.visualize(models, text2)

image

Adding/generating a key parameter to pass to those widgets should do.

Original forum thread

visualize_spans

I have been trying to use this but keep on getting this error
ImportError: cannot import name 'visualize_spans' from partially initialized module 'spacy_streamlit' (most likely due to a circular import)

spacy 3.3.1
spacy_streamlit 1.0.4

Support for span categorizer?

Is it possible to visualize spancat predictions via spacy-streamlit yet? Either directly or by customizing one of the existing visualizers? I haven't been able to find any examples of this.

ModuleNotFoundError in demo

when opening https://ines-spacy-streamlit-demo-app-a6h19v.streamlit.app/ I encounter a ModuleNotFoundError

Traceback:
File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.dict)
File "/app/spacy-streamlit-demo/app.py", line 1, in
import spacy_streamlit
File "/home/appuser/venv/lib/python3.7/site-packages/spacy_streamlit/init.py", line 1, in
from .visualizer import visualize, visualize_parser, visualize_ner, visualize_spans
File "/home/appuser/venv/lib/python3.7/site-packages/spacy_streamlit/visualizer.py", line 3, in
import spacy
File "/home/appuser/venv/lib/python3.7/site-packages/spacy/init.py", line 14, in
from .cli.info import info # noqa: F401
File "/home/appuser/venv/lib/python3.7/site-packages/spacy/cli/init.py", line 3, in
from ._util import app, setup_cli # noqa: F401
File "/home/appuser/venv/lib/python3.7/site-packages/spacy/cli/_util.py", line 8, in
import typer
File "/home/appuser/venv/lib/python3.7/site-packages/typer/init.py", line 29, in
from .main import Typer as Typer
File "/home/appuser/venv/lib/python3.7/site-packages/typer/main.py", line 11, in
from .completion import get_completion_inspect_parameters
File "/home/appuser/venv/lib/python3.7/site-packages/typer/completion.py", line 10, in
import click._bashcomplete

'NoneType' object has no attribute 'replace'

When I ran the example:

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)

Have this problem

AttributeError Traceback (most recent call last)
in
3 models = ["en_core_web_sm", "en_core_web_md"]
4 default_text = "Sundar Pichai is the CEO of Google."
----> 5 spacy_streamlit.visualize(models, default_text)

~/anaconda3/lib/python3.7/site-packages/spacy_streamlit/visualizer.py in visualize(models, default_text, visualizers, ner_labels, ner_attrs, similarity_texts, token_attrs, show_json_doc, show_model_meta, sidebar_title, sidebar_description, show_logo, color)
51
52 if "parser" in visualizers:
---> 53 visualize_parser(doc)
54 if "ner" in visualizers:
55 ner_labels = ner_labels or nlp.get_pipe("ner").labels

~/anaconda3/lib/python3.7/site-packages/spacy_streamlit/visualizer.py in visualize_parser(doc, title, sidebar_title)
98 html = displacy.render(sent, options=options, style="dep")
99 # Double newlines seem to mess with the rendering
--> 100 html = html.replace("\n\n", "\n")
101 if split_sents and len(docs) > 1:
102 st.markdown(f"> {sent.text}")

AttributeError: 'NoneType' object has no attribute 'replace'

`visualize_ner()` does not remove duplicate labels for spans.

I think the issue is best illustrated using a picture:
image

So my doc has multiple spans with label LOC or ORG. I would expect the selector component here only show me these two values. However, looking at the code for visualize_ner() I see we do not remove duplicates from labels for different spans. I am not sure if this has been there all along or something changed with recent versions of streamlit.

I am using the following version of the packages:

spacy-streamlit           1.0.4
streamlit                 1.12.0

Option to annotate

Hi,
Is there an option to input text and annotate content?
Like a text annotation tool, can this be used?

Visualization of nested spans

Dear all,

I'm trying to use the new span visualizer to display some custom, nested spans predicted by spancat.
This is my code:

MODEL_PATH = # ...
DEFAULT_TEXT = """Cetuximab ist ein monoklonaler AntikΓΆrper, der gegen den epidermalen Wachstumsfaktorrezeptor (EGFR) gerichtet ist und \
    dient zur Therapie des fortgeschrittenen kolorektalen Karzinoms zusammen mit Irinotecan oder in Kombination mit FOLFOX bzw. \
    allein nach Versagen einer Behandlung mit Oxaliplatin und Irinotecan."""

st.set_page_config(layout="wide")

text = st.text_area("Text to analyze", DEFAULT_TEXT)
doc = process_text(MODEL_PATH, text)

visualize_spans(doc, spans_key="snomed", displacy_options=
    {"colors": {
        "Clinical_Drug": "#99FF99", "External_Substance" : "#CCFFCC", "Nutrient_or_Body_Substance" : "#E5FFCC", 
        "Therapeutic" : "#66B2FF", "Diagnostic" : "#CCE5FF",
        "Other_Finding" : "#FFE5CC", "Diagnosis_or_Pathology" : "#FFCCCC"}} )

myapp

The nested spans are all shown on the same line, which makes it very hard to read.
I learned from the feature on LinkedIn, where a video was shown with nested spans nicely displayed.

demo

How can I achieve this behavior? Do I need to pass some other options?

spaCy 3.1 Support

The spacy-streamlit module currently requires spacy>=3.0.0,<3.1.0, making it unusable with spaCy 3.1. As far as I can tell, it shouldn't break with 3.1. Can this requirement be updated to allow 3.1?

spacy>=3.0.0,<3.1.0

Unexpected keyword errors

Hello,

I aa looking to use spacy-streamlit to visualise custom sentences and entities output from my own model.

When running some of your example .py files, I have found errors
https://github.com/explosion/spacy-streamlit/tree/master/examples

03_visualize-ner-manual.py
The error says:
TypeError: visualize_ner() got an unexpected keyword argument 'manual'

04_visualize-ner-extra-options.pyThe error says:
TypeError: visualize_ner() got an unexpected keyword argument 'displacy_options'

I think this may be a big. Is there a way around this?

NotImplementedError: [E894] The 'noun_chunks' syntax iterator is not implemented for language 'it'.

I'm trying to use the italian model and getting this error why usine visualize_parser.

File "/home/irfan/environments/The-Entities-Swissknife/lib/python3.7/site-packages/streamlit/scriptrunner/script_runner.py", line 443, in _run_script
    exec(code, module.__dict__)
  File "/home/irfan/PycharmProjects/TES-Clone/main.py", line 385, in <module>
    visualize_parser(doc)
  File "/home/irfan/environments/The-Entities-Swissknife/lib/python3.7/site-packages/spacy_streamlit/visualizer.py", line 156, in visualize_parser
    html = displacy.render(sent, options=options, style="dep")
  File "/home/irfan/environments/The-Entities-Swissknife/lib/python3.7/site-packages/spacy/displacy/__init__.py", line 57, in render
    parsed = [converter(doc, options) for doc in docs] if not manual else docs  # type: ignore
  File "/home/irfan/environments/The-Entities-Swissknife/lib/python3.7/site-packages/spacy/displacy/__init__.py", line 57, in <listcomp>
    parsed = [converter(doc, options) for doc in docs] if not manual else docs  # type: ignore
  File "/home/irfan/environments/The-Entities-Swissknife/lib/python3.7/site-packages/spacy/displacy/__init__.py", line 131, in parse_deps
    for np in list(doc.noun_chunks):
  File "spacy/tokens/doc.pyx", line 852, in noun_chunks
NotImplementedError: [E894] The 'noun_chunks' syntax iterator is not implemented for language 'it'.

Here are the models and library info

en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.2.0/en_core_web_sm-3.2.0-py3-none-any.whl
it-core-news-sm @ https://github.com/explosion/spacy-models/releases/download/it_core_news_sm-3.2.0/it_core_news_sm-3.2.0-py3-none-any.whl
spacy==3.2.4
spacy-legacy==3.0.9
spacy-loggers==1.0.2
spacy-streamlit==1.0.3

No HTML object returned by displacy.render()

Running the set examples in Jupyter notebook causes:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[47], line 2
      1 doc = nlp('The tenants have mobility issues and cannot see well.')
----> 2 spacy_streamlit.visualize_spans(doc, 
      3                                 spans_key='sc', 
      4                                 displacy_options={'colors': {"MOBILITY":'#09a3d5'}}, 
      5                                 show_table=False)

File ~\.conda\envs\prodigy\lib\site-packages\spacy_streamlit\visualizer.py:326, in visualize_spans(doc, spans_key, attrs, show_table, title, manual, displacy_options)
    317         st.warning(
    318             "When the parameter 'manual' is set to True, the parameter 'doc' must be of type 'Dict', not 'spacy.tokens.Doc'."
    319         )
    320 html = displacy.render(
    321     doc,
    322     style="span",
    323     options=displacy_options,
    324     manual=manual,
    325 )
--> 326 st.write(f"{get_html(html)}", unsafe_allow_html=True)
    328 if show_table:
    329     data = [
    330         [str(getattr(span, attr)) for attr in attrs]
    331         for span in doc.spans[spans_key]
    332     ]

File ~\.conda\envs\prodigy\lib\site-packages\spacy_streamlit\util.py:30, in get_html(html)
     28 WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
     29 # Newlines seem to mess with the rendering
---> 30 html = html.replace("\n", " ")
     31 return WRAPPER.format(html)

AttributeError: 'NoneType' object has no attribute 'replace'

Setup:
Windows Server 2019 Standard
Jupyter 1.0.0
spaCy version 3.6.1
spacy-streamlit 1.0.6

Code:

import spacy_streamlit
import streamlit as st

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"}}
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.