Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
import torch
|
2 |
import gradio as gr
|
|
|
|
|
|
|
3 |
from diffusers import (
|
4 |
StableDiffusionPipeline,
|
5 |
StableDiffusionInstructPix2PixPipeline,
|
@@ -7,59 +10,65 @@ from diffusers import (
|
|
7 |
WanPipeline,
|
8 |
)
|
9 |
from diffusers.utils import export_to_video, load_image
|
10 |
-
import random
|
11 |
-
import numpy as np
|
12 |
|
13 |
-
|
14 |
-
|
|
|
15 |
MAX_SEED = np.iinfo(np.int32).max
|
16 |
|
17 |
-
#
|
18 |
TXT2IMG_PIPE = None
|
19 |
IMG2IMG_PIPE = None
|
20 |
TXT2VID_PIPE = None
|
21 |
IMG2VID_PIPE = None
|
22 |
|
|
|
23 |
def make_pipe(cls, model_id, **kwargs):
|
24 |
pipe = cls.from_pretrained(model_id, torch_dtype=dtype, **kwargs)
|
25 |
-
pipe.
|
26 |
return pipe
|
27 |
|
28 |
-
#
|
29 |
def generate_image_from_text(prompt, seed, randomize_seed):
|
30 |
global TXT2IMG_PIPE
|
31 |
if TXT2IMG_PIPE is None:
|
32 |
-
TXT2IMG_PIPE = make_pipe(StableDiffusionPipeline, "stabilityai/stable-diffusion-2-1-base")
|
33 |
if randomize_seed:
|
34 |
seed = random.randint(0, MAX_SEED)
|
35 |
generator = torch.manual_seed(seed)
|
36 |
image = TXT2IMG_PIPE(prompt=prompt, num_inference_steps=20, generator=generator).images[0]
|
37 |
return image, seed
|
38 |
|
|
|
39 |
def generate_image_from_image_and_prompt(image, prompt, seed, randomize_seed):
|
40 |
global IMG2IMG_PIPE
|
41 |
if IMG2IMG_PIPE is None:
|
42 |
-
IMG2IMG_PIPE = make_pipe(StableDiffusionInstructPix2PixPipeline, "timbrooks/instruct-pix2pix")
|
43 |
if randomize_seed:
|
44 |
seed = random.randint(0, MAX_SEED)
|
45 |
generator = torch.manual_seed(seed)
|
46 |
out = IMG2IMG_PIPE(prompt=prompt, image=image, num_inference_steps=8, generator=generator)
|
47 |
return out.images[0], seed
|
48 |
|
|
|
49 |
def generate_video_from_text(prompt, seed, randomize_seed):
|
50 |
global TXT2VID_PIPE
|
51 |
if TXT2VID_PIPE is None:
|
52 |
-
TXT2VID_PIPE = make_pipe(WanPipeline, "Wan-AI/Wan2.1-T2V-1.3B-Diffusers")
|
53 |
if randomize_seed:
|
54 |
seed = random.randint(0, MAX_SEED)
|
55 |
generator = torch.manual_seed(seed)
|
56 |
frames = TXT2VID_PIPE(prompt=prompt, num_frames=12, generator=generator).frames[0]
|
57 |
return export_to_video(frames, "/tmp/wan_video.mp4", fps=8), seed
|
58 |
|
|
|
59 |
def generate_video_from_image(image, seed, randomize_seed):
|
60 |
global IMG2VID_PIPE
|
61 |
if IMG2VID_PIPE is None:
|
62 |
-
IMG2VID_PIPE = make_pipe(
|
|
|
|
|
|
|
63 |
if randomize_seed:
|
64 |
seed = random.randint(0, MAX_SEED)
|
65 |
generator = torch.manual_seed(seed)
|
@@ -67,65 +76,61 @@ def generate_video_from_image(image, seed, randomize_seed):
|
|
67 |
frames = IMG2VID_PIPE(image=image, num_inference_steps=16, generator=generator).frames[0]
|
68 |
return export_to_video(frames, "/tmp/svd_video.mp4", fps=8), seed
|
69 |
|
70 |
-
#
|
71 |
-
with gr.Blocks(
|
72 |
-
gr.Markdown("# π§ AI Playground β
|
73 |
|
74 |
with gr.Tabs():
|
75 |
# Text β Image
|
76 |
with gr.Tab("Text β Image"):
|
77 |
-
|
78 |
-
|
79 |
-
generate_btn = gr.Button("Generate")
|
80 |
result_img = gr.Image()
|
81 |
seed_txt = gr.Slider(0, MAX_SEED, value=42, label="Seed")
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
inputs=[prompt_txt, seed_txt,
|
86 |
outputs=[result_img, seed_txt]
|
87 |
)
|
88 |
|
89 |
# Image β Image
|
90 |
with gr.Tab("Image β Image"):
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
generate_btn2 = gr.Button("Generate")
|
95 |
result_img2 = gr.Image()
|
96 |
seed_img = gr.Slider(0, MAX_SEED, value=123, label="Seed")
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
inputs=[image_in, prompt_img, seed_img,
|
101 |
outputs=[result_img2, seed_img]
|
102 |
)
|
103 |
|
104 |
# Text β Video
|
105 |
with gr.Tab("Text β Video"):
|
106 |
-
|
107 |
-
|
108 |
-
generate_btn3 = gr.Button("Generate")
|
109 |
result_vid = gr.Video()
|
110 |
seed_vid = gr.Slider(0, MAX_SEED, value=555, label="Seed")
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
inputs=[prompt_vid, seed_vid,
|
115 |
outputs=[result_vid, seed_vid]
|
116 |
)
|
117 |
|
118 |
# Image β Video
|
119 |
with gr.Tab("Image β Video"):
|
120 |
-
|
121 |
-
|
122 |
-
generate_btn4 = gr.Button("Animate")
|
123 |
result_vid2 = gr.Video()
|
124 |
seed_vid2 = gr.Slider(0, MAX_SEED, value=999, label="Seed")
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
inputs=[
|
129 |
outputs=[result_vid2, seed_vid2]
|
130 |
)
|
131 |
|
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import random
|
5 |
+
|
6 |
from diffusers import (
|
7 |
StableDiffusionPipeline,
|
8 |
StableDiffusionInstructPix2PixPipeline,
|
|
|
10 |
WanPipeline,
|
11 |
)
|
12 |
from diffusers.utils import export_to_video, load_image
|
|
|
|
|
13 |
|
14 |
+
# Force CPU mode
|
15 |
+
device = "cpu"
|
16 |
+
dtype = torch.float32
|
17 |
MAX_SEED = np.iinfo(np.int32).max
|
18 |
|
19 |
+
# Global pipeline holders
|
20 |
TXT2IMG_PIPE = None
|
21 |
IMG2IMG_PIPE = None
|
22 |
TXT2VID_PIPE = None
|
23 |
IMG2VID_PIPE = None
|
24 |
|
25 |
+
# Helper to load models
|
26 |
def make_pipe(cls, model_id, **kwargs):
|
27 |
pipe = cls.from_pretrained(model_id, torch_dtype=dtype, **kwargs)
|
28 |
+
pipe.to(device)
|
29 |
return pipe
|
30 |
|
31 |
+
# Text β Image
|
32 |
def generate_image_from_text(prompt, seed, randomize_seed):
|
33 |
global TXT2IMG_PIPE
|
34 |
if TXT2IMG_PIPE is None:
|
35 |
+
TXT2IMG_PIPE = make_pipe(StableDiffusionPipeline, "stabilityai/stable-diffusion-2-1-base")
|
36 |
if randomize_seed:
|
37 |
seed = random.randint(0, MAX_SEED)
|
38 |
generator = torch.manual_seed(seed)
|
39 |
image = TXT2IMG_PIPE(prompt=prompt, num_inference_steps=20, generator=generator).images[0]
|
40 |
return image, seed
|
41 |
|
42 |
+
# Image β Image
|
43 |
def generate_image_from_image_and_prompt(image, prompt, seed, randomize_seed):
|
44 |
global IMG2IMG_PIPE
|
45 |
if IMG2IMG_PIPE is None:
|
46 |
+
IMG2IMG_PIPE = make_pipe(StableDiffusionInstructPix2PixPipeline, "timbrooks/instruct-pix2pix")
|
47 |
if randomize_seed:
|
48 |
seed = random.randint(0, MAX_SEED)
|
49 |
generator = torch.manual_seed(seed)
|
50 |
out = IMG2IMG_PIPE(prompt=prompt, image=image, num_inference_steps=8, generator=generator)
|
51 |
return out.images[0], seed
|
52 |
|
53 |
+
# Text β Video
|
54 |
def generate_video_from_text(prompt, seed, randomize_seed):
|
55 |
global TXT2VID_PIPE
|
56 |
if TXT2VID_PIPE is None:
|
57 |
+
TXT2VID_PIPE = make_pipe(WanPipeline, "Wan-AI/Wan2.1-T2V-1.3B-Diffusers")
|
58 |
if randomize_seed:
|
59 |
seed = random.randint(0, MAX_SEED)
|
60 |
generator = torch.manual_seed(seed)
|
61 |
frames = TXT2VID_PIPE(prompt=prompt, num_frames=12, generator=generator).frames[0]
|
62 |
return export_to_video(frames, "/tmp/wan_video.mp4", fps=8), seed
|
63 |
|
64 |
+
# Image β Video
|
65 |
def generate_video_from_image(image, seed, randomize_seed):
|
66 |
global IMG2VID_PIPE
|
67 |
if IMG2VID_PIPE is None:
|
68 |
+
IMG2VID_PIPE = make_pipe(
|
69 |
+
StableVideoDiffusionPipeline,
|
70 |
+
"stabilityai/stable-video-diffusion-img2vid-xt"
|
71 |
+
)
|
72 |
if randomize_seed:
|
73 |
seed = random.randint(0, MAX_SEED)
|
74 |
generator = torch.manual_seed(seed)
|
|
|
76 |
frames = IMG2VID_PIPE(image=image, num_inference_steps=16, generator=generator).frames[0]
|
77 |
return export_to_video(frames, "/tmp/svd_video.mp4", fps=8), seed
|
78 |
|
79 |
+
# Gradio Interface
|
80 |
+
with gr.Blocks() as demo:
|
81 |
+
gr.Markdown("# π§ AI Playground β Text & Image β Image & Video")
|
82 |
|
83 |
with gr.Tabs():
|
84 |
# Text β Image
|
85 |
with gr.Tab("Text β Image"):
|
86 |
+
prompt_txt = gr.Textbox(label="Prompt")
|
87 |
+
btn_txt2img = gr.Button("Generate")
|
|
|
88 |
result_img = gr.Image()
|
89 |
seed_txt = gr.Slider(0, MAX_SEED, value=42, label="Seed")
|
90 |
+
rand_txt = gr.Checkbox(label="Randomize seed", value=True)
|
91 |
+
btn_txt2img.click(
|
92 |
+
generate_image_from_text,
|
93 |
+
inputs=[prompt_txt, seed_txt, rand_txt],
|
94 |
outputs=[result_img, seed_txt]
|
95 |
)
|
96 |
|
97 |
# Image β Image
|
98 |
with gr.Tab("Image β Image"):
|
99 |
+
image_in = gr.Image(label="Input Image")
|
100 |
+
prompt_img = gr.Textbox(label="Edit Prompt")
|
101 |
+
btn_img2img = gr.Button("Generate")
|
|
|
102 |
result_img2 = gr.Image()
|
103 |
seed_img = gr.Slider(0, MAX_SEED, value=123, label="Seed")
|
104 |
+
rand_img = gr.Checkbox(label="Randomize seed", value=True)
|
105 |
+
btn_img2img.click(
|
106 |
+
generate_image_from_image_and_prompt,
|
107 |
+
inputs=[image_in, prompt_img, seed_img, rand_img],
|
108 |
outputs=[result_img2, seed_img]
|
109 |
)
|
110 |
|
111 |
# Text β Video
|
112 |
with gr.Tab("Text β Video"):
|
113 |
+
prompt_vid = gr.Textbox(label="Prompt")
|
114 |
+
btn_txt2vid = gr.Button("Generate")
|
|
|
115 |
result_vid = gr.Video()
|
116 |
seed_vid = gr.Slider(0, MAX_SEED, value=555, label="Seed")
|
117 |
+
rand_vid = gr.Checkbox(label="Randomize seed", value=True)
|
118 |
+
btn_txt2vid.click(
|
119 |
+
generate_video_from_text,
|
120 |
+
inputs=[prompt_vid, seed_vid, rand_vid],
|
121 |
outputs=[result_vid, seed_vid]
|
122 |
)
|
123 |
|
124 |
# Image β Video
|
125 |
with gr.Tab("Image β Video"):
|
126 |
+
image_vid = gr.Image(label="Input Image")
|
127 |
+
btn_img2vid = gr.Button("Animate")
|
|
|
128 |
result_vid2 = gr.Video()
|
129 |
seed_vid2 = gr.Slider(0, MAX_SEED, value=999, label="Seed")
|
130 |
+
rand_vid2 = gr.Checkbox(label="Randomize seed", value=True)
|
131 |
+
btn_img2vid.click(
|
132 |
+
generate_video_from_image,
|
133 |
+
inputs=[image_vid, seed_vid2, rand_vid2],
|
134 |
outputs=[result_vid2, seed_vid2]
|
135 |
)
|
136 |
|