Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +45 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import scipy.io.wavfile as wavfile
|
| 4 |
+
|
| 5 |
+
def process_audio(sample_rate, audio_data):
|
| 6 |
+
"""Resample to 16kHz and save as .wav file"""
|
| 7 |
+
if audio_data is None:
|
| 8 |
+
return "No audio recorded"
|
| 9 |
+
|
| 10 |
+
target_sample_rate = 16000
|
| 11 |
+
|
| 12 |
+
# Resample if needed
|
| 13 |
+
if sample_rate != target_sample_rate:
|
| 14 |
+
duration = len(audio_data) / sample_rate
|
| 15 |
+
num_samples = int(duration * target_sample_rate)
|
| 16 |
+
audio_data = np.interp(
|
| 17 |
+
np.linspace(0, len(audio_data), num_samples),
|
| 18 |
+
np.arange(len(audio_data)),
|
| 19 |
+
audio_data
|
| 20 |
+
)
|
| 21 |
+
audio_data = audio_data.astype(np.float32)
|
| 22 |
+
|
| 23 |
+
# Save to file
|
| 24 |
+
output_path = "recording.wav"
|
| 25 |
+
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 26 |
+
wavfile.write(output_path, target_sample_rate, audio_int16)
|
| 27 |
+
|
| 28 |
+
return f"Saved to {output_path} (16kHz, {len(audio_int16)/target_sample_rate:.2f}s)"
|
| 29 |
+
|
| 30 |
+
with gr.Blocks() as demo:
|
| 31 |
+
gr.Markdown("# Voice Recorder")
|
| 32 |
+
gr.Markdown("Record audio from microphone and save as 16kHz .wav file")
|
| 33 |
+
|
| 34 |
+
audio_input = gr.Audio(sources=["microphone"], type="numpy")
|
| 35 |
+
record_btn = gr.Button("Record")
|
| 36 |
+
output_text = gr.Textbox(label="Status")
|
| 37 |
+
|
| 38 |
+
record_btn.click(
|
| 39 |
+
fn=lambda x: process_audio(16000, x) if x is not None else "No audio",
|
| 40 |
+
inputs=[audio_input],
|
| 41 |
+
outputs=[output_text]
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
scipy>=1.11.0
|
| 3 |
+
numpy>=1.24.0
|