Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,32 +24,54 @@ import time # Added for history update delay
|
|
| 24 |
|
| 25 |
from gradio_client import Client, handle_file
|
| 26 |
import tempfile
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
def turn_into_video(input_images, output_images, prompt):
|
| 29 |
"""Calls multimodalart/wan-2-2-first-last-frame space to generate a video."""
|
| 30 |
if not input_images or not output_images:
|
| 31 |
-
raise gr.Error("Please generate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
start_img = input_images[0][0] if isinstance(input_images[0], tuple) else input_images[0]
|
| 35 |
-
end_img = output_images[0]
|
| 36 |
|
| 37 |
-
# Save them temporarily
|
| 38 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_start, \
|
| 39 |
tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_end:
|
| 40 |
start_img.save(tmp_start.name)
|
| 41 |
end_img.save(tmp_end.name)
|
| 42 |
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
result = client.predict(
|
| 47 |
start_image_pil={"image": handle_file(tmp_start.name)},
|
| 48 |
end_image_pil={"image": handle_file(tmp_end.name)},
|
| 49 |
-
prompt=prompt or "
|
| 50 |
-
|
|
|
|
| 51 |
)
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
SYSTEM_PROMPT = '''
|
|
@@ -650,9 +672,13 @@ with gr.Blocks(css=css) as demo:
|
|
| 650 |
input_images.change(fn=suggest_next_scene_prompt, inputs=[input_images], outputs=[prompt])
|
| 651 |
|
| 652 |
turn_video_btn.click(
|
| 653 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
inputs=[input_images, result, prompt],
|
| 655 |
-
outputs=[output_video
|
| 656 |
)
|
| 657 |
|
| 658 |
|
|
|
|
| 24 |
|
| 25 |
from gradio_client import Client, handle_file
|
| 26 |
import tempfile
|
| 27 |
+
from PIL import Image
|
| 28 |
+
import os
|
| 29 |
+
import gradio as gr
|
| 30 |
|
| 31 |
+
def turn_into_video(input_images, output_images, prompt, progress=gr.Progress(track_tqdm=True)):
|
| 32 |
"""Calls multimodalart/wan-2-2-first-last-frame space to generate a video."""
|
| 33 |
if not input_images or not output_images:
|
| 34 |
+
raise gr.Error("Please generate an output image first.")
|
| 35 |
+
|
| 36 |
+
progress(0.02, desc="Preparing images...")
|
| 37 |
+
|
| 38 |
+
# Safely extract PIL images from Gradio galleries
|
| 39 |
+
def extract_pil(img_entry):
|
| 40 |
+
if isinstance(img_entry, tuple) and isinstance(img_entry[0], Image.Image):
|
| 41 |
+
return img_entry[0]
|
| 42 |
+
elif isinstance(img_entry, Image.Image):
|
| 43 |
+
return img_entry
|
| 44 |
+
elif isinstance(img_entry, str):
|
| 45 |
+
return Image.open(img_entry)
|
| 46 |
+
else:
|
| 47 |
+
raise gr.Error(f"Unsupported image format: {type(img_entry)}")
|
| 48 |
+
|
| 49 |
+
start_img = extract_pil(input_images[0])
|
| 50 |
+
end_img = extract_pil(output_images[0])
|
| 51 |
|
| 52 |
+
progress(0.10, desc="Saving temp files...")
|
|
|
|
|
|
|
| 53 |
|
|
|
|
| 54 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_start, \
|
| 55 |
tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_end:
|
| 56 |
start_img.save(tmp_start.name)
|
| 57 |
end_img.save(tmp_end.name)
|
| 58 |
|
| 59 |
+
progress(0.20, desc="Connecting to Wan space...")
|
| 60 |
|
| 61 |
+
client = Client("multimodalart/wan-2-2-first-last-frame")
|
| 62 |
+
|
| 63 |
+
progress(0.35, desc="generating video...")
|
| 64 |
result = client.predict(
|
| 65 |
start_image_pil={"image": handle_file(tmp_start.name)},
|
| 66 |
end_image_pil={"image": handle_file(tmp_end.name)},
|
| 67 |
+
prompt=prompt or "smooth cinematic transition",
|
| 68 |
+
|
| 69 |
+
api_name="/generate_video"
|
| 70 |
)
|
| 71 |
+
|
| 72 |
+
progress(0.95, desc="Finalizing...")
|
| 73 |
+
return result
|
| 74 |
+
|
| 75 |
|
| 76 |
|
| 77 |
SYSTEM_PROMPT = '''
|
|
|
|
| 672 |
input_images.change(fn=suggest_next_scene_prompt, inputs=[input_images], outputs=[prompt])
|
| 673 |
|
| 674 |
turn_video_btn.click(
|
| 675 |
+
fn=lambda: gr.update(visible=True),
|
| 676 |
+
inputs=None,
|
| 677 |
+
outputs=[output_video],
|
| 678 |
+
).then(
|
| 679 |
+
fn=turn_into_video,
|
| 680 |
inputs=[input_images, result, prompt],
|
| 681 |
+
outputs=[output_video],
|
| 682 |
)
|
| 683 |
|
| 684 |
|