Spaces:
Running
Running
Remove temp folder, add func
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import os
|
2 |
import sys
|
3 |
import subprocess
|
4 |
-
import tempfile
|
5 |
import base64
|
|
|
6 |
from io import BytesIO
|
7 |
|
8 |
import streamlit as st
|
@@ -36,6 +36,15 @@ def ensure_scripts_exist():
|
|
36 |
return False, error_msg
|
37 |
return True, ""
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def main():
|
40 |
# ---------------- CUSTOM CSS FOR A PROFESSIONAL, DARK THEME ----------------
|
41 |
st.markdown(
|
@@ -204,37 +213,31 @@ def main():
|
|
204 |
"Custom βοΈ"
|
205 |
],
|
206 |
index=0,
|
207 |
-
label_visibility="collapsed"
|
208 |
)
|
209 |
|
210 |
# Determine preset defaults based on selection
|
211 |
if preset_option.startswith("Maximum quality"):
|
212 |
-
# "Maximum quality, highest inference time π"
|
213 |
preset_model = "Base Stable Diffusion V2-1"
|
214 |
preset_film = True
|
215 |
preset_lcm = False
|
216 |
elif preset_option.startswith("Medium quality"):
|
217 |
-
# "Medium quality, medium inference time βοΈ"
|
218 |
preset_model = "Base Stable Diffusion V2-1"
|
219 |
preset_film = False
|
220 |
preset_lcm = False
|
221 |
elif preset_option.startswith("Low quality"):
|
222 |
-
# "Low quality, lowest inference time β‘"
|
223 |
preset_model = "Base Stable Diffusion V1-5"
|
224 |
preset_film = False
|
225 |
preset_lcm = True
|
226 |
elif preset_option.startswith("Creative morph"):
|
227 |
-
# "Creative morph π¨"
|
228 |
preset_model = "Dreamshaper-7 (fine-tuned SD V1-5)"
|
229 |
preset_film = True
|
230 |
preset_lcm = True
|
231 |
else:
|
232 |
-
# "Custom βοΈ"
|
233 |
preset_model = None
|
234 |
preset_film = None
|
235 |
preset_lcm = None
|
236 |
|
237 |
-
# Auto-expand advanced options if "Custom βοΈ" is chosen
|
238 |
advanced_expanded = True if preset_option.endswith("βοΈ") else False
|
239 |
|
240 |
# Advanced Options for fine-tuning
|
@@ -253,15 +256,12 @@ def main():
|
|
253 |
with col_left:
|
254 |
st.markdown("##### Keyframe Generator Parameters")
|
255 |
num_frames = st.number_input("Number of keyframes (2β50)", min_value=2, max_value=50, value=16)
|
256 |
-
# Note: LCM compatibility check updated
|
257 |
lcm_default = preset_lcm if preset_lcm is not None else False
|
258 |
-
|
259 |
enable_lcm_lora = st.checkbox(
|
260 |
"Enable LCM-LoRA",
|
261 |
value=lcm_default,
|
262 |
help="Accelerates inference with slight quality decrease"
|
263 |
)
|
264 |
-
|
265 |
use_adain = st.checkbox("Use AdaIN", value=True, help="Adaptive Instance Normalization for improved generation")
|
266 |
use_reschedule = st.checkbox("Use reschedule sampling", value=True, help="Better sampling strategy")
|
267 |
keyframe_duration = st.number_input("Keyframe Duration (seconds, only if not using FILM)", min_value=0.01, max_value=5.0, value=0.1, step=0.01)
|
@@ -282,123 +282,122 @@ def main():
|
|
282 |
# ---------------- SECTION 3: EXECUTE MORPH PIPELINE ----------------
|
283 |
st.subheader("3. Generate Morphing Video")
|
284 |
st.markdown("Once satisfied with your inputs, click below to start the process.")
|
285 |
-
|
|
|
|
|
|
|
286 |
if st.button("Run Morphing Pipeline", key="run_pipeline"):
|
287 |
-
# Validate inputs
|
288 |
if not (uploaded_image_A and uploaded_image_B):
|
289 |
st.error("Please upload both images before running the morphing pipeline.")
|
290 |
return
|
291 |
|
292 |
-
#
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
330 |
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
# Update progress status
|
351 |
-
for i in range(1, 11):
|
352 |
-
status_text.text(f"Step {i}/10: {'Preparing images' if i <= 2 else 'Generating keyframes' if i <= 6 else 'Interpolating frames' if i <= 9 else 'Finalizing video'}")
|
353 |
-
progress_bar.progress(i * 10)
|
354 |
-
|
355 |
-
if i == 3: # Start actual processing
|
356 |
-
try:
|
357 |
-
subprocess.run(cmd, check=True)
|
358 |
-
except subprocess.CalledProcessError as e:
|
359 |
-
st.error(f"Error running morphing pipeline: {e}")
|
360 |
-
return
|
361 |
-
break
|
362 |
-
|
363 |
-
# Set progress to 100% when done
|
364 |
-
progress_bar.progress(100)
|
365 |
-
status_text.text("Processing complete!")
|
366 |
-
|
367 |
-
# Check for output video
|
368 |
-
video_found = False
|
369 |
-
|
370 |
-
# Always check FILM output directory first
|
371 |
-
possible_outputs = [f for f in os.listdir(film_output_dir) if f.endswith(".mp4")]
|
372 |
if possible_outputs:
|
373 |
-
final_video_path = os.path.join(
|
374 |
video_found = True
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
file_name="metamorph_result.mp4",
|
393 |
-
mime="video/mp4"
|
394 |
-
)
|
395 |
-
except Exception as e:
|
396 |
-
st.error(f"Error preparing video for download: {e}")
|
397 |
-
else:
|
398 |
-
st.warning("No output video was generated. Check logs for details.")
|
399 |
|
400 |
-
|
401 |
-
|
402 |
|
403 |
if __name__ == "__main__":
|
404 |
main()
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
import subprocess
|
|
|
4 |
import base64
|
5 |
+
import datetime
|
6 |
from io import BytesIO
|
7 |
|
8 |
import streamlit as st
|
|
|
36 |
return False, error_msg
|
37 |
return True, ""
|
38 |
|
39 |
+
def create_temp_folder():
|
40 |
+
"""Create a persistent temporary folder in the repo for processing."""
|
41 |
+
base_folder = os.path.join(os.getcwd(), "temp_run")
|
42 |
+
os.makedirs(base_folder, exist_ok=True)
|
43 |
+
# Create a subfolder with a timestamp to avoid collisions
|
44 |
+
run_folder = os.path.join(base_folder, datetime.datetime.now().strftime("run_%Y%m%d_%H%M%S"))
|
45 |
+
os.makedirs(run_folder)
|
46 |
+
return run_folder
|
47 |
+
|
48 |
def main():
|
49 |
# ---------------- CUSTOM CSS FOR A PROFESSIONAL, DARK THEME ----------------
|
50 |
st.markdown(
|
|
|
213 |
"Custom βοΈ"
|
214 |
],
|
215 |
index=0,
|
216 |
+
label_visibility="collapsed"
|
217 |
)
|
218 |
|
219 |
# Determine preset defaults based on selection
|
220 |
if preset_option.startswith("Maximum quality"):
|
|
|
221 |
preset_model = "Base Stable Diffusion V2-1"
|
222 |
preset_film = True
|
223 |
preset_lcm = False
|
224 |
elif preset_option.startswith("Medium quality"):
|
|
|
225 |
preset_model = "Base Stable Diffusion V2-1"
|
226 |
preset_film = False
|
227 |
preset_lcm = False
|
228 |
elif preset_option.startswith("Low quality"):
|
|
|
229 |
preset_model = "Base Stable Diffusion V1-5"
|
230 |
preset_film = False
|
231 |
preset_lcm = True
|
232 |
elif preset_option.startswith("Creative morph"):
|
|
|
233 |
preset_model = "Dreamshaper-7 (fine-tuned SD V1-5)"
|
234 |
preset_film = True
|
235 |
preset_lcm = True
|
236 |
else:
|
|
|
237 |
preset_model = None
|
238 |
preset_film = None
|
239 |
preset_lcm = None
|
240 |
|
|
|
241 |
advanced_expanded = True if preset_option.endswith("βοΈ") else False
|
242 |
|
243 |
# Advanced Options for fine-tuning
|
|
|
256 |
with col_left:
|
257 |
st.markdown("##### Keyframe Generator Parameters")
|
258 |
num_frames = st.number_input("Number of keyframes (2β50)", min_value=2, max_value=50, value=16)
|
|
|
259 |
lcm_default = preset_lcm if preset_lcm is not None else False
|
|
|
260 |
enable_lcm_lora = st.checkbox(
|
261 |
"Enable LCM-LoRA",
|
262 |
value=lcm_default,
|
263 |
help="Accelerates inference with slight quality decrease"
|
264 |
)
|
|
|
265 |
use_adain = st.checkbox("Use AdaIN", value=True, help="Adaptive Instance Normalization for improved generation")
|
266 |
use_reschedule = st.checkbox("Use reschedule sampling", value=True, help="Better sampling strategy")
|
267 |
keyframe_duration = st.number_input("Keyframe Duration (seconds, only if not using FILM)", min_value=0.01, max_value=5.0, value=0.1, step=0.01)
|
|
|
282 |
# ---------------- SECTION 3: EXECUTE MORPH PIPELINE ----------------
|
283 |
st.subheader("3. Generate Morphing Video")
|
284 |
st.markdown("Once satisfied with your inputs, click below to start the process.")
|
285 |
+
|
286 |
+
# New checkbox for SLAB execution toggle
|
287 |
+
using_slab = st.checkbox("Using SLAB GPU Cluster?", value=False, help="If enabled, the pipeline command will be prefixed with SLAB cluster execution parameters.")
|
288 |
+
|
289 |
if st.button("Run Morphing Pipeline", key="run_pipeline"):
|
|
|
290 |
if not (uploaded_image_A and uploaded_image_B):
|
291 |
st.error("Please upload both images before running the morphing pipeline.")
|
292 |
return
|
293 |
|
294 |
+
# Instead of using /tmp, create a folder in the repo for temporary processing.
|
295 |
+
temp_dir = create_temp_folder()
|
296 |
+
|
297 |
+
try:
|
298 |
+
# Save uploaded images
|
299 |
+
imgA_path = os.path.join(temp_dir, "imageA.png")
|
300 |
+
imgB_path = os.path.join(temp_dir, "imageB.png")
|
301 |
+
save_uploaded_file(uploaded_image_A, imgA_path)
|
302 |
+
save_uploaded_file(uploaded_image_B, imgB_path)
|
303 |
+
|
304 |
+
# Create output directories
|
305 |
+
output_dir = os.path.join(temp_dir, "morph_results")
|
306 |
+
film_output_dir = os.path.join(temp_dir, "film_output")
|
307 |
+
os.makedirs(output_dir, exist_ok=True)
|
308 |
+
os.makedirs(film_output_dir, exist_ok=True)
|
309 |
+
|
310 |
+
duration_ms = int(keyframe_duration * 1000)
|
311 |
+
|
312 |
+
actual_model_path = (
|
313 |
+
"lykon/dreamshaper-7" if model_option == "Dreamshaper-7 (fine-tuned SD V1-5)"
|
314 |
+
else "stabilityai/stable-diffusion-2-1-base" if model_option == "Base Stable Diffusion V2-1"
|
315 |
+
else "sd-legacy/stable-diffusion-v1-5"
|
316 |
+
)
|
317 |
+
|
318 |
+
# Build the command for run_morphing.py
|
319 |
+
cmd = [
|
320 |
+
sys.executable, "run_morphing.py",
|
321 |
+
"--model_path", actual_model_path,
|
322 |
+
"--image_path_0", imgA_path,
|
323 |
+
"--image_path_1", imgB_path,
|
324 |
+
"--prompt_0", prompt_A,
|
325 |
+
"--prompt_1", prompt_B,
|
326 |
+
"--output_path", output_dir,
|
327 |
+
"--film_output_folder", film_output_dir,
|
328 |
+
"--num_frames", str(num_frames),
|
329 |
+
"--duration", str(duration_ms)
|
330 |
+
]
|
331 |
+
|
332 |
+
if enable_lcm_lora:
|
333 |
+
cmd.append("--use_lcm")
|
334 |
+
if use_adain:
|
335 |
+
cmd.append("--use_adain")
|
336 |
+
if use_reschedule:
|
337 |
+
cmd.append("--use_reschedule")
|
338 |
+
if use_film:
|
339 |
+
cmd.append("--use_film")
|
340 |
+
|
341 |
+
cmd.extend(["--film_fps", str(film_fps)])
|
342 |
+
cmd.extend(["--film_num_recursions", str(film_recursions)])
|
343 |
+
|
344 |
+
if using_slab:
|
345 |
+
slab_prefix = [
|
346 |
+
"srun", "-p", "rtx3090_slab", "-w", "slabgpu05", "--gres=gpu:1",
|
347 |
+
"--job-name=test", "--kill-on-bad-exit=1"
|
348 |
]
|
349 |
+
cmd = slab_prefix + cmd
|
350 |
+
|
351 |
+
st.info("Initializing pipeline. This may take a few minutes...")
|
352 |
+
progress_bar = st.progress(0)
|
353 |
+
status_text = st.empty()
|
354 |
+
|
355 |
+
for i in range(1, 11):
|
356 |
+
status_text.text(f"Step {i}/10: {'Preparing images' if i <= 2 else 'Generating keyframes' if i <= 6 else 'Interpolating frames' if i <= 9 else 'Finalizing video'}")
|
357 |
+
progress_bar.progress(i * 10)
|
358 |
|
359 |
+
if i == 3:
|
360 |
+
try:
|
361 |
+
subprocess.run(cmd, check=True)
|
362 |
+
except subprocess.CalledProcessError as e:
|
363 |
+
st.error(f"Error running morphing pipeline: {e}")
|
364 |
+
return
|
365 |
+
break
|
366 |
+
|
367 |
+
progress_bar.progress(100)
|
368 |
+
status_text.text("Processing complete!")
|
369 |
+
|
370 |
+
video_found = False
|
371 |
+
possible_outputs = [f for f in os.listdir(film_output_dir) if f.endswith(".mp4")]
|
372 |
+
if possible_outputs:
|
373 |
+
final_video_path = os.path.join(film_output_dir, possible_outputs[0])
|
374 |
+
video_found = True
|
375 |
+
|
376 |
+
if not video_found:
|
377 |
+
possible_outputs = [f for f in os.listdir(output_dir) if f.endswith(".mp4")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
378 |
if possible_outputs:
|
379 |
+
final_video_path = os.path.join(output_dir, possible_outputs[0])
|
380 |
video_found = True
|
381 |
+
|
382 |
+
if video_found:
|
383 |
+
st.success("Morphing complete! π")
|
384 |
+
st.video(final_video_path)
|
385 |
+
try:
|
386 |
+
with open(final_video_path, "rb") as f:
|
387 |
+
video_bytes = f.read()
|
388 |
+
st.download_button(
|
389 |
+
"Download Result Video",
|
390 |
+
data=video_bytes,
|
391 |
+
file_name="metamorph_result.mp4",
|
392 |
+
mime="video/mp4"
|
393 |
+
)
|
394 |
+
except Exception as e:
|
395 |
+
st.error(f"Error preparing video for download: {e}")
|
396 |
+
else:
|
397 |
+
st.warning("No output video was generated. Check logs for details.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
398 |
|
399 |
+
except Exception as e:
|
400 |
+
st.error(f"An error occurred during processing: {e}")
|
401 |
|
402 |
if __name__ == "__main__":
|
403 |
main()
|