Download app.py from ltduy95/personal: direct link, hf CLI and curl.
- Browser
- Download file 4.09 kB
-
https://huggingface.co/spaces/ltduy95/personal/resolve/main/app.py
- Command line
-
hf download hf://spaces/ltduy95/personal/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ltduy95/personal/resolve/main/app.py
4.09 kB
| import gradio as gr | |
| import numpy as np | |
| import torch | |
| import librosa | |
| from transformers import WhisperProcessor, WhisperForConditionalGeneration | |
| # Initialize the Whisper model and processor | |
| model_name = "openai/whisper-medium" | |
| processor = WhisperProcessor.from_pretrained(model_name) | |
| model = WhisperForConditionalGeneration.from_pretrained(model_name) | |
| TARGET_SR = 16000 | |
| def resample_audio(y, sr): | |
| # Resample the audio to 16000 Hz | |
| return librosa.resample(y, orig_sr=sr, target_sr=TARGET_SR) | |
| def transcribe_audio(audio_path, language): | |
| if audio_path is None: | |
| return "" | |
| # Load audio file | |
| y, sr = librosa.load(audio_path, sr=None) | |
| # Convert to mono if stereo | |
| if y.ndim > 1: | |
| y = y.mean(axis=1) | |
| y = y.astype(np.float32) | |
| y /= np.max(np.abs(y)) | |
| # Resample to 16000 Hz | |
| y = resample_audio(y, sr) | |
| # Limit input length to 30 seconds | |
| max_length = min(len(y), 30 * TARGET_SR) | |
| y = y[:max_length] | |
| # Process the audio | |
| input_features = processor(y, sampling_rate=TARGET_SR, return_tensors="pt").input_features | |
| # Generate tokens using the model | |
| forced_decoder_ids = processor.get_decoder_prompt_ids(language=language, task="transcribe") | |
| generated_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids) | |
| # Decode the tokens to text | |
| transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
| return transcription | |
| def transcribe_stream(stream, new_chunk, language): | |
| sr, y = new_chunk | |
| # Convert to mono if stereo | |
| if y.ndim > 1: | |
| y = y.mean(axis=1) | |
| y = y.astype(np.float32) | |
| y /= np.max(np.abs(y)) | |
| # Resample to 16000 Hz | |
| y = resample_audio(y, sr) | |
| if stream is not None: | |
| stream = np.concatenate([stream, y]) | |
| else: | |
| stream = y | |
| # Limit input length to 30 seconds | |
| max_length = min(len(stream), 30 * TARGET_SR) | |
| stream = stream[:max_length] | |
| # Transcribe the current stream | |
| transcription = transcribe_audio((TARGET_SR, stream), language) | |
| return stream, transcription | |
| def process_multiple_files(audio_files, language): | |
| transcriptions = [] | |
| for audio_file in audio_files: | |
| if audio_file is not None: | |
| transcription = transcribe_audio(audio_file.name, language) | |
| transcriptions.append(f"File: {audio_file.name}\n{transcription}") | |
| combined_transcription = "\n\n".join(transcriptions) | |
| return combined_transcription | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Audio Transcription Demo (Vietnamese and English)") | |
| gr.Markdown("Using Whisper Medium model for improved accuracy") | |
| language = gr.Dropdown(["vi", "en"], label="Language", value="vi") | |
| with gr.Tabs(): | |
| with gr.TabItem("Microphone Input"): | |
| audio_stream = gr.Audio(streaming=True) | |
| text_output_stream = gr.Textbox(label="Transcription") | |
| audio_stream.stream(transcribe_stream, inputs=[gr.State(), audio_stream, language], outputs=[gr.State(), text_output_stream]) | |
| with gr.TabItem("File Upload"): | |
| audio_files = gr.File(file_count="multiple", type="filepath", label="Upload Audio Files") | |
| text_output_file = gr.Textbox(label="Transcription") | |
| transcribe_btn = gr.Button("Transcribe") | |
| download_btn = gr.Button("Download Transcription") | |
| transcribe_btn.click( | |
| process_multiple_files, | |
| inputs=[audio_files, language], | |
| outputs=[text_output_file] | |
| ) | |
| download_btn.click( | |
| lambda x: gr.File.update(value=x, visible=True), | |
| inputs=[text_output_file], | |
| outputs=[gr.File(label="Download Transcription", visible=False)] | |
| ) | |
| gr.Markdown("Note: Each audio input is limited to 30 seconds for transcription. Select 'vi' for Vietnamese or 'en' for English.") | |
| gr.Markdown("Please note that using the medium model may increase processing time.") | |
| demo.launch() | |