import torch import gradio as gr import yt_dlp as youtube_dl from transformers import pipeline from transformers.pipelines.audio_utils import ffmpeg_read import tempfile import os import time # Model and setup MODEL_NAME = "openai/whisper-large-v3" BATCH_SIZE = 8 YT_LENGTH_LIMIT_S = 3600 # 1-hour limit for YouTube files device = 0 if torch.cuda.is_available() else "cpu" pipe = pipeline( task="automatic-speech-recognition", model=MODEL_NAME, chunk_length_s=30, device=device, ) # Function to transcribe audio def transcribe(inputs, task): if inputs is None: raise gr.Error("No audio file submitted! Please upload or record an audio file.") text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"] return text # YouTube video processing functions def _return_yt_html_embed(yt_url): video_id = yt_url.split("?v=")[-1] return f'
' def download_yt_audio(yt_url, filename): # [ ... existing code for download_yt_audio ... ] def yt_transcribe(yt_url, task): html_embed_str = _return_yt_html_embed(yt_url) with tempfile.TemporaryDirectory() as tmpdirname: filepath = os.path.join(tmpdirname, "video.mp4") download_yt_audio(yt_url, filepath) with open(filepath, "rb") as f: inputs = f.read() inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate) inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate} text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"] return html_embed_str, text # Gradio interfaces mf_transcribe = gr.Interface( fn=transcribe, inputs=[ gr.inputs.Audio(source="microphone", type="filepath", optional=True), gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"), ], outputs="text", layout="horizontal", theme="huggingface", title="Whisper Large V3: Transcribe Audio", description="Transcribe long-form microphone or audio inputs with the click of a button!" ) file_transcribe = gr.Interface( fn=transcribe, inputs=[ gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Audio file"), gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"), ], outputs="text", layout="horizontal", theme="NoCrypt/miku@1.2.1", title="Whisper Large V3: Transcribe Audio", description="Transcribe long-form microphone or audio inputs with the click of a button!" ) yt_transcribe = gr.Interface( fn=yt_transcribe, inputs=[ gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"), gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe") ], outputs=["html", "text"], layout="horizontal", theme="NoCrypt/miku@1.2.1", title="Whisper Large V3: Transcribe YouTube", description="Transcribe long-form YouTube videos with the click of a button!" ) # Main Gradio application with gr.Blocks(theme="NoCrypt/miku@1.2.1") as demo: gr.HTML("

AI Assistant

") gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"]) demo.launch(enable_queue=True)