Spaces:
Runtime error
Runtime error
Improvements
Browse files- .gitignore +1 -0
- app.py +58 -89
- css/style.css +167 -0
- requirements.txt +1 -1
- utils.py +92 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
__pycache__
|
app.py
CHANGED
@@ -1,111 +1,80 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import spacy
|
4 |
-
from spacy import displacy
|
5 |
|
6 |
-
|
7 |
|
8 |
-
# --
|
9 |
-
|
10 |
-
|
11 |
-
def postag(tk):
|
12 |
-
tag = ""
|
13 |
-
plural_tags = ["NNS", "NNPS"]
|
14 |
-
if tk.tag_ in plural_tags:
|
15 |
-
tag = " ({}) (Plural)".format(tk.tag_)
|
16 |
-
else:
|
17 |
-
tag = " ({})".format(tk.tag_)
|
18 |
-
return tag
|
19 |
-
|
20 |
-
def genFlatDepthTree(expr):
|
21 |
-
doc = nlp(expr)
|
22 |
-
root = next(doc.sents).root
|
23 |
-
node = Node(""+root.text+": (Root)"+postag(root), parent=None)
|
24 |
-
|
25 |
-
def tree(tk, last_node, depth):
|
26 |
-
if tk.n_lefts + tk.n_rights > 0:
|
27 |
-
for ch in tk.children:
|
28 |
-
tree(ch, Node(""+ch.text+": "+str(depth+1)+postag(ch), parent=last_node), depth+1)
|
29 |
-
|
30 |
-
tree(root, node, 0)
|
31 |
-
flat_tree = ""
|
32 |
-
|
33 |
-
for pre, fill, node in RenderTree(node):
|
34 |
-
flat_tree += """{}{}\n""".format(pre, node.name)
|
35 |
|
36 |
-
|
|
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
# [walk_tree(child, depth + 1) for child in tk.children]
|
48 |
|
49 |
-
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
# return image_tree,flat_tree
|
56 |
-
|
57 |
-
# -- Interface --
|
58 |
-
demo = gr.Blocks(css=".container { max-width: 900px; margin: auto;}", title="Syntactic tree generator")
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
with gr.Column():
|
64 |
-
gr.Image(value="https://img.unocero.com/2019/11/facebook-app-para-hacer-memes-1-1024x576.jpg",label="", type="URL")
|
65 |
-
with gr.Column():
|
66 |
-
input_expr = gr.Textbox(label="Input", placeholder="Enter an expression here")
|
67 |
-
out_flat_tree = gr.Textbox(label="Flat tree", value="")
|
68 |
-
with gr.Row(): btn_get_tree = gr.Button(value="Get Tree")
|
69 |
-
|
70 |
-
|
71 |
-
out_image_tree = gr.HTML(label="")
|
72 |
|
73 |
gr.Examples(
|
74 |
-
|
|
|
75 |
"glass in guys hand on right first guy",
|
76 |
"i have been happy",
|
77 |
"the happy spiders",
|
78 |
"the best dog out there",
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
examples_per_page=25,
|
98 |
)
|
99 |
|
100 |
-
# input_expr.change(
|
101 |
-
# fn=genFlatDepthTree,
|
102 |
-
# inputs=input_expr,
|
103 |
-
# outputs=[out_image_tree, out_flat_tree])
|
104 |
btn_get_tree.click(
|
105 |
-
fn=
|
106 |
-
inputs=
|
107 |
-
outputs=[
|
|
|
108 |
)
|
109 |
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from utils import Pipeline
|
|
|
|
|
3 |
|
4 |
+
pip = Pipeline()
|
5 |
|
6 |
+
# -- Interface --
|
7 |
+
iface = gr.Blocks(css="css/style.css")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
with iface:
|
10 |
+
gr.Markdown("<center> <h2>🌳 Syntactic Tree Generator 🌳</h2> </center>")
|
11 |
|
12 |
+
with gr.Row():
|
13 |
+
with gr.Column():
|
14 |
+
image = gr.Image(
|
15 |
+
value="https://img.unocero.com/2019/11/facebook-app-para-hacer-memes-1-1024x576.jpg",show_label=False
|
16 |
+
)
|
17 |
|
18 |
+
with gr.Column():
|
19 |
+
in_sentence = gr.Textbox(
|
20 |
+
label="Input",
|
21 |
+
placeholder="Enter an expression here"
|
22 |
+
)
|
23 |
|
24 |
+
btn_get_tree = gr.Button(
|
25 |
+
value="Generate Tree!"
|
26 |
+
)
|
|
|
27 |
|
28 |
+
out_str_tree = gr.Textbox(
|
29 |
+
label="Flat tree",
|
30 |
+
elem_id="no-outline"
|
31 |
+
)
|
32 |
|
33 |
+
error = gr.HTML(
|
34 |
+
show_label=False
|
35 |
+
)
|
36 |
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
out_html_tree = gr.HTML(
|
39 |
+
show_label=False
|
40 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
gr.Examples(
|
43 |
+
inputs = in_sentence,
|
44 |
+
examples = [
|
45 |
"glass in guys hand on right first guy",
|
46 |
"i have been happy",
|
47 |
"the happy spiders",
|
48 |
"the best dog out there",
|
49 |
+
"girl 's face",
|
50 |
+
'bottom grass',
|
51 |
+
'rock people are sitting on',
|
52 |
+
'blue sky center below clouds',
|
53 |
+
'group of people on left',
|
54 |
+
'tree middle',
|
55 |
+
'the lump of grass above the bright rock in the bottom left',
|
56 |
+
'berries in middle',
|
57 |
+
'red shirt kid',
|
58 |
+
'middle rock',
|
59 |
+
'grass below the person in the straw hat',
|
60 |
+
'grass on the left',
|
61 |
+
'wall between stairs 2nd lv on right',
|
62 |
+
'the large group of clouds',
|
63 |
+
'sky top left',
|
64 |
+
'rock structure on far right',
|
65 |
+
'left donkey'],
|
66 |
+
examples_per_page=10,
|
|
|
67 |
)
|
68 |
|
|
|
|
|
|
|
|
|
69 |
btn_get_tree.click(
|
70 |
+
fn = pip.compute,
|
71 |
+
inputs = in_sentence,
|
72 |
+
outputs = [error, out_html_tree, out_str_tree],
|
73 |
+
api_name= "gen_tree"
|
74 |
)
|
75 |
|
76 |
+
iface.launch(
|
77 |
+
server_name="0.0.0.0",
|
78 |
+
# server_port=9090,
|
79 |
+
# share = True
|
80 |
+
)
|
css/style.css
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.container {
|
2 |
+
max-width: 85%;
|
3 |
+
margin: auto;
|
4 |
+
}
|
5 |
+
|
6 |
+
h1, h2, h3, h4, h5, h6 {
|
7 |
+
margin-top: 0;
|
8 |
+
margin-bottom: 0.5rem;
|
9 |
+
}
|
10 |
+
|
11 |
+
h1, h2, h3, h4, h5, h6,
|
12 |
+
.h1, .h2, .h3, .h4, .h5, .h6 {
|
13 |
+
margin-bottom: 0.5rem;
|
14 |
+
font-weight: 500;
|
15 |
+
line-height: 1.2;
|
16 |
+
}
|
17 |
+
|
18 |
+
h1, .h1 {
|
19 |
+
font-size: 2.5rem;
|
20 |
+
}
|
21 |
+
|
22 |
+
h2, .h2 {
|
23 |
+
font-size: 2rem;
|
24 |
+
}
|
25 |
+
|
26 |
+
h3, .h3 {
|
27 |
+
font-size: 1.75rem;
|
28 |
+
}
|
29 |
+
|
30 |
+
h4, .h4 {
|
31 |
+
font-size: 1.5rem;
|
32 |
+
}
|
33 |
+
|
34 |
+
h5, .h5 {
|
35 |
+
font-size: 1.25rem;
|
36 |
+
}
|
37 |
+
|
38 |
+
h6, .h6 {
|
39 |
+
font-size: 1rem;
|
40 |
+
}
|
41 |
+
|
42 |
+
.no-outline {
|
43 |
+
border: 0px none;
|
44 |
+
}
|
45 |
+
|
46 |
+
.alert {
|
47 |
+
position: relative;
|
48 |
+
padding: 0.75rem 1.25rem;
|
49 |
+
margin-bottom: 1rem;
|
50 |
+
border: 1px solid transparent;
|
51 |
+
border-radius: 0.25rem;
|
52 |
+
}
|
53 |
+
|
54 |
+
.alert-primary {
|
55 |
+
color: #004085;
|
56 |
+
background-color: #cce5ff;
|
57 |
+
border-color: #b8daff;
|
58 |
+
}
|
59 |
+
|
60 |
+
.alert-secondary {
|
61 |
+
color: #383d41;
|
62 |
+
background-color: #e2e3e5;
|
63 |
+
border-color: #d6d8db;
|
64 |
+
}
|
65 |
+
|
66 |
+
.alert-success {
|
67 |
+
color: #155724;
|
68 |
+
background-color: #d4edda;
|
69 |
+
border-color: #c3e6cb;
|
70 |
+
}
|
71 |
+
|
72 |
+
.alert-info {
|
73 |
+
color: #0c5460;
|
74 |
+
background-color: #d1ecf1;
|
75 |
+
border-color: #bee5eb;
|
76 |
+
}
|
77 |
+
|
78 |
+
.alert-warning {
|
79 |
+
color: #856404;
|
80 |
+
background-color: #fff3cd;
|
81 |
+
border-color: #ffeeba;
|
82 |
+
}
|
83 |
+
|
84 |
+
.alert-danger {
|
85 |
+
color: #721c24;
|
86 |
+
background-color: #f8d7da;
|
87 |
+
border-color: #f5c6cb;
|
88 |
+
}
|
89 |
+
|
90 |
+
.alert-light {
|
91 |
+
color: #818182;
|
92 |
+
background-color: #fefefe;
|
93 |
+
border-color: #fdfdfe;
|
94 |
+
}
|
95 |
+
|
96 |
+
.alert-dark {
|
97 |
+
color: #1b1e21;
|
98 |
+
background-color: #d6d8d9;
|
99 |
+
border-color: #c6c8ca;
|
100 |
+
}
|
101 |
+
|
102 |
+
.btn {
|
103 |
+
display: inline-block;
|
104 |
+
font-weight: 400;
|
105 |
+
color: #212529;
|
106 |
+
text-align: center;
|
107 |
+
vertical-align: middle;
|
108 |
+
-webkit-user-select: none;
|
109 |
+
-moz-user-select: none;
|
110 |
+
-ms-user-select: none;
|
111 |
+
user-select: none;
|
112 |
+
background-color: transparent;
|
113 |
+
border: 1px solid transparent;
|
114 |
+
padding: 0.375rem 0.75rem;
|
115 |
+
font-size: 1rem;
|
116 |
+
line-height: 1.5;
|
117 |
+
border-radius: 0.25rem;
|
118 |
+
transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
|
119 |
+
}
|
120 |
+
|
121 |
+
.btn-primary {
|
122 |
+
color: #fff;
|
123 |
+
background-color: #007bff;
|
124 |
+
border-color: #007bff;
|
125 |
+
}
|
126 |
+
|
127 |
+
.btn-secondary {
|
128 |
+
color: #fff;
|
129 |
+
background-color: #6c757d;
|
130 |
+
border-color: #6c757d;
|
131 |
+
}
|
132 |
+
|
133 |
+
.btn-success {
|
134 |
+
color: #fff;
|
135 |
+
background-color: #28a745;
|
136 |
+
border-color: #28a745;
|
137 |
+
}
|
138 |
+
|
139 |
+
.btn-info {
|
140 |
+
color: #fff;
|
141 |
+
background-color: #17a2b8;
|
142 |
+
border-color: #17a2b8;
|
143 |
+
}
|
144 |
+
|
145 |
+
.btn-warning {
|
146 |
+
color: #212529;
|
147 |
+
background-color: #ffc107;
|
148 |
+
border-color: #ffc107;
|
149 |
+
}
|
150 |
+
|
151 |
+
.btn-danger {
|
152 |
+
color: #fff;
|
153 |
+
background-color: #dc3545;
|
154 |
+
border-color: #dc3545;
|
155 |
+
}
|
156 |
+
|
157 |
+
.btn-light {
|
158 |
+
color: #212529;
|
159 |
+
background-color: #f8f9fa;
|
160 |
+
border-color: #f8f9fa;
|
161 |
+
}
|
162 |
+
|
163 |
+
.btn-dark {
|
164 |
+
color: #fff;
|
165 |
+
background-color: #343a40;
|
166 |
+
border-color: #343a40;
|
167 |
+
}
|
requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
spacy
|
2 |
anytree
|
3 |
-
https://huggingface.co/spacy/en_core_web_md/resolve/main/en_core_web_md-any-py3-none-any.whl
|
|
|
1 |
spacy
|
2 |
anytree
|
3 |
+
# https://huggingface.co/spacy/en_core_web_md/resolve/main/en_core_web_md-any-py3-none-any.whl
|
utils.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spacy
|
2 |
+
from typing import Tuple
|
3 |
+
from spacy import displacy
|
4 |
+
from anytree import Node, RenderTree
|
5 |
+
|
6 |
+
class Pipeline:
|
7 |
+
def __init__(
|
8 |
+
self
|
9 |
+
) -> None:
|
10 |
+
|
11 |
+
self.nlp = spacy.load("en_core_web_md")
|
12 |
+
self.__ch_html_tree = None
|
13 |
+
self.__ch_str_tree = None
|
14 |
+
self.__ch_sentence = None
|
15 |
+
|
16 |
+
def __postag(
|
17 |
+
self,
|
18 |
+
tk: str
|
19 |
+
) -> str:
|
20 |
+
|
21 |
+
tag = ""
|
22 |
+
plural_tags = ["NNS", "NNPS"]
|
23 |
+
if tk.tag_ in plural_tags:
|
24 |
+
tag = " ({}) (Plural)".format(tk.tag_)
|
25 |
+
else:
|
26 |
+
tag = " ({})".format(tk.tag_)
|
27 |
+
return tag
|
28 |
+
|
29 |
+
def __genSyntacticTree(
|
30 |
+
self,
|
31 |
+
expr: str
|
32 |
+
) -> Tuple[str,str]:
|
33 |
+
|
34 |
+
doc = self.nlp(expr)
|
35 |
+
root = next(doc.sents).root
|
36 |
+
node = Node("" + root.text + ": (Root)" + self.__postag(root), parent=None)
|
37 |
+
|
38 |
+
def tree(
|
39 |
+
tk: str,
|
40 |
+
last_node: Node,
|
41 |
+
depth: int
|
42 |
+
) -> None:
|
43 |
+
|
44 |
+
if tk.n_lefts + tk.n_rights > 0:
|
45 |
+
for child in tk.children:
|
46 |
+
tree(
|
47 |
+
child,
|
48 |
+
Node(
|
49 |
+
"" + child.text + ": " + str(depth + 1) + self.__postag(child),
|
50 |
+
parent=last_node
|
51 |
+
),
|
52 |
+
depth+1
|
53 |
+
)
|
54 |
+
|
55 |
+
tree(root, node, 0)
|
56 |
+
syntactic_str_tree = ""
|
57 |
+
|
58 |
+
for pre, fill, node in RenderTree(node):
|
59 |
+
syntactic_str_tree += """{}{}\n""".format(pre, node.name)
|
60 |
+
|
61 |
+
syntactic_tree = displacy.render(doc, style='dep', options={'distance': 100})
|
62 |
+
syntactic_html_tree = f"""
|
63 |
+
<center>
|
64 |
+
<div style='max-width: 800px; overflow-x:auto;'>
|
65 |
+
{syntactic_tree}
|
66 |
+
</div>
|
67 |
+
</center>
|
68 |
+
"""
|
69 |
+
return syntactic_html_tree, syntactic_str_tree
|
70 |
+
|
71 |
+
def compute(
|
72 |
+
self,
|
73 |
+
sentence: str
|
74 |
+
) -> Tuple[str,str,str]:
|
75 |
+
|
76 |
+
error = ""
|
77 |
+
error_template = """
|
78 |
+
<center>
|
79 |
+
<div class="alert alert-warning" role="alert">
|
80 |
+
<h6><b>{}</b></h6>
|
81 |
+
</div>
|
82 |
+
</center>
|
83 |
+
"""
|
84 |
+
if sentence.strip() == "":
|
85 |
+
error = error_template.format("The sentence can not be empty!")
|
86 |
+
return error, "", ""
|
87 |
+
|
88 |
+
if sentence != self.__ch_sentence:
|
89 |
+
self.__ch_sentence = sentence
|
90 |
+
self.__ch_html_tree, self.__ch_str_tree = self.__genSyntacticTree(sentence)
|
91 |
+
|
92 |
+
return error, self.__ch_html_tree, self.__ch_str_tree
|