ai-kit / text_analysis /ui_text_analysis.py
Kim Adams
all of the things
2cd1c76
Raw History Blame Contribute Delete
2.59 kB
import gradio as gr
import os
import base64
import spacy
from utilities import constants
from spacy import displacy
os.system('python -m spacy download en_core_web_sm')
nlp = spacy.load("en_core_web_sm")
input_examples=[
"Every day may not be good, but there's something good in every day.",
"The best preparation for tomorrow is doing your best today.",
"Believe you can, and you're halfway there."]
def AnalyzeText(text):
doc = nlp(text)
svg = displacy.render(doc, style='dep')
svg_base64_encoded = base64.b64encode(svg.encode('utf-8')).decode('utf-8')
nlp_html = f"""
<div style='display: flex; justify-content: center; align-items: center; width: 100%; overflow-x: auto;'>
<img src="data:image/svg+xml;base64,{svg_base64_encoded}"
style='display: block; max-width: 100%; height: auto; margin: auto;'/>
</div>
"""
pos_count = {
"char_count": len(text),
"token_count": len(doc)
}
pos_tokens = []
for token in doc:
pos_tokens.extend([(token.text, token.pos_), (" ", None)])
return pos_tokens, pos_count, nlp_html
def Clear(clearBtn):
return(constants.NLP_PROMPT, [], {}, [])
with gr.Blocks() as ui:
label = gr.Label(show_label=False, value=constants.TEXT_ANALYSIS, container=False)
with gr.Column():
with gr.Row():
with gr.Column():
gr.Markdown(constants.PURPOSE_MD)
gr.Markdown(constants.NLP_ANALYSIS_PURPOSE)
with gr.Column():
gr.Markdown(constants.DIRECTIONS_MD)
gr.Markdown(value=constants.NLP_ANALYSIS_DIRECTIONS)
with gr.Row():
with gr.Column():
inputString=gr.Textbox(placeholder=constants.NLP_PROMPT,
label="Input Text", lines=3,interactive=True)
with gr.Row():
clearBtn=gr.Button(constants.CLEAR, variant="secondary")
submitBtn=gr.Button(constants.SUBMIT, variant="primary")
with gr.Column():
posTags=gr.HighlightedText(label=constants.TOKENS)
gr.Markdown(constants.NLP_ANALYSIS_MD)
posCount=gr.JSON()
inputExampleSelect = gr.Examples(input_examples,inputs=[inputString],label="Or select an example." )
gr.Markdown(constants.NLP_POS_MAP_MD)
posTokens=gr.HTML()
submitBtn.click(AnalyzeText, inputs=[inputString], outputs=[posTags,posCount,posTokens])
clearBtn.click(Clear, inputs=[clearBtn], outputs=[inputString,posTags,posCount,posTokens])