Spaces:
Sleeping
Sleeping
Download app.py from Winsmano/AudioDataCollector: direct link, hf CLI and curl.
- Browser
- Download file 17.1 kB
-
https://huggingface.co/spaces/Winsmano/AudioDataCollector/resolve/main/app.py
- Command line
-
hf download hf://spaces/Winsmano/AudioDataCollector/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Winsmano/AudioDataCollector/resolve/main/app.py
17.1 kB
| """ | |
| Gradio Audio Dataset Collector | |
| -------------------------------- | |
| Features: | |
| - Upload a CSV of English sentences. Optional column: pidgin_text. | |
| - For each row: view English (read-only), edit/add Pidgin translation, record audio from mic. | |
| - Listen before saving; overwrite allowed. No timestamps in filenames — saved as `<row_id>.wav`. | |
| - Navigate Previous/Next or jump via dropdown. Review table updates live. | |
| - Export metadata CSV and a ZIP (audio + metadata) for sharing. | |
| CSV requirements: | |
| - If your CSV has columns named `english_text` (required) and optionally `pidgin_text` and `row_id`. | |
| - If `row_id` is missing, it will be created as 0..N-1. | |
| Run: | |
| pip install gradio pandas numpy soundfile scipy | |
| python app.py | |
| Then open the local URL Gradio prints. | |
| """ | |
| import os | |
| import io | |
| import shutil | |
| from typing import Tuple, Optional, List | |
| import numpy as np | |
| import pandas as pd | |
| import gradio as gr | |
| # Optional audio writers | |
| try: | |
| import soundfile as sf | |
| HAVE_SF = True | |
| except Exception: | |
| HAVE_SF = False | |
| from scipy.io import wavfile | |
| AUDIO_DIR = "recordings" | |
| META_CSV_PATH = "combined_sentences.csv" | |
| EXPORT_ZIP_PATH = "dataset_export.zip" | |
| DEFAULT_CSV_PATH = "combined_sentences.csv" | |
| STATE_FILE = "app_state.txt" | |
| os.makedirs(AUDIO_DIR, exist_ok=True) | |
| # ----------------------------- | |
| # Helpers | |
| # ----------------------------- | |
| def save_state(idx: int): | |
| with open(STATE_FILE, 'w') as f: | |
| f.write(str(idx)) | |
| def load_state() -> int: | |
| if os.path.exists(STATE_FILE): | |
| try: | |
| with open(STATE_FILE, 'r') as f: | |
| return int(f.read().strip()) | |
| except: | |
| pass | |
| return 0 | |
| def load_csv(fileobj) -> Tuple[pd.DataFrame, int, List[int]]: | |
| df = pd.read_csv(fileobj) | |
| # Normalize columns | |
| # Expect english_text; allow variations | |
| if "english_text" not in df.columns: | |
| # Try to infer a reasonable column | |
| candidates = [c for c in df.columns if str(c).lower() in ["english", "text", "sentence", "prompt"]] | |
| if candidates: | |
| df = df.rename(columns={candidates[0]: "english_text"}) | |
| else: | |
| raise ValueError("CSV must include an 'english_text' column (or a similar one like 'sentence').") | |
| if "pidgin_text" not in df.columns: | |
| df["pidgin_text"] = "" | |
| if "row_id" not in df.columns: | |
| df["row_id"] = range(len(df)) | |
| if "audio_file_path" not in df.columns: | |
| df["audio_file_path"] = "" | |
| # Ensure types | |
| df["english_text"] = df["english_text"].astype(str) | |
| df["pidgin_text"] = df["pidgin_text"].astype(str) | |
| df["row_id"] = df["row_id"].astype(int) | |
| df["audio_file_path"] = df["audio_file_path"].astype(str) | |
| # Sort by row_id to keep ordering stable | |
| df = df.sort_values("row_id").reset_index(drop=True) | |
| idx = load_state() | |
| idx = max(0, min(idx, len(df) - 1)) | |
| options = df["row_id"].tolist() | |
| return df, idx, options | |
| def get_row_view(df: pd.DataFrame, idx: int): | |
| row = df.iloc[idx] | |
| english = row["english_text"] | |
| pidgin = row["pidgin_text"] | |
| row_id = int(row["row_id"]) | |
| audio_path = row.get("audio_file_path", "") or "" | |
| audio_display = audio_path if (audio_path and os.path.exists(audio_path)) else None | |
| return row_id, english, pidgin, audio_display | |
| def get_progress(df: pd.DataFrame) -> str: | |
| if df.empty: | |
| return "No data loaded" | |
| has_text = df["pidgin_text"].str.strip() != "" | |
| has_audio = df["audio_file_path"].str.strip() != "" | |
| audio_exists = df["audio_file_path"].apply(lambda x: os.path.exists(x) if x else False) | |
| completed = len(df[has_text & has_audio & audio_exists]) | |
| total = len(df) | |
| return f"Progress: {completed}/{total} completed ({completed/total*100:.1f}%)" | |
| def find_next_incomplete(df: pd.DataFrame, current_idx: int) -> int: | |
| if df.empty: | |
| return current_idx | |
| incomplete = df[(df["pidgin_text"].str.strip() == "") | (df["audio_file_path"].str.strip() == "")] | |
| if incomplete.empty: | |
| return current_idx | |
| next_incomplete = incomplete[incomplete.index > current_idx] | |
| if not next_incomplete.empty: | |
| return next_incomplete.index[0] | |
| return incomplete.index[0] | |
| def write_wav(filepath: str, audio_tuple) -> None: | |
| """Write (sr, np.ndarray) to filepath as 16-bit PCM WAV.""" | |
| if audio_tuple is None: | |
| return | |
| sr, data = audio_tuple | |
| # Ensure data is numpy array | |
| data = np.array(data, dtype=np.float32) | |
| # Ensure mono | |
| if data.ndim > 1: | |
| data = np.mean(data, axis=1) | |
| # Normalize to [-1, 1] range | |
| if np.max(np.abs(data)) > 0: | |
| data = data / np.max(np.abs(data)) | |
| if HAVE_SF: | |
| sf.write(filepath, data, samplerate=sr, subtype="PCM_16") | |
| else: | |
| # Convert to int16 for scipy writer | |
| data_i16 = np.int16(data * 32767) | |
| wavfile.write(filepath, sr, data_i16) | |
| def save_current(df: pd.DataFrame, idx: int, pidgin_text: str, audio_np) -> Tuple[pd.DataFrame, str, Optional[str]]: | |
| row = df.iloc[idx] | |
| row_id = int(row["row_id"]) | |
| # Update text first | |
| df.at[idx, "pidgin_text"] = pidgin_text or "" | |
| saved_audio_path = None | |
| # Save audio if provided; otherwise leave existing file as-is | |
| if audio_np is not None: | |
| # No timestamps — fixed name per row | |
| saved_audio_path = os.path.join(AUDIO_DIR, f"{row_id}.wav") | |
| write_wav(saved_audio_path, audio_np) | |
| df.at[idx, "audio_file_path"] = saved_audio_path | |
| else: | |
| # Keep existing path | |
| saved_audio_path = df.at[idx, "audio_file_path"] or None | |
| # Persist metadata CSV and state | |
| df.to_csv(META_CSV_PATH, index=False) | |
| save_state(idx) | |
| status = "Saved (text and audio)." if audio_np is not None else "Saved text (no new audio)." | |
| return df, status, saved_audio_path | |
| def goto_index(df: pd.DataFrame, idx: int): | |
| idx = max(0, min(idx, len(df) - 1)) | |
| return get_row_view(df, idx), idx | |
| def export_dataset(df: pd.DataFrame) -> Tuple[str, str]: | |
| # Always refresh metadata before export | |
| df.to_csv(META_CSV_PATH, index=False) | |
| # Create ZIP containing recordings/ + metadata.csv | |
| if os.path.exists(EXPORT_ZIP_PATH): | |
| os.remove(EXPORT_ZIP_PATH) | |
| # Make an archive in a temp folder structure | |
| tmp_root = "_export_tmp" | |
| if os.path.exists(tmp_root): | |
| shutil.rmtree(tmp_root) | |
| os.makedirs(tmp_root, exist_ok=True) | |
| # Copy metadata | |
| shutil.copy2(META_CSV_PATH, os.path.join(tmp_root, os.path.basename(META_CSV_PATH))) | |
| # Copy audio dir | |
| out_audio = os.path.join(tmp_root, os.path.basename(AUDIO_DIR)) | |
| shutil.copytree(AUDIO_DIR, out_audio, dirs_exist_ok=True) | |
| shutil.make_archive("dataset_export", "zip", tmp_root) | |
| shutil.rmtree(tmp_root) | |
| return META_CSV_PATH, EXPORT_ZIP_PATH | |
| # ----------------------------- | |
| # UI | |
| # ----------------------------- | |
| with gr.Blocks(title="English→Pidgin Audio Collector", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown(""" | |
| # English → Pidgin Audio Collector | |
| Upload your CSV of English sentences, or use the default provided below. | |
| """) | |
| state_df = gr.State(pd.DataFrame()) | |
| state_idx = gr.State(0) | |
| with gr.Row(): | |
| csv_in = gr.File(label="Upload CSV", file_types=[".csv"], file_count="single") | |
| load_btn = gr.Button("Load CSV", variant="primary") | |
| default_btn = gr.Button("Use Default CSV", variant="secondary") # NEW BUTTON | |
| with gr.Row(): | |
| row_picker = gr.Dropdown(label="Jump to Row ID", choices=[], interactive=True, allow_custom_value=True) | |
| with gr.Row(): | |
| row_id_out = gr.Number(label="Row ID", interactive=False) | |
| progress_out = gr.Markdown("No data loaded") | |
| english_box = gr.Textbox(label="English (read-only)", interactive=False, lines=3) | |
| pidgin_box = gr.Textbox(label="Pidgin (editable)", interactive=True, lines=3, placeholder="Type Pidgin translation here…") | |
| def auto_save_text(df, idx, text): | |
| try: | |
| if df.empty: return df | |
| df.at[idx, "pidgin_text"] = text or "" | |
| df.to_csv(META_CSV_PATH, index=False) | |
| return df | |
| except Exception: | |
| return df | |
| pidgin_box.change(auto_save_text, [state_df, state_idx, pidgin_box], [state_df]) | |
| gr.Markdown("### Record / Review") | |
| with gr.Row(): | |
| mic = gr.Audio(sources=["microphone"], type="numpy", label="Record Pidgin (mic)") | |
| saved_audio_player = gr.Audio(label="Current Saved Audio", interactive=False) | |
| with gr.Row(): | |
| save_btn = gr.Button("💾 Save / Overwrite", variant="primary") | |
| rerecord_btn = gr.Button("↺ Clear Recording (Re-record)") | |
| with gr.Row(): | |
| prev_btn = gr.Button("◀ Previous") | |
| next_btn = gr.Button("Next ▶") | |
| skip_btn = gr.Button("⏭ Skip to Incomplete", variant="secondary") | |
| status_out = gr.Markdown(visible=True) | |
| gr.Markdown("### Review Table (live)") | |
| table = gr.Dataframe( | |
| headers=["row_id", "english_text", "pidgin_text", "audio_file_path"], | |
| interactive=False, | |
| column_widths=["10%", "35%", "35%", "20%"] | |
| ) | |
| with gr.Row(): | |
| export_meta_btn = gr.Button("Export Metadata CSV") | |
| export_zip_btn = gr.Button("Export ZIP (Audio + Metadata)") | |
| meta_file = gr.File(label="Download metadata.csv", interactive=False) | |
| zip_file = gr.File(label="Download dataset_export.zip", interactive=False) | |
| # ----------------------------- | |
| # Callbacks | |
| # ----------------------------- | |
| def on_load(file): | |
| try: | |
| if file is None: | |
| raise gr.Error("Please upload a CSV file.") | |
| df, idx, options = load_csv(file.name) | |
| (row_id, eng, pid, audio), idx = goto_index(df, idx) | |
| table_df = df[["row_id", "english_text", "pidgin_text", "audio_file_path"]].copy() | |
| table_df["english_text"] = table_df["english_text"].str[:50] + "..." | |
| table_df["pidgin_text"] = table_df["pidgin_text"].str[:50] + "..." | |
| progress = get_progress(df) | |
| return ( | |
| df, idx, options, gr.update(value=row_id, interactive=False), eng, pid, | |
| audio, table_df, gr.update(choices=options, value=row_id), "Loaded CSV.", progress | |
| ) | |
| except Exception as e: | |
| return pd.DataFrame(), 0, [], None, "", "", None, pd.DataFrame(), gr.update(choices=[]), f"Error: {str(e)}", "No data loaded" | |
| load_btn.click( | |
| fn=on_load, | |
| inputs=[csv_in], | |
| outputs=[state_df, state_idx, row_picker, row_id_out, english_box, pidgin_box, saved_audio_player, table, row_picker, status_out, progress_out] | |
| ) | |
| # NEW: Load default CSV callback | |
| def on_load_default(): | |
| try: | |
| if not os.path.exists(DEFAULT_CSV_PATH): | |
| return pd.DataFrame(), 0, [], None, "", "", None, pd.DataFrame(), gr.update(choices=[]), f"Default CSV not found at {DEFAULT_CSV_PATH}", "No data loaded" | |
| df, idx, options = load_csv(DEFAULT_CSV_PATH) | |
| (row_id, eng, pid, audio), idx = goto_index(df, idx) | |
| table_df = df[["row_id", "english_text", "pidgin_text", "audio_file_path"]].copy() | |
| table_df["english_text"] = table_df["english_text"].str[:50] + "..." | |
| table_df["pidgin_text"] = table_df["pidgin_text"].str[:50] + "..." | |
| progress = get_progress(df) | |
| return ( | |
| df, idx, gr.update(choices=options, value=row_id), gr.update(value=row_id, interactive=False), eng, pid, | |
| audio, table_df, gr.update(choices=options, value=row_id), "Loaded default CSV.", progress | |
| ) | |
| except Exception as e: | |
| return pd.DataFrame(), 0, gr.update(choices=[]), None, "", "", None, pd.DataFrame(), gr.update(choices=[]), f"Error: {str(e)}", "No data loaded" | |
| default_btn.click( | |
| fn=on_load_default, | |
| inputs=None, | |
| outputs=[state_df, state_idx, row_picker, row_id_out, english_box, pidgin_box, saved_audio_player, table, row_picker, status_out, progress_out] | |
| ) | |
| def on_pick(df, current_idx, pick_id): | |
| try: | |
| if df is None or len(df) == 0: | |
| return current_idx, None, "", "", None | |
| idx = int(df.index[df["row_id"] == int(pick_id)][0]) | |
| (row_id, eng, pid, audio), idx = goto_index(df, idx) | |
| return idx, row_id, eng, pid, audio | |
| except Exception: | |
| return current_idx, None, "", "", None | |
| row_picker.change( | |
| fn=on_pick, | |
| inputs=[state_df, state_idx, row_picker], | |
| outputs=[state_idx, row_id_out, english_box, pidgin_box, saved_audio_player] | |
| ) | |
| def nav(df, idx, step): | |
| try: | |
| if df is None or len(df) == 0: | |
| return idx, None, "", "", None, gr.update(), "No data loaded" | |
| idx = max(0, min(idx + step, len(df) - 1)) | |
| (row_id, eng, pid, audio), idx = goto_index(df, idx) | |
| progress = get_progress(df) | |
| return idx, row_id, eng, pid, audio, gr.update(value=row_id), progress | |
| except Exception: | |
| return idx, None, "", "", None, gr.update(), "Error navigating" | |
| def skip_to_incomplete(df, idx): | |
| try: | |
| if df is None or len(df) == 0: | |
| return idx, None, "", "", None, gr.update(), "No data loaded" | |
| idx = find_next_incomplete(df, idx) | |
| (row_id, eng, pid, audio), idx = goto_index(df, idx) | |
| progress = get_progress(df) | |
| return idx, row_id, eng, pid, audio, gr.update(value=row_id), progress | |
| except Exception: | |
| return idx, None, "", "", None, gr.update(), "Error finding incomplete" | |
| prev_btn.click(lambda df, idx: nav(df, idx, -1), [state_df, state_idx], [state_idx, row_id_out, english_box, pidgin_box, saved_audio_player, row_picker, progress_out]) | |
| next_btn.click(lambda df, idx: nav(df, idx, 1), [state_df, state_idx], [state_idx, row_id_out, english_box, pidgin_box, saved_audio_player, row_picker, progress_out]) | |
| skip_btn.click(skip_to_incomplete, [state_df, state_idx], [state_idx, row_id_out, english_box, pidgin_box, saved_audio_player, row_picker, progress_out]) | |
| def on_save(df, idx, pidgin_text, audio_np): | |
| try: | |
| if df is None or len(df) == 0: | |
| return df, None, pd.DataFrame(), "No data to save", None, "No data loaded", "" | |
| df, status, saved_path = save_current(df, idx, pidgin_text, audio_np) | |
| table_df = df[["row_id", "english_text", "pidgin_text", "audio_file_path"]].copy() | |
| table_df["english_text"] = table_df["english_text"].str[:50] + "..." | |
| table_df["pidgin_text"] = table_df["pidgin_text"].str[:50] + "..." | |
| (row_id, eng, pid, audio), idx = goto_index(df, idx) | |
| progress = get_progress(df) | |
| return df, saved_path if saved_path else None, table_df, status, None, progress, pid | |
| except Exception as e: | |
| return df, None, pd.DataFrame(), f"Save error: {str(e)}", None, "Error", "" | |
| save_btn.click( | |
| fn=on_save, | |
| inputs=[state_df, state_idx, pidgin_box, mic], | |
| outputs=[state_df, saved_audio_player, table, status_out, mic, progress_out, pidgin_box] | |
| ) | |
| def on_rerecord(): | |
| return gr.update(value=None), "Recording cleared. You can re-record now." | |
| rerecord_btn.click(on_rerecord, outputs=[mic, status_out]) | |
| def do_export_meta(df): | |
| if df is None or len(df) == 0: | |
| raise gr.Error("Load a CSV first.") | |
| meta_path, _ = export_dataset(df) | |
| return gr.update(value=meta_path) | |
| export_meta_btn.click(do_export_meta, inputs=[state_df], outputs=[meta_file]) | |
| def do_export_zip(df): | |
| if df is None or len(df) == 0: | |
| raise gr.Error("Load a CSV first.") | |
| _, zip_path = export_dataset(df) | |
| return gr.update(value=zip_path) | |
| export_zip_btn.click(do_export_zip, inputs=[state_df], outputs=[zip_file]) | |
| # Auto-load default CSV on startup | |
| def auto_load(): | |
| try: | |
| if os.path.exists(DEFAULT_CSV_PATH): | |
| df, idx, options = load_csv(DEFAULT_CSV_PATH) | |
| (row_id, eng, pid, audio), idx = goto_index(df, idx) | |
| table_df = df[["row_id", "english_text", "pidgin_text", "audio_file_path"]].copy() | |
| table_df["english_text"] = table_df["english_text"].str[:50] + "..." | |
| table_df["pidgin_text"] = table_df["pidgin_text"].str[:50] + "..." | |
| progress = get_progress(df) | |
| return ( | |
| df, idx, gr.update(choices=options, value=row_id), row_id, eng, pid, | |
| audio, table_df, "Auto-loaded default CSV.", progress | |
| ) | |
| except Exception: | |
| pass | |
| return pd.DataFrame(), 0, gr.update(choices=[]), None, "", "", None, pd.DataFrame(), "No default CSV found.", "No data loaded" | |
| demo.load( | |
| fn=auto_load, | |
| outputs=[state_df, state_idx, row_picker, row_id_out, english_box, pidgin_box, saved_audio_player, table, status_out, progress_out] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(share=True) | |