2021-05-24 23:31:44 +08:00
|
|
|
import spacy
|
|
|
|
from spacy import displacy
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2021-05-24 23:31:44 +08:00
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
nlp = spacy.load("en_core_web_sm")
|
|
|
|
|
|
|
|
|
|
|
|
def text_analysis(text):
|
|
|
|
doc = nlp(text)
|
|
|
|
html = displacy.render(doc, style="dep", page=True)
|
2022-01-21 21:44:12 +08:00
|
|
|
html = (
|
|
|
|
"<div style='max-width:100%; max-height:360px; overflow:auto'>"
|
|
|
|
+ html
|
|
|
|
+ "</div>"
|
|
|
|
)
|
2021-05-24 23:31:44 +08:00
|
|
|
pos_count = {
|
|
|
|
"char_count": len(text),
|
|
|
|
"token_count": 0,
|
|
|
|
}
|
|
|
|
pos_tokens = []
|
|
|
|
|
|
|
|
for token in doc:
|
|
|
|
pos_tokens.extend([(token.text, token.pos_), (" ", None)])
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2021-05-24 23:31:44 +08:00
|
|
|
return pos_tokens, pos_count, html
|
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface(
|
|
|
|
text_analysis,
|
|
|
|
gr.inputs.Textbox(placeholder="Enter sentence here..."),
|
2022-01-21 21:44:12 +08:00
|
|
|
["highlight", "key_values", "html"],
|
2021-05-24 23:31:44 +08:00
|
|
|
examples=[
|
|
|
|
["What a beautiful morning for a walk!"],
|
|
|
|
["It was the best of times, it was the worst of times."],
|
2022-01-21 21:44:12 +08:00
|
|
|
],
|
2021-05-24 23:31:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
iface.test_launch()
|
|
|
|
if __name__ == "__main__":
|
|
|
|
iface.launch()
|