Update app.py
Browse files
app.py
CHANGED
@@ -55,23 +55,12 @@ def text_to_speech(voice_id, text, session_id):
|
|
55 |
if response.status_code != 200:
|
56 |
return None
|
57 |
|
|
|
58 |
audio_file_path = f'temp_voice_{session_id}.mp3'
|
59 |
with open(audio_file_path, 'wb') as audio_file:
|
60 |
audio_file.write(response.content)
|
61 |
return audio_file_path
|
62 |
|
63 |
-
def handle_uploaded_audio(audio_file, session_id):
|
64 |
-
if audio_file is None:
|
65 |
-
return None
|
66 |
-
|
67 |
-
temp_path = f'temp_voice_{session_id}.mp3'
|
68 |
-
if isinstance(audio_file, str): # If it's already a path
|
69 |
-
os.rename(audio_file, temp_path)
|
70 |
-
else: # If it's a file object from Gradio
|
71 |
-
with open(temp_path, 'wb') as f:
|
72 |
-
f.write(audio_file)
|
73 |
-
return temp_path
|
74 |
-
|
75 |
def upload_file(file_path):
|
76 |
with open(file_path, 'rb') as file:
|
77 |
files = {'fileToUpload': (os.path.basename(file_path), file)}
|
@@ -103,7 +92,7 @@ def lipsync_api_call(video_url, audio_url):
|
|
103 |
|
104 |
def check_job_status(job_id):
|
105 |
headers = {"x-api-key": B_KEY}
|
106 |
-
max_attempts = 30
|
107 |
|
108 |
for _ in range(max_attempts):
|
109 |
response = requests.get(f"{API_URL}/{job_id}", headers=headers)
|
@@ -126,20 +115,12 @@ def combine_audio_video(video_path, audio_path, output_path):
|
|
126 |
]
|
127 |
subprocess.run(cmd, check=True)
|
128 |
|
129 |
-
def process_video(voice, model, text,
|
130 |
-
session_id = str(uuid.uuid4())
|
131 |
-
|
132 |
-
|
133 |
-
if
|
134 |
-
|
135 |
-
audio_path = text_to_speech(voice, text, session_id)
|
136 |
-
if not audio_path:
|
137 |
-
return None, "Failed to generate speech audio."
|
138 |
-
else: # audio upload
|
139 |
-
progress(0, desc="Processing uploaded audio...")
|
140 |
-
audio_path = handle_uploaded_audio(audio_file, session_id)
|
141 |
-
if not audio_path:
|
142 |
-
return None, "Failed to process uploaded audio."
|
143 |
|
144 |
progress(0.2, desc="Processing video...")
|
145 |
video_path = os.path.join("models", model)
|
@@ -196,57 +177,23 @@ def create_interface():
|
|
196 |
gr.Markdown("# JSON Train")
|
197 |
with gr.Row():
|
198 |
with gr.Column():
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
value="text"
|
203 |
-
)
|
204 |
-
|
205 |
-
# Text-to-speech components
|
206 |
-
with gr.Group() as text_group:
|
207 |
-
voice_dropdown = gr.Dropdown(
|
208 |
-
choices=[v[0] for v in voices],
|
209 |
-
label="Select Voice",
|
210 |
-
value=voices[0][0] if voices else None
|
211 |
-
)
|
212 |
-
text_input = gr.Textbox(label="Enter text", lines=3)
|
213 |
-
|
214 |
-
# Audio upload component
|
215 |
-
with gr.Group() as audio_group:
|
216 |
-
audio_input = gr.Audio(label="Upload Audio File", type="filepath")
|
217 |
-
|
218 |
-
model_dropdown = gr.Dropdown(
|
219 |
-
choices=models,
|
220 |
-
label="Select Video Model",
|
221 |
-
value=models[0] if models else None
|
222 |
-
)
|
223 |
generate_btn = gr.Button("Generate Video")
|
224 |
-
|
225 |
with gr.Column():
|
226 |
video_output = gr.Video(label="Generated Video")
|
227 |
status_output = gr.Textbox(label="Status", interactive=False)
|
228 |
|
229 |
-
def
|
230 |
-
return (
|
231 |
-
gr.Group.update(visible=(choice == "text")),
|
232 |
-
gr.Group.update(visible=(choice == "audio"))
|
233 |
-
)
|
234 |
-
|
235 |
-
input_type.change(
|
236 |
-
fn=toggle_input_groups,
|
237 |
-
inputs=[input_type],
|
238 |
-
outputs=[text_group, audio_group]
|
239 |
-
)
|
240 |
-
|
241 |
-
def on_generate(input_type, voice_name, text, audio_file, model_name):
|
242 |
voice_id = next((v[1] for v in voices if v[0] == voice_name), None)
|
243 |
-
if
|
244 |
return None, "Invalid voice selected."
|
245 |
-
return process_video(voice_id, model_name, text
|
246 |
|
247 |
generate_btn.click(
|
248 |
fn=on_generate,
|
249 |
-
inputs=[
|
250 |
outputs=[video_output, status_output]
|
251 |
)
|
252 |
|
|
|
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)}
|
|
|
92 |
|
93 |
def check_job_status(job_id):
|
94 |
headers = {"x-api-key": B_KEY}
|
95 |
+
max_attempts = 30 # Limit the number of attempts
|
96 |
|
97 |
for _ in range(max_attempts):
|
98 |
response = requests.get(f"{API_URL}/{job_id}", headers=headers)
|
|
|
115 |
]
|
116 |
subprocess.run(cmd, check=True)
|
117 |
|
118 |
+
def process_video(voice, model, text, progress=gr.Progress()):
|
119 |
+
session_id = str(uuid.uuid4()) # Generate a unique session ID
|
120 |
+
progress(0, desc="Generating speech...")
|
121 |
+
audio_path = text_to_speech(voice, text, session_id)
|
122 |
+
if not audio_path:
|
123 |
+
return None, "Failed to generate speech audio."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
progress(0.2, desc="Processing video...")
|
126 |
video_path = os.path.join("models", model)
|
|
|
177 |
gr.Markdown("# JSON Train")
|
178 |
with gr.Row():
|
179 |
with gr.Column():
|
180 |
+
voice_dropdown = gr.Dropdown(choices=[v[0] for v in voices], label="Select", value=voices[0][0] if voices else None)
|
181 |
+
model_dropdown = gr.Dropdown(choices=models, label="Select", value=models[0] if models else None)
|
182 |
+
text_input = gr.Textbox(label="Enter text", lines=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
generate_btn = gr.Button("Generate Video")
|
|
|
184 |
with gr.Column():
|
185 |
video_output = gr.Video(label="Generated Video")
|
186 |
status_output = gr.Textbox(label="Status", interactive=False)
|
187 |
|
188 |
+
def on_generate(voice_name, model_name, text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
voice_id = next((v[1] for v in voices if v[0] == voice_name), None)
|
190 |
+
if not voice_id:
|
191 |
return None, "Invalid voice selected."
|
192 |
+
return process_video(voice_id, model_name, text)
|
193 |
|
194 |
generate_btn.click(
|
195 |
fn=on_generate,
|
196 |
+
inputs=[voice_dropdown, model_dropdown, text_input],
|
197 |
outputs=[video_output, status_output]
|
198 |
)
|
199 |
|