Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -804,37 +804,49 @@ def highlight_sentence_if_ssccv(sentence):
|
|
| 804 |
return highlight_tokens_html(tokens, preds)
|
| 805 |
###############################################################################
|
| 806 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 807 |
###############################################################################
|
| 808 |
def highlight_final_output(final_text):
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
legend = (
|
| 814 |
-
'<div style="font-size:14px;margin-bottom:10px">'
|
| 815 |
-
"<b>Cognitive / communicative VP</b> — "
|
| 816 |
-
"<u style='font-weight:700'>bold underline = complementiser introducing a content clause</u> · "
|
| 817 |
-
"<em style='text-decoration:underline dotted'>italic dotted = before/after the complementiser</em>"
|
| 818 |
-
"</div>"
|
| 819 |
-
)
|
| 820 |
-
|
| 821 |
para_html = []
|
| 822 |
for para in re.split(r"\n\s*\n", final_text):
|
| 823 |
para = para.strip()
|
| 824 |
if not para:
|
| 825 |
continue
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
sents = [para]
|
| 830 |
-
sent_bits = [highlight_sentence_if_ssccv(s) for s in sents]
|
| 831 |
-
para_html.append(
|
| 832 |
-
'<p style="font-size:16px;line-height:1.55;margin:0 0 12px 0">'
|
| 833 |
-
+ " ".join(sent_bits)
|
| 834 |
-
+ "</p>"
|
| 835 |
-
)
|
| 836 |
|
| 837 |
-
return
|
| 838 |
###############################################################################
|
| 839 |
|
| 840 |
###############################################################################
|
|
@@ -2735,13 +2747,12 @@ def process_text(input_text):
|
|
| 2735 |
final_output = "\n\n".join(output_paragraphs)
|
| 2736 |
print(f"\n=== FINAL OUTPUT ===\n{final_output}")
|
| 2737 |
highlighted = highlight_final_output(final_output)
|
| 2738 |
-
return
|
| 2739 |
|
| 2740 |
except Exception as e:
|
| 2741 |
import traceback
|
| 2742 |
print(traceback.format_exc())
|
| 2743 |
-
|
| 2744 |
-
return err, html.escape(err)
|
| 2745 |
|
| 2746 |
###########################################################################
|
| 2747 |
|
|
@@ -2753,12 +2764,10 @@ print(" All seven models loaded successfully!")
|
|
| 2753 |
print("Creating Gradio interface...")
|
| 2754 |
|
| 2755 |
custom_css = """
|
| 2756 |
-
/* Slightly larger base font */
|
| 2757 |
.gradio-container, .gradio-container * {
|
| 2758 |
font-size: 16px !important;
|
| 2759 |
}
|
| 2760 |
|
| 2761 |
-
/* Input = original text → bright pink */
|
| 2762 |
.input-text textarea {
|
| 2763 |
font-size: 17px !important;
|
| 2764 |
color: #ffd0d0 !important;
|
|
@@ -2766,49 +2775,33 @@ custom_css = """
|
|
| 2766 |
line-height: 1.45 !important;
|
| 2767 |
}
|
| 2768 |
|
| 2769 |
-
|
| 2770 |
-
.output-text textarea {
|
| 2771 |
-
font-size: 17px !important;
|
| 2772 |
-
color: #d0ffd0 !important;
|
| 2773 |
-
font-weight: 500 !important;
|
| 2774 |
-
line-height: 1.45 !important;
|
| 2775 |
-
}
|
| 2776 |
-
|
| 2777 |
-
/* Labels under the boxes */
|
| 2778 |
-
.input-text label,
|
| 2779 |
-
.output-text label {
|
| 2780 |
font-size: 15px !important;
|
| 2781 |
font-weight: 600 !important;
|
| 2782 |
-
}
|
| 2783 |
-
|
| 2784 |
-
.input-text label {
|
| 2785 |
color: #ffd0d0 !important;
|
| 2786 |
}
|
| 2787 |
|
| 2788 |
-
|
| 2789 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2790 |
}
|
| 2791 |
|
| 2792 |
-
|
| 2793 |
-
|
| 2794 |
-
|
| 2795 |
-
border-radius: 8px !important;
|
| 2796 |
-
min-height: 8em;
|
| 2797 |
-
font-size: 17px !important;
|
| 2798 |
-
line-height: 1.6 !important;
|
| 2799 |
}
|
| 2800 |
|
| 2801 |
-
.
|
|
|
|
|
|
|
| 2802 |
font-weight: 700 !important;
|
| 2803 |
text-decoration: underline !important;
|
| 2804 |
text-underline-offset: 3px !important;
|
| 2805 |
}
|
| 2806 |
-
|
| 2807 |
-
.highlighted-output em {
|
| 2808 |
-
font-style: italic !important;
|
| 2809 |
-
text-decoration: underline dotted !important;
|
| 2810 |
-
text-underline-offset: 3px !important;
|
| 2811 |
-
}
|
| 2812 |
"""
|
| 2813 |
|
| 2814 |
with gr.Blocks(
|
|
@@ -2835,7 +2828,6 @@ Paste text, or click examples to **add** them into the box below
|
|
| 2835 |
"""
|
| 2836 |
)
|
| 2837 |
|
| 2838 |
-
# 1) Complex input
|
| 2839 |
input_text = gr.Textbox(
|
| 2840 |
lines=12,
|
| 2841 |
label="Original text",
|
|
@@ -2843,26 +2835,17 @@ Paste text, or click examples to **add** them into the box below
|
|
| 2843 |
elem_classes=["input-text"],
|
| 2844 |
)
|
| 2845 |
|
| 2846 |
-
# 2) Bridge button (directly under input)
|
| 2847 |
btn = gr.Button("Process & Simplify", variant="primary")
|
| 2848 |
|
| 2849 |
-
|
| 2850 |
-
output_text = gr.Textbox(
|
| 2851 |
-
lines=12,
|
| 2852 |
-
label="Simplified text",
|
| 2853 |
-
elem_classes=["output-text"],
|
| 2854 |
-
)
|
| 2855 |
-
|
| 2856 |
-
# 4) Highlighted cognitive / communicative VPs
|
| 2857 |
-
highlighted_html = gr.HTML(
|
| 2858 |
value="",
|
| 2859 |
-
|
| 2860 |
)
|
| 2861 |
|
| 2862 |
btn.click(
|
| 2863 |
fn=process_text,
|
| 2864 |
inputs=input_text,
|
| 2865 |
-
outputs=
|
| 2866 |
)
|
| 2867 |
|
| 2868 |
EXAMPLE_SENTENCES = [
|
|
@@ -2905,7 +2888,6 @@ if __name__ == "__main__":
|
|
| 2905 |
debug=True,
|
| 2906 |
share=False,
|
| 2907 |
)
|
| 2908 |
-
|
| 2909 |
|
| 2910 |
'''
|
| 2911 |
test = (
|
|
|
|
| 804 |
return highlight_tokens_html(tokens, preds)
|
| 805 |
###############################################################################
|
| 806 |
|
| 807 |
+
###############################################################################
|
| 808 |
+
def highlight_tokens_html(tokens, labels):
|
| 809 |
+
parts = []
|
| 810 |
+
i = 0
|
| 811 |
+
n = len(tokens)
|
| 812 |
+
while i < n:
|
| 813 |
+
lab = labels[i] if i < len(labels) else ""
|
| 814 |
+
if lab in HIGHLIGHT_CCV:
|
| 815 |
+
j = i + 1
|
| 816 |
+
while j < n and (labels[j] if j < len(labels) else "") == lab:
|
| 817 |
+
j += 1
|
| 818 |
+
chunk = re.sub(r"\s+([.,!?;:])", r"\1", " ".join(tokens[i:j]))
|
| 819 |
+
esc = html.escape(chunk)
|
| 820 |
+
if lab == "IN_COGNITIVE_COMMUNICATIVE_VP":
|
| 821 |
+
parts.append(f"<strong><u>{esc}</u></strong>")
|
| 822 |
+
else:
|
| 823 |
+
parts.append(f"<u>{esc}</u>")
|
| 824 |
+
i = j
|
| 825 |
+
else:
|
| 826 |
+
tok = tokens[i]
|
| 827 |
+
if tok in ".,!?;:" and parts:
|
| 828 |
+
parts[-1] = parts[-1] + html.escape(tok)
|
| 829 |
+
else:
|
| 830 |
+
parts.append(html.escape(tok))
|
| 831 |
+
i += 1
|
| 832 |
+
return " ".join(parts)
|
| 833 |
+
###############################################################################
|
| 834 |
+
|
| 835 |
###############################################################################
|
| 836 |
def highlight_final_output(final_text):
|
| 837 |
+
if not final_text or str(final_text).startswith("ERROR"):
|
| 838 |
+
return f'<div class="simp-out">{html.escape(final_text or "")}</div>'
|
| 839 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 840 |
para_html = []
|
| 841 |
for para in re.split(r"\n\s*\n", final_text):
|
| 842 |
para = para.strip()
|
| 843 |
if not para:
|
| 844 |
continue
|
| 845 |
+
sents = split_into_sentences(para) if para else [para]
|
| 846 |
+
bits = [highlight_sentence_if_ssccv(s) for s in sents]
|
| 847 |
+
para_html.append("<p>" + " ".join(bits) + "</p>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 848 |
|
| 849 |
+
return '<div class="simp-out">' + "".join(para_html) + "</div>"
|
| 850 |
###############################################################################
|
| 851 |
|
| 852 |
###############################################################################
|
|
|
|
| 2747 |
final_output = "\n\n".join(output_paragraphs)
|
| 2748 |
print(f"\n=== FINAL OUTPUT ===\n{final_output}")
|
| 2749 |
highlighted = highlight_final_output(final_output)
|
| 2750 |
+
return highlighted
|
| 2751 |
|
| 2752 |
except Exception as e:
|
| 2753 |
import traceback
|
| 2754 |
print(traceback.format_exc())
|
| 2755 |
+
return f'<div class="simp-out">{html.escape("ERROR: " + str(e))}</div>'
|
|
|
|
| 2756 |
|
| 2757 |
###########################################################################
|
| 2758 |
|
|
|
|
| 2764 |
print("Creating Gradio interface...")
|
| 2765 |
|
| 2766 |
custom_css = """
|
|
|
|
| 2767 |
.gradio-container, .gradio-container * {
|
| 2768 |
font-size: 16px !important;
|
| 2769 |
}
|
| 2770 |
|
|
|
|
| 2771 |
.input-text textarea {
|
| 2772 |
font-size: 17px !important;
|
| 2773 |
color: #ffd0d0 !important;
|
|
|
|
| 2775 |
line-height: 1.45 !important;
|
| 2776 |
}
|
| 2777 |
|
| 2778 |
+
.input-text label {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2779 |
font-size: 15px !important;
|
| 2780 |
font-weight: 600 !important;
|
|
|
|
|
|
|
|
|
|
| 2781 |
color: #ffd0d0 !important;
|
| 2782 |
}
|
| 2783 |
|
| 2784 |
+
/* Merged simplified output: green text, VP underlined */
|
| 2785 |
+
.simp-out {
|
| 2786 |
+
color: #7CFF7C !important;
|
| 2787 |
+
font-size: 17px !important;
|
| 2788 |
+
font-weight: 500 !important;
|
| 2789 |
+
line-height: 1.55 !important;
|
| 2790 |
+
padding: 12px 14px !important;
|
| 2791 |
}
|
| 2792 |
|
| 2793 |
+
.simp-out p {
|
| 2794 |
+
margin: 0 0 12px 0 !important;
|
| 2795 |
+
color: #7CFF7C !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2796 |
}
|
| 2797 |
|
| 2798 |
+
.simp-out strong u,
|
| 2799 |
+
.simp-out u {
|
| 2800 |
+
color: #7CFF7C !important;
|
| 2801 |
font-weight: 700 !important;
|
| 2802 |
text-decoration: underline !important;
|
| 2803 |
text-underline-offset: 3px !important;
|
| 2804 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2805 |
"""
|
| 2806 |
|
| 2807 |
with gr.Blocks(
|
|
|
|
| 2828 |
"""
|
| 2829 |
)
|
| 2830 |
|
|
|
|
| 2831 |
input_text = gr.Textbox(
|
| 2832 |
lines=12,
|
| 2833 |
label="Original text",
|
|
|
|
| 2835 |
elem_classes=["input-text"],
|
| 2836 |
)
|
| 2837 |
|
|
|
|
| 2838 |
btn = gr.Button("Process & Simplify", variant="primary")
|
| 2839 |
|
| 2840 |
+
output_html = gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2841 |
value="",
|
| 2842 |
+
label="Simplified text",
|
| 2843 |
)
|
| 2844 |
|
| 2845 |
btn.click(
|
| 2846 |
fn=process_text,
|
| 2847 |
inputs=input_text,
|
| 2848 |
+
outputs=output_html,
|
| 2849 |
)
|
| 2850 |
|
| 2851 |
EXAMPLE_SENTENCES = [
|
|
|
|
| 2888 |
debug=True,
|
| 2889 |
share=False,
|
| 2890 |
)
|
|
|
|
| 2891 |
|
| 2892 |
'''
|
| 2893 |
test = (
|