Spaces:
Sleeping
Sleeping
| import os | |
| # from pytube import YouTube | |
| from pytubefix import YouTube | |
| from pytubefix.cli import on_progress | |
| import torch | |
| from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline | |
| import gradio as gr | |
| def seconds_to_hms(seconds): | |
| if type(seconds).__name__ != 'float': | |
| return seconds | |
| hours = seconds // 3600 | |
| minutes = (seconds % 3600) // 60 | |
| seconds = seconds % 60 | |
| return f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}" | |
| def chunk_youtube(yt_url): | |
| yt = YouTube(yt_url, on_progress_callback=on_progress) | |
| audio = yt.streams.filter(only_audio=True, file_extension='mp4').first() | |
| audio.download(filename="audio.mp3") | |
| yield "Stage 1: Audio download DONE" | |
| device = "cuda:0" if torch.cuda.is_available() else "cpu" | |
| torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 | |
| torch.cuda.empty_cache() | |
| model_id = "openai/whisper-large-v3" | |
| model = AutoModelForSpeechSeq2Seq.from_pretrained( | |
| model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True | |
| ) | |
| model.to(device) | |
| processor = AutoProcessor.from_pretrained(model_id) | |
| pipe = pipeline( | |
| "automatic-speech-recognition", | |
| model=model, | |
| tokenizer=processor.tokenizer, | |
| feature_extractor=processor.feature_extractor, | |
| max_new_tokens=128, | |
| chunk_length_s=30, | |
| batch_size=16, | |
| return_timestamps=True, | |
| torch_dtype=torch_dtype, | |
| device=device, | |
| ) | |
| result = pipe("audio.mp3", return_timestamps=True) | |
| transcript = result["chunks"] | |
| yield "Stage 2: LLM processing DONE" | |
| output = "" | |
| for i in transcript: | |
| output += str(seconds_to_hms(i["timestamp"][0])) + " - " + str(seconds_to_hms(i["timestamp"][1])) + ":\n" + i[ | |
| "text"] + "\n" | |
| yield output | |
| iface = gr.Interface(fn=chunk_youtube, inputs="text", outputs="text", live=True) | |
| iface.launch() | |