File size: 1,726 Bytes
2e72f8a
d1fb69a
4f3fe7a
5b6528f
2e72f8a
d1fb69a
4f3fe7a
2e72f8a
d1fb69a
 
 
5e7503b
d1fb69a
 
 
 
 
 
5e7503b
 
 
 
 
 
 
 
 
d1fb69a
 
 
 
 
 
2e72f8a
d1fb69a
5e7503b
 
d1fb69a
 
5e7503b
7271e01
d1fb69a
4f3fe7a
7271e01
 
d1fb69a
a86e3dc
5e7503b
 
 
2e72f8a
5e7503b
d1fb69a
 
5e7503b
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
import gradio as gr
import yt_dlp
import whisper
import os

# Load Whisper model
model = whisper.load_model("base")

def download_audio(url):
    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': '/tmp/audio.%(ext)s',  # Use /tmp directory for outputs
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            info_dict = ydl.extract_info(url, download=True)
            audio_file = ydl.prepare_filename(info_dict).replace('.webm', '.mp3').replace('.m4a', '.mp3')
        return audio_file
    except yt_dlp.utils.DownloadError as e:
        return f"Download Error: {str(e)}"
    except Exception as e:
        return f"An unexpected error occurred: {str(e)}"

def transcribe_audio(audio_file):
    result = model.transcribe(audio_file)
    return result["text"]

def process_video(url):
    try:
        audio_file = download_audio(url)
        if "Error" in audio_file:
            return audio_file  # Return error message if download failed
        transcript = transcribe_audio(audio_file)
        os.remove(audio_file)  # Clean up the audio file
        return transcript
    except Exception as e:
        return f"An error occurred: {str(e)}"

# Gradio Interface
iface = gr.Interface(
    fn=process_video,
    inputs=gr.Textbox(value="https://www.youtube.com/watch?v=RiI1NkaDXlQ", label="YouTube Video URL"),
    outputs=gr.Textbox(label="Transcript"),
    title="YouTube Video Transcriber",
    description="Enter a YouTube video URL to get a transcript of its content."
)

if __name__ == "__main__":
    iface.launch()