MeysamSh commited on
Commit
e045c77
·
1 Parent(s): 0a7b185
Files changed (2) hide show
  1. annotations.csv +3 -0
  2. app.py +29 -7
annotations.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ user_id,gender,audio_file,score
2
+ ,[],audio1.wav,3
3
+ ,[],audio2.wav,3
app.py CHANGED
@@ -2,15 +2,17 @@ import gradio as gr
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}
@@ -21,8 +23,9 @@ def load_next_audio(state):
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}
@@ -34,6 +37,7 @@ def submit_annotation(user_id, gender, score, state):
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,
@@ -43,39 +47,57 @@ def submit_annotation(user_id, gender, score, state):
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()
 
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
 
9
+ # Annotation file stored in persistent space storage
10
  ANNOTATION_FILE = "annotations.csv"
11
  if not os.path.exists(ANNOTATION_FILE):
12
  df = pd.DataFrame(columns=["user_id", "gender", "audio_file", "score"])
13
  df.to_csv(ANNOTATION_FILE, index=False)
14
 
15
+ # Load next audio per user session
16
  def load_next_audio(state):
17
  if state is None:
18
  state = {"index": 0}
 
23
  return None, state, "All audio files annotated."
24
 
25
  filepath = os.path.join(AUDIO_FOLDER, audio_files[idx])
26
+ return filepath, state, f"Loaded {audio_files[idx]}"
27
 
28
+ # Save rating
29
  def submit_annotation(user_id, gender, score, state):
30
  if state is None:
31
  state = {"index": 0}
 
37
 
38
  audio_file = audio_files[idx]
39
 
40
+ # Append to CSV
41
  df = pd.read_csv(ANNOTATION_FILE)
42
  df = pd.concat([df, pd.DataFrame([{
43
  "user_id": user_id,
 
47
  }])], ignore_index=True)
48
  df.to_csv(ANNOTATION_FILE, index=False)
49
 
50
+ # Move to next audio
51
  state["index"] += 1
52
+
53
  return state, f"Saved rating for {audio_file}."
54
 
55
+ # Gradio UI
56
  with gr.Blocks() as demo:
57
  gr.Markdown("# Audio MOS Annotation Tool")
58
 
59
+ # Per-user session state
60
  state = gr.State({"index": 0})
61
 
62
+ # User registration
63
  with gr.Row():
64
  user_id = gr.Textbox(label="User ID")
65
  gender = gr.Dropdown(["Male", "Female", "Other"], label="Gender")
66
 
67
+ # Audio playback + rating
68
  audio_player = gr.Audio(label="Audio File", autoplay=False)
69
+ score = gr.Slider(minimum=1, maximum=5, step=1, label="MOS Score (1–5)")
70
 
71
  submit_btn = gr.Button("Submit Score")
72
  status = gr.Textbox(label="Status", interactive=False)
73
 
74
+ # Download annotations.csv
75
+ download_btn = gr.File(label="Download annotations.csv")
76
+
77
+ # Load first audio automatically
78
+ demo.load(
79
+ load_next_audio,
80
+ inputs=state,
81
+ outputs=[audio_player, state, status]
82
+ )
83
 
84
+ # When submitting a score:
85
+ # 1) Save annotation
86
  submit_btn.click(
87
  submit_annotation,
88
  inputs=[user_id, gender, score, state],
89
  outputs=[state, status],
90
  )
91
 
92
+ # 2) Load next audio
93
  submit_btn.click(
94
  load_next_audio,
95
  inputs=state,
96
  outputs=[audio_player, state, status],
97
  )
98
 
99
+ # Always update the download button with current CSV
100
+ demo.load(lambda: ANNOTATION_FILE, None, download_btn)
101
+ submit_btn.click(lambda: ANNOTATION_FILE, None, download_btn)
102
+
103
  demo.launch()