changes for ZeroGPU
Browse files
app.py
CHANGED
@@ -31,11 +31,13 @@ from src.priors.prior_transformer import (
|
|
31 |
from src.pipelines.pipeline_kandinsky_prior import KandinskyPriorPipeline
|
32 |
|
33 |
from diffusers import DiffusionPipeline
|
|
|
34 |
|
35 |
|
36 |
__DEVICE__ = "cpu"
|
37 |
if torch.cuda.is_available():
|
38 |
__DEVICE__ = "cuda"
|
|
|
39 |
|
40 |
class Ours:
|
41 |
def __init__(self, device):
|
@@ -43,7 +45,7 @@ class Ours:
|
|
43 |
CLIPTextModelWithProjection.from_pretrained(
|
44 |
"laion/CLIP-ViT-bigG-14-laion2B-39B-b160k",
|
45 |
projection_dim=1280,
|
46 |
-
torch_dtype=torch.
|
47 |
)
|
48 |
.eval()
|
49 |
.requires_grad_(False)
|
@@ -55,7 +57,7 @@ class Ours:
|
|
55 |
|
56 |
prior = PriorTransformer.from_pretrained(
|
57 |
"ECLIPSE-Community/ECLIPSE_KandinskyV22_Prior",
|
58 |
-
torch_dtype=torch.
|
59 |
)
|
60 |
|
61 |
self.pipe_prior = KandinskyPriorPipeline.from_pretrained(
|
@@ -63,16 +65,16 @@ class Ours:
|
|
63 |
prior=prior,
|
64 |
text_encoder=text_encoder,
|
65 |
tokenizer=tokenizer,
|
66 |
-
torch_dtype=torch.
|
67 |
).to(device)
|
68 |
|
69 |
self.pipe = DiffusionPipeline.from_pretrained(
|
70 |
-
"kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.
|
71 |
).to(device)
|
72 |
|
73 |
def inference(self, text, negative_text, steps, guidance_scale):
|
74 |
gen_images = []
|
75 |
-
for i in range(
|
76 |
image_emb, negative_image_emb = self.pipe_prior(
|
77 |
text, negative_prompt=negative_text
|
78 |
).to_tuple()
|
@@ -88,13 +90,13 @@ class Ours:
|
|
88 |
|
89 |
selected_model = Ours(device=__DEVICE__)
|
90 |
|
91 |
-
|
92 |
def get_images(text, negative_text, steps, guidance_scale):
|
93 |
images = selected_model.inference(text, negative_text, steps, guidance_scale)
|
94 |
new_images = []
|
95 |
for img in images:
|
96 |
new_images.append(img)
|
97 |
-
return new_images
|
98 |
|
99 |
|
100 |
with gr.Blocks() as demo:
|
@@ -137,9 +139,9 @@ with gr.Blocks() as demo:
|
|
137 |
with gr.Row():
|
138 |
btn = gr.Button(value="Generate Image")
|
139 |
|
140 |
-
gallery = gr.
|
141 |
-
|
142 |
-
)
|
143 |
|
144 |
btn.click(
|
145 |
get_images,
|
|
|
31 |
from src.pipelines.pipeline_kandinsky_prior import KandinskyPriorPipeline
|
32 |
|
33 |
from diffusers import DiffusionPipeline
|
34 |
+
import spaces
|
35 |
|
36 |
|
37 |
__DEVICE__ = "cpu"
|
38 |
if torch.cuda.is_available():
|
39 |
__DEVICE__ = "cuda"
|
40 |
+
__DEVICE__ = "cuda"
|
41 |
|
42 |
class Ours:
|
43 |
def __init__(self, device):
|
|
|
45 |
CLIPTextModelWithProjection.from_pretrained(
|
46 |
"laion/CLIP-ViT-bigG-14-laion2B-39B-b160k",
|
47 |
projection_dim=1280,
|
48 |
+
torch_dtype=torch.float16,
|
49 |
)
|
50 |
.eval()
|
51 |
.requires_grad_(False)
|
|
|
57 |
|
58 |
prior = PriorTransformer.from_pretrained(
|
59 |
"ECLIPSE-Community/ECLIPSE_KandinskyV22_Prior",
|
60 |
+
torch_dtype=torch.float16,
|
61 |
)
|
62 |
|
63 |
self.pipe_prior = KandinskyPriorPipeline.from_pretrained(
|
|
|
65 |
prior=prior,
|
66 |
text_encoder=text_encoder,
|
67 |
tokenizer=tokenizer,
|
68 |
+
torch_dtype=torch.float16,
|
69 |
).to(device)
|
70 |
|
71 |
self.pipe = DiffusionPipeline.from_pretrained(
|
72 |
+
"kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16
|
73 |
).to(device)
|
74 |
|
75 |
def inference(self, text, negative_text, steps, guidance_scale):
|
76 |
gen_images = []
|
77 |
+
for i in range(4):
|
78 |
image_emb, negative_image_emb = self.pipe_prior(
|
79 |
text, negative_prompt=negative_text
|
80 |
).to_tuple()
|
|
|
90 |
|
91 |
selected_model = Ours(device=__DEVICE__)
|
92 |
|
93 |
+
@spaces.GPU
|
94 |
def get_images(text, negative_text, steps, guidance_scale):
|
95 |
images = selected_model.inference(text, negative_text, steps, guidance_scale)
|
96 |
new_images = []
|
97 |
for img in images:
|
98 |
new_images.append(img)
|
99 |
+
return new_images
|
100 |
|
101 |
|
102 |
with gr.Blocks() as demo:
|
|
|
139 |
with gr.Row():
|
140 |
btn = gr.Button(value="Generate Image")
|
141 |
|
142 |
+
gallery = gr.Gallery(
|
143 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
144 |
+
, columns=[4], rows=[1], object_fit="contain", height="auto")
|
145 |
|
146 |
btn.click(
|
147 |
get_images,
|