Update app.py
Browse files
app.py
CHANGED
@@ -2,41 +2,61 @@ import gradio as gr
|
|
2 |
import soundfile as sf
|
3 |
import datetime
|
4 |
import numpy as np
|
|
|
5 |
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
#
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
-
#
|
16 |
-
|
|
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
|
|
|
|
|
21 |
|
22 |
-
#
|
23 |
def create_interface():
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
if __name__ == "__main__":
|
41 |
interface = create_interface()
|
42 |
interface.launch()
|
|
|
2 |
import soundfile as sf
|
3 |
import datetime
|
4 |
import numpy as np
|
5 |
+
import hashlib
|
6 |
|
7 |
+
# Function to save audio with label and speaker name
|
8 |
+
def save_audio(audio, dropdown_label, custom_label, speaker_name):
|
9 |
+
# Determine the final label
|
10 |
+
label = custom_label if dropdown_label == "Custom" else dropdown_label
|
11 |
|
12 |
+
if not label:
|
13 |
+
raise gr.Error("Label cannot be empty 💥!", duration=5)
|
14 |
+
if not speaker_name:
|
15 |
+
raise gr.Error("User name cannot be empty 💥!", duration=5)
|
16 |
|
17 |
+
# Generate speaker_id using a hash function to ensure consistency
|
18 |
+
speaker_id = hashlib.sha256(speaker_name.encode()).hexdigest()[:8]
|
19 |
+
|
20 |
+
# Generate file name
|
21 |
+
filename = f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{speaker_id}_{label}.wav"
|
22 |
|
23 |
+
# Get sample rate and audio data from Gradio
|
24 |
+
sample_rate = audio[0]
|
25 |
+
audio_data = np.array(audio[1])
|
26 |
|
27 |
+
# Save the audio file in wav format
|
28 |
+
sf.write(filename, audio_data, sample_rate)
|
29 |
|
30 |
+
# Return only the download link
|
31 |
+
return gr.File(filename)
|
32 |
|
33 |
+
# Interface design using gr.Blocks
|
34 |
def create_interface():
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
labels = ["Label1", "Label2", "Label3", "Custom"] # Add a "Custom" option for custom label
|
37 |
+
label_dropdown = gr.Dropdown(choices=labels, label="Select Label")
|
38 |
+
custom_label = gr.Textbox(label="Enter Custom Label", visible=False) # Initially hidden
|
39 |
+
|
40 |
+
# Function to show/hide custom label textbox based on dropdown selection
|
41 |
+
def toggle_custom_label(selected_label):
|
42 |
+
return gr.update(visible=True) if selected_label == "Custom" else gr.update(visible=False)
|
43 |
+
|
44 |
+
label_dropdown.change(toggle_custom_label, inputs=label_dropdown, outputs=custom_label)
|
45 |
+
|
46 |
+
speaker_name = gr.Textbox(label="Enter User Name")
|
47 |
+
audio = gr.Audio(sources="microphone", type="numpy", label="Record Audio")
|
48 |
+
|
49 |
+
submit_button = gr.Button("Submit")
|
50 |
+
|
51 |
+
submit_button.click(
|
52 |
+
fn=save_audio,
|
53 |
+
inputs=[audio, label_dropdown, custom_label, speaker_name],
|
54 |
+
outputs=[gr.File()],
|
55 |
+
)
|
56 |
+
|
57 |
+
return demo
|
58 |
+
|
59 |
+
# Launch the interface
|
60 |
if __name__ == "__main__":
|
61 |
interface = create_interface()
|
62 |
interface.launch()
|