|
|
import gradio as gr |
|
|
import soundfile as sf |
|
|
import datetime |
|
|
import numpy as np |
|
|
import hashlib |
|
|
|
|
|
|
|
|
MAX_LENGTH = 1.5 |
|
|
|
|
|
|
|
|
uploaded_files = [] |
|
|
help_en = f""" |
|
|
### Usage Instructions: |
|
|
1. **Select a Label**: Choose a label from the dropdown menu, or select "Custom" to enter your own label. |
|
|
2. **Enter User Name**: Input your user name, which will be used to generate a unique ID for your recordings. |
|
|
3. **Record Audio**: Click the "Record Audio" button to start recording. The maximum length for each recording is {MAX_LENGTH} second. |
|
|
4. **Submit Recording**: After recording, click the "Submit" button to save the recording. |
|
|
5. **Download Recordings**: Use the provided download links to retrieve all recorded files. |
|
|
""" |
|
|
help_zh_tw = f""" |
|
|
### 使用流程: |
|
|
1. **選擇標記**: 從下拉選單中選擇一個標記,或選擇“自定義”以輸入自己的標記。 |
|
|
2. **輸入用戶名稱**: 輸入您的用戶名稱,用於生成錄音唯一ID。 |
|
|
3. **錄製音頻**: 點擊“錄製音頻”按鈕開始錄音。每次錄音的最長時間為{MAX_LENGTH}秒。 |
|
|
4. **提交錄音**: 錄製完成後,點擊“提交”按鈕保存錄音。 |
|
|
5. **下載錄音**: 使用提供的下載鏈接下載所有已錄製的文件。 |
|
|
""" |
|
|
|
|
|
|
|
|
def save_audio(audio, dropdown_label, custom_label, speaker_name): |
|
|
global uploaded_files |
|
|
|
|
|
|
|
|
label = custom_label if dropdown_label == "Custom" else dropdown_label |
|
|
|
|
|
if not label: |
|
|
raise gr.Error("Label cannot be empty 💥!", duration=5) |
|
|
if not speaker_name: |
|
|
raise gr.Error("User name cannot be empty 💥!", duration=5) |
|
|
|
|
|
|
|
|
sample_rate = audio[0] |
|
|
audio_data = np.array(audio[1]) |
|
|
|
|
|
|
|
|
audio_length = len(audio_data) / sample_rate |
|
|
|
|
|
|
|
|
if audio_length > MAX_LENGTH: |
|
|
raise gr.Error(f"Recording is longer than {MAX_LENGTH} second 💥!", duration=5) |
|
|
|
|
|
|
|
|
speaker_id = hashlib.sha256(speaker_name.encode()).hexdigest()[:8] |
|
|
|
|
|
|
|
|
filename = f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{speaker_id}_{label}.wav" |
|
|
|
|
|
|
|
|
sf.write(filename, audio_data, sample_rate) |
|
|
|
|
|
|
|
|
uploaded_files.append(filename) |
|
|
|
|
|
|
|
|
info_message = f"Recorded audio length: {audio_length:.2f} seconds." |
|
|
|
|
|
|
|
|
gr.Info(info_message, duration=5) |
|
|
|
|
|
|
|
|
return uploaded_files |
|
|
|
|
|
|
|
|
def create_interface(): |
|
|
with gr.Blocks() as demo: |
|
|
labels = ["Label1", "Label2", "Label3", "Custom"] |
|
|
label_dropdown = gr.Dropdown(choices=labels, label="Select Label") |
|
|
custom_label = gr.Textbox(label="Enter Custom Label", visible=False) |
|
|
|
|
|
|
|
|
def toggle_custom_label(selected_label): |
|
|
return gr.update(visible=True) if selected_label == "Custom" else gr.update(visible=False) |
|
|
|
|
|
label_dropdown.change(toggle_custom_label, inputs=label_dropdown, outputs=custom_label) |
|
|
|
|
|
speaker_name = gr.Textbox(label="Enter User Name") |
|
|
audio = gr.Audio( |
|
|
sources=["microphone", "upload"], |
|
|
type="numpy", |
|
|
label="Record Audio" |
|
|
) |
|
|
|
|
|
submit_button = gr.Button("Submit") |
|
|
|
|
|
|
|
|
file_list = gr.Files(label="Download your recordings") |
|
|
|
|
|
|
|
|
info_display = gr.Info() |
|
|
|
|
|
|
|
|
submit_button.click( |
|
|
fn=save_audio, |
|
|
inputs=[audio, label_dropdown, custom_label, speaker_name], |
|
|
outputs=[file_list], |
|
|
) |
|
|
|
|
|
|
|
|
gr.Markdown(help_en) |
|
|
gr.Markdown(help_zh_tw) |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
interface = create_interface() |
|
|
interface.launch() |
|
|
|