Victoria Slocum commited on
Commit
2d2033e
1 Parent(s): df8e4de

fix: scrollbar, feat: download depen

Browse files
Files changed (2) hide show
  1. app.py +77 -22
  2. scrollbar.css +16 -7
app.py CHANGED
@@ -1,9 +1,13 @@
 
1
  import spacy
2
  from spacy import displacy
3
  import random
4
  from spacy.tokens import Span
5
  import gradio as gr
6
  import pandas as pd
 
 
 
7
 
8
  DEFAULT_MODEL = "en_core_web"
9
  DEFAULT_TEXT = "Apple is looking at buying U.K. startup for $1 billion."
@@ -16,6 +20,7 @@ texts = {"en": DEFAULT_TEXT, "ca": "Apple està buscant comprar una startup del
16
  "ja": "アップルがイギリスの新興企業を10億ドルで購入を検討", "ko": "애플이 영국의 스타트업을 10억 달러에 인수하는 것을 알아보고 있다.", "lt": "Jaunikis pirmąją vestuvinę naktį iškeitė į areštinės gultą", "nb": "Apple vurderer å kjøpe britisk oppstartfirma for en milliard dollar.", "nl": "Apple overweegt om voor 1 miljard een U.K. startup te kopen",
17
  "pl": "Poczuł przyjemną woń mocnej kawy.", "pt": "Apple está querendo comprar uma startup do Reino Unido por 100 milhões de dólares", "ro": "Apple plănuiește să cumpere o companie britanică pentru un miliard de dolari", "ru": "Apple рассматривает возможность покупки стартапа из Соединённого Королевства за $1 млрд", "sv": "Apple överväger att köpa brittisk startup för 1 miljard dollar.", "zh": "作为语言而言,为世界使用人数最多的语言,目前世界有五分之一人口做为母语。"}
18
 
 
19
 
20
  def get_all_models():
21
  with open("requirements.txt") as f:
@@ -31,22 +36,44 @@ def get_all_models():
31
 
32
  models = get_all_models()
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  def dependency(text, col_punct, col_phrase, compact, bg, font, model):
36
  nlp = spacy.load(model + "_sm")
37
  doc = nlp(text)
38
  options = {"compact": compact, "collapse_phrases": col_phrase,
39
  "collapse_punct": col_punct, "bg": bg, "color": font}
40
- html = displacy.render(doc, style="dep", options=options)
41
- return html
 
42
 
43
 
44
  def entity(text, ents, model):
45
  nlp = spacy.load(model + "_sm")
46
  doc = nlp(text)
47
  options = {"ents": ents}
48
- html = displacy.render(doc, style="ent", options=options)
49
- return html
 
50
 
51
 
52
  def token(text, attributes, model):
@@ -128,8 +155,9 @@ def span(text, span1, span2, label1, label2, model):
128
  Span(doc, idx2_1, idx2_2, label2),
129
  ]
130
 
131
- html = displacy.render(doc, style="span")
132
- return html
 
133
 
134
 
135
  def get_text(model):
@@ -196,8 +224,19 @@ with demo:
196
  label="Text Color", value="black")
197
 
198
  depen_output = gr.HTML(value=dependency(
199
- DEFAULT_TEXT, True, True, False, DEFAULT_COLOR, "black", DEFAULT_MODEL))
200
- dep_button = gr.Button("Generate Dependency Parser")
 
 
 
 
 
 
 
 
 
 
 
201
  gr.Markdown(" ")
202
  with gr.Box():
203
  with gr.Column():
@@ -205,11 +244,20 @@ with demo:
205
  "## [Entity Recognizer](https://spacy.io/usage/visualizers#ent)")
206
  gr.Markdown(
207
  "The entity visualizer highlights named entities and their labels in a text.")
208
- entity_input = gr.CheckboxGroup(
209
  DEFAULT_ENTS, value=DEFAULT_ENTS)
210
- entity_output = gr.HTML(value=entity(
211
  DEFAULT_TEXT, DEFAULT_ENTS, DEFAULT_MODEL))
212
- ent_button = gr.Button("Generate Entity Recognizer")
 
 
 
 
 
 
 
 
 
213
  with gr.Box():
214
  with gr.Column():
