wagner-austin
commited on
Commit
Β·
11127ec
1
Parent(s):
ef24f0a
modified app.py to show progress bar
Browse files
app.py
CHANGED
|
@@ -9,12 +9,59 @@ Gradio front-end for the UCI Phonotactic Calculator
|
|
| 9 |
from pathlib import Path
|
| 10 |
import tempfile, os, pandas as pd, gradio as gr
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# ---> public, documented API wrapper around the CLI
|
| 13 |
from uci_phonotactic_calculator.ngram_calculator import run as ngram_run
|
| 14 |
from uci_phonotactic_calculator.plugins import PluginRegistry
|
| 15 |
from uci_phonotactic_calculator.cli_demo_data import get_demo_paths
|
| 16 |
|
| 17 |
TMP_DIR = Path(tempfile.gettempdir())
|
|
|
|
| 18 |
|
| 19 |
# ---------------------------------------------------------------------
|
| 20 |
# Back-end helper
|
|
@@ -40,31 +87,45 @@ def score(
|
|
| 40 |
raise gr.Error("Upload BOTH training & test CSVs *or* tick the demo-data box.")
|
| 41 |
train_path, test_path = train_csv.name, test_csv.name
|
| 42 |
|
| 43 |
-
out_file = TMP_DIR / "
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# -------------------- translate filters -------------------------
|
| 46 |
filters = {}
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
if "=" not in tok:
|
| 50 |
raise gr.Error(f"Filter β{tok}β must look like key=value")
|
| 51 |
k, v = tok.split("=", 1)
|
| 52 |
filters[k] = v
|
| 53 |
|
| 54 |
-
# -------------------- invoke library ---------------------------
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
model=None if run_full_grid else model,
|
| 60 |
-
run_all=run_full_grid,
|
| 61 |
-
filters=filters,
|
| 62 |
-
show_progress=not hide_progress,
|
| 63 |
-
extra_args=["-n", str(ngram_order)],
|
| 64 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
df = pd.read_csv(out_file)
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
# ---------------------------------------------------------------------
|
| 70 |
# Gradio UI
|
|
@@ -91,17 +152,24 @@ with gr.Blocks(title="UCI Phonotactic Calculator") as demo:
|
|
| 91 |
run_grid = gr.Checkbox(label="Run full variant grid", value=False)
|
| 92 |
n_slider = gr.Slider(1, 4, step=1, value=2, label="n-gram order")
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
with gr.Accordion("Advanced", open=False):
|
| 95 |
filt_txt = gr.Textbox(
|
| 96 |
-
label="
|
| 97 |
placeholder="example: weight_mode=raw prob_mode=joint"
|
| 98 |
)
|
| 99 |
-
hide_prog = gr.Checkbox(label="Hide progress
|
| 100 |
|
| 101 |
go_btn = gr.Button("Score")
|
| 102 |
|
| 103 |
with gr.Column():
|
| 104 |
-
out_df = gr.Dataframe(label="Scores (preview)")
|
| 105 |
out_csv = gr.File(label="Download full CSV")
|
| 106 |
|
| 107 |
go_btn.click(
|
|
|
|
| 9 |
from pathlib import Path
|
| 10 |
import tempfile, os, pandas as pd, gradio as gr
|
| 11 |
|
| 12 |
+
# --- Gradio progress adapter for Rich-style progress ---
|
| 13 |
+
class _GradioProgressAdapter:
|
| 14 |
+
"""
|
| 15 |
+
Drop-in replacement for uci_phonotactic_calculator.progress.progress()
|
| 16 |
+
that streams status into the Gradio UI.
|
| 17 |
+
|
| 18 |
+
It only implements the bits the library actually calls:
|
| 19 |
+
with progress(...) as bar:
|
| 20 |
+
tid = bar.add_task("Training", total=N)
|
| 21 |
+
...
|
| 22 |
+
bar.update(tid, advance=1)
|
| 23 |
+
"""
|
| 24 |
+
def __init__(self, enabled: bool = True):
|
| 25 |
+
self.enabled = enabled
|
| 26 |
+
self._g_prog = None # gr.Progress instance
|
| 27 |
+
self._tasks = {} # local id β (current, total)
|
| 28 |
+
|
| 29 |
+
def __enter__(self):
|
| 30 |
+
if self.enabled:
|
| 31 |
+
# keep both the CM *and* the callable tracker
|
| 32 |
+
self._cm = gr.Progress() # context-manager
|
| 33 |
+
self._g_prog = self._cm.__enter__() # callable returned by __enter__
|
| 34 |
+
return self
|
| 35 |
+
|
| 36 |
+
def __exit__(self, exc_type, exc, tb):
|
| 37 |
+
if getattr(self, "_cm", None):
|
| 38 |
+
self._cm.__exit__(exc_type, exc, tb)
|
| 39 |
+
|
| 40 |
+
# βββ Rich-look-alike API βββββββββββββββββββββββββββββββββββββββββ
|
| 41 |
+
def add_task(self, description: str, total: int | None = None):
|
| 42 |
+
task_id = len(self._tasks) + 1
|
| 43 |
+
self._tasks[task_id] = [0, total or 0]
|
| 44 |
+
if self._g_prog:
|
| 45 |
+
# The callable has set_description() only on Gradio β₯4.3
|
| 46 |
+
if hasattr(self._g_prog, "set_description"):
|
| 47 |
+
self._g_prog.set_description(description)
|
| 48 |
+
self._g_prog(0, total or 0)
|
| 49 |
+
return task_id
|
| 50 |
+
|
| 51 |
+
def update(self, task_id: int, advance: int = 1):
|
| 52 |
+
cur, tot = self._tasks[task_id]
|
| 53 |
+
cur += advance
|
| 54 |
+
self._tasks[task_id][0] = cur
|
| 55 |
+
if self._g_prog:
|
| 56 |
+
self._g_prog(cur, tot)
|
| 57 |
+
|
| 58 |
# ---> public, documented API wrapper around the CLI
|
| 59 |
from uci_phonotactic_calculator.ngram_calculator import run as ngram_run
|
| 60 |
from uci_phonotactic_calculator.plugins import PluginRegistry
|
| 61 |
from uci_phonotactic_calculator.cli_demo_data import get_demo_paths
|
| 62 |
|
| 63 |
TMP_DIR = Path(tempfile.gettempdir())
|
| 64 |
+
from uuid import uuid4, uuid1
|
| 65 |
|
| 66 |
# ---------------------------------------------------------------------
|
| 67 |
# Back-end helper
|
|
|
|
| 87 |
raise gr.Error("Upload BOTH training & test CSVs *or* tick the demo-data box.")
|
| 88 |
train_path, test_path = train_csv.name, test_csv.name
|
| 89 |
|
| 90 |
+
out_file = TMP_DIR / f"scores_{uuid4().hex}.csv"
|
| 91 |
+
import atexit, functools
|
| 92 |
+
atexit.register(functools.partial(out_file.unlink, missing_ok=True))
|
| 93 |
|
| 94 |
# -------------------- translate filters -------------------------
|
| 95 |
filters = {}
|
| 96 |
+
tokens = filter_string.split()
|
| 97 |
+
if tokens and tokens[0] == "--filter":
|
| 98 |
+
tokens = tokens[1:] # drop the flag if present
|
| 99 |
+
if tokens:
|
| 100 |
+
for tok in tokens:
|
| 101 |
if "=" not in tok:
|
| 102 |
raise gr.Error(f"Filter β{tok}β must look like key=value")
|
| 103 |
k, v = tok.split("=", 1)
|
| 104 |
filters[k] = v
|
| 105 |
|
| 106 |
+
# -------------------- invoke library with Gradio progress patch ---------------------------
|
| 107 |
+
import uci_phonotactic_calculator.progress as _p
|
| 108 |
+
_orig_progress = _p.progress # keep to restore later
|
| 109 |
+
_p.progress = lambda enabled=True: _GradioProgressAdapter(
|
| 110 |
+
enabled=enabled and not hide_progress
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
)
|
| 112 |
+
try:
|
| 113 |
+
ngram_run(
|
| 114 |
+
train_file=train_path,
|
| 115 |
+
test_file=test_path,
|
| 116 |
+
output_file=str(out_file),
|
| 117 |
+
model=None if run_full_grid else model,
|
| 118 |
+
run_all=run_full_grid,
|
| 119 |
+
filters=filters,
|
| 120 |
+
show_progress=not hide_progress, # still disables library chatter
|
| 121 |
+
extra_args=["-n", str(ngram_order)],
|
| 122 |
+
)
|
| 123 |
+
finally:
|
| 124 |
+
_p.progress = _orig_progress # guarantee cleanup
|
| 125 |
|
| 126 |
df = pd.read_csv(out_file)
|
| 127 |
+
df_preview = df.head(50).iloc[:, :30] # show only first 50 rows, 30 cols in UI
|
| 128 |
+
return df_preview, str(out_file)
|
| 129 |
|
| 130 |
# ---------------------------------------------------------------------
|
| 131 |
# Gradio UI
|
|
|
|
| 152 |
run_grid = gr.Checkbox(label="Run full variant grid", value=False)
|
| 153 |
n_slider = gr.Slider(1, 4, step=1, value=2, label="n-gram order")
|
| 154 |
|
| 155 |
+
# Disable model dropdown when 'Run full grid' is checked
|
| 156 |
+
run_grid.change(
|
| 157 |
+
lambda g: gr.update(interactive=not g),
|
| 158 |
+
inputs=run_grid,
|
| 159 |
+
outputs=model_dd
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
with gr.Accordion("Advanced", open=False):
|
| 163 |
filt_txt = gr.Textbox(
|
| 164 |
+
label="Filter (space-separated key=value β¦)",
|
| 165 |
placeholder="example: weight_mode=raw prob_mode=joint"
|
| 166 |
)
|
| 167 |
+
hide_prog = gr.Checkbox(label="Hide progress indicator", value=False)
|
| 168 |
|
| 169 |
go_btn = gr.Button("Score")
|
| 170 |
|
| 171 |
with gr.Column():
|
| 172 |
+
out_df = gr.Dataframe(label="Scores (preview)", interactive=False, height=350)
|
| 173 |
out_csv = gr.File(label="Download full CSV")
|
| 174 |
|
| 175 |
go_btn.click(
|