BHW commited on
Commit
3a37e60
·
verified ·
1 Parent(s): 8ed425b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -27
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
- def save_audio(audio, label, sample_rate, bit_depth):
9
- # 產生檔案名稱
10
- filename = f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{label}_{sample_rate}Hz_{bit_depth}bit.wav"
11
 
12
- # 將音頻數據轉換成 numpy 陣列
13
- audio_data = np.array(audio[1])
 
 
 
14
 
15
- # 將錄音儲存為 wav 檔案
16
- sf.write(filename, audio_data, int(sample_rate), subtype=f'PCM_{bit_depth}')
 
17
 
18
- # 返回檔案名作為下載連結
19
- return filename, gr.File(filename)
20
 
 
 
21
 
22
- # 介面設計
23
  def create_interface():
24
- sample_rate = gr.Dropdown(["44100", "48000"], label="選擇採樣率", value="44100")
25
- bit_depth = gr.Dropdown(["16", "24"], label="選擇位深度", value="16")
26
- label = gr.Textbox(label="輸入標記")
27
- audio = gr.Audio(sources="microphone", type="numpy", label="錄音")
28
-
29
- interface = gr.Interface(
30
- fn=save_audio,
31
- inputs=[audio, label, sample_rate, bit_depth],
32
- outputs=["text", "file"],
33
- title="錄音程式",
34
- description="錄音最大長度為 1 秒,儲存為 WAV 格式。",
35
- )
36
- return interface
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()