text_analysis / app.py
aliabd's picture
aliabd HF Staff
Create new file
76b8969
raw
history blame
1.06 kB
# URL: https://huggingface.co/spaces/gradio/text_analysis
# imports
import gradio as gr
import spacy
from spacy import displacy
# load the model
nlp = spacy.load("en_core_web_sm")
# define the core function
def text_analysis(text):
doc = nlp(text)
html = displacy.render(doc, style="dep", page=True)
html = (
"<div style='max-width:100%; max-height:360px; overflow:auto'>"
+ html
+ "</div>"
)
pos_count = {
"char_count": len(text),
"token_count": 0,
}
pos_tokens = []
for token in doc:
pos_tokens.extend([(token.text, token.pos_), (" ", None)])
return pos_tokens, pos_count, html
# define the interface, with a textbox input, and three outputs: HighlightedText, JSON, and HTML
demo = gr.Interface(
text_analysis,
gr.Textbox(placeholder="Enter sentence here..."),
["highlight", "json", "html"],
examples=[
["What a beautiful morning for a walk!"],
["It was the best of times, it was the worst of times."],
],
)
# launch
demo.launch()