File size: 2,588 Bytes
26ead64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cd1c76
26ead64
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

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])