Scribe / app.py
ritwikraha
add: first app
0c75fa8
raw
history blame
2.57 kB
# 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>Free Fast YouTube URL Video-to-Text using <a href=https://openai.com/blog/whisper/ target=_blank>OpenAI's Whisper</a> Model</center></h1>")
gr.Markdown("<center>Enter the link of any YouTube video to generate a text transcript of the video.</center>")
gr.Markdown("<center><b>'Whisper is a neural net that approaches human level robustness and accuracy on English speech recognition.'</b></center>")
gr.Markdown("<center>Transcription takes 5-10 seconds per minute of the video. #patience<br />If you have time while waiting, check out my <a href=https://www.artificial-intelligence.blog target=_blank>AI blog</a> (opens in new tab).</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(default_enabled=True).launch(debug=True)
# Launch the Gradio interface
if __name__ == "__main__":
create_gradio_interface()