Spaces:
Sleeping
Sleeping
Nathan Schneider
commited on
Commit
·
2a5f71e
1
Parent(s):
c5eeb74
custom layout (#16), HighlightedText as default output (#17). OUPTUT NOT TESTED
Browse files
app.py
CHANGED
|
@@ -6,17 +6,11 @@ import torch
|
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
#Description text for the Gradio interface
|
| 9 |
-
|
| 10 |
-
<
|
| 11 |
-
|
| 12 |
-
The tagger adds semantic labels from the SNACS tagset to indicate spatial, temporal, and other kinds of relationships.
|
| 13 |
-
See the <a href="https://www.xposition.org/">Xposition site</a> and <a href="https://arxiv.org/abs/1704.02134">PDF manual</a> for details.</p>
|
| 14 |
-
|
| 15 |
-
<p>The tagger is a machine learning <a href="https://github.com/WesScivetti/snacs/tree/main">system</a> (specifically XLM-RoBERTa-large)
|
| 16 |
-
that has been fine-tuned on manually tagged data in 5 target languages: English, Mandarin Chinese, Hindi, Gujarati, and Japanese.
|
| 17 |
-
The system output is not always correct (even if the model’s confidence estimate is close to 100%),
|
| 18 |
-
and will likely be less accurate beyond the target languages.</p>
|
| 19 |
|
|
|
|
| 20 |
<details><summary>Linguistic notes</summary>
|
| 21 |
<ul>
|
| 22 |
<li>Some of the tagged items are single words (like <b><i>to</i></b>); others are multiword expressions (like <b><i>according to</i></b>).</li>
|
|
@@ -28,10 +22,22 @@ and will likely be less accurate beyond the target languages.</p>
|
|
| 28 |
</ul>
|
| 29 |
</details>
|
| 30 |
|
| 31 |
-
<p>Try the examples below, or enter your own text in the box
|
| 32 |
</p>
|
| 33 |
"""
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# short labels shown on the buttons, long text inserted into the textbox
|
| 36 |
EXAMPLES = [
|
| 37 |
['When Mr. and Mrs. Dursley woke up on the dull, gray Tuesday our story starts, there was nothing about the cloudy sky outside to suggest that strange and mysterious things would soon be happening all over the country. Mr. Dursley hummed as he picked out his most boring tie for work, and Mrs. Dursley gossiped away happily as she wrestled a screaming Dudley into his high chair.'], # inserts "example 1" text
|
|
@@ -328,7 +334,7 @@ def classify_tokens(text: str):
|
|
| 328 |
|
| 329 |
styled_html1 = f"<div style='font-family:sans-serif;line-height:1.6;'>{output1}</div>"
|
| 330 |
styled_html2 = f"<div style='font-family:sans-serif;line-height:1.6;'>{output2}</div>"
|
| 331 |
-
return styled_html1, table_html, styled_html2
|
| 332 |
# except Exception as e:
|
| 333 |
# # Force the real error into the Space logs
|
| 334 |
# import traceback, sys
|
|
@@ -337,20 +343,34 @@ def classify_tokens(text: str):
|
|
| 337 |
# return f"<pre>{html.escape(repr(e))}</pre>", "", ""
|
| 338 |
|
| 339 |
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
gr.
|
| 346 |
-
|
| 347 |
-
gr.
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
)
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
#Description text for the Gradio interface
|
| 9 |
+
DESCR_TOP = """
|
| 10 |
+
<h1 style="text-align: center">SNACS Tagging</h1>
|
| 11 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
DESCR_PART_3 = """
|
| 14 |
<details><summary>Linguistic notes</summary>
|
| 15 |
<ul>
|
| 16 |
<li>Some of the tagged items are single words (like <b><i>to</i></b>); others are multiword expressions (like <b><i>according to</i></b>).</li>
|
|
|
|
| 22 |
</ul>
|
| 23 |
</details>
|
| 24 |
|
| 25 |
+
<p>Try the examples below, or enter your own text in the box and click the Tag! button.
|
| 26 |
</p>
|
| 27 |
"""
|
| 28 |
|
| 29 |
+
DESCR_PARA_1 = """<p>🌐 Enter text <b>in any language</b> to analyze the in-context meanings of adpositions/possessives/case markers.
|
| 30 |
+
An <b>adposition</b> is a <i>pre</i>position (that precedes a noun, as in English) or a <i>post</i>position (that follows a noun, as in Japanese).
|
| 31 |
+
The tagger adds semantic labels from the SNACS tagset to indicate spatial, temporal, and other kinds of relationships.
|
| 32 |
+
See the <a href="https://www.xposition.org/">Xposition site</a> and <a href="https://arxiv.org/abs/1704.02134">PDF manual</a> for details.</p>
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
DESCR_PARA_2 = """<p>🤖 The tagger is a machine learning <a href="https://github.com/WesScivetti/snacs/tree/main">system</a> (specifically XLM-RoBERTa-large)
|
| 36 |
+
that has been fine-tuned on manually tagged data in 5 target languages: English, Mandarin Chinese, Hindi, Gujarati, and Japanese.
|
| 37 |
+
The system output is not always correct (even if the model’s confidence estimate is close to 100%),
|
| 38 |
+
and will likely be less accurate beyond the target languages.</p>
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
# short labels shown on the buttons, long text inserted into the textbox
|
| 42 |
EXAMPLES = [
|
| 43 |
['When Mr. and Mrs. Dursley woke up on the dull, gray Tuesday our story starts, there was nothing about the cloudy sky outside to suggest that strange and mysterious things would soon be happening all over the country. Mr. Dursley hummed as he picked out his most boring tie for work, and Mrs. Dursley gossiped away happily as she wrestled a screaming Dudley into his high chair.'], # inserts "example 1" text
|
|
|
|
| 334 |
|
| 335 |
styled_html1 = f"<div style='font-family:sans-serif;line-height:1.6;'>{output1}</div>"
|
| 336 |
styled_html2 = f"<div style='font-family:sans-serif;line-height:1.6;'>{output2}</div>"
|
| 337 |
+
return sorted_results1, styled_html1, table_html, styled_html2
|
| 338 |
# except Exception as e:
|
| 339 |
# # Force the real error into the Space logs
|
| 340 |
# import traceback, sys
|
|
|
|
| 343 |
# return f"<pre>{html.escape(repr(e))}</pre>", "", ""
|
| 344 |
|
| 345 |
|
| 346 |
+
with gr.Blocks(title="SNACS Tagging", theme="light") as demo:
|
| 347 |
+
with gr.Row():
|
| 348 |
+
description = gr.HTML(DESCR_TOP)
|
| 349 |
+
|
| 350 |
+
with gr.Row():
|
| 351 |
+
with gr.Column():
|
| 352 |
+
para1 = gr.HTML(DESCR_PARA_1)
|
| 353 |
+
with gr.Column():
|
| 354 |
+
para2 = gr.HTML(DESCR_PARA_2)
|
| 355 |
+
|
| 356 |
+
with gr.Row():
|
| 357 |
+
description = gr.HTML(DESCR_PART_3)
|
| 358 |
+
|
| 359 |
+
with gr.Row():
|
| 360 |
+
with gr.Column():
|
| 361 |
+
input_text = gr.Textbox(lines=4, placeholder="Enter a sentence...", label="Input Text"),
|
| 362 |
+
tag_btn = gr.Button("Tag!", variant="primary")
|
| 363 |
+
examples = gr.Examples(EXAMPLES, input_text, example_labels=EXAMPLE_LABELS)
|
| 364 |
+
with gr.Column() as output:
|
| 365 |
+
with gr.Tab("Simple Output"):
|
| 366 |
+
simple_output = gr.HighlightedText(label="Tagged Text")
|
| 367 |
+
with gr.Tab("Detailed Output"):
|
| 368 |
+
output1 = gr.HTML(label="SNACS Tagged Sentence")
|
| 369 |
+
output2 = gr.HTML(label="SNACS Table with Colored Labels")
|
| 370 |
+
output3 = gr.HTML(label="SNACS Tagged Sentence with No Label Aggregation")
|
| 371 |
+
|
| 372 |
+
examples.outputs = [simple_output,output1,output2,output3]
|
| 373 |
+
tag_btn.click(fn=classify_tokens, inputs=input_text, outputs=[simple_output,output1,output2,output3])
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
demo.launch()
|