Upload app.py
Browse files
app.py
CHANGED
|
@@ -464,7 +464,9 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 464 |
su_user = gr.Textbox(label="Username", info="3β32 chars: letters, numbers, _ . -")
|
| 465 |
su_email = gr.Textbox(label="Email (optional)")
|
| 466 |
su_pw = gr.Textbox(label="Password", type="password", info="At least 6 characters")
|
| 467 |
-
|
|
|
|
|
|
|
| 468 |
su_code = gr.Textbox(label="Admin code (optional)", type="password",
|
| 469 |
info="Leave blank for a normal reviewer account.")
|
| 470 |
su_btn = gr.Button("Create account", variant="primary")
|
|
@@ -519,13 +521,6 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 519 |
progress_tbl = gr.Dataframe(
|
| 520 |
headers=["Language", "Rated", "Total", "Remaining"], interactive=False)
|
| 521 |
|
| 522 |
-
# ---------- Profile tab ----------
|
| 523 |
-
with gr.Tab("My languages"):
|
| 524 |
-
gr.Markdown("Choose which languages you are eligible to evaluate.")
|
| 525 |
-
prof_langs = gr.CheckboxGroup(label="Languages", choices=lang_choices())
|
| 526 |
-
prof_save = gr.Button("Save", variant="primary", size="sm")
|
| 527 |
-
prof_msg = gr.Markdown()
|
| 528 |
-
|
| 529 |
# ---------- Admin view (entirely separate from reviewer tabs) ----------
|
| 530 |
with gr.Column(visible=False) as admin_panel:
|
| 531 |
gr.Markdown("### Languages")
|
|
@@ -555,8 +550,9 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 555 |
headers=["id", "language", "sample_name", "model", "reference"],
|
| 556 |
interactive=False, label="Samples")
|
| 557 |
with gr.Row():
|
| 558 |
-
|
| 559 |
-
|
|
|
|
| 560 |
|
| 561 |
gr.Markdown("### Users")
|
| 562 |
refresh_users_btn = gr.Button("Refresh users", size="sm")
|
|
@@ -591,7 +587,7 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 591 |
if not user:
|
| 592 |
# keep typed credentials so the user can correct them
|
| 593 |
return (gr.update(), gr.update(), None, "β Invalid username or password.",
|
| 594 |
-
gr.update(), gr.update(), gr.update(), gr.update(),
|
| 595 |
gr.update(), gr.update())
|
| 596 |
sess = user_session(user["id"])
|
| 597 |
is_admin = sess["role"] == "admin"
|
|
@@ -605,8 +601,6 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 605 |
gr.update(visible=not is_admin), # reviewer_tabs
|
| 606 |
gr.update(visible=is_admin), # admin_panel
|
| 607 |
gr.update(choices=rl, value=None), # rate_lang
|
| 608 |
-
gr.update(choices=lang_choices(),
|
| 609 |
-
value=[l["id"] for l in sess["languages"]]), # prof_langs
|
| 610 |
gr.update(value=""), # li_user (clear)
|
| 611 |
gr.update(value=""), # li_pw (clear)
|
| 612 |
)
|
|
@@ -614,27 +608,28 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 614 |
li_btn.click(
|
| 615 |
do_login, [li_user, li_pw],
|
| 616 |
[auth_col, app_col, session, li_msg, greeting, reviewer_tabs, admin_panel,
|
| 617 |
-
rate_lang,
|
| 618 |
)
|
| 619 |
li_pw.submit( # allow Enter-to-login
|
| 620 |
do_login, [li_user, li_pw],
|
| 621 |
[auth_col, app_col, session, li_msg, greeting, reviewer_tabs, admin_panel,
|
| 622 |
-
rate_lang,
|
| 623 |
)
|
| 624 |
|
| 625 |
-
def do_signup(username, email, password,
|
| 626 |
role = "admin" if (code and code == ADMIN_CODE) else "reviewer"
|
|
|
|
| 627 |
try:
|
| 628 |
-
create_user(username, email, password, role=role, language_ids=lang_ids
|
| 629 |
except ValueError as e:
|
| 630 |
# keep fields so the user can fix the problem
|
| 631 |
return (f"β {e}", gr.update(), gr.update(), gr.update(), gr.update(), gr.update())
|
| 632 |
note = " (admin account)" if role == "admin" else ""
|
| 633 |
return ("β
Account created" + note + ". Switch to the **Sign in** tab to continue.",
|
| 634 |
gr.update(value=""), gr.update(value=""), gr.update(value=""),
|
| 635 |
-
gr.update(value=
|
| 636 |
-
su_btn.click(do_signup, [su_user, su_email, su_pw,
|
| 637 |
-
[su_msg, su_user, su_email, su_pw,
|
| 638 |
|
| 639 |
def do_logout():
|
| 640 |
return (gr.update(visible=True), gr.update(visible=False), None, "")
|
|
@@ -715,23 +710,13 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 715 |
return f"Progress for **{sess['username']}**:", rows
|
| 716 |
refresh_prog_btn.click(load_progress, [session], [progress_md, progress_tbl])
|
| 717 |
|
| 718 |
-
# ---- Profile ----
|
| 719 |
-
def save_profile(sess, lang_ids):
|
| 720 |
-
if not sess:
|
| 721 |
-
return "β Not signed in.", sess, gr.update()
|
| 722 |
-
set_user_languages(sess["id"], lang_ids or [])
|
| 723 |
-
new_sess = user_session(sess["id"])
|
| 724 |
-
rl = reviewer_lang_choices(new_sess)
|
| 725 |
-
return "β
Languages updated.", new_sess, gr.update(choices=rl, value=None)
|
| 726 |
-
prof_save.click(save_profile, [session, prof_langs], [prof_msg, session, rate_lang])
|
| 727 |
-
|
| 728 |
# ---- Admin: languages ----
|
| 729 |
def _languages_table():
|
| 730 |
return [[l["id"], l["code"], l["name"]] for l in list_languages()]
|
| 731 |
|
| 732 |
def _lang_dropdown_updates():
|
| 733 |
ch = lang_choices()
|
| 734 |
-
return (gr.update(choices=ch), gr.update(choices=ch), gr.update(choices=ch)
|
| 735 |
|
| 736 |
def admin_add_language(sess, code, name):
|
| 737 |
if not sess or sess["role"] != "admin":
|
|
@@ -743,7 +728,7 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 743 |
return (f"β
Added {name} ({code}).", _languages_table(), *_lang_dropdown_updates())
|
| 744 |
al_btn.click(
|
| 745 |
admin_add_language, [session, al_code, al_name],
|
| 746 |
-
[al_msg, langs_tbl, up_lang, res_lang,
|
| 747 |
)
|
| 748 |
|
| 749 |
# ---- Admin: samples ----
|
|
@@ -751,13 +736,17 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 751 |
return [[s["id"], f"{s['language_name']} ({s['language_code']})", s["sample_name"],
|
| 752 |
s["model_name"], "yes" if s["is_reference"] else "no"] for s in list_samples()]
|
| 753 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
def admin_upload(sess, language_id, files, model, is_ref, transcript):
|
| 755 |
if not sess or sess["role"] != "admin":
|
| 756 |
-
return "β Admin only.", _samples_table()
|
| 757 |
if not language_id:
|
| 758 |
-
return "β Choose a language first.", _samples_table()
|
| 759 |
if not files:
|
| 760 |
-
return "β No files selected.", _samples_table()
|
| 761 |
count = 0
|
| 762 |
for f in files:
|
| 763 |
path = f if isinstance(f, str) else getattr(f, "name", None)
|
|
@@ -765,19 +754,23 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 765 |
add_sample(language_id, path, model_name=model, is_reference=is_ref, transcript=transcript)
|
| 766 |
count += 1
|
| 767 |
except Exception as e: # noqa
|
| 768 |
-
return f"β Error on a file: {e}", _samples_table()
|
| 769 |
-
|
|
|
|
|
|
|
| 770 |
up_btn.click(admin_upload, [session, up_lang, up_files, up_model, up_isref, up_transcript],
|
| 771 |
-
[up_msg, samples_tbl])
|
| 772 |
|
| 773 |
def admin_delete_sample(sess, sid):
|
| 774 |
if not sess or sess["role"] != "admin":
|
| 775 |
-
return "β Admin only.", _samples_table()
|
| 776 |
if not sid:
|
| 777 |
-
return "β
|
|
|
|
| 778 |
delete_sample(int(sid))
|
| 779 |
-
return f"β
Deleted sample {int(sid)}.", _samples_table()
|
| 780 |
-
|
|
|
|
| 781 |
|
| 782 |
# ---- Admin: users ----
|
| 783 |
def _users_table():
|
|
@@ -823,17 +816,18 @@ with gr.Blocks(title="Plotweaver AI β TTS MOS Evaluation",
|
|
| 823 |
return export_results(language_id)
|
| 824 |
export_btn.click(admin_export, [session, res_lang], [res_file])
|
| 825 |
|
| 826 |
-
# Populate admin tables right after an admin logs in.
|
| 827 |
-
li_btn.click(lambda s: (_languages_table(), _samples_table(), _users_table()
|
| 828 |
-
|
| 829 |
-
[
|
|
|
|
| 830 |
|
| 831 |
# Refresh language-dependent choices on every page load, so newly added
|
| 832 |
# languages appear in the signup form (and admin dropdowns) without a restart.
|
| 833 |
def refresh_lang_choices():
|
| 834 |
ch = lang_choices()
|
| 835 |
return (gr.update(choices=ch), gr.update(choices=ch), gr.update(choices=ch))
|
| 836 |
-
demo.load(refresh_lang_choices, None, [
|
| 837 |
|
| 838 |
|
| 839 |
if __name__ == "__main__":
|
|
|
|
| 464 |
su_user = gr.Textbox(label="Username", info="3β32 chars: letters, numbers, _ . -")
|
| 465 |
su_email = gr.Textbox(label="Email (optional)")
|
| 466 |
su_pw = gr.Textbox(label="Password", type="password", info="At least 6 characters")
|
| 467 |
+
su_lang = gr.Dropdown(label="Language", choices=lang_choices(),
|
| 468 |
+
allow_custom_value=True,
|
| 469 |
+
info="The language you will evaluate.")
|
| 470 |
su_code = gr.Textbox(label="Admin code (optional)", type="password",
|
| 471 |
info="Leave blank for a normal reviewer account.")
|
| 472 |
su_btn = gr.Button("Create account", variant="primary")
|
|
|
|
| 521 |
progress_tbl = gr.Dataframe(
|
| 522 |
headers=["Language", "Rated", "Total", "Remaining"], interactive=False)
|
| 523 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
# ---------- Admin view (entirely separate from reviewer tabs) ----------
|
| 525 |
with gr.Column(visible=False) as admin_panel:
|
| 526 |
gr.Markdown("### Languages")
|
|
|
|
| 550 |
headers=["id", "language", "sample_name", "model", "reference"],
|
| 551 |
interactive=False, label="Samples")
|
| 552 |
with gr.Row():
|
| 553 |
+
del_sample = gr.Dropdown(label="Select a sample to delete", choices=[],
|
| 554 |
+
allow_custom_value=True, scale=3)
|
| 555 |
+
del_btn = gr.Button("Delete selected", variant="stop", scale=0, size="sm")
|
| 556 |
|
| 557 |
gr.Markdown("### Users")
|
| 558 |
refresh_users_btn = gr.Button("Refresh users", size="sm")
|
|
|
|
| 587 |
if not user:
|
| 588 |
# keep typed credentials so the user can correct them
|
| 589 |
return (gr.update(), gr.update(), None, "β Invalid username or password.",
|
| 590 |
+
gr.update(), gr.update(), gr.update(), gr.update(),
|
| 591 |
gr.update(), gr.update())
|
| 592 |
sess = user_session(user["id"])
|
| 593 |
is_admin = sess["role"] == "admin"
|
|
|
|
| 601 |
gr.update(visible=not is_admin), # reviewer_tabs
|
| 602 |
gr.update(visible=is_admin), # admin_panel
|
| 603 |
gr.update(choices=rl, value=None), # rate_lang
|
|
|
|
|
|
|
| 604 |
gr.update(value=""), # li_user (clear)
|
| 605 |
gr.update(value=""), # li_pw (clear)
|
| 606 |
)
|
|
|
|
| 608 |
li_btn.click(
|
| 609 |
do_login, [li_user, li_pw],
|
| 610 |
[auth_col, app_col, session, li_msg, greeting, reviewer_tabs, admin_panel,
|
| 611 |
+
rate_lang, li_user, li_pw],
|
| 612 |
)
|
| 613 |
li_pw.submit( # allow Enter-to-login
|
| 614 |
do_login, [li_user, li_pw],
|
| 615 |
[auth_col, app_col, session, li_msg, greeting, reviewer_tabs, admin_panel,
|
| 616 |
+
rate_lang, li_user, li_pw],
|
| 617 |
)
|
| 618 |
|
| 619 |
+
def do_signup(username, email, password, lang_id, code):
|
| 620 |
role = "admin" if (code and code == ADMIN_CODE) else "reviewer"
|
| 621 |
+
lang_ids = [lang_id] if lang_id else []
|
| 622 |
try:
|
| 623 |
+
create_user(username, email, password, role=role, language_ids=lang_ids)
|
| 624 |
except ValueError as e:
|
| 625 |
# keep fields so the user can fix the problem
|
| 626 |
return (f"β {e}", gr.update(), gr.update(), gr.update(), gr.update(), gr.update())
|
| 627 |
note = " (admin account)" if role == "admin" else ""
|
| 628 |
return ("β
Account created" + note + ". Switch to the **Sign in** tab to continue.",
|
| 629 |
gr.update(value=""), gr.update(value=""), gr.update(value=""),
|
| 630 |
+
gr.update(value=None), gr.update(value=""))
|
| 631 |
+
su_btn.click(do_signup, [su_user, su_email, su_pw, su_lang, su_code],
|
| 632 |
+
[su_msg, su_user, su_email, su_pw, su_lang, su_code])
|
| 633 |
|
| 634 |
def do_logout():
|
| 635 |
return (gr.update(visible=True), gr.update(visible=False), None, "")
|
|
|
|
| 710 |
return f"Progress for **{sess['username']}**:", rows
|
| 711 |
refresh_prog_btn.click(load_progress, [session], [progress_md, progress_tbl])
|
| 712 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
# ---- Admin: languages ----
|
| 714 |
def _languages_table():
|
| 715 |
return [[l["id"], l["code"], l["name"]] for l in list_languages()]
|
| 716 |
|
| 717 |
def _lang_dropdown_updates():
|
| 718 |
ch = lang_choices()
|
| 719 |
+
return (gr.update(choices=ch), gr.update(choices=ch), gr.update(choices=ch))
|
| 720 |
|
| 721 |
def admin_add_language(sess, code, name):
|
| 722 |
if not sess or sess["role"] != "admin":
|
|
|
|
| 728 |
return (f"β
Added {name} ({code}).", _languages_table(), *_lang_dropdown_updates())
|
| 729 |
al_btn.click(
|
| 730 |
admin_add_language, [session, al_code, al_name],
|
| 731 |
+
[al_msg, langs_tbl, up_lang, res_lang, su_lang],
|
| 732 |
)
|
| 733 |
|
| 734 |
# ---- Admin: samples ----
|
|
|
|
| 736 |
return [[s["id"], f"{s['language_name']} ({s['language_code']})", s["sample_name"],
|
| 737 |
s["model_name"], "yes" if s["is_reference"] else "no"] for s in list_samples()]
|
| 738 |
|
| 739 |
+
def _sample_delete_choices():
|
| 740 |
+
return [(f"#{s['id']} β {s['sample_name']} ({s['model_name']}, {s['language_code']})", s["id"])
|
| 741 |
+
for s in list_samples()]
|
| 742 |
+
|
| 743 |
def admin_upload(sess, language_id, files, model, is_ref, transcript):
|
| 744 |
if not sess or sess["role"] != "admin":
|
| 745 |
+
return "β Admin only.", _samples_table(), gr.update()
|
| 746 |
if not language_id:
|
| 747 |
+
return "β Choose a language first.", _samples_table(), gr.update()
|
| 748 |
if not files:
|
| 749 |
+
return "β No files selected.", _samples_table(), gr.update()
|
| 750 |
count = 0
|
| 751 |
for f in files:
|
| 752 |
path = f if isinstance(f, str) else getattr(f, "name", None)
|
|
|
|
| 754 |
add_sample(language_id, path, model_name=model, is_reference=is_ref, transcript=transcript)
|
| 755 |
count += 1
|
| 756 |
except Exception as e: # noqa
|
| 757 |
+
return (f"β Error on a file: {e}", _samples_table(),
|
| 758 |
+
gr.update(choices=_sample_delete_choices()))
|
| 759 |
+
return (f"β
Uploaded {count} sample(s).", _samples_table(),
|
| 760 |
+
gr.update(choices=_sample_delete_choices(), value=None))
|
| 761 |
up_btn.click(admin_upload, [session, up_lang, up_files, up_model, up_isref, up_transcript],
|
| 762 |
+
[up_msg, samples_tbl, del_sample])
|
| 763 |
|
| 764 |
def admin_delete_sample(sess, sid):
|
| 765 |
if not sess or sess["role"] != "admin":
|
| 766 |
+
return "β Admin only.", _samples_table(), gr.update()
|
| 767 |
if not sid:
|
| 768 |
+
return ("β Select a sample to delete.", _samples_table(),
|
| 769 |
+
gr.update(choices=_sample_delete_choices()))
|
| 770 |
delete_sample(int(sid))
|
| 771 |
+
return (f"β
Deleted sample #{int(sid)} (and any ratings it had).", _samples_table(),
|
| 772 |
+
gr.update(choices=_sample_delete_choices(), value=None))
|
| 773 |
+
del_btn.click(admin_delete_sample, [session, del_sample], [up_msg, samples_tbl, del_sample])
|
| 774 |
|
| 775 |
# ---- Admin: users ----
|
| 776 |
def _users_table():
|
|
|
|
| 816 |
return export_results(language_id)
|
| 817 |
export_btn.click(admin_export, [session, res_lang], [res_file])
|
| 818 |
|
| 819 |
+
# Populate admin tables + delete picker right after an admin logs in.
|
| 820 |
+
li_btn.click(lambda s: (_languages_table(), _samples_table(), _users_table(),
|
| 821 |
+
gr.update(choices=_sample_delete_choices()))
|
| 822 |
+
if s and s["role"] == "admin" else ([], [], [], gr.update(choices=[])),
|
| 823 |
+
[session], [langs_tbl, samples_tbl, users_tbl, del_sample])
|
| 824 |
|
| 825 |
# Refresh language-dependent choices on every page load, so newly added
|
| 826 |
# languages appear in the signup form (and admin dropdowns) without a restart.
|
| 827 |
def refresh_lang_choices():
|
| 828 |
ch = lang_choices()
|
| 829 |
return (gr.update(choices=ch), gr.update(choices=ch), gr.update(choices=ch))
|
| 830 |
+
demo.load(refresh_lang_choices, None, [su_lang, up_lang, res_lang])
|
| 831 |
|
| 832 |
|
| 833 |
if __name__ == "__main__":
|