Spaces:
Runtime error
Runtime error
File size: 3,827 Bytes
71fdb6a 9eb138a 9203701 9eb138a 9203701 71fdb6a 9203701 9eb138a 9203701 b1afd25 e8106fb b1afd25 9eb138a 7074257 71fdb6a 125944e fd5f563 f941f95 4bcd188 07c0dff c39439f f2323af 4bcd188 f2323af 4bcd188 07c0dff 4bcd188 c39439f f2323af 4bcd188 f2323af ba19334 4bcd188 7074257 71fdb6a 9eb138a 9f4588e 9eb138a b1afd25 9eb138a 71fdb6a |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
#100
import gradio as gr
import os
os.system('python -m spacy download en_core_web_sm')
import spacy
from spacy import displacy
import pandas as pd
from io import BytesIO
import base64
nlp = spacy.load("en_core_web_sm")
def render_dep_chart(doc):
svg = displacy.render(doc, style="dep")
img = BytesIO()
img.write(svg.encode())
img.seek(0)
b64 = base64.b64encode(img.read()).decode()
return f"<img id='zoomable' src='data:image/svg+xml;base64,{b64}'/>"
def text_analysis(text):
doc = nlp(text)
dependency_parsing = render_dep_chart(doc)
visual1 = (
"<div style='max-width:100%; overflow:auto'>"
+ dependency_parsing
+ "</div>"
)
rows = []
for token in doc:
rows.append((token.text, token.lemma_, token.pos_, token.tag_, token.dep_,
token.shape_, token.is_alpha, token.is_stop))
table = pd.DataFrame(rows, columns = ["TEXT", "LEMMA","POS","TAG","DEP","SHAPE","ALPHA","STOP"])
return table, visual1
css = """
footer {display:none !important}
.overflow-x-scroll {
overflow-x: scroll !important;
height: 15rem !important;
overflow-y: scroll !important;
}
.hover\:bg-orange-50:hover {
--tw-bg-opacity: 1 !important;
background-color: rgb(229,225,255) !important;
}
#zoomable{
cursor: pointer;
height: 13em;
max-width: none !important;
}
.output-markdown h1, .output-markdown h2{
z-index: 14;
align-self: flex-start;
min-width: 0px;
order: 5;
min-height: 0px;
height: max-content;
flex-grow: 0;
flex-shrink: 0;
width: calc(100% - 0px);
margin: 5px 0px;
white-space: pre-wrap;
overflow: visible;
word-break: break-word;
font-size: 18px !important;
font-weight: 500 !important;
color: rgb(9, 23, 71) !important;
line-height: 1 !important;
border-radius: 0px !important;
opacity: 1 !important;
}
.gr-button-lg {
z-index: 14;
width: 113px;
height: 30px;
left: 0px;
top: 0px;
padding: 0px;
cursor: pointer !important;
background: none rgb(17, 20, 45) !important;
border: none !important;
text-align: center !important;
font-size: 14px !important;
font-weight: 500 !important;
color: rgb(255, 255, 255) !important;
line-height: 1 !important;
border-radius: 6px !important;
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
box-shadow: none !important;
}
.gr-button-lg:hover{
z-index: 14;
width: 113px;
height: 30px;
left: 0px;
top: 0px;
padding: 0px;
cursor: pointer !important;
background: none rgb(66, 133, 244) !important;
border: none !important;
text-align: center !important;
font-size: 14px !important;
font-weight: 500 !important;
color: rgb(255, 255, 255) !important;
line-height: 1 !important;
border-radius: 6px !important;
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
}
"""
with gr.Blocks(title="Analyze Text | Data Science Dojo", css = css) as demo:
with gr.Row():
inp = gr.Textbox(placeholder="Enter text to analyze...", label="Input")
btn = gr.Button("Analyze text")
gr.Markdown("""
# Analysis""")
with gr.Row():
table = gr.Dataframe()
gr.Markdown("""## Dependency Parsing""")
with gr.Row():
visual1 = gr.HTML()
with gr.Row():
gr.Examples(
examples=[
["Data Science Dojo is the leading platform providing training in data science, data analytics, and machine learning."],
["It's the best time to execute the plan."],
], fn=text_analysis, inputs=inp, outputs=[table, visual1], cache_examples=True)
btn.click(fn=text_analysis, inputs=inp, outputs=[table, visual1])
demo.launch() |