File size: 3,698 Bytes
b51d214
42e246d
 
760234c
b51d214
760234c
42e246d
 
 
 
760234c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b51d214
 
760234c
 
 
 
 
 
 
 
 
 
 
 
 
b51d214
760234c
 
 
 
 
 
 
 
 
 
 
b51d214
760234c
 
b51d214
760234c
 
 
 
 
 
b51d214
760234c
 
b51d214
760234c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import whisper
import yt_dlp
import gradio as gr
import os

def youtube_(link):
    if not link.strip():
        gr.Info("You need to provide a download link.")
        print("You need to provide a download link")
        return None
    
    try:
        ydl_opts = {
            'format': 'bestaudio/best',
            'outtmpl': '%(title)s.%(ext)s',
            'nocheckcertificate': True,
            'ignoreerrors': True,
            'no_warnings': True,
            'quiet': True,
            'extractaudio': True,
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'wav',
                'preferredquality': '192',
            }],
        }
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            result = ydl.extract_info(link, download=True)
            download_path = ydl.prepare_filename(result, outtmpl='%(title)s.wav')
            
        return download_path
    
    except Exception as e:
        gr.Error(f"Error downloading video: {str(e)}")
        print(f"Error: {e}")
        return None

def whisper_(input_audio):
    try:
        model = whisper.load_model("base")  # Using base for faster demo, change to "medium" if needed
        
        # Transcribe the audio
        result = model.transcribe(input_audio)
        
        text_result = result["text"]
        language = result["language"]
        
        print(f"Detected language: {language}")
        print(f"Transcription: {text_result}")
        
        return text_result
    
    except Exception as e:
        gr.Error(f"Error during transcription: {str(e)}")
        print(f"Error: {e}")
        return None

def transcribe(url_audio):
    # Download audio from YouTube
    audio_path = youtube_(url_audio)
    
    if audio_path is None:
        return "Failed to download audio. Please check the URL and try again."
    
    # Transcribe the audio
    result = whisper_(audio_path)
    
    # Clean up downloaded file
    try:
        if os.path.exists(audio_path):
            os.remove(audio_path)
    except:
        pass
    
    if result is None:
        return "Failed to transcribe audio. Please try again."
    
    return result

# Gradio interface
with gr.Blocks(title="YouTube Audio Transcriber") as demo:
    gr.Markdown("""
    # 🎙️ YouTube Audio Transcriber
    Enter a YouTube URL and get the audio transcribed using Whisper AI.
    """)
    
    with gr.Row():
        with gr.Column(scale=4):
            url_input = gr.Textbox(
                label="YouTube URL",
                placeholder="https://www.youtube.com/watch?v=...",
                lines=2
            )
            transcribe_btn = gr.Button("Transcribe", variant="primary", size="lg")
        
        with gr.Column(scale=5):
            output_text = gr.Textbox(
                label="Transcription",
                lines=10,
                placeholder="The transcribed text will appear here...",
                interactive=False
            )
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("""
            ### ⚡ Tips:
            - Works with most YouTube videos
            - Supports multiple languages
            - Audio is automatically downloaded and processed
            - Processing may take a few seconds depending on video length
            """)
    
    # Wire up the function
    transcribe_btn.click(
        fn=transcribe,
        inputs=url_input,
        outputs=output_text
    )
    
    # Also support Enter key
    url_input.submit(
        fn=transcribe,
        inputs=url_input,
        outputs=output_text
    )

# Launch the app
if __name__ == "__main__":
    demo.launch(share=True)