gradio/demo/text_analysis.py

36 lines
773 B
Python
Raw Normal View History

2020-08-22 05:20:05 +08:00
# Demo: (Textbox) -> (HighlightedText, KeyValues, HTML)
2020-08-20 05:27:22 +08:00
import spacy
from spacy import displacy
import gradio as gr
nlp = spacy.load("en_core_web_sm")
2020-08-28 23:56:03 +08:00
2020-08-20 05:27:22 +08:00
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
2020-08-28 23:56:03 +08:00
io = gr.Interface(
2020-08-20 05:27:22 +08:00
text_analysis,
gr.inputs.Textbox(placeholder="Enter sentence here..."),
[
"highlight", "key_values", "html"
]
2020-08-28 23:56:03 +08:00
)
io.test_launch()
io.launch()