Download main.py from sonic-coder/YoutubeTranscriptAI: direct link, hf CLI and curl.
- Browser
- Download file 3.7 kB
-
https://huggingface.co/sonic-coder/YoutubeTranscriptAI/resolve/main/main.py
- Command line
-
hf download hf://sonic-coder/YoutubeTranscriptAI/main.py
-
curl -L -o main.py https://huggingface.co/sonic-coder/YoutubeTranscriptAI/resolve/main/main.py
3.7 kB
| 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) |