215
  gr.Markdown(
@@ -224,7 +272,7 @@ with demo:
224
  gr.Markdown("")
225
  tok_output = gr.Dataframe(headers=DEFAULT_TOK_ATTR, value=default_token(
226
  DEFAULT_TEXT, DEFAULT_TOK_ATTR, DEFAULT_MODEL), overflow_row_behaviour="paginate")
227
- tok_button = gr.Button("Generate Token Properties")
228
  with gr.Box():
229
  with gr.Column():
230
  gr.Markdown(
@@ -244,7 +292,7 @@ with demo:
244
  with gr.Column():
245
  gr.Markdown("")
246
  sim_random_button = gr.Button("Generate random words")
247
- sim_button = gr.Button("Generate similarity")
248
  with gr.Box():
249
  with gr.Column():
250
  gr.Markdown(
@@ -276,31 +324,38 @@ with demo:
276
  gr.Markdown("")
277
  span_output = gr.HTML(value=span(
278
  DEFAULT_TEXT, "U.K. startup", "U.K.", "ORG", "GPE", DEFAULT_MODEL))
279
- gr.Markdown(value="")
280
- gr.Markdown(value="")
281
- span_button = gr.Button("Generate spans")
 
 
 
 
 
 
 
282
 
283
  model_input.change(get_text, inputs=[model_input], outputs=text_input)
284
  button.click(dependency, inputs=[
285
- text_input, col_punct, col_phrase, compact, bg, text, model_input], outputs=depen_output)
286
  button.click(
287
- entity, inputs=[text_input, entity_input, model_input], outputs=entity_output)
288
  button.click(
289
  token, inputs=[text_input, tok_input, model_input], outputs=tok_output)
290
  button.click(vectors, inputs=[sim_text1,
291
  sim_text2, model_input], outputs=sim_output)
292
  button.click(
293
- span, inputs=[text_input, span1, span2, label1, label2, model_input], outputs=span_output)
294
  dep_button.click(dependency, inputs=[
295
- text_input, col_punct, col_phrase, compact, bg, text, model_input], outputs=depen_output)
296
  ent_button.click(
297
- entity, inputs=[text_input, entity_input, model_input], outputs=entity_output)
298
  tok_button.click(
299
  token, inputs=[text_input, tok_input, model_input], outputs=[tok_output])
300
  sim_button.click(vectors, inputs=[
301
  sim_text1, sim_text2, model_input], outputs=sim_output)
302
  span_button.click(
303
- span, inputs=[text_input, span1, span2, label1, label2, model_input], outputs=span_output)
304
  sim_random_button.click(random_vectors, inputs=[text_input, model_input], outputs=[
305
  sim_output, sim_text1, sim_text2])
306
  demo.launch()
 
1
+ from turtle import down
2
  import spacy
3
  from spacy import displacy
4
  import random
5
  from spacy.tokens import Span
6
  import gradio as gr
7
  import pandas as pd
8
+ import cairosvg
9
+ import base64
10
+
11
 
12
  DEFAULT_MODEL = "en_core_web"
13
  DEFAULT_TEXT = "Apple is looking at buying U.K. startup for $1 billion."
 
20
  "ja": "アップルがイギリスの新興企業を10億ドルで購入を検討", "ko": "애플이 영국의 스타트업을 10억 달러에 인수하는 것을 알아보고 있다.", "lt": "Jaunikis pirmąją vestuvinę naktį iškeitė į areštinės gultą", "nb": "Apple vurderer å kjøpe britisk oppstartfirma for en milliard dollar.", "nl": "Apple overweegt om voor 1 miljard een U.K. startup te kopen",
21
  "pl": "Poczuł przyjemną woń mocnej kawy.", "pt": "Apple está querendo comprar uma startup do Reino Unido por 100 milhões de dólares", "ro": "Apple plănuiește să cumpere o companie britanică pentru un miliard de dolari", "ru": "Apple рассматривает возможность покупки стартапа из Соединённого Королевства за $1 млрд", "sv": "Apple överväger att köpa brittisk startup för 1 miljard dollar.", "zh": "作为语言而言,为世界使用人数最多的语言,目前世界有五分之一人口做为母语。"}
22
 
23
+ button_css = "--tw-border-opacity: 1; border-color: rgb(75 85 99 / var(--tw-border-opacity)); --tw-gradient-from: #4b5563; --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to, rgb(75 85 99 / 0));--tw-gradient-to: #374151; --tw-text-opacity: 1; color: rgb(255 255 255 / var(--tw-text-opacity)); background-color: rgb(55 65 81 / var(--tw-bg-opacity)); border-radius: 0.5rem; padding-top: 0.5rem; padding-bottom: 0.5rem; padding-left: 1rem; padding-right: 1rem; font-size: 1rem; line-height: 1.5rem; font-weight: 600; -webkit-appearance: button; --tw-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05); --tw-shadow-colored: 0 1px 2px 0 var(--tw-shadow-color); box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow); border-width: 1px; align-items: center; justify-content: center; display: inline-flex;"
24
 
25
  def get_all_models():
26
  with open("requirements.txt") as f:
 
36
 
37
  models = get_all_models()
38
 
39
+ def download_svg(svg):
40
+ encode = base64.b64encode(bytes(svg, 'utf-8'))
41
+ img = 'data:image/svg+xml;base64,' + str(encode)[2:-1]
42
+ html = f'<a download="displacy.svg" href="{img}" style="{button_css}">Download as SVG</a>'
43
+ return html
44
+
45
+ # def download_png(svg):
46
+ # encode = base64.b64encode(bytes(svg, 'utf-8'))
47
+ # svg_uri = 'data:image/svg+xml;base64,' + str(encode)[2:-1]
48
+ # output = cairosvg.svg2png(url=svg_uri)
49
+ # encoded = base64.b64encode(output)
50
+ # img = 'data:image/png;base64,' + str(encoded)[2:-1]
51
+ # html = f'<a download="displacy.png" href="{img}" style="{button_css}">Download as PNG</a>'
52
+ # return html
53
+
54
+ # def download(type, svg):
55
+ # if type == 'png':
56
+ # return download_png(svg)
57
+ # elif type == 'svg':
58
+ # return download_svg(svg)
59
 
60
  def dependency(text, col_punct, col_phrase, compact, bg, font, model):
61
  nlp = spacy.load(model + "_sm")
62
  doc = nlp(text)
63
  options = {"compact": compact, "collapse_phrases": col_phrase,
64
  "collapse_punct": col_punct, "bg": bg, "color": font}
65
+ svg = displacy.render(doc, style="dep", options=options)
66
+ download = download_svg(svg)
67
+ return svg, download
68
 
69
 
70
  def entity(text, ents, model):
71
  nlp = spacy.load(model + "_sm")
72
  doc = nlp(text)
73
  options = {"ents": ents}
74
+ svg = displacy.render(doc, style="ent", options=options)
75
+ # download = download_svg('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:lang="en" id="97e9d3ac65344f2bb6e6ce517bd13b1e-0" class="displacy" width="1800" height="399.5" direction="ltr" style="max-width: none; height: 399.5px; color: black; font-family: Arial; direction: ltr">' + svg + "</svg>")
76
+ return svg
77
 
78
 
79
  def token(text, attributes, model):
 
155
  Span(doc, idx2_1, idx2_2, label2),
156
  ]
157
 
158
+ svg = displacy.render(doc, style="span")
159
+ # download = download_svg(svg)
160
+ return svg
161
 
162
 
163
  def get_text(model):
 
224
  label="Text Color", value="black")
