File size: 2,052 Bytes
4949e74
c8018ef
 
 
4949e74
 
 
0c75fa8
4949e74
0c75fa8
 
4949e74
 
c8018ef
4949e74
 
 
 
0c75fa8
 
4949e74
 
0c75fa8
4949e74
 
 
 
 
 
 
 
0c75fa8
 
4949e74
0c75fa8
4949e74
 
 
c8018ef
4949e74
 
 
c8018ef
4949e74
 
 
 
 
 
 
 
b26d75a
 
4949e74
 
 
 
 
0c75fa8
4949e74
0c75fa8
4949e74
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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()