From 4973a6ebd6a2e56ba04849ebb98d930d4954705c Mon Sep 17 00:00:00 2001 From: Titusz Pan Date: Thu, 15 Aug 2024 09:56:49 +0200 Subject: [PATCH] feat: update example text handling and improve process_text function in demo.py --- iscc_sct/demo.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/iscc_sct/demo.py b/iscc_sct/demo.py index 5776edc..d6df441 100644 --- a/iscc_sct/demo.py +++ b/iscc_sct/demo.py @@ -164,9 +164,10 @@ def generate_similarity_bar(similarity): gr.Examples( label="Click to use sample text", - examples=[[truncate_text(sample_text_en)]], + examples=[sample_text_en], inputs=[in_text_a], examples_per_page=1, + example_labels=[truncate_text(sample_text_en)] ) out_code_a = gr.Textbox(label="ISCC Code for Text A") gr.ClearButton(components=[in_text_a]) @@ -180,9 +181,10 @@ def generate_similarity_bar(similarity): gr.Examples( label="Click to use sample text", - examples=[[truncate_text(sample_text_de)]], + examples=[sample_text_de], inputs=[in_text_b], examples_per_page=1, + example_labels=[truncate_text(sample_text_de)] ) out_code_b = gr.Textbox(label="ISCC Code for Text B") gr.ClearButton(components=[in_text_b]) @@ -194,17 +196,11 @@ def generate_similarity_bar(similarity): def process_text(text, nbits, suffix): log.debug(f"{text[:20]}") if not text: - return None, text - # Use the full sample text if it matches the truncated version - full_text = ( - sample_text_en - if text == truncate_text(sample_text_en) - else sample_text_de - if text == truncate_text(sample_text_de) - else text - ) - iscc = sct.Metadata(**sct.gen_text_code_semantic(full_text, bits=nbits)) - return iscc.iscc, full_text + return + out_code_func = globals().get(f"out_code_{suffix}") + iscc = sct.Metadata(**sct.gen_text_code_semantic(text, bits=nbits)) + result = {out_code_func: gr.Textbox(value=iscc.iscc)} + return result def recalculate_iscc(text_a, text_b, nbits): code_a = sct.gen_text_code_semantic(text_a, bits=nbits)["iscc"] if text_a else None @@ -222,16 +218,18 @@ def recalculate_iscc(text_a, text_b, nbits): ) in_text_a.change( - process_text, + lambda text, nbits: process_text(text, nbits, "a"), inputs=[in_text_a, in_iscc_bits], - outputs=[out_code_a, in_text_a], + outputs=[out_code_a], show_progress="full", + trigger_mode="always_last", ) in_text_b.change( - process_text, + lambda text, nbits: process_text(text, nbits, "b"), inputs=[in_text_b, in_iscc_bits], - outputs=[out_code_b, in_text_b], + outputs=[out_code_b], show_progress="full", + trigger_mode="always_last", ) in_iscc_bits.change(