Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import os, re, string, json
|
| 2 |
import html
|
| 3 |
import inspect
|
| 4 |
import importlib.resources as importlib_resources
|
|
@@ -96,12 +96,12 @@ CSS = """
|
|
| 96 |
color:#0b1b19 !important;
|
| 97 |
}
|
| 98 |
|
| 99 |
-
#lang_fo_off, #lang_en_off{
|
| 100 |
background:#C6DAD6 !important;
|
| 101 |
border-color:#6F9992 !important;
|
| 102 |
color:#0b1b19 !important;
|
| 103 |
}
|
| 104 |
-
#lang_fo_off:hover, #lang_en_off:hover{
|
| 105 |
background:#89AFA9 !important;
|
| 106 |
border-color:#6F9992 !important;
|
| 107 |
color:#0b1b19 !important;
|
|
@@ -109,12 +109,12 @@ CSS = """
|
|
| 109 |
|
| 110 |
/* Dark mode: make the INACTIVE buttons match what you had before (darker, readable) */
|
| 111 |
@media (prefers-color-scheme: dark){
|
| 112 |
-
#lang_fo_off, #lang_en_off{
|
| 113 |
background:#2a3b38 !important;
|
| 114 |
border-color:#6F9992 !important;
|
| 115 |
color:#e7eceb !important;
|
| 116 |
}
|
| 117 |
-
#lang_fo_off:hover, #lang_en_off:hover{
|
| 118 |
background:#89AFA9 !important;
|
| 119 |
border-color:#6F9992 !important;
|
| 120 |
color:#0b1b19 !important;
|
|
@@ -140,6 +140,26 @@ CSS = """
|
|
| 140 |
min-width:120px !important;
|
| 141 |
flex:0 0 auto !important;
|
| 142 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
/* Remove the big Gradio panel/frame around the textbox (keep textarea normal) */
|
| 144 |
#input_col,
|
| 145 |
#input_col > div,
|
|
@@ -640,6 +660,35 @@ def render(rows_state, lang: str):
|
|
| 640 |
|
| 641 |
return (_make_html_table(cols_main, out_main), _make_html_table(cols_mean, out_mean), build_overview(lang))
|
| 642 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 643 |
with gr.Blocks(css=CSS, title="Marka") as demo:
|
| 644 |
with gr.Row(equal_height=False):
|
| 645 |
with gr.Column(scale=2, elem_id="input_col"):
|
|
@@ -663,10 +712,15 @@ with gr.Blocks(css=CSS, title="Marka") as demo:
|
|
| 663 |
btn_lang_fo_off = gr.Button("Føroyskt", variant="secondary", elem_id="lang_fo_off", visible=False)
|
| 664 |
btn_lang_en_on = gr.Button("English", variant="primary", elem_id="lang_en_on", visible=False)
|
| 665 |
btn_lang_en_off = gr.Button("English", variant="secondary", elem_id="lang_en_off", visible=False)
|
|
|
|
| 666 |
out_df = gr.HTML(value="", elem_id="out_df", visible=False)
|
| 667 |
|
| 668 |
expanded_acc = gr.Accordion("Útgreinað marking / Expanded tags", open=False, visible=False)
|
| 669 |
with expanded_acc:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 670 |
out_mean_df = gr.HTML(value="", elem_id="out_mean_df")
|
| 671 |
|
| 672 |
overview_acc = gr.Accordion("Markayvirlit / Tag Overview", open=False, visible=True)
|
|
@@ -680,6 +734,10 @@ with gr.Blocks(css=CSS, title="Marka") as demo:
|
|
| 680 |
show_fo = (lang_current == "fo")
|
| 681 |
show_en = (lang_current == "en")
|
| 682 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 683 |
return (
|
| 684 |
rows,
|
| 685 |
gr.update(value=df_main, visible=True),
|
|
@@ -690,6 +748,8 @@ with gr.Blocks(css=CSS, title="Marka") as demo:
|
|
| 690 |
gr.update(visible=not show_fo),
|
| 691 |
gr.update(visible=show_en),
|
| 692 |
gr.update(visible=not show_en),
|
|
|
|
|
|
|
| 693 |
lang_current,
|
| 694 |
)
|
| 695 |
|
|
@@ -699,6 +759,10 @@ with gr.Blocks(css=CSS, title="Marka") as demo:
|
|
| 699 |
show_fo = (lang_value == "fo")
|
| 700 |
show_en = (lang_value == "en")
|
| 701 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 702 |
return (
|
| 703 |
lang_value,
|
| 704 |
gr.update(value=df_main),
|
|
@@ -708,8 +772,9 @@ with gr.Blocks(css=CSS, title="Marka") as demo:
|
|
| 708 |
gr.update(visible=not show_fo),
|
| 709 |
gr.update(visible=show_en),
|
| 710 |
gr.update(visible=not show_en),
|
|
|
|
|
|
|
| 711 |
)
|
| 712 |
-
|
| 713 |
def on_set_fo(rows):
|
| 714 |
return on_set_lang(rows, "fo")
|
| 715 |
|
|
@@ -720,7 +785,8 @@ with gr.Blocks(css=CSS, title="Marka") as demo:
|
|
| 720 |
on_tag,
|
| 721 |
inputs=[inp, lang_state],
|
| 722 |
outputs=[state, out_df, out_mean_df, overview_md, expanded_acc,
|
| 723 |
-
btn_lang_fo_on, btn_lang_fo_off, btn_lang_en_on, btn_lang_en_off,
|
|
|
|
| 724 |
queue=False,
|
| 725 |
)
|
| 726 |
|
|
@@ -728,28 +794,32 @@ with gr.Blocks(css=CSS, title="Marka") as demo:
|
|
| 728 |
on_set_fo,
|
| 729 |
inputs=[state],
|
| 730 |
outputs=[lang_state, out_df, out_mean_df, overview_md,
|
| 731 |
-
btn_lang_fo_on, btn_lang_fo_off, btn_lang_en_on, btn_lang_en_off
|
|
|
|
| 732 |
queue=False,
|
| 733 |
)
|
| 734 |
btn_lang_fo_off.click(
|
| 735 |
on_set_fo,
|
| 736 |
inputs=[state],
|
| 737 |
outputs=[lang_state, out_df, out_mean_df, overview_md,
|
| 738 |
-
btn_lang_fo_on, btn_lang_fo_off, btn_lang_en_on, btn_lang_en_off
|
|
|
|
| 739 |
queue=False,
|
| 740 |
)
|
| 741 |
btn_lang_en_on.click(
|
| 742 |
on_set_en,
|
| 743 |
inputs=[state],
|
| 744 |
outputs=[lang_state, out_df, out_mean_df, overview_md,
|
| 745 |
-
btn_lang_fo_on, btn_lang_fo_off, btn_lang_en_on, btn_lang_en_off
|
|
|
|
| 746 |
queue=False,
|
| 747 |
)
|
| 748 |
btn_lang_en_off.click(
|
| 749 |
on_set_en,
|
| 750 |
inputs=[state],
|
| 751 |
outputs=[lang_state, out_df, out_mean_df, overview_md,
|
| 752 |
-
btn_lang_fo_on, btn_lang_fo_off, btn_lang_en_on, btn_lang_en_off
|
|
|
|
| 753 |
queue=False,
|
| 754 |
)
|
| 755 |
|
|
|
|
| 1 |
+
import os, re, string, json, tempfile, uuid
|
| 2 |
import html
|
| 3 |
import inspect
|
| 4 |
import importlib.resources as importlib_resources
|
|
|
|
| 96 |
color:#0b1b19 !important;
|
| 97 |
}
|
| 98 |
|
| 99 |
+
#lang_fo_off, #lang_en_off, #btn_dl_main, #btn_dl_exp{
|
| 100 |
background:#C6DAD6 !important;
|
| 101 |
border-color:#6F9992 !important;
|
| 102 |
color:#0b1b19 !important;
|
| 103 |
}
|
| 104 |
+
#lang_fo_off:hover, #lang_en_off:hover, #btn_dl_main:hover, #btn_dl_exp:hover{
|
| 105 |
background:#89AFA9 !important;
|
| 106 |
border-color:#6F9992 !important;
|
| 107 |
color:#0b1b19 !important;
|
|
|
|
| 109 |
|
| 110 |
/* Dark mode: make the INACTIVE buttons match what you had before (darker, readable) */
|
| 111 |
@media (prefers-color-scheme: dark){
|
| 112 |
+
#lang_fo_off, #lang_en_off, #btn_dl_main, #btn_dl_exp{
|
| 113 |
background:#2a3b38 !important;
|
| 114 |
border-color:#6F9992 !important;
|
| 115 |
color:#e7eceb !important;
|
| 116 |
}
|
| 117 |
+
#lang_fo_off:hover, #lang_en_off:hover, #btn_dl_main:hover, #btn_dl_exp:hover{
|
| 118 |
background:#89AFA9 !important;
|
| 119 |
border-color:#6F9992 !important;
|
| 120 |
color:#0b1b19 !important;
|
|
|
|
| 140 |
min-width:120px !important;
|
| 141 |
flex:0 0 auto !important;
|
| 142 |
}
|
| 143 |
+
|
| 144 |
+
/* Expanded-tags header: keep download button hard-right */
|
| 145 |
+
#expanded_hdr{
|
| 146 |
+
display:flex !important;
|
| 147 |
+
align-items:center !important;
|
| 148 |
+
gap:12px !important;
|
| 149 |
+
}
|
| 150 |
+
#expanded_hdr > .gr-markdown{ flex:1 1 auto !important; }
|
| 151 |
+
#expanded_buttons{
|
| 152 |
+
display:flex !important;
|
| 153 |
+
gap:10px !important;
|
| 154 |
+
justify-content:flex-end !important;
|
| 155 |
+
align-items:center !important;
|
| 156 |
+
flex-wrap:nowrap !important;
|
| 157 |
+
}
|
| 158 |
+
#expanded_buttons .gr-button, #expanded_buttons button{
|
| 159 |
+
width:auto !important;
|
| 160 |
+
min-width:120px !important;
|
| 161 |
+
flex:0 0 auto !important;
|
| 162 |
+
}
|
| 163 |
/* Remove the big Gradio panel/frame around the textbox (keep textarea normal) */
|
| 164 |
#input_col,
|
| 165 |
#input_col > div,
|
|
|
|
| 660 |
|
| 661 |
return (_make_html_table(cols_main, out_main), _make_html_table(cols_mean, out_mean), build_overview(lang))
|
| 662 |
|
| 663 |
+
|
| 664 |
+
def _write_tsv(df: pd.DataFrame, filename: str) -> str:
|
| 665 |
+
"""Write a TSV file to a unique temp folder and return the absolute path."""
|
| 666 |
+
tmpdir = os.path.join(tempfile.gettempdir(), "marka_downloads", str(uuid.uuid4()))
|
| 667 |
+
os.makedirs(tmpdir, exist_ok=True)
|
| 668 |
+
path = os.path.join(tmpdir, filename)
|
| 669 |
+
df.to_csv(path, sep="\t", index=False, encoding="utf-8")
|
| 670 |
+
return path
|
| 671 |
+
|
| 672 |
+
def build_download_main(rows_state) -> str:
|
| 673 |
+
"""TSV with 2 columns: Faroese analysis, then English analysis."""
|
| 674 |
+
fo_vals, en_vals = [], []
|
| 675 |
+
for r in (rows_state or []):
|
| 676 |
+
vec = torch.tensor(r["vec"])
|
| 677 |
+
fo_vals.append(analysis_text(vec, "fo"))
|
| 678 |
+
en_vals.append(analysis_text(vec, "en"))
|
| 679 |
+
df = pd.DataFrame({UI["fo"]["s"]: fo_vals, UI["en"]["s"]: en_vals})
|
| 680 |
+
return _write_tsv(df, "Markað.tsv")
|
| 681 |
+
|
| 682 |
+
def build_download_expanded(rows_state, lang: str) -> str:
|
| 683 |
+
"""TSV with the Expanded tags column in the currently selected UI language."""
|
| 684 |
+
lang = "fo" if lang == "fo" else "en"
|
| 685 |
+
vals = []
|
| 686 |
+
for r in (rows_state or []):
|
| 687 |
+
vec = torch.tensor(r["vec"])
|
| 688 |
+
vals.append(expanded_text(vec, lang))
|
| 689 |
+
df = pd.DataFrame({UI[lang]["m"]: vals})
|
| 690 |
+
return _write_tsv(df, "Markað_útgreinað.tsv")
|
| 691 |
+
|
| 692 |
with gr.Blocks(css=CSS, title="Marka") as demo:
|
| 693 |
with gr.Row(equal_height=False):
|
| 694 |
with gr.Column(scale=2, elem_id="input_col"):
|
|
|
|
| 712 |
btn_lang_fo_off = gr.Button("Føroyskt", variant="secondary", elem_id="lang_fo_off", visible=False)
|
| 713 |
btn_lang_en_on = gr.Button("English", variant="primary", elem_id="lang_en_on", visible=False)
|
| 714 |
btn_lang_en_off = gr.Button("English", variant="secondary", elem_id="lang_en_off", visible=False)
|
| 715 |
+
btn_dl_main = gr.DownloadButton("Tak niður / Download", variant="secondary", elem_id="btn_dl_main", visible=False)
|
| 716 |
out_df = gr.HTML(value="", elem_id="out_df", visible=False)
|
| 717 |
|
| 718 |
expanded_acc = gr.Accordion("Útgreinað marking / Expanded tags", open=False, visible=False)
|
| 719 |
with expanded_acc:
|
| 720 |
+
with gr.Row(elem_id="expanded_hdr"):
|
| 721 |
+
gr.Markdown(" ")
|
| 722 |
+
with gr.Row(elem_id="expanded_buttons"):
|
| 723 |
+
btn_dl_exp = gr.DownloadButton("Tak niður / Download", variant="secondary", elem_id="btn_dl_exp", visible=False)
|
| 724 |
out_mean_df = gr.HTML(value="", elem_id="out_mean_df")
|
| 725 |
|
| 726 |
overview_acc = gr.Accordion("Markayvirlit / Tag Overview", open=False, visible=True)
|
|
|
|
| 734 |
show_fo = (lang_current == "fo")
|
| 735 |
show_en = (lang_current == "en")
|
| 736 |
|
| 737 |
+
have_rows = bool(rows)
|
| 738 |
+
dl_main_path = build_download_main(rows) if have_rows else None
|
| 739 |
+
dl_exp_path = build_download_expanded(rows, lang_current) if have_rows else None
|
| 740 |
+
|
| 741 |
return (
|
| 742 |
rows,
|
| 743 |
gr.update(value=df_main, visible=True),
|
|
|
|
| 748 |
gr.update(visible=not show_fo),
|
| 749 |
gr.update(visible=show_en),
|
| 750 |
gr.update(visible=not show_en),
|
| 751 |
+
gr.update(value=dl_main_path, visible=have_rows),
|
| 752 |
+
gr.update(value=dl_exp_path, visible=have_rows),
|
| 753 |
lang_current,
|
| 754 |
)
|
| 755 |
|
|
|
|
| 759 |
show_fo = (lang_value == "fo")
|
| 760 |
show_en = (lang_value == "en")
|
| 761 |
|
| 762 |
+
have_rows = bool(rows)
|
| 763 |
+
dl_main_path = build_download_main(rows) if have_rows else None
|
| 764 |
+
dl_exp_path = build_download_expanded(rows, lang_value) if have_rows else None
|
| 765 |
+
|
| 766 |
return (
|
| 767 |
lang_value,
|
| 768 |
gr.update(value=df_main),
|
|
|
|
| 772 |
gr.update(visible=not show_fo),
|
| 773 |
gr.update(visible=show_en),
|
| 774 |
gr.update(visible=not show_en),
|
| 775 |
+
gr.update(value=dl_main_path, visible=have_rows),
|
| 776 |
+
gr.update(value=dl_exp_path, visible=have_rows),
|
| 777 |
)
|
|
|
|
| 778 |
def on_set_fo(rows):
|
| 779 |
return on_set_lang(rows, "fo")
|
| 780 |
|
|
|
|
| 785 |
on_tag,
|
| 786 |
inputs=[inp, lang_state],
|
| 787 |
outputs=[state, out_df, out_mean_df, overview_md, expanded_acc,
|
| 788 |
+
btn_lang_fo_on, btn_lang_fo_off, btn_lang_en_on, btn_lang_en_off,
|
| 789 |
+
btn_dl_main, btn_dl_exp, lang_state],
|
| 790 |
queue=False,
|
| 791 |
)
|
| 792 |
|
|
|
|
| 794 |
on_set_fo,
|
| 795 |
inputs=[state],
|
| 796 |
outputs=[lang_state, out_df, out_mean_df, overview_md,
|
| 797 |
+
btn_lang_fo_on, btn_lang_fo_off, btn_lang_en_on, btn_lang_en_off,
|
| 798 |
+
btn_dl_main, btn_dl_exp],
|
| 799 |
queue=False,
|
| 800 |
)
|
| 801 |
btn_lang_fo_off.click(
|
| 802 |
on_set_fo,
|
| 803 |
inputs=[state],
|
| 804 |
outputs=[lang_state, out_df, out_mean_df, overview_md,
|
| 805 |
+
btn_lang_fo_on, btn_lang_fo_off, btn_lang_en_on, btn_lang_en_off,
|
| 806 |
+
btn_dl_main, btn_dl_exp],
|
| 807 |
queue=False,
|
| 808 |
)
|
| 809 |
btn_lang_en_on.click(
|
| 810 |
on_set_en,
|
| 811 |
inputs=[state],
|
| 812 |
outputs=[lang_state, out_df, out_mean_df, overview_md,
|
| 813 |
+
btn_lang_fo_on, btn_lang_fo_off, btn_lang_en_on, btn_lang_en_off,
|
| 814 |
+
btn_dl_main, btn_dl_exp],
|
| 815 |
queue=False,
|
| 816 |
)
|
| 817 |
btn_lang_en_off.click(
|
| 818 |
on_set_en,
|
| 819 |
inputs=[state],
|
| 820 |
outputs=[lang_state, out_df, out_mean_df, overview_md,
|
| 821 |
+
btn_lang_fo_on, btn_lang_fo_off, btn_lang_en_on, btn_lang_en_off,
|
| 822 |
+
btn_dl_main, btn_dl_exp],
|
| 823 |
queue=False,
|
| 824 |
)
|
| 825 |
|