Spaces:
Running
Running
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spacy
|
3 |
+
from spacy import displacy
|
4 |
+
|
5 |
+
nlp = spacy.load("en_core_web_md")
|
6 |
+
|
7 |
+
# -- Main functions --
|
8 |
+
def syntacticTree(expr):
|
9 |
+
doc = nlp(expr)
|
10 |
+
depths = {}
|
11 |
+
|
12 |
+
def walk_tree(tk, depth):
|
13 |
+
depths[tk.text] = depth
|
14 |
+
if tk.n_lefts + tk.n_rights > 0: # if Doesn't root
|
15 |
+
[walk_tree(child, depth + 1) for child in tk.children]
|
16 |
+
|
17 |
+
[walk_tree(sent.root, 0) for sent in doc.sents]
|
18 |
+
|
19 |
+
flat_tree = [tk.text+" - "+str(depths[tk.text]) for tk in doc]
|
20 |
+
image_tree = displacy.render(doc, style='dep', options={'distance': 100})
|
21 |
+
image_tree = "<center><div style='max-width: 800px; overflow-x:auto;'>"+image_tree+"</div></center>"
|
22 |
+
|
23 |
+
return image_tree,flat_tree
|
24 |
+
|
25 |
+
# -- Interface --
|
26 |
+
demo = gr.Blocks(css=".container { max-width: 900px; margin: auto;}", title="Syntactic tree generator")
|
27 |
+
|
28 |
+
with demo:
|
29 |
+
gr.Markdown("""<center> <h1>Syntactic tree generator</h1> </center>""")
|
30 |
+
with gr.Row():
|
31 |
+
with gr.Column():
|
32 |
+
gr.Image(value="https://img.unocero.com/2019/11/facebook-app-para-hacer-memes-1-1024x576.jpg",label="", type="URL")
|
33 |
+
with gr.Column():
|
34 |
+
input_expr = gr.Textbox(label="Input", placeholder="Enter an expression here")
|
35 |
+
out_flat_tree = gr.Text(label="Flat tree", value="")
|
36 |
+
|
37 |
+
out_image_tree = gr.HTML(label="")
|
38 |
+
|
39 |
+
gr.Examples(
|
40 |
+
examples=[ "Glass in guys hand on right first guy",
|
41 |
+
"I have been happy",
|
42 |
+
"The happy spiders",
|
43 |
+
"Gradio is a package that allows users to create simple web apps with just a few lines of code"],
|
44 |
+
inputs=input_expr,
|
45 |
+
examples_per_page=20,
|
46 |
+
)
|
47 |
+
|
48 |
+
inp.change(
|
49 |
+
fn=syntacticTree,
|
50 |
+
inputs=input_expr,
|
51 |
+
outputs=[out_image_tree, out_flat_tree])
|
52 |
+
|
53 |
+
demo.launch()
|