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|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
import json
|
| 5 |
+
from typing import Optional, Dict
|
| 6 |
+
from elevenlabs import ElevenLabs
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
# Load environment variables
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
# Initialize ElevenLabs client
|
| 13 |
+
eleven = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
|
| 14 |
+
|
| 15 |
+
# Define language options with codes
|
| 16 |
+
languages: Dict[str, str] = {
|
| 17 |
+
"English": "en", "Hindi": "hi", "Portuguese": "pt", "Chinese": "zh", "Spanish": "es",
|
| 18 |
+
"French": "fr", "German": "de", "Japanese": "ja", "Arabic": "ar", "Russian": "ru",
|
| 19 |
+
"Korean": "ko", "Indonesian": "id", "Italian": "it", "Dutch": "nl", "Turkish": "tr",
|
| 20 |
+
"Polish": "pl", "Swedish": "sv", "Filipino": "fil", "Malay": "ms", "Romanian": "ro",
|
| 21 |
+
"Ukrainian": "uk", "Greek": "el", "Czech": "cs", "Danish": "da", "Finnish": "fi",
|
| 22 |
+
"Bulgarian": "bg", "Croatian": "hr", "Slovak": "sk", "Tamil": "ta"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def wait_for_dubbing_completion(dubbing_id: str, progress: Optional[gr.Progress] = None) -> bool:
|
| 26 |
+
MAX_ATTEMPTS = 360 # Increased to allow for up to 1 hour of processing
|
| 27 |
+
CHECK_INTERVAL = 10 # In seconds
|
| 28 |
+
|
| 29 |
+
for attempt in range(MAX_ATTEMPTS):
|
| 30 |
+
try:
|
| 31 |
+
metadata = eleven.dubbing.get_dubbing_project_metadata(dubbing_id)
|
| 32 |
+
if metadata.status == "dubbed":
|
| 33 |
+
if progress:
|
| 34 |
+
progress(1, desc="Dubbing completed successfully.")
|
| 35 |
+
return True
|
| 36 |
+
elif metadata.status == "dubbing":
|
| 37 |
+
if progress:
|
| 38 |
+
progress((attempt + 1) / MAX_ATTEMPTS, desc=f"Dubbing in progress... Time elapsed: {attempt * CHECK_INTERVAL} seconds")
|
| 39 |
+
time.sleep(CHECK_INTERVAL)
|
| 40 |
+
else:
|
| 41 |
+
if progress:
|
| 42 |
+
progress(1, desc=f"Dubbing failed: {metadata.error_message}")
|
| 43 |
+
return False
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"Error in wait_for_dubbing_completion: {str(e)}")
|
| 46 |
+
if progress:
|
| 47 |
+
progress(1, desc=f"Error checking dubbing status: {str(e)}")
|
| 48 |
+
return False
|
| 49 |
+
|
| 50 |
+
if progress:
|
| 51 |
+
progress(1, desc="Dubbing timed out after 1 hour")
|
| 52 |
+
return False
|
| 53 |
+
|
| 54 |
+
def download_dubbed_file(dubbing_id: str, language_code: str, output_dir: str, progress: Optional[gr.Progress] = None) -> Optional[str]:
|
| 55 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 56 |
+
|
| 57 |
+
file_path = os.path.join(output_dir, f"{dubbing_id}_{language_code}.mp4")
|
| 58 |
+
try:
|
| 59 |
+
with open(file_path, "wb") as file:
|
| 60 |
+
for chunk in eleven.dubbing.get_dubbed_file(dubbing_id, language_code):
|
| 61 |
+
file.write(chunk)
|
| 62 |
+
if progress:
|
| 63 |
+
progress(1, desc=f"File downloaded successfully: {file_path}")
|
| 64 |
+
return file_path
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Error in download_dubbed_file: {str(e)}")
|
| 67 |
+
if progress:
|
| 68 |
+
progress(1, desc=f"Error downloading dubbed file: {str(e)}")
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
def create_dub(input_type: str, input_data, source_language: str, target_language: str, output_dir: str, progress: Optional[gr.Progress] = None) -> str:
|
| 72 |
+
try:
|
| 73 |
+
if progress:
|
| 74 |
+
progress(0.1, desc="Initiating dubbing process")
|
| 75 |
+
|
| 76 |
+
if input_type == "file":
|
| 77 |
+
if not input_data:
|
| 78 |
+
return "No file uploaded."
|
| 79 |
+
file_path = input_data.name
|
| 80 |
+
with open(file_path, "rb") as audio_file:
|
| 81 |
+
response = eleven.dubbing.dub_a_video_or_an_audio_file(
|
| 82 |
+
file=(os.path.basename(file_path), audio_file, "video/mp4"),
|
| 83 |
+
target_lang=languages[target_language],
|
| 84 |
+
source_lang=languages[source_language],
|
| 85 |
+
)
|
| 86 |
+
else: # URL
|
| 87 |
+
response = eleven.dubbing.dub_a_video_or_an_audio_file(
|
| 88 |
+
source_url=input_data,
|
| 89 |
+
target_lang=languages[target_language],
|
| 90 |
+
source_lang=languages[source_language],
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
dubbing_id = response.dubbing_id
|
| 94 |
+
if progress:
|
| 95 |
+
progress(0.2, desc="Dubbing initiated, waiting for completion")
|
| 96 |
+
|
| 97 |
+
if wait_for_dubbing_completion(dubbing_id, progress):
|
| 98 |
+
if progress:
|
| 99 |
+
progress(0.9, desc="Dubbing completed, downloading file")
|
| 100 |
+
output_file_path = download_dubbed_file(dubbing_id, languages[target_language], output_dir, progress)
|
| 101 |
+
if output_file_path:
|
| 102 |
+
return f"Dubbing was successful! File saved at: {output_file_path}"
|
| 103 |
+
else:
|
| 104 |
+
return "Dubbing completed but file download failed."
|
| 105 |
+
else:
|
| 106 |
+
return "Dubbing failed or timed out."
|
| 107 |
+
except Exception as e:
|
| 108 |
+
print(f"Error in create_dub: {str(e)}")
|
| 109 |
+
error_message = str(e)
|
| 110 |
+
if hasattr(e, 'status_code') and hasattr(e, 'body'):
|
| 111 |
+
try:
|
| 112 |
+
error_body = json.loads(e.body)
|
| 113 |
+
if 'detail' in error_body:
|
| 114 |
+
error_message = f"Error {e.status_code}: {error_body['detail']['message']}"
|
| 115 |
+
except json.JSONDecodeError:
|
| 116 |
+
error_message = f"Error {e.status_code}: {e.body}"
|
| 117 |
+
return f"An error occurred: {error_message}"
|
| 118 |
+
|
| 119 |
+
# Create Gradio interface
|
| 120 |
+
with gr.Blocks() as iface:
|
| 121 |
+
gr.Markdown("# ElevenLabs Video Dubbing Interface")
|
| 122 |
+
|
| 123 |
+
with gr.Tab("File Upload"):
|
| 124 |
+
file_input = gr.File(label="Upload Video File")
|
| 125 |
+
file_source_lang = gr.Dropdown(list(languages.keys()), label="Source Language", value="English")
|
| 126 |
+
file_target_lang = gr.Dropdown(list(languages.keys()), label="Target Language", value="Spanish")
|
| 127 |
+
file_output_dir = gr.Textbox(label="Output Directory", value="output", placeholder="Enter the output directory path")
|
| 128 |
+
file_submit = gr.Button("Dub Video File")
|
| 129 |
+
file_output = gr.Textbox(label="Result")
|
| 130 |
+
|
| 131 |
+
with gr.Tab("URL Input"):
|
| 132 |
+
url_input = gr.Textbox(label="Enter Video URL (YouTube, TikTok, Twitter, or Vimeo)")
|
| 133 |
+
url_source_lang = gr.Dropdown(list(languages.keys()), label="Source Language", value="English")
|
| 134 |
+
url_target_lang = gr.Dropdown(list(languages.keys()), label="Target Language", value="Spanish")
|
| 135 |
+
url_output_dir = gr.Textbox(label="Output Directory", value="output", placeholder="Enter the output directory path")
|
| 136 |
+
url_submit = gr.Button("Dub Video from URL")
|
| 137 |
+
url_output = gr.Textbox(label="Result")
|
| 138 |
+
|
| 139 |
+
file_submit.click(
|
| 140 |
+
create_dub,
|
| 141 |
+
inputs=[
|
| 142 |
+
gr.Textbox(value="file", visible=False),
|
| 143 |
+
file_input,
|
| 144 |
+
file_source_lang,
|
| 145 |
+
file_target_lang,
|
| 146 |
+
file_output_dir
|
| 147 |
+
],
|
| 148 |
+
outputs=file_output
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
url_submit.click(
|
| 152 |
+
create_dub,
|
| 153 |
+
inputs=[
|
| 154 |
+
gr.Textbox(value="url", visible=False),
|
| 155 |
+
url_input,
|
| 156 |
+
url_source_lang,
|
| 157 |
+
url_target_lang,
|
| 158 |
+
url_output_dir
|
| 159 |
+
],
|
| 160 |
+
outputs=url_output
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
iface.launch()
|