Update app.py
Browse files
app.py
CHANGED
|
@@ -3,49 +3,56 @@ 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 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Step 2: Transcribe
|
| 23 |
-
result = asr_model.transcribe(
|
| 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
|
| 30 |
hindi_audio = generate_audio(hindi_text)
|
| 31 |
-
|
|
|
|
| 32 |
f.write(hindi_audio)
|
| 33 |
|
| 34 |
-
# Step 5: Merge audio with video
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 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="
|
| 47 |
outputs=[
|
| 48 |
gr.Textbox(label="Hindi Translation"),
|
| 49 |
gr.Video(label="Dubbed Hindi Video")
|
| 50 |
-
]
|
|
|
|
|
|
|
| 51 |
).launch()
|
|
|
|
| 3 |
import whisper
|
| 4 |
from transformers import pipeline
|
| 5 |
from bark import generate_audio, preload_models
|
|
|
|
| 6 |
import moviepy.editor as mp
|
| 7 |
+
import os
|
| 8 |
+
import tempfile
|
| 9 |
|
| 10 |
# Load models
|
| 11 |
asr_model = whisper.load_model("base")
|
| 12 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")
|
|
|
|
|
|
|
| 13 |
preload_models()
|
| 14 |
|
| 15 |
def process_video(link):
|
| 16 |
try:
|
| 17 |
+
# Download audio from YouTube
|
| 18 |
yt = YouTube(link)
|
| 19 |
+
audio_stream = yt.streams.filter(only_audio=True).first()
|
| 20 |
+
temp_dir = tempfile.mkdtemp()
|
| 21 |
+
audio_path = os.path.join(temp_dir, "audio.mp4")
|
| 22 |
+
audio_stream.download(filename=audio_path)
|
| 23 |
|
| 24 |
+
# Step 2: Transcribe using Whisper
|
| 25 |
+
result = asr_model.transcribe(audio_path)
|
| 26 |
english_text = result["text"]
|
| 27 |
|
| 28 |
# Step 3: Translate to Hindi
|
| 29 |
hindi_text = translator(english_text)[0]['translation_text']
|
| 30 |
|
| 31 |
+
# Step 4: Generate Hindi audio using Bark
|
| 32 |
hindi_audio = generate_audio(hindi_text)
|
| 33 |
+
hindi_audio_path = os.path.join(temp_dir, "hindi.wav")
|
| 34 |
+
with open(hindi_audio_path, "wb") as f:
|
| 35 |
f.write(hindi_audio)
|
| 36 |
|
| 37 |
+
# Step 5: Merge audio with video
|
| 38 |
+
audio_clip = mp.AudioFileClip(hindi_audio_path)
|
| 39 |
+
video_clip = mp.ColorClip(size=(1280, 720), color=(0, 0, 0), duration=audio_clip.duration)
|
| 40 |
+
final_video = video_clip.set_audio(audio_clip)
|
| 41 |
+
output_path = os.path.join(temp_dir, "final_output.mp4")
|
| 42 |
+
final_video.write_videofile(output_path, fps=24)
|
| 43 |
|
| 44 |
return hindi_text, output_path
|
| 45 |
+
|
| 46 |
except Exception as e:
|
| 47 |
+
return f"Error: {str(e)}", None
|
| 48 |
|
| 49 |
gr.Interface(
|
| 50 |
fn=process_video,
|
| 51 |
+
inputs=gr.Textbox(label="YouTube Video Link"),
|
| 52 |
outputs=[
|
| 53 |
gr.Textbox(label="Hindi Translation"),
|
| 54 |
gr.Video(label="Dubbed Hindi Video")
|
| 55 |
+
],
|
| 56 |
+
title="ποΈ YouTube Hindi Dubber AI",
|
| 57 |
+
description="Paste a YouTube video link. This app transcribes it, translates it to Hindi, and dubs it using Bark AI."
|
| 58 |
).launch()
|