Spaces:
Runtime error
Runtime error
| #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() |