Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pytube import YouTube
|
| 3 |
+
import whisper
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from bark import generate_audio, preload_models
|
| 6 |
+
import os
|
| 7 |
+
import moviepy.editor as mp
|
| 8 |
+
|
| 9 |
+
# Load models
|
| 10 |
+
asr_model = whisper.load_model("base")
|
| 11 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")
|
| 12 |
+
|
| 13 |
+
# Preload Bark models
|
| 14 |
+
preload_models()
|
| 15 |
+
|
| 16 |
+
def process_video(link):
|
| 17 |
+
try:
|
| 18 |
+
yt = YouTube(link)
|
| 19 |
+
stream = yt.streams.filter(only_audio=True).first()
|
| 20 |
+
file_path = stream.download(filename="audio.mp4")
|
| 21 |
+
|
| 22 |
+
# Step 2: Transcribe English
|
| 23 |
+
result = asr_model.transcribe(file_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: Generate Hindi Audio using Bark
|
| 30 |
+
hindi_audio = generate_audio(hindi_text)
|
| 31 |
+
with open("hindi.wav", "wb") as f:
|
| 32 |
+
f.write(hindi_audio)
|
| 33 |
+
|
| 34 |
+
# Step 5: Merge audio with video (optional)
|
| 35 |
+
original = mp.AudioFileClip("hindi.wav")
|
| 36 |
+
video = mp.VideoFileClip(file_path.replace(".mp4", ".mp4")).set_audio(original)
|
| 37 |
+
output_path = "dubbed_video.mp4"
|
| 38 |
+
video.write_videofile(output_path)
|
| 39 |
+
|
| 40 |
+
return hindi_text, output_path
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return str(e), None
|
| 43 |
+
|
| 44 |
+
gr.Interface(
|
| 45 |
+
fn=process_video,
|
| 46 |
+
inputs=gr.Textbox(label="Enter YouTube Video URL"),
|
| 47 |
+
outputs=[
|
| 48 |
+
gr.Textbox(label="Hindi Translation"),
|
| 49 |
+
gr.Video(label="Dubbed Hindi Video")
|
| 50 |
+
]
|
| 51 |
+
).launch()
|