225
 
226
  depen_output = gr.HTML(value=dependency(
227
+ DEFAULT_TEXT, True, True, False, DEFAULT_COLOR, "black", DEFAULT_MODEL)[0])
228
+ with gr.Row():
229
+ with gr.Column():
230
+ dep_button = gr.Button("Regenerate Dependency Parser", variant="primary")
231
+ with gr.Column():
232
+ dep_download_button = gr.HTML(value=download_svg(depen_output.value))
233
+ with gr.Column():
234
+ gr.Markdown(" ")
235
+ with gr.Column():
236
+ gr.Markdown(" ")
237
+
238
+
239
+
240
  gr.Markdown(" ")
241
  with gr.Box():
242
  with gr.Column():
 
244
  "## [Entity Recognizer](https://spacy.io/usage/visualizers#ent)")
245
  gr.Markdown(
246
  "The entity visualizer highlights named entities and their labels in a text.")
247
+ ent_input = gr.CheckboxGroup(
248
  DEFAULT_ENTS, value=DEFAULT_ENTS)
249
+ ent_output = gr.HTML(value=entity(
250
  DEFAULT_TEXT, DEFAULT_ENTS, DEFAULT_MODEL))
251
+ ent_button = gr.Button("Regenerate Entity Recognizer", variant="primary")
252
+ # with gr.Row():
253
+ # with gr.Column():
254
+ # ent_button = gr.Button("Regenerate Entity Recognizer", variant="primary")
255
+ # with gr.Column():
256
+ # ent_download_button = gr.HTML(value=download_svg(ent_output.value))
257
+ # with gr.Column():
258
+ # gr.Markdown(" ")
259
+ # with gr.Column():
260
+ # gr.Markdown(" ")
261
  with gr.Box():
262
  with gr.Column():
