Spaces:
Runtime error
Runtime error
# Import required libraries | |
import os | |
import re | |
import logging | |
import whisper | |
from pytube import YouTube | |
import gradio as gr | |
# Setup logging | |
logging.basicConfig(level=logging.INFO) | |
# Load the Whisper model | |
model = whisper.load_model("base") | |
def download_audio_from_youtube(url): | |
""" | |
Download the audio from a YouTube video and return the path to the audio file. | |
""" | |
yt = YouTube(url) | |
video = yt.streams.filter(only_audio=True).first() | |
out_file = video.download(output_path=".") | |
return out_file | |
def get_text(url): | |
""" | |
Transcribe the audio from a YouTube video and return the transcript. | |
""" | |
if not url: | |
return '' | |
out_file = download_audio_from_youtube(url) | |
file_stats = os.stat(out_file) | |
logging.info(f'Size of audio file in Bytes: {file_stats.st_size}') | |
if file_stats.st_size > 30000000: | |
logging.error('Videos for transcription on this space are limited to about 1.5 hours...') | |
return '' | |
base, ext = os.path.splitext(out_file) | |
new_file = base + '.mp3' | |
os.rename(out_file, new_file) | |
result = model.transcribe(new_file) | |
return result['text'].strip() | |
def create_gradio_interface(): | |
""" | |
Create and launch a Gradio interface for transcribing YouTube videos. | |
""" | |
with gr.Blocks() as demo: | |
gr.Markdown("<h1><center>Trascribe Videos using <a href=https://openai.com/blog/whisper/ target=_blank>Whisper</a></center></h1>") | |
gr.Markdown("<center>Enter the link of any YouTube video.</center>") | |
input_text_url = gr.Textbox(placeholder='Youtube video URL', label='YouTube URL') | |
result_button_transcribe = gr.Button('Transcribe') | |
output_text_transcribe = gr.Textbox(placeholder='Transcript of the YouTube video.', label='Transcript') | |
result_button_transcribe.click(get_text, inputs=input_text_url, outputs=output_text_transcribe) | |
demo.queue().launch(debug=True) | |
# Launch the Gradio interface | |
if __name__ == "__main__": | |
create_gradio_interface() | |