Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,110 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
import numpy as np
|
4 |
import modin.pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from PIL import Image
|
6 |
-
|
|
|
|
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
|
9 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
10 |
torch.cuda.max_memory_allocated(device=device)
|
11 |
torch.cuda.empty_cache()
|
12 |
-
pipe =
|
13 |
-
|
14 |
torch.cuda.empty_cache()
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
def genie (Prompt, negative_prompt, height, width, scale, steps, seed):
|
23 |
-
generator = np.random.seed(0) if seed == 0 else torch.manual_seed(seed)
|
24 |
-
#generator=np.random.seed(0)
|
25 |
-
int_image = pipe(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale, output_type="latent").images
|
26 |
-
image = refiner(Prompt, negative_prompt=negative_prompt, image=int_image, denoising_start=.99).images[0]
|
27 |
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import numpy as np
|
3 |
import modin.pandas as pd
|
4 |
+
import torch
|
5 |
+
import os
|
6 |
+
from glob import glob
|
7 |
+
from pathlib import Path
|
8 |
+
from typing import Optional
|
9 |
+
from diffusers import LTXImageToVideoPipeline
|
10 |
+
from diffusers.utils import load_image, export_to_video
|
11 |
from PIL import Image
|
12 |
+
|
13 |
+
import uuid
|
14 |
+
import random
|
15 |
from huggingface_hub import hf_hub_download
|
16 |
|
17 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
18 |
torch.cuda.max_memory_allocated(device=device)
|
19 |
torch.cuda.empty_cache()
|
20 |
+
pipe = LTXImageToVideoPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16)
|
21 |
+
pipe.to(device)
|
22 |
torch.cuda.empty_cache()
|
23 |
+
max_64_bit_int = 2**63 - 1
|
24 |
|
25 |
+
def sample(
|
26 |
+
image: Image, prompt, negative_prompt,
|
27 |
+
seed: Optional[int] = 42,
|
28 |
+
randomize_seed: bool = True,
|
29 |
+
num_inference_steps: int = 25,
|
30 |
+
num_frames: int = 60,
|
31 |
+
fps_id: int = 12,
|
32 |
+
decode_noise_scale: float = 0.3,
|
33 |
+
max_sequence_length: int = 512,
|
34 |
+
decoding_t: int = 3, # Number of frames decoded at a time! This eats most VRAM. Reduce if necessary.
|
35 |
+
device: str = "cuda",
|
36 |
+
output_folder: str = "outputs",
|
37 |
+
):
|
38 |
+
if image.mode == "RGBA":
|
39 |
+
image = image.convert("RGB")
|
40 |
+
|
41 |
+
if(randomize_seed):
|
42 |
+
seed = random.randint(0, max_64_bit_int)
|
43 |
+
torch.cuda.max_memory_allocated(device=device)
|
44 |
+
torch.cuda.empty_cache()
|
45 |
+
generator = torch.manual_seed(seed)
|
46 |
+
|
47 |
+
os.makedirs(output_folder, exist_ok=True)
|
48 |
+
base_count = len(glob(os.path.join(output_folder, "*.mp4")))
|
49 |
+
video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
|
50 |
+
video = pipe(image=image, prompt=prompt, negative_prompt=negative_prompt, width=768, height=512, num_frames=num_frames, num_inference_steps=num_inference_steps, guidance_scale=3.5, decode_noise_scale=0.3, max_sequence_length=512).frames[0]
|
51 |
+
export_to_video(video, video_path, fps=fps_id)
|
52 |
+
torch.manual_seed(seed)
|
53 |
+
torch.cuda.empty_cache()
|
54 |
+
return video_path, seed
|
55 |
+
|
56 |
+
def resize_image(image, output_size=(768, 512)):
|
57 |
+
# Calculate aspect ratios
|
58 |
+
target_aspect = output_size[0] / output_size[1] # Aspect ratio of the desired size
|
59 |
+
image_aspect = image.width / image.height # Aspect ratio of the original image
|
60 |
|
61 |
+
# Resize then crop if the original image is larger
|
62 |
+
if image_aspect > target_aspect:
|
63 |
+
# Resize the image to match the target height, maintaining aspect ratio
|
64 |
+
new_height = output_size[1]
|
65 |
+
new_width = int(new_height * image_aspect)
|
66 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
67 |
+
# Calculate coordinates for cropping
|
68 |
+
left = (new_width - output_size[0]) / 2
|
69 |
+
top = 0
|
70 |
+
right = (new_width + output_size[0]) / 2
|
71 |
+
bottom = output_size[1]
|
72 |
+
else:
|
73 |
+
# Resize the image to match the target width, maintaining aspect ratio
|
74 |
+
new_width = output_size[0]
|
75 |
+
new_height = int(new_width / image_aspect)
|
76 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
77 |
+
# Calculate coordinates for cropping
|
78 |
+
left = 0
|
79 |
+
top = (new_height - output_size[1]) / 2
|
80 |
+
right = output_size[0]
|
81 |
+
bottom = (new_height + output_size[1]) / 2
|
82 |
|
|
|
|
|
|
|
|
|
|
|
83 |
torch.cuda.empty_cache()
|
84 |
+
cropped_image = resized_image.crop((left, top, right, bottom))
|
85 |
+
return cropped_image
|
86 |
+
|
87 |
+
with gr.Blocks() as demo:
|
88 |
+
|
89 |
+
with gr.Row():
|
90 |
+
with gr.Column():
|
91 |
+
image = gr.Image(label="Upload your image", type="pil")
|
92 |
+
generate_btn = gr.Button("Generate")
|
93 |
|
94 |
+
video = gr.Video()
|
95 |
+
with gr.Accordion("Advanced options", open=False):
|
96 |
+
seed = gr.Slider(label="Seed", value=42, randomize=True, minimum=0, maximum=max_64_bit_int, step=1)
|
97 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
+
prompt=gr.Textbox(label="Prompt")
|
99 |
+
negative_prompt=gr.Textbox(label="Negative Prompt", value="Blur, Motion Blur, Artifacts, Motion Artifacts, Poor Quality, Low Resolution, Smudging, Streaks, Motion Streaks, Pixels, Pixelated, Ugly, Mutation, Mutated")
|
100 |
+
num_inference_steps=gr.Slider(label="Number of Inference Steps", value=25, minimum=25, maximum=100, step=25)
|
101 |
+
num_frames=gr.Slider(label = "Number of Frames", value=60, minimum=30, maximum=120)
|
102 |
+
fps_id = gr.Slider(label="Frames per second", info="The length of your video in seconds will be frames/fps", value=12, minimum=5, maximum=30, step=1)
|
103 |
+
|
104 |
+
image.upload(fn=resize_image, inputs=image, outputs=image, queue=False)
|
105 |
+
generate_btn.click(fn=sample, inputs=[image, prompt, negative_prompt, seed, randomize_seed, num_inference_steps, num_frames, fps_id], outputs=[video, seed], api_name="video")
|
106 |
+
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
demo.queue(max_size=20, api_open=False)
|
110 |
+
demo.launch(show_api=False)
|