Spaces:
Sleeping
Sleeping
Update script typo and better error handling
#1
by dusmel - opened
app.py
CHANGED
|
@@ -19,23 +19,24 @@ TSV_FILE = os.path.join(DATA_ROOT, "validated.tsv")
|
|
| 19 |
|
| 20 |
os.makedirs(AUDIO_FOLDER, exist_ok=True)
|
| 21 |
|
| 22 |
-
# 3. SCRIPTS
|
| 23 |
SCRIPTS = [
|
| 24 |
"Nagerageje kohereza amafaranga kuri MoMo, ariko rezo yari mbi cyane sinabasha kubona mesaje yemeza ko yagiye.",
|
| 25 |
"Mbere yo kwinjira muri mudasobwa, banza urebe niba sisitemu yakoze update, hanyuma ushyiremo ijambo ry'ibanga kugira ngo ufungure.",
|
| 26 |
"Nta megabayiti zihagije mfite muri telefoni, reka nshakishe wifi hano hafi kugira ngo mbashe gukurura iyo porogaramu vuba.",
|
| 27 |
-
"Iyi sharijeri ya smartphone iragura ibihumbi bitanu, ariko niba ushaka n
|
| 28 |
-
"Nugera kuri banki i Nyarugenge, ubwire ejenti agufashe kubikuza ayo madolari mbere y
|
| 29 |
-
"Wohereze
|
| 30 |
"Ugomba gusiba amafoto adakenewe kugira ngo ubone aho kubika izindi gigabayiti ebyiri, cyangwa ukoreshe flash.",
|
| 31 |
"Koresha Mokash wishyure iyo fagitire, kode ni zeru gatatu rimwe, ubundi tujye Gasabo ku wa Gatanu.",
|
| 32 |
"Koresha kibodi wandike ubutumwa, hanyuma uwohereze kuri WhatsApp kuko interineti yo mu Majyaruguru iragenda buhoro.",
|
| 33 |
-
"
|
| 34 |
]
|
| 35 |
|
| 36 |
# --- HELPER FUNCTIONS ---
|
| 37 |
|
| 38 |
def generate_tsv():
|
|
|
|
| 39 |
cv_header = ["client_id", "path", "sentence", "up_votes", "down_votes", "age", "gender", "accent", "locale", "segment"]
|
| 40 |
tsv_content = []
|
| 41 |
tsv_content.append("\t".join(cv_header))
|
|
@@ -65,6 +66,7 @@ def generate_tsv():
|
|
| 65 |
f.write("\n".join(tsv_content))
|
| 66 |
|
| 67 |
def backup_to_dataset_repo(repo_name=BACKUP_REPO_NAME):
|
|
|
|
| 68 |
if not HF_TOKEN:
|
| 69 |
print("⚠️ Backup Skipped: HF_TOKEN missing.")
|
| 70 |
return
|
|
@@ -89,9 +91,22 @@ def backup_to_dataset_repo(repo_name=BACKUP_REPO_NAME):
|
|
| 89 |
print(f"⚠️ Backup Failed: {e}")
|
| 90 |
|
| 91 |
def save_data(age, gender, region, *audios):
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 96 |
participant_id = f"user_{timestamp}"
|
| 97 |
filenames = []
|
|
@@ -102,6 +117,7 @@ def save_data(age, gender, region, *audios):
|
|
| 102 |
shutil.copy(audio_path, save_path)
|
| 103 |
filenames.append(filename)
|
| 104 |
|
|
|
|
| 105 |
file_exists = os.path.exists(CSV_FILE)
|
| 106 |
with open(CSV_FILE, mode='a', newline='', encoding='utf-8') as f:
|
| 107 |
writer = csv.writer(f)
|
|
@@ -120,7 +136,6 @@ def transition_to_recorder(age, gender, region):
|
|
| 120 |
# --- UI BUILDER ---
|
| 121 |
|
| 122 |
# CSS: System Sans-Serif Stack (Fastest & Cleanest)
|
| 123 |
-
# Uses "Inter" or "San Francisco" on Mac, "Segoe UI" on Windows, "Roboto" on Android.
|
| 124 |
system_sans_css = """
|
| 125 |
body, .gradio-container {
|
| 126 |
font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", "Liberation Sans", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji" !important;
|
|
@@ -131,7 +146,7 @@ button, input, textarea, span, div, label, p, h1, h2, h3 {
|
|
| 131 |
}
|
| 132 |
"""
|
| 133 |
|
| 134 |
-
with gr.Blocks(title="Kinyarwanda
|
| 135 |
gr.Markdown("# 🇷🇼 Improve Kinyarwanda AI")
|
| 136 |
|
| 137 |
# PAGE 1: METADATA
|
|
@@ -172,12 +187,14 @@ with gr.Blocks(title="Kinyarwanda Data Tool", css=system_sans_css, theme=gr.them
|
|
| 172 |
# --- WIRING ---
|
| 173 |
next_btn.click(fn=transition_to_recorder, inputs=[age_input, gender_input, region_input], outputs=[metadata_page, recording_page, error_msg])
|
| 174 |
|
|
|
|
| 175 |
save_event = submit_btn.click(
|
| 176 |
fn=save_data,
|
| 177 |
inputs=[age_input, gender_input, region_input] + audio_inputs,
|
| 178 |
outputs=[status_msg, recording_page, success_page]
|
| 179 |
)
|
| 180 |
|
|
|
|
| 181 |
save_event.then(
|
| 182 |
fn=lambda: backup_to_dataset_repo(),
|
| 183 |
inputs=None,
|
|
|
|
| 19 |
|
| 20 |
os.makedirs(AUDIO_FOLDER, exist_ok=True)
|
| 21 |
|
| 22 |
+
# 3. SCRIPTS (Anonymized & Improved)
|
| 23 |
SCRIPTS = [
|
| 24 |
"Nagerageje kohereza amafaranga kuri MoMo, ariko rezo yari mbi cyane sinabasha kubona mesaje yemeza ko yagiye.",
|
| 25 |
"Mbere yo kwinjira muri mudasobwa, banza urebe niba sisitemu yakoze update, hanyuma ushyiremo ijambo ry'ibanga kugira ngo ufungure.",
|
| 26 |
"Nta megabayiti zihagije mfite muri telefoni, reka nshakishe wifi hano hafi kugira ngo mbashe gukurura iyo porogaramu vuba.",
|
| 27 |
+
"Iyi sharijeri ya smartphone iragura ibihumbi bitanu, ariko niba ushaka n'ibirahure bya ecran, turaguha byose ku icumi.",
|
| 28 |
+
"Nugera kuri banki i Nyarugenge, ubwire ejenti agufashe kubikuza ayo madolari mbere y'uko ukwezi kwa Gicurasi kurangira.",
|
| 29 |
+
"Wohereze imeli itarimo amakosa kugira ngo ubashe guhindura mot de passe, hanyuma ukande kuri linki iri bube kuri ecran yawe.",
|
| 30 |
"Ugomba gusiba amafoto adakenewe kugira ngo ubone aho kubika izindi gigabayiti ebyiri, cyangwa ukoreshe flash.",
|
| 31 |
"Koresha Mokash wishyure iyo fagitire, kode ni zeru gatatu rimwe, ubundi tujye Gasabo ku wa Gatanu.",
|
| 32 |
"Koresha kibodi wandike ubutumwa, hanyuma uwohereze kuri WhatsApp kuko interineti yo mu Majyaruguru iragenda buhoro.",
|
| 33 |
+
"Nurangiza gukoresha iyo apurikasiyo, wibuke gukora log out kugira ngo hatagira undi umukiriya ureba amabanga yawe."
|
| 34 |
]
|
| 35 |
|
| 36 |
# --- HELPER FUNCTIONS ---
|
| 37 |
|
| 38 |
def generate_tsv():
|
| 39 |
+
"""Generates the Standard Mozilla 'validated.tsv' for the backup."""
|
| 40 |
cv_header = ["client_id", "path", "sentence", "up_votes", "down_votes", "age", "gender", "accent", "locale", "segment"]
|
| 41 |
tsv_content = []
|
| 42 |
tsv_content.append("\t".join(cv_header))
|
|
|
|
| 66 |
f.write("\n".join(tsv_content))
|
| 67 |
|
| 68 |
def backup_to_dataset_repo(repo_name=BACKUP_REPO_NAME):
|
| 69 |
+
"""Backs up data to Hugging Face Dataset."""
|
| 70 |
if not HF_TOKEN:
|
| 71 |
print("⚠️ Backup Skipped: HF_TOKEN missing.")
|
| 72 |
return
|
|
|
|
| 91 |
print(f"⚠️ Backup Failed: {e}")
|
| 92 |
|
| 93 |
def save_data(age, gender, region, *audios):
|
| 94 |
+
"""
|
| 95 |
+
Saves data locally.
|
| 96 |
+
Includes specific error checking to tell user WHICH audio is missing.
|
| 97 |
+
"""
|
| 98 |
+
# 1. Identify missing recordings
|
| 99 |
+
missing_indices = []
|
| 100 |
+
for i, audio in enumerate(audios):
|
| 101 |
+
if audio is None:
|
| 102 |
+
missing_indices.append(str(i+1)) # Store 1-based index
|
| 103 |
+
|
| 104 |
+
if missing_indices:
|
| 105 |
+
missing_str = ", ".join(missing_indices)
|
| 106 |
+
error_msg = f"⚠️ Error: You missed Sentence(s): {missing_str}. Please record them."
|
| 107 |
+
return f"<span style='color: red'>{error_msg}</span>", gr.update(visible=True), gr.update(visible=True)
|
| 108 |
+
|
| 109 |
+
# 2. Generate Filenames & Save
|
| 110 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 111 |
participant_id = f"user_{timestamp}"
|
| 112 |
filenames = []
|
|
|
|
| 117 |
shutil.copy(audio_path, save_path)
|
| 118 |
filenames.append(filename)
|
| 119 |
|
| 120 |
+
# 3. Append to CSV
|
| 121 |
file_exists = os.path.exists(CSV_FILE)
|
| 122 |
with open(CSV_FILE, mode='a', newline='', encoding='utf-8') as f:
|
| 123 |
writer = csv.writer(f)
|
|
|
|
| 136 |
# --- UI BUILDER ---
|
| 137 |
|
| 138 |
# CSS: System Sans-Serif Stack (Fastest & Cleanest)
|
|
|
|
| 139 |
system_sans_css = """
|
| 140 |
body, .gradio-container {
|
| 141 |
font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", "Liberation Sans", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji" !important;
|
|
|
|
| 146 |
}
|
| 147 |
"""
|
| 148 |
|
| 149 |
+
with gr.Blocks(title="Improve Kinyarwanda AI", css=system_sans_css, theme=gr.themes.Soft()) as demo:
|
| 150 |
gr.Markdown("# 🇷🇼 Improve Kinyarwanda AI")
|
| 151 |
|
| 152 |
# PAGE 1: METADATA
|
|
|
|
| 187 |
# --- WIRING ---
|
| 188 |
next_btn.click(fn=transition_to_recorder, inputs=[age_input, gender_input, region_input], outputs=[metadata_page, recording_page, error_msg])
|
| 189 |
|
| 190 |
+
# 1. Save Local
|
| 191 |
save_event = submit_btn.click(
|
| 192 |
fn=save_data,
|
| 193 |
inputs=[age_input, gender_input, region_input] + audio_inputs,
|
| 194 |
outputs=[status_msg, recording_page, success_page]
|
| 195 |
)
|
| 196 |
|
| 197 |
+
# 2. Auto-Backup to Cloud (Background)
|
| 198 |
save_event.then(
|
| 199 |
fn=lambda: backup_to_dataset_repo(),
|
| 200 |
inputs=None,
|