263
  gr.Markdown(
 
272
  gr.Markdown("")
273
  tok_output = gr.Dataframe(headers=DEFAULT_TOK_ATTR, value=default_token(
274
  DEFAULT_TEXT, DEFAULT_TOK_ATTR, DEFAULT_MODEL), overflow_row_behaviour="paginate")
275
+ tok_button = gr.Button("Regenerate Token Properties", variant="primary")
276
  with gr.Box():
277
  with gr.Column():
278
  gr.Markdown(
 
292
  with gr.Column():
293
  gr.Markdown("")
294
  sim_random_button = gr.Button("Generate random words")
295
+ sim_button = gr.Button("Regenerate similarity", variant="primary")
296
  with gr.Box():
297
  with gr.Column():
298
  gr.Markdown(
 
324
  gr.Markdown("")
325
  span_output = gr.HTML(value=span(
326
  DEFAULT_TEXT, "U.K. startup", "U.K.", "ORG", "GPE", DEFAULT_MODEL))
327
+ span_button = gr.Button("Regenerate Spans", variant="primary")
328
+ # with gr.Row():
329
+ # with gr.Column():
330
+ # span_button = gr.Button("Regenerate Spans", variant="primary")
331
+ # with gr.Column():
332
+ # span_download_button = gr.HTML(value=download_svg(span_output.value))
333
+ # with gr.Column():
334
+ # gr.Markdown(" ")
335
+ # with gr.Column():
336
+ # gr.Markdown(" ")
337
 
338
  model_input.change(get_text, inputs=[model_input], outputs=text_input)
339
  button.click(dependency, inputs=[
340
+ text_input, col_punct, col_phrase, compact, bg, text, model_input], outputs=[depen_output, dep_download_button])
341
  button.click(
342
+ entity, inputs=[text_input, ent_input, model_input], outputs=[ent_output])
343
  button.click(
344
  token, inputs=[text_input, tok_input, model_input], outputs=tok_output)
345
  button.click(vectors, inputs=[sim_text1,
346
  sim_text2, model_input], outputs=sim_output)
347
  button.click(
348
+ span, inputs=[text_input, span1, span2, label1, label2, model_input], outputs=[span_output])
349
  dep_button.click(dependency, inputs=[
350
+ text_input, col_punct, col_phrase, compact, bg, text, model_input], outputs=[depen_output, dep_download_button])
351
  ent_button.click(
352
+ entity, inputs=[text_input, ent_input, model_input], outputs=[ent_output])
353
  tok_button.click(
354
  token, inputs=[text_input, tok_input, model_input], outputs=[tok_output])
355
  sim_button.click(vectors, inputs=[
356
  sim_text1, sim_text2, model_input], outputs=sim_output)
357
  span_button.click(
358
+ span, inputs=[text_input, span1, span2, label1, label2, model_input], outputs=[span_output])
359
  sim_random_button.click(random_vectors, inputs=[text_input, model_input], outputs=[
360
  sim_output, sim_text1, sim_text2])
361
  demo.launch()
scrollbar.css CHANGED
@@ -1,22 +1,31 @@
1
- .overflow-auto::-webkit-scrollbar {
 
 
 
 
 
2
  -webkit-appearance: none;
3
  }
4
 
5
- .overflow-auto::-webkit-scrollbar:vertical {
6
- width: 11px;
7
  }
8
 
9
- .overflow-auto::-webkit-scrollbar:horizontal {
10
  height: 11px;
11
  }
12
 
13
- .overflow-auto::-webkit-scrollbar-thumb {
14
  border-radius: 8px;
15
  border: 2px solid white; /* should match background, can't be transparent */
16
  background-color: rgba(0, 0, 0, .5);
17
  }
18
 
19
- .overflow-auto::-webkit-scrollbar-track {
20
  background-color: #fff;
21
  border-radius: 8px;
22
- }
 
 
 
 
 
1
+ .output-html {
2
+ overflow-x: auto;
3
+ overflow-y: visible;
4
+ }
5
+
6
+ .output-html::-webkit-scrollbar {
7
  -webkit-appearance: none;
8
  }
9
 
10
+ .output-html::-webkit-scrollbar:vertical {
11
+ width: 0px;
12
  }
13
 
14
+ .output-html::-webkit-scrollbar:horizontal {
15
  height: 11px;
16
  }
17
 
18
+ .output-html::-webkit-scrollbar-thumb {
19
  border-radius: 8px;
20
  border: 2px solid white; /* should match background, can't be transparent */
21
  background-color: rgba(0, 0, 0, .5);
22
  }
23
 
24
+ .output-html::-webkit-scrollbar-track {
25
  background-color: #fff;
26
  border-radius: 8px;
27
+ }
28
+
29
+ .spans {
30
+ min-height: 75px;
31
+ }