MeysamSh commited on
Commit
0a7b185
·
1 Parent(s): 996d3a6

Add file examples

Browse files
Files changed (1) hide show
  1. app.py +52 -33
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
- # Function to save annotation
17
- def save_annotation(user_id, gender, score):
18
- global current_index
19
- if current_index >= len(audio_files):
20
- return "No more audio files.", None
21
-
22
- audio_file = audio_files[current_index]
23
-
24
- # Save to CSV
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- current_index += 1
35
- if current_index < len(audio_files):
36
- next_audio = os.path.join(AUDIO_FOLDER, audio_files[current_index])
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(label="Gender", choices=["Male", "Female", "Other"])
47
- audio_player = gr.Audio(label="Audio File", source="upload", type="filepath")
48
- score = gr.Slider(label="MOS Score (1-5)", minimum=1, maximum=5, step=1)
 
 
49
  submit_btn = gr.Button("Submit Score")
50
- status = gr.Textbox(label="Status")
51
-
 
 
 
 
52
  submit_btn.click(
53
- fn=save_annotation,
54
- inputs=[user_id, gender, score],
55
- outputs=[status, audio_player]
56
  )
57
 
58
- # Load first audio
59
- audio_player.value = os.path.join(AUDIO_FOLDER, audio_files[current_index])
 
 
 
 
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()