Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import yt_dlp
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
|
| 6 |
+
# Load pipelines
|
| 7 |
+
asr = pipeline("automatic-speech-recognition", model="openai/whisper-base", task="transcribe")
|
| 8 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")
|
| 9 |
+
|
| 10 |
+
def youtube_to_hindi_dub(url):
|
| 11 |
+
# Step 1: Download audio from YouTube
|
| 12 |
+
ydl_opts = {
|
| 13 |
+
'format': 'bestaudio/best',
|
| 14 |
+
'outtmpl': 'audio.%(ext)s',
|
| 15 |
+
'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3'}],
|
| 16 |
+
'quiet': True
|
| 17 |
+
}
|
| 18 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 19 |
+
ydl.download([url])
|
| 20 |
+
audio_path = "audio.mp3"
|
| 21 |
+
|
| 22 |
+
# Step 2: Transcribe English
|
| 23 |
+
result = asr(audio_path)
|
| 24 |
+
english_text = result["text"]
|
| 25 |
+
|
| 26 |
+
# Step 3: Translate to Hindi
|
| 27 |
+
hindi_text = translator(english_text)[0]['translation_text']
|
| 28 |
+
|
| 29 |
+
# Step 4: Convert to Hindi Audio
|
| 30 |
+
tts = gTTS(hindi_text, lang="hi")
|
| 31 |
+
tts.save("dubbed.mp3")
|
| 32 |
+
|
| 33 |
+
return "dubbed.mp3"
|
| 34 |
+
|
| 35 |
+
# Gradio UI
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=youtube_to_hindi_dub,
|
| 38 |
+
inputs=gr.Textbox(label="YouTube Video Link"),
|
| 39 |
+
outputs=gr.Audio(label="Dubbed Hindi Audio", type="filepath"),
|
| 40 |
+
title="🎧 YouTube Video to Hindi Dubber",
|
| 41 |
+
description="Paste an English YouTube link, and get Hindi AI voice dubbing."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
iface.launch()
|