import torch import gradio as gr import time import numpy as np import scipy.io.wavfile from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline # ✅ 1️⃣ Force Model to Run on CPU device = "cpu" torch_dtype = torch.float32 # Use CPU-friendly float type MODEL_NAME = "openai/whisper-tiny" # ✅ Switched to smallest model for fastest performance # ✅ 2️⃣ Load Whisper Tiny Model on CPU (Removed `low_cpu_mem_usage=True`) model = AutoModelForSpeechSeq2Seq.from_pretrained( MODEL_NAME, torch_dtype=torch_dtype, use_safetensors=True # ✅ Removed low_cpu_mem_usage ) model.to(device) # ✅ 3️⃣ Load Processor & Pipeline processor = AutoProcessor.from_pretrained(MODEL_NAME) pipe = pipeline( task="automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, chunk_length_s=2, # ✅ Process in 2-second chunks for ultra-low latency torch_dtype=torch_dtype, device=device, ) # ✅ 4️⃣ Real-Time Streaming Transcription (Microphone) def stream_transcribe(stream, new_chunk): start_time = time.time() try: sr, y = new_chunk # ✅ Convert stereo to mono if y.ndim > 1: y = y.mean(axis=1) y = y.astype(np.float32) y /= np.max(np.abs(y)) # ✅ Append to Stream if stream is not None: stream = np.concatenate([stream, y]) else: stream = y # ✅ Run Transcription transcription = pipe({"sampling_rate": sr, "raw": stream})["text"] latency = time.time() - start_time return stream, transcription, f"{latency:.2f} sec" except Exception as e: print(f"Error: {e}") return stream, str(e), "Error" # ✅ 5️⃣ Transcription for File Upload def transcribe(inputs, previous_transcription): start_time = time.time() try: # ✅ Convert file input to correct format sample_rate, audio_data = inputs transcription = pipe({"sampling_rate": sample_rate, "raw": audio_data})["text"] previous_transcription += transcription latency = time.time() - start_time return previous_transcription, f"{latency:.2f} sec" except Exception as e: print(f"Error: {e}") return previous_transcription, "Error" # ✅ 6️⃣ Clear Function def clear(): return "" # ✅ 7️⃣ Gradio Interface (Microphone Streaming) with gr.Blocks() as microphone: gr.Markdown(f"# Whisper Tiny - Real-Time Transcription (CPU) 🎙️") gr.Markdown(f"Using [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) for ultra-fast speech-to-text.") with gr.Row(): input_audio_microphone = gr.Audio(sources=["microphone"], type="numpy", streaming=True) output = gr.Textbox(label="Live Transcription", value="") latency_textbox = gr.Textbox(label="Latency (seconds)", value="0.0") with gr.Row(): clear_button = gr.Button("Clear Output") state = gr.State() input_audio_microphone.stream( stream_transcribe, [state, input_audio_microphone], [state, output, latency_textbox], time_limit=30, stream_every=1 ) clear_button.click(clear, outputs=[output]) # ✅ 8️⃣ Gradio Interface (File Upload) with gr.Blocks() as file: gr.Markdown(f"# Upload Audio File for Transcription 🎵") gr.Markdown(f"Using [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) for speech-to-text.") with gr.Row(): input_audio = gr.Audio(sources=["upload"], type="numpy") output = gr.Textbox(label="Transcription", value="") latency_textbox = gr.Textbox(label="Latency (seconds)", value="0.0") with gr.Row(): submit_button = gr.Button("Submit") clear_button = gr.Button("Clear Output") submit_button.click(transcribe, [input_audio, output], [output, latency_textbox]) clear_button.click(clear, outputs=[output]) # ✅ 9️⃣ Final Gradio App (Supports Microphone & File Upload) with gr.Blocks(theme=gr.themes.Ocean()) as demo: gr.TabbedInterface([microphone, file], ["Microphone", "Upload Audio"]) # ✅ 1️⃣0️⃣ Run Gradio Locally if __name__ == "__main__": demo.launch()