Update main.py
Browse files
main.py
CHANGED
|
@@ -1,51 +1,130 @@
|
|
| 1 |
import whisper
|
| 2 |
import yt_dlp
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
def
|
| 6 |
if not link.strip():
|
| 7 |
gr.Info("You need to provide a download link.")
|
| 8 |
print("You need to provide a download link")
|
| 9 |
return None
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
'
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def whisper_(input_audio):
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
print(f"Detected language: {max(probs, key=probs.get)}")
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import whisper
|
| 2 |
import yt_dlp
|
| 3 |
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
def youtube_(link):
|
| 7 |
if not link.strip():
|
| 8 |
gr.Info("You need to provide a download link.")
|
| 9 |
print("You need to provide a download link")
|
| 10 |
return None
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
ydl_opts = {
|
| 14 |
+
'format': 'bestaudio/best',
|
| 15 |
+
'outtmpl': '%(title)s.%(ext)s',
|
| 16 |
+
'nocheckcertificate': True,
|
| 17 |
+
'ignoreerrors': True,
|
| 18 |
+
'no_warnings': True,
|
| 19 |
+
'quiet': True,
|
| 20 |
+
'extractaudio': True,
|
| 21 |
+
'postprocessors': [{
|
| 22 |
+
'key': 'FFmpegExtractAudio',
|
| 23 |
+
'preferredcodec': 'wav',
|
| 24 |
+
'preferredquality': '192',
|
| 25 |
+
}],
|
| 26 |
+
}
|
| 27 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 28 |
+
result = ydl.extract_info(link, download=True)
|
| 29 |
+
download_path = ydl.prepare_filename(result, outtmpl='%(title)s.wav')
|
| 30 |
+
|
| 31 |
+
return download_path
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
gr.Error(f"Error downloading video: {str(e)}")
|
| 35 |
+
print(f"Error: {e}")
|
| 36 |
+
return None
|
| 37 |
|
| 38 |
def whisper_(input_audio):
|
| 39 |
+
try:
|
| 40 |
+
model = whisper.load_model("base") # Using base for faster demo, change to "medium" if needed
|
| 41 |
+
|
| 42 |
+
# Transcribe the audio
|
| 43 |
+
result = model.transcribe(input_audio)
|
| 44 |
+
|
| 45 |
+
text_result = result["text"]
|
| 46 |
+
language = result["language"]
|
| 47 |
+
|
| 48 |
+
print(f"Detected language: {language}")
|
| 49 |
+
print(f"Transcription: {text_result}")
|
| 50 |
+
|
| 51 |
+
return text_result
|
| 52 |
|
| 53 |
+
except Exception as e:
|
| 54 |
+
gr.Error(f"Error during transcription: {str(e)}")
|
| 55 |
+
print(f"Error: {e}")
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
def transcribe(url_audio):
|
| 59 |
+
# Download audio from YouTube
|
| 60 |
+
audio_path = youtube_(url_audio)
|
| 61 |
+
|
| 62 |
+
if audio_path is None:
|
| 63 |
+
return "Failed to download audio. Please check the URL and try again."
|
| 64 |
|
| 65 |
+
# Transcribe the audio
|
| 66 |
+
result = whisper_(audio_path)
|
|
|
|
| 67 |
|
| 68 |
+
# Clean up downloaded file
|
| 69 |
+
try:
|
| 70 |
+
if os.path.exists(audio_path):
|
| 71 |
+
os.remove(audio_path)
|
| 72 |
+
except:
|
| 73 |
+
pass
|
| 74 |
|
| 75 |
+
if result is None:
|
| 76 |
+
return "Failed to transcribe audio. Please try again."
|
| 77 |
|
| 78 |
+
return result
|
| 79 |
+
|
| 80 |
+
# Gradio interface
|
| 81 |
+
with gr.Blocks(title="YouTube Audio Transcriber") as demo:
|
| 82 |
+
gr.Markdown("""
|
| 83 |
+
# 🎙️ YouTube Audio Transcriber
|
| 84 |
+
Enter a YouTube URL and get the audio transcribed using Whisper AI.
|
| 85 |
+
""")
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
with gr.Column(scale=4):
|
| 89 |
+
url_input = gr.Textbox(
|
| 90 |
+
label="YouTube URL",
|
| 91 |
+
placeholder="https://www.youtube.com/watch?v=...",
|
| 92 |
+
lines=2
|
| 93 |
+
)
|
| 94 |
+
transcribe_btn = gr.Button("Transcribe", variant="primary", size="lg")
|
| 95 |
+
|
| 96 |
+
with gr.Column(scale=5):
|
| 97 |
+
output_text = gr.Textbox(
|
| 98 |
+
label="Transcription",
|
| 99 |
+
lines=10,
|
| 100 |
+
placeholder="The transcribed text will appear here...",
|
| 101 |
+
interactive=False
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
with gr.Row():
|
| 105 |
+
with gr.Column():
|
| 106 |
+
gr.Markdown("""
|
| 107 |
+
### ⚡ Tips:
|
| 108 |
+
- Works with most YouTube videos
|
| 109 |
+
- Supports multiple languages
|
| 110 |
+
- Audio is automatically downloaded and processed
|
| 111 |
+
- Processing may take a few seconds depending on video length
|
| 112 |
+
""")
|
| 113 |
+
|
| 114 |
+
# Wire up the function
|
| 115 |
+
transcribe_btn.click(
|
| 116 |
+
fn=transcribe,
|
| 117 |
+
inputs=url_input,
|
| 118 |
+
outputs=output_text
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# Also support Enter key
|
| 122 |
+
url_input.submit(
|
| 123 |
+
fn=transcribe,
|
| 124 |
+
inputs=url_input,
|
| 125 |
+
outputs=output_text
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Launch the app
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.launch(share=True)
|