Update app.py
Browse files
app.py
CHANGED
@@ -11,56 +11,15 @@ from dotenv import load_dotenv
|
|
11 |
load_dotenv()
|
12 |
|
13 |
# API Keys
|
14 |
-
A_KEY = os.getenv("A_KEY")
|
15 |
B_KEY = os.getenv("B_KEY")
|
16 |
|
17 |
# URLs
|
18 |
API_URL = os.getenv("API_URL")
|
19 |
UPLOAD_URL = os.getenv("UPLOAD_URL")
|
20 |
|
21 |
-
def get_voices():
|
22 |
-
url = "https://api.elevenlabs.io/v1/voices"
|
23 |
-
headers = {
|
24 |
-
"Accept": "application/json",
|
25 |
-
"xi-api-key": A_KEY
|
26 |
-
}
|
27 |
-
|
28 |
-
response = requests.get(url, headers=headers)
|
29 |
-
if response.status_code != 200:
|
30 |
-
return []
|
31 |
-
return [(voice['name'], voice['voice_id']) for voice in response.json().get('voices', [])]
|
32 |
-
|
33 |
def get_video_models():
|
34 |
return [f for f in os.listdir("models") if f.endswith((".mp4", ".avi", ".mov"))]
|
35 |
|
36 |
-
def text_to_speech(voice_id, text, session_id):
|
37 |
-
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"
|
38 |
-
|
39 |
-
headers = {
|
40 |
-
"Accept": "audio/mpeg",
|
41 |
-
"Content-Type": "application/json",
|
42 |
-
"xi-api-key": A_KEY
|
43 |
-
}
|
44 |
-
|
45 |
-
data = {
|
46 |
-
"text": text,
|
47 |
-
"model_id": "eleven_turbo_v2_5",
|
48 |
-
"voice_settings": {
|
49 |
-
"stability": 0.5,
|
50 |
-
"similarity_boost": 0.5
|
51 |
-
}
|
52 |
-
}
|
53 |
-
|
54 |
-
response = requests.post(url, json=data, headers=headers)
|
55 |
-
if response.status_code != 200:
|
56 |
-
return None
|
57 |
-
|
58 |
-
# Save temporary audio file with session ID
|
59 |
-
audio_file_path = f'temp_voice_{session_id}.mp3'
|
60 |
-
with open(audio_file_path, 'wb') as audio_file:
|
61 |
-
audio_file.write(response.content)
|
62 |
-
return audio_file_path
|
63 |
-
|
64 |
def upload_file(file_path):
|
65 |
with open(file_path, 'rb') as file:
|
66 |
files = {'fileToUpload': (os.path.basename(file_path), file)}
|
@@ -139,15 +98,12 @@ def combine_audio_video(video_path, audio_path, output_path):
|
|
139 |
|
140 |
subprocess.run(cmd, check=True)
|
141 |
|
142 |
-
def process_video(
|
143 |
session_id = str(uuid.uuid4()) # Generate a unique session ID
|
144 |
-
progress(0, desc="Generating speech...")
|
145 |
-
audio_path = text_to_speech(voice, text, session_id)
|
146 |
-
if not audio_path:
|
147 |
-
return None, "Failed to generate speech audio."
|
148 |
|
149 |
progress(0.2, desc="Processing video...")
|
150 |
video_path = os.path.join("models", model)
|
|
|
151 |
|
152 |
try:
|
153 |
progress(0.3, desc="Uploading files...")
|
@@ -188,36 +144,29 @@ def process_video(voice, model, text, progress=gr.Progress()):
|
|
188 |
return output_path, f"Used fallback method. Original error: {str(e)}"
|
189 |
except Exception as fallback_error:
|
190 |
return None, f"All methods failed. Error: {str(fallback_error)}"
|
191 |
-
finally:
|
192 |
-
# Cleanup
|
193 |
-
if os.path.exists(audio_path):
|
194 |
-
os.remove(audio_path)
|
195 |
|
196 |
def create_interface():
|
197 |
-
voices = get_voices()
|
198 |
models = get_video_models()
|
199 |
|
200 |
with gr.Blocks() as app:
|
201 |
-
gr.Markdown("#
|
202 |
with gr.Row():
|
203 |
with gr.Column():
|
204 |
-
|
205 |
-
|
206 |
-
text_input = gr.Textbox(label="Enter text", lines=3)
|
207 |
generate_btn = gr.Button("Generate Video")
|
208 |
with gr.Column():
|
209 |
video_output = gr.Video(label="Generated Video")
|
210 |
status_output = gr.Textbox(label="Status", interactive=False)
|
211 |
|
212 |
-
def on_generate(
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
return process_video(voice_id, model_name, text)
|
217 |
|
218 |
generate_btn.click(
|
219 |
fn=on_generate,
|
220 |
-
inputs=[
|
221 |
outputs=[video_output, status_output]
|
222 |
)
|
223 |
|
@@ -225,4 +174,4 @@ def create_interface():
|
|
225 |
|
226 |
if __name__ == "__main__":
|
227 |
app = create_interface()
|
228 |
-
app.launch()
|
|
|
11 |
load_dotenv()
|
12 |
|
13 |
# API Keys
|
|
|
14 |
B_KEY = os.getenv("B_KEY")
|
15 |
|
16 |
# URLs
|
17 |
API_URL = os.getenv("API_URL")
|
18 |
UPLOAD_URL = os.getenv("UPLOAD_URL")
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def get_video_models():
|
21 |
return [f for f in os.listdir("models") if f.endswith((".mp4", ".avi", ".mov"))]
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def upload_file(file_path):
|
24 |
with open(file_path, 'rb') as file:
|
25 |
files = {'fileToUpload': (os.path.basename(file_path), file)}
|
|
|
98 |
|
99 |
subprocess.run(cmd, check=True)
|
100 |
|
101 |
+
def process_video(model, audio_file, progress=gr.Progress()):
|
102 |
session_id = str(uuid.uuid4()) # Generate a unique session ID
|
|
|
|
|
|
|
|
|
103 |
|
104 |
progress(0.2, desc="Processing video...")
|
105 |
video_path = os.path.join("models", model)
|
106 |
+
audio_path = audio_file.name # Get the path of the uploaded audio file
|
107 |
|
108 |
try:
|
109 |
progress(0.3, desc="Uploading files...")
|
|
|
144 |
return output_path, f"Used fallback method. Original error: {str(e)}"
|
145 |
except Exception as fallback_error:
|
146 |
return None, f"All methods failed. Error: {str(fallback_error)}"
|
|
|
|
|
|
|
|
|
147 |
|
148 |
def create_interface():
|
|
|
149 |
models = get_video_models()
|
150 |
|
151 |
with gr.Blocks() as app:
|
152 |
+
gr.Markdown("# Video Lip Sync")
|
153 |
with gr.Row():
|
154 |
with gr.Column():
|
155 |
+
model_dropdown = gr.Dropdown(choices=models, label="Select Video Model", value=models[0] if models else None)
|
156 |
+
audio_input = gr.Audio(label="Upload Audio File", type="filepath")
|
|
|
157 |
generate_btn = gr.Button("Generate Video")
|
158 |
with gr.Column():
|
159 |
video_output = gr.Video(label="Generated Video")
|
160 |
status_output = gr.Textbox(label="Status", interactive=False)
|
161 |
|
162 |
+
def on_generate(model_name, audio_file):
|
163 |
+
if not audio_file:
|
164 |
+
return None, "Please upload an audio file."
|
165 |
+
return process_video(model_name, audio_file)
|
|
|
166 |
|
167 |
generate_btn.click(
|
168 |
fn=on_generate,
|
169 |
+
inputs=[model_dropdown, audio_input],
|
170 |
outputs=[video_output, status_output]
|
171 |
)
|
172 |
|
|
|
174 |
|
175 |
if __name__ == "__main__":
|
176 |
app = create_interface()
|
177 |
+
app.launch()
|