Spaces:
Sleeping
Sleeping
Add file examples
Browse files
app.py
CHANGED
|
@@ -2,26 +2,38 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Folder with your audio files
|
| 6 |
AUDIO_FOLDER = "audio_files"
|
| 7 |
audio_files = sorted(os.listdir(AUDIO_FOLDER))
|
| 8 |
-
current_index = 0
|
| 9 |
|
| 10 |
-
# CSV to store annotations
|
| 11 |
ANNOTATION_FILE = "annotations.csv"
|
| 12 |
if not os.path.exists(ANNOTATION_FILE):
|
| 13 |
df = pd.DataFrame(columns=["user_id", "gender", "audio_file", "score"])
|
| 14 |
df.to_csv(ANNOTATION_FILE, index=False)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
df = pd.read_csv(ANNOTATION_FILE)
|
| 26 |
df = pd.concat([df, pd.DataFrame([{
|
| 27 |
"user_id": user_id,
|
|
@@ -30,33 +42,40 @@ def save_annotation(user_id, gender, score):
|
|
| 30 |
"score": score
|
| 31 |
}])], ignore_index=True)
|
| 32 |
df.to_csv(ANNOTATION_FILE, index=False)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
else:
|
| 38 |
-
next_audio = None
|
| 39 |
-
|
| 40 |
-
return "Saved! Next audio loaded." if next_audio else "All audios completed.", next_audio
|
| 41 |
-
|
| 42 |
-
# Gradio interface
|
| 43 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
with gr.Row():
|
| 45 |
user_id = gr.Textbox(label="User ID")
|
| 46 |
-
gender = gr.Dropdown(
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
submit_btn = gr.Button("Submit Score")
|
| 50 |
-
status = gr.Textbox(label="Status")
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
submit_btn.click(
|
| 53 |
-
|
| 54 |
-
inputs=[user_id, gender, score],
|
| 55 |
-
outputs=[
|
| 56 |
)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
demo.launch()
|
| 62 |
-
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
AUDIO_FOLDER = "audio_files"
|
| 6 |
audio_files = sorted(os.listdir(AUDIO_FOLDER))
|
|
|
|
| 7 |
|
|
|
|
| 8 |
ANNOTATION_FILE = "annotations.csv"
|
| 9 |
if not os.path.exists(ANNOTATION_FILE):
|
| 10 |
df = pd.DataFrame(columns=["user_id", "gender", "audio_file", "score"])
|
| 11 |
df.to_csv(ANNOTATION_FILE, index=False)
|
| 12 |
|
| 13 |
+
# Main function (per-user session state)
|
| 14 |
+
def load_next_audio(state):
|
| 15 |
+
if state is None:
|
| 16 |
+
state = {"index": 0}
|
| 17 |
+
|
| 18 |
+
idx = state["index"]
|
| 19 |
+
|
| 20 |
+
if idx >= len(audio_files):
|
| 21 |
+
return None, state, "All audio files annotated."
|
| 22 |
+
|
| 23 |
+
filepath = os.path.join(AUDIO_FOLDER, audio_files[idx])
|
| 24 |
+
return filepath, state, f"Loaded file {audio_files[idx]}"
|
| 25 |
+
|
| 26 |
+
def submit_annotation(user_id, gender, score, state):
|
| 27 |
+
if state is None:
|
| 28 |
+
state = {"index": 0}
|
| 29 |
+
|
| 30 |
+
idx = state["index"]
|
| 31 |
+
|
| 32 |
+
if idx >= len(audio_files):
|
| 33 |
+
return state, "No more audio files."
|
| 34 |
+
|
| 35 |
+
audio_file = audio_files[idx]
|
| 36 |
+
|
| 37 |
df = pd.read_csv(ANNOTATION_FILE)
|
| 38 |
df = pd.concat([df, pd.DataFrame([{
|
| 39 |
"user_id": user_id,
|
|
|
|
| 42 |
"score": score
|
| 43 |
}])], ignore_index=True)
|
| 44 |
df.to_csv(ANNOTATION_FILE, index=False)
|
| 45 |
+
|
| 46 |
+
state["index"] += 1
|
| 47 |
+
return state, f"Saved rating for {audio_file}."
|
| 48 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# Audio MOS Annotation Tool")
|
| 51 |
+
|
| 52 |
+
state = gr.State({"index": 0})
|
| 53 |
+
|
| 54 |
with gr.Row():
|
| 55 |
user_id = gr.Textbox(label="User ID")
|
| 56 |
+
gender = gr.Dropdown(["Male", "Female", "Other"], label="Gender")
|
| 57 |
+
|
| 58 |
+
audio_player = gr.Audio(label="Audio File", autoplay=False)
|
| 59 |
+
score = gr.Slider(label="MOS Score (1–5)", minimum=1, maximum=5, step=1)
|
| 60 |
+
|
| 61 |
submit_btn = gr.Button("Submit Score")
|
| 62 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 63 |
+
|
| 64 |
+
# Load audio on startup
|
| 65 |
+
demo.load(load_next_audio, inputs=state,
|
| 66 |
+
outputs=[audio_player, state, status])
|
| 67 |
+
|
| 68 |
submit_btn.click(
|
| 69 |
+
submit_annotation,
|
| 70 |
+
inputs=[user_id, gender, score, state],
|
| 71 |
+
outputs=[state, status],
|
| 72 |
)
|
| 73 |
|
| 74 |
+
# After submitting, load next audio
|
| 75 |
+
submit_btn.click(
|
| 76 |
+
load_next_audio,
|
| 77 |
+
inputs=state,
|
| 78 |
+
outputs=[audio_player, state, status],
|
| 79 |
+
)
|
| 80 |
|
| 81 |
demo.launch()
|
|
|