MeysamSh commited on
Commit
668743e
·
1 Parent(s): 76b45f4

First working version

Browse files
Files changed (2) hide show
  1. annotations.csv +0 -11
  2. app.py +53 -38
annotations.csv CHANGED
@@ -1,15 +1,4 @@
1
  user_id,gender,audio_file,score
2
- ,[],audio1.wav,3.0
3
- ,[],audio2.wav,3.0
4
  dkl.,Female,audio1.wav,3.0
5
  dkl.,Female,audio2.wav,3.0
6
- ,Other,audio1.wav,1.0
7
- jhgs,Other,audio1.wav,3.0
8
- ermjb,Other,audio1.wav,
9
- ermjb,Other,audio2.wav,1.0
10
- gvsdg,Other,audio1.wav,
11
- gvsdg,Other,audio2.wav,
12
- dsfsd,Other,audio1.wav,
13
- dsfsd,Other,audio2.wav,1.0
14
- fdsfsd,Other,audio1.wav,3.0
15
  dfsd,Other,audio1.wav,2
 
1
  user_id,gender,audio_file,score
 
 
2
  dkl.,Female,audio1.wav,3.0
3
  dkl.,Female,audio2.wav,3.0
 
 
 
 
 
 
 
 
 
4
  dfsd,Other,audio1.wav,2
app.py CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
2
  import pandas as pd
3
  import os
4
  import hashlib
 
 
5
 
6
  # ============================
7
  # CONFIGURATION
@@ -10,22 +12,44 @@ import hashlib
10
  AUDIO_FOLDER = "audio_files"
11
  audio_files = sorted(os.listdir(AUDIO_FOLDER))
12
 
13
- ANNOTATION_FILE = "annotations.csv"
14
-
15
  ADMIN_PASSWORD_HASH = "eb32da077dfaf326cd6f73e0716b628da6427aa318a2d0b9fafa9ef315b5e885"
16
 
17
  # ============================
18
- # STORAGE INITIALIZATION
19
  # ============================
20
 
21
- if not os.path.exists(ANNOTATION_FILE):
22
- df = pd.DataFrame(columns=["user_id", "gender", "audio_file", "score"])
23
- df.to_csv(ANNOTATION_FILE, index=False)
24
 
25
  # ============================
26
  # USER FUNCTIONS
27
  # ============================
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  def load_next_audio(state):
30
  if state is None:
31
  state = {"index": 0}
@@ -33,13 +57,11 @@ def load_next_audio(state):
33
  idx = state["index"]
34
 
35
  if idx >= len(audio_files):
36
- return None, state, False, "All audio files annotated."
37
 
38
  filepath = os.path.join(AUDIO_FOLDER, audio_files[idx])
39
- # return filepath, state, False, f"Loaded {audio_files[idx]}"
40
  return filepath, state, {"played": 0}, f"Loaded {audio_files[idx]}"
41
 
42
-
43
  def submit_annotation(user_id, gender, score, state):
44
  if state is None:
45
  state = {"index": 0}
@@ -51,14 +73,7 @@ def submit_annotation(user_id, gender, score, state):
51
 
52
  audio_file = audio_files[idx]
53
 
54
- df = pd.read_csv(ANNOTATION_FILE)
55
- df = pd.concat([df, pd.DataFrame([{
56
- "user_id": user_id,
57
- "gender": gender,
58
- "audio_file": audio_file,
59
- "score": score
60
- }])], ignore_index=True)
61
- df.to_csv(ANNOTATION_FILE, index=False)
62
 
63
  state["index"] += 1
64
  return state, f"Saved rating for {audio_file}."
@@ -67,8 +82,8 @@ def submit_annotation(user_id, gender, score, state):
67
  # SUBMIT BUTTON CONTROL
68
  # ============================
69
 
70
- def check_submit_ready(user_id, audio_played,score):
71
- ready = len(user_id.strip())>1 and audio_played['played'] == 1 and score != "None"
72
  return gr.update(interactive=ready)
73
 
74
  def mark_audio_played():
@@ -83,11 +98,15 @@ def hash_password(password: str) -> str:
83
 
84
  def admin_login(input_password):
85
  if hash_password(input_password) == ADMIN_PASSWORD_HASH:
