BHW commited on
Commit
061a546
·
verified ·
1 Parent(s): 0ae09c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -5
app.py CHANGED
@@ -6,6 +6,24 @@ import hashlib
6
 
7
  # Initialize a list to store file paths
8
  uploaded_files = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Function to save audio with label and speaker name
11
  def save_audio(audio, dropdown_label, custom_label, speaker_name):
@@ -23,9 +41,12 @@ def save_audio(audio, dropdown_label, custom_label, speaker_name):
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
@@ -40,8 +61,11 @@ def save_audio(audio, dropdown_label, custom_label, speaker_name):
40
  # Add the new file path to the list of uploaded files
41
  uploaded_files.append(filename)
42
 
43
- # Return the list of all file paths for download
44
- return uploaded_files
 
 
 
45
 
46
  # Interface design using gr.Blocks
47
  def create_interface():
@@ -57,19 +81,25 @@ def create_interface():
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(sources="microphone", type="numpy", label="Record Audio")
61
 
62
  submit_button = gr.Button("Submit")
63
 
64
  # Create a list to display all uploaded files
65
  file_list = gr.Files(label="Download your recordings")
66
 
 
67
  submit_button.click(
68
  fn=save_audio,
69
  inputs=[audio, label_dropdown, custom_label, speaker_name],
70
- outputs=file_list,
71
  )
72
 
 
 
 
 
 
73
  return demo
74
 
75
  # Launch the interface
 
6
 
7
  # Initialize a list to store file paths
8
  uploaded_files = []
9
+ help_en = """
10
+ ### Usage Instructions:
11
+
12
+ 1. **Select a Label**: Choose a label from the dropdown menu, or select "Custom" to enter your own label.
13
+ 2. **Enter User Name**: Input your user name, which will be used to generate a unique ID for your recordings.
14
+ 3. **Record Audio**: Click the "Record Audio" button to start recording. The maximum length for each recording is 1 second.
15
+ 4. **Submit Recording**: After recording, click the "Submit" button to save the recording.
16
+ 5. **Download Recordings**: Use the provided download links to retrieve all recorded files.
17
+ """
18
+ help_zh_tw = """
19
+ ### 使用流程:
20
+
21
+ 1. **選擇標記**: 從下拉選單中選擇一個標記,或選擇“自定義”以輸入自己的標記。
22
+ 2. **輸入用戶名稱**: 輸入您的用戶名稱,用於生成錄音唯一ID。
23
+ 3. **錄製音頻**: 點擊“錄製音頻”按鈕開始錄音。每次錄音的最長時間為1秒。
24
+ 4. **提交錄音**: 錄製完成後,點擊“提交”按鈕保存錄音。
25
+ 5. **下載錄音**: 使用提供的下載鏈接下載所有已錄製的文件。
26
+ """
27
 
28
  # Function to save audio with label and speaker name
29
  def save_audio(audio, dropdown_label, custom_label, speaker_name):
 
41
  sample_rate = audio[0]
42
  audio_data = np.array(audio[1])
43
 
44
+ # Calculate the audio length in seconds
45
+ audio_length = len(audio_data) / sample_rate
46
+
47
  # Check if the audio length exceeds 1 second
48
  max_length = 1 # in seconds
49
+ if audio_length > max_length:
50
  raise gr.Error("Recording is longer than 1 second 💥!", duration=5)
51
 
52
  # Generate speaker_id using a hash function to ensure consistency
 
61
  # Add the new file path to the list of uploaded files
62
  uploaded_files.append(filename)
63
 
64
+ # Create an info message
65
+ info_message = f"Recorded audio length: {audio_length:.2f} seconds."
66
+
67
+ # Return the list of all file paths for download and the info message
68
+ return uploaded_files, gr.Info(info_message, duration=5)
69
 
70
  # Interface design using gr.Blocks
71
  def create_interface():
 
81
  label_dropdown.change(toggle_custom_label, inputs=label_dropdown, outputs=custom_label)
82
 
83
  speaker_name = gr.Textbox(label="Enter User Name")
84
+ audio = gr.Audio(source="microphone", type="numpy", label="Record Audio")
85
 
86
  submit_button = gr.Button("Submit")
87
 
88
  # Create a list to display all uploaded files
89
  file_list = gr.Files(label="Download your recordings")
90
 
91
+ # Add the info output to display recording length
92
  submit_button.click(
93
  fn=save_audio,
94
  inputs=[audio, label_dropdown, custom_label, speaker_name],
95
+ outputs=[file_list, gr.Info()],
96
  )
97
 
98
+ # Add a Markdown component at the bottom to display usage instructions
99
+ gr.Markdown(help_en)
100
+ gr.Markdown(help_zh_tw)
101
+
102
+
103
  return demo
104
 
105
  # Launch the interface