Spaces:
Sleeping
Sleeping
Nathan Schneider
commited on
Commit
·
e4e6e6a
1
Parent(s):
4db3425
attempt to connect Examples to output components with proxy objects
Browse files
app.py
CHANGED
|
@@ -259,7 +259,7 @@ def classify_tokens(text: str, use_canned=False):
|
|
| 259 |
results_tokens = pipe(text, aggregation_strategy="none", ignore_labels=[])
|
| 260 |
results_tokens.sort(key=lambda x: x["start"])
|
| 261 |
print(results_tokens)
|
| 262 |
-
|
| 263 |
text = "fox in socks"
|
| 264 |
results_spans = [{"start": 4, "end": 6, "entity_group": "p.Locus-p.Locus",
|
| 265 |
"score": 0.46, "word": "in"}]
|
|
@@ -271,6 +271,12 @@ def classify_tokens(text: str, use_canned=False):
|
|
| 271 |
{"start": 7, "end": 12, "entity": "O", "score": 1,
|
| 272 |
"probabilities": {"O": 1}}
|
| 273 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
# color helper that tolerates B-/I- prefixes
|
| 276 |
def pick_color(label: str, lbl2color: dict) -> str:
|
|
@@ -370,6 +376,27 @@ def classify_tokens(text: str, use_canned=False):
|
|
| 370 |
# return f"<pre>{html.escape(repr(e))}</pre>", "", ""
|
| 371 |
|
| 372 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
#final rendering of the Gradio interface
|
| 374 |
with gr.Blocks(title="SNACS Tagging", css=CUSTOM_CSS) as demo:
|
| 375 |
with gr.Row():
|
|
@@ -388,23 +415,20 @@ with gr.Blocks(title="SNACS Tagging", css=CUSTOM_CSS) as demo:
|
|
| 388 |
with gr.Column():
|
| 389 |
input_text = gr.Textbox(lines=4, placeholder="Enter a sentence...", label="Input Text"),
|
| 390 |
tag_btn = gr.Button("Tag!", variant="primary")
|
| 391 |
-
examples = gr.Examples(EXAMPLES, input_text, example_labels=EXAMPLE_LABELS)
|
| 392 |
with gr.Column() as output:
|
| 393 |
with gr.Tab("Simple Output"):
|
| 394 |
-
|
| 395 |
with gr.Tab("Detailed Output"):
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
with gr.Tab("JSON Spans"):
|
| 400 |
-
|
| 401 |
with gr.Tab("JSON Tokens"):
|
| 402 |
-
|
| 403 |
|
| 404 |
-
|
| 405 |
-
#DEBUG NEEDED
|
| 406 |
-
#examples.cache_examples = True
|
| 407 |
-
tag_btn.click(fn=classify_tokens, inputs=input_text, outputs=examples.outputs)
|
| 408 |
|
| 409 |
|
| 410 |
demo.launch()
|
|
|
|
| 259 |
results_tokens = pipe(text, aggregation_strategy="none", ignore_labels=[])
|
| 260 |
results_tokens.sort(key=lambda x: x["start"])
|
| 261 |
print(results_tokens)
|
| 262 |
+
elif use_canned==1: # canned example to test the output display
|
| 263 |
text = "fox in socks"
|
| 264 |
results_spans = [{"start": 4, "end": 6, "entity_group": "p.Locus-p.Locus",
|
| 265 |
"score": 0.46, "word": "in"}]
|
|
|
|
| 271 |
{"start": 7, "end": 12, "entity": "O", "score": 1,
|
| 272 |
"probabilities": {"O": 1}}
|
| 273 |
]
|
| 274 |
+
else: # longer canned example
|
| 275 |
+
text = EXAMPLES[0][0]
|
| 276 |
+
with open('harrypotter_spans.json') as spansF:
|
| 277 |
+
results_spans = json.load(spansF)
|
| 278 |
+
with open('harrypotter_tokens.json') as toksF:
|
| 279 |
+
results_tokens = json.load(toksF)
|
| 280 |
|
| 281 |
# color helper that tolerates B-/I- prefixes
|
| 282 |
def pick_color(label: str, lbl2color: dict) -> str:
|
|
|
|
| 376 |
# return f"<pre>{html.escape(repr(e))}</pre>", "", ""
|
| 377 |
|
| 378 |
|
| 379 |
+
# instantiate output component proxies before layout so they can be provided to Examples component
|
| 380 |
+
# the component itself has to be instantiated during layout
|
| 381 |
+
class LazyObj():
|
| 382 |
+
"""Proxy for object whose construction is delayed"""
|
| 383 |
+
def __init__(self, constructor_fxn):
|
| 384 |
+
self._construct = constructor_fxn
|
| 385 |
+
self._o = None
|
| 386 |
+
self.__getattr__ = lambda self, name: self._o.__getattr__(name)
|
| 387 |
+
self.__setattr__ = lambda self, name, val: self._o.__setattr__(name, val) # type: ignore
|
| 388 |
+
def __call__(self):
|
| 389 |
+
assert self._o is None
|
| 390 |
+
self._o = self._construct()
|
| 391 |
+
return self._o
|
| 392 |
+
|
| 393 |
+
simple_output = LazyObj(lambda: gr.HighlightedText(label="Tagged Text"))
|
| 394 |
+
output1 = lambda: gr.HTML(label="SNACS Tagged Sentence")
|
| 395 |
+
output2 = lambda: gr.HTML(label="SNACS Table with Colored Labels")
|
| 396 |
+
output3 = lambda: gr.HTML(label="SNACS Tagged Sentence with No Label Aggregation")
|
| 397 |
+
json_spans = lambda: gr.Code(language="json")
|
| 398 |
+
json_tokens = lambda: gr.Code(language="json")
|
| 399 |
+
|
| 400 |
#final rendering of the Gradio interface
|
| 401 |
with gr.Blocks(title="SNACS Tagging", css=CUSTOM_CSS) as demo:
|
| 402 |
with gr.Row():
|
|
|
|
| 415 |
with gr.Column():
|
| 416 |
input_text = gr.Textbox(lines=4, placeholder="Enter a sentence...", label="Input Text"),
|
| 417 |
tag_btn = gr.Button("Tag!", variant="primary")
|
| 418 |
+
examples = gr.Examples(EXAMPLES, input_text, [simple_output,json_spans,json_tokens,output1,output2,output3], example_labels=EXAMPLE_LABELS) # type: ignore
|
| 419 |
with gr.Column() as output:
|
| 420 |
with gr.Tab("Simple Output"):
|
| 421 |
+
_simple_output = simple_output()
|
| 422 |
with gr.Tab("Detailed Output"):
|
| 423 |
+
_output1 = output1()
|
| 424 |
+
_output2 = output2()
|
| 425 |
+
_output3 = output3()
|
| 426 |
with gr.Tab("JSON Spans"):
|
| 427 |
+
_json_spans = json_spans()
|
| 428 |
with gr.Tab("JSON Tokens"):
|
| 429 |
+
_json_tokens = json_tokens()
|
| 430 |
|
| 431 |
+
tag_btn.click(fn=classify_tokens, inputs=input_text, outputs=[_simple_output,_json_spans,_json_tokens,_output1,_output2,_output3])
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
|
| 434 |
demo.launch()
|