86
- df = pd.read_csv(ANNOTATION_FILE)
 
 
 
 
87
  return (
88
  gr.update(visible=True),
89
  df,
90
- ANNOTATION_FILE,
91
  gr.update(value="Admin authentication successful.")
92
  )
93
  else:
@@ -117,8 +136,7 @@ with gr.Blocks() as demo:
117
  user_id = gr.Textbox(label="User ID")
118
  gender = gr.Dropdown(["Male", "Female", "Other"], label="Gender", value="Other")
119
 
120
- audio_player = gr.Audio(
121
- label="Audio File")
122
 
123
  with gr.Row():
124
  score = gr.Dropdown(choices=["None","1", "2", "3", "4", "5"], value="None", label="MOS Score (1–5)")
@@ -143,19 +161,17 @@ with gr.Blocks() as demo:
143
  # Enable submit button only when conditions are met
144
  user_id.change(
145
  check_submit_ready,
146
- inputs=[user_id, audio_played,score],
147
  outputs=submit_btn
148
  )
149
-
150
  audio_played.change(
151
  check_submit_ready,
152
- inputs=[user_id, audio_played,score],
153
  outputs=submit_btn
154
  )
155
-
156
  score.change(
157
  check_submit_ready,
158
- inputs=[user_id, audio_played,score],
159
  outputs=submit_btn
160
  )
161
 
@@ -180,18 +196,15 @@ with gr.Blocks() as demo:
180
  gr.Markdown("## Admin Dashboard (Restricted Access)")
181
 
182
  with gr.Row():
183
- admin_password = gr.Textbox(
184
- label="Admin Password",
185
- type="password"
186
- )
187
  admin_login_btn = gr.Button("Login")
188
 
189
- login_status = gr.Textbox(label="Login Status:",interactive=False)
190
 
191
  with gr.Column(visible=False) as admin_panel:
192
  gr.Markdown("### Annotation Results")
193
- results_table = gr.DataFrame(interactive=False,value=pd.read_csv(ANNOTATION_FILE))
194
- download_admin = gr.File(label="Download annotations.csv",value=ANNOTATION_FILE)
195
 
196
  admin_login_btn.click(
197
  admin_login,
@@ -199,7 +212,9 @@ with gr.Blocks() as demo:
199
  outputs=[admin_panel, results_table, download_admin, login_status]
200
  )
201
 
 
 
 
 
202
  if __name__ == "__main__":
203
- demo.launch(share=True)
204
- # demo.launch(server_name="0.0.0.0", server_port=7860)
205
- # demo.launch(server_name="0.0.0.0", server_port=7860, prevent_thread_lock=True)
 
2
  import pandas as pd
3
  import os
4
  import hashlib
5
+ from datasets import Dataset, concatenate_datasets, load_dataset
6
+ from huggingface_hub import login
7
 
8
  # ============================
9
  # CONFIGURATION
 
12
  AUDIO_FOLDER = "audio_files"
13
  audio_files = sorted(os.listdir(AUDIO_FOLDER))
14
 
 
 
15
  ADMIN_PASSWORD_HASH = "eb32da077dfaf326cd6f73e0716b628da6427aa318a2d0b9fafa9ef315b5e885"
16
 
17
  # ============================
18
+ # HF DATASET CONFIGURATION
19
  # ============================
20
 
21
+ HF_DATASET_NAME = "MeysamSh/Annotate_Samples" # replace with your HF dataset
22
+ HF_TOKEN = os.environ.get("HF_TOKEN") # store your token as a secret in Spaces
23
+ login(HF_TOKEN)
24
 
25
  # ============================
26
  # USER FUNCTIONS
27
  # ============================
28
 
