Update app.py
Browse files
app.py
CHANGED
|
@@ -19,16 +19,21 @@ def save_audio(audio, dropdown_label, custom_label, speaker_name):
|
|
| 19 |
if not speaker_name:
|
| 20 |
raise gr.Error("User name cannot be empty 💥!", duration=5)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Generate speaker_id using a hash function to ensure consistency
|
| 23 |
speaker_id = hashlib.sha256(speaker_name.encode()).hexdigest()[:8]
|
| 24 |
|
| 25 |
# Generate file name
|
| 26 |
filename = f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{speaker_id}_{label}.wav"
|
| 27 |
|
| 28 |
-
# Get sample rate and audio data from Gradio
|
| 29 |
-
sample_rate = audio[0]
|
| 30 |
-
audio_data = np.array(audio[1])
|
| 31 |
-
|
| 32 |
# Save the audio file in wav format
|
| 33 |
sf.write(filename, audio_data, sample_rate)
|
| 34 |
|
|
@@ -52,7 +57,7 @@ def create_interface():
|
|
| 52 |
label_dropdown.change(toggle_custom_label, inputs=label_dropdown, outputs=custom_label)
|
| 53 |
|
| 54 |
speaker_name = gr.Textbox(label="Enter User Name")
|
| 55 |
-
audio = gr.Audio(
|
| 56 |
|
| 57 |
submit_button = gr.Button("Submit")
|
| 58 |
|
|
|
|
| 19 |
if not speaker_name:
|
| 20 |
raise gr.Error("User name cannot be empty 💥!", duration=5)
|
| 21 |
|
| 22 |
+
# Get sample rate and audio data from Gradio
|
| 23 |
+
sample_rate = audio[0]
|
| 24 |
+
audio_data = np.array(audio[1])
|
| 25 |
+
|
| 26 |
+
# Check if the audio length exceeds 1 second
|
| 27 |
+
max_length = 1 # in seconds
|
| 28 |
+
if len(audio_data) / sample_rate > max_length:
|
| 29 |
+
raise gr.Error("Recording is longer than 1 second 💥!", duration=5)
|
| 30 |
+
|
| 31 |
# Generate speaker_id using a hash function to ensure consistency
|
| 32 |
speaker_id = hashlib.sha256(speaker_name.encode()).hexdigest()[:8]
|
| 33 |
|
| 34 |
# Generate file name
|
| 35 |
filename = f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{speaker_id}_{label}.wav"
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Save the audio file in wav format
|
| 38 |
sf.write(filename, audio_data, sample_rate)
|
| 39 |
|
|
|
|
| 57 |
label_dropdown.change(toggle_custom_label, inputs=label_dropdown, outputs=custom_label)
|
| 58 |
|
| 59 |
speaker_name = gr.Textbox(label="Enter User Name")
|
| 60 |
+
audio = gr.Audio(source="microphone", type="numpy", label="Record Audio")
|
| 61 |
|
| 62 |
submit_button = gr.Button("Submit")
|
| 63 |
|