29
+ def load_hf_dataset():
30
+ """Load existing HF Dataset or create empty one if not exists."""
31
+ try:
32
+ ds = load_dataset(HF_DATASET_NAME, split="train")
33
+ except:
34
+ # Dataset does not exist yet
35
+ df = pd.DataFrame(columns=["user_id", "gender", "audio_file", "score"])
36
+ ds = Dataset.from_pandas(df)
37
+ ds.push_to_hub(HF_DATASET_NAME, private=True)
38
+ return ds
39
+
40
+ def submit_annotation_hf(user_id, gender, score, audio_file):
41
+ """Append annotation to HF Dataset."""
42
+ ds = load_hf_dataset()
43
+ new_row = pd.DataFrame([{
44
+ "user_id": user_id,
45
+ "gender": gender,
46
+ "audio_file": audio_file,
47
+ "score": score
48
+ }])
49
+ ds_new = Dataset.from_pandas(new_row)
50
+ ds_combined = concatenate_datasets([ds, ds_new])
51
+ ds_combined.push_to_hub(HF_DATASET_NAME)
52
+
53
  def load_next_audio(state):
54
  if state is None:
55
  state = {"index": 0}
 
57
  idx = state["index"]
58
 
59
  if idx >= len(audio_files):
60
+ return None, state, {"played": 0}, "All audio files annotated."
61
 
62
  filepath = os.path.join(AUDIO_FOLDER, audio_files[idx])
 
63
  return filepath, state, {"played": 0}, f"Loaded {audio_files[idx]}"
64
 
 
65
  def submit_annotation(user_id, gender, score, state):
66
  if state is None:
67
  state = {"index": 0}
 
73
 
74
  audio_file = audio_files[idx]
75
 
76
+ submit_annotation_hf(user_id, gender, score, audio_file)
 
 
 
 
 
 
 
77
 
78
  state["index"] += 1
79
  return state, f"Saved rating for {audio_file}."
 
82
  # SUBMIT BUTTON CONTROL
83
  # ============================
84
 
85
+ def check_submit_ready(user_id, audio_played, score):
86
+ ready = len(user_id.strip()) > 1 and audio_played['played'] == 1 and score != "None"
87
  return gr.update(interactive=ready)
88
 
89
  def mark_audio_played():
 
98
 
99
  def admin_login(input_password):
100
  if hash_password(input_password) == ADMIN_PASSWORD_HASH:
101
+ ds = load_hf_dataset()
102
+ df = ds.to_pandas()
103
+ # For download: write temp CSV
104
+ temp_csv = "annotations_export.csv"
105
+ df.to_csv(temp_csv, index=False)
106
  return (
107
  gr.update(visible=True),
108
  df,
109
+ temp_csv,
110
  gr.update(value="Admin authentication successful.")
111
  )
112
  else:
 
136
  user_id = gr.Textbox(label="User ID")
137
  gender = gr.Dropdown(["Male", "Female", "Other"], label="Gender", value="Other")
138
 
139
+ audio_player = gr.Audio(label="Audio File")
 
140
 
141
  with gr.Row():
142
  score = gr.Dropdown(choices=["None","1", "2", "3", "4", "5"], value="None", label="MOS Score (1–5)")
 
161
  # Enable submit button only when conditions are met
162
  user_id.change(
163
  check_submit_ready,
164
+ inputs=[user_id, audio_played, score],
165
  outputs=submit_btn
166
  )
 
167
  audio_played.change(
168
  check_submit_ready,
169
+ inputs=[user_id, audio_played, score],
170
  outputs=submit_btn
171
  )
 
172
  score.change(
173
  check_submit_ready,
174
+ inputs=[user_id, audio_played, score],
175
  outputs=submit_btn
176
  )
177
 
 
196
  gr.Markdown("## Admin Dashboard (Restricted Access)")
197
 
198
  with gr.Row():
199
+ admin_password = gr.Textbox(label="Admin Password", type="password")
 
 
 
200
  admin_login_btn = gr.Button("Login")
201
 
202
+ login_status = gr.Textbox(label="Login Status:", interactive=False)
203
 
204
  with gr.Column(visible=False) as admin_panel:
205
  gr.Markdown("### Annotation Results")
206
+ results_table = gr.DataFrame(interactive=False, value=pd.DataFrame(columns=["user_id","gender","audio_file","score"]))
207
+ download_admin = gr.File(label="Download annotations.csv", value=None)
208
 
209
  admin_login_btn.click(
210
  admin_login,
 
212
  outputs=[admin_panel, results_table, download_admin, login_status]
213
  )
214
 
215
+ # ============================
216
+ # APP LAUNCH
217
+ # ============================
218
+
219
  if __name__ == "__main__":
220
+ demo.launch()