Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,66 @@
|
|
1 |
-
import
|
|
|
2 |
import numpy as np
|
|
|
3 |
import random
|
4 |
-
from
|
5 |
-
from diffusers.utils import make_image_grid, load_image
|
6 |
|
7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
|
9 |
if torch.cuda.is_available():
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
MAX_IMAGE_SIZE = 1024
|
20 |
|
21 |
-
|
|
|
|
|
22 |
|
23 |
-
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
css="""
|
45 |
#col-container {
|
@@ -48,17 +69,12 @@ css="""
|
|
48 |
}
|
49 |
"""
|
50 |
|
51 |
-
if torch.cuda.is_available():
|
52 |
-
power_device = "GPU"
|
53 |
-
else:
|
54 |
-
power_device = "CPU"
|
55 |
-
|
56 |
with gr.Blocks(css=css) as demo:
|
57 |
|
58 |
with gr.Column(elem_id="col-container"):
|
59 |
gr.Markdown(f"""
|
60 |
# Text-to-Image Gradio Template
|
61 |
-
Currently running on {
|
62 |
""")
|
63 |
|
64 |
with gr.Row():
|
@@ -81,7 +97,7 @@ with gr.Blocks(css=css) as demo:
|
|
81 |
label="Negative prompt",
|
82 |
max_lines=1,
|
83 |
placeholder="Enter a negative prompt",
|
84 |
-
visible=
|
85 |
)
|
86 |
|
87 |
seed = gr.Slider(
|
@@ -101,7 +117,7 @@ with gr.Blocks(css=css) as demo:
|
|
101 |
minimum=256,
|
102 |
maximum=MAX_IMAGE_SIZE,
|
103 |
step=32,
|
104 |
-
value=
|
105 |
)
|
106 |
|
107 |
height = gr.Slider(
|
@@ -109,7 +125,7 @@ with gr.Blocks(css=css) as demo:
|
|
109 |
minimum=256,
|
110 |
maximum=MAX_IMAGE_SIZE,
|
111 |
step=32,
|
112 |
-
value=
|
113 |
)
|
114 |
|
115 |
with gr.Row():
|
@@ -119,26 +135,31 @@ with gr.Blocks(css=css) as demo:
|
|
119 |
minimum=0.0,
|
120 |
maximum=10.0,
|
121 |
step=0.1,
|
122 |
-
value=
|
123 |
)
|
124 |
|
125 |
num_inference_steps = gr.Slider(
|
126 |
label="Number of inference steps",
|
127 |
minimum=1,
|
128 |
-
maximum=
|
129 |
step=1,
|
130 |
-
value=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
)
|
132 |
-
|
133 |
-
gr.Examples(
|
134 |
-
examples = examples,
|
135 |
-
inputs = [prompt]
|
136 |
-
)
|
137 |
|
138 |
run_button.click(
|
139 |
-
fn =
|
140 |
-
inputs = [prompt, negative_prompt,
|
141 |
outputs = [result]
|
142 |
)
|
143 |
|
144 |
-
demo.
|
|
|
1 |
+
import torch
|
2 |
+
from diffusers import StableDiffusionXLPipeline
|
3 |
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
import random
|
6 |
+
from compel import Compel, ReturnedEmbeddingsType
|
|
|
7 |
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
|
10 |
if torch.cuda.is_available():
|
11 |
+
torch.cuda.max_memory_allocated(device=device)
|
12 |
+
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
13 |
+
pipe = pipe.to(device)
|
14 |
+
else:
|
15 |
+
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
16 |
+
pipe = pipe.to(device)
|
17 |
+
|
18 |
+
pipe.safety_checker = None
|
19 |
+
|
20 |
+
pipe.load_lora_weights("artificialguybr/ps1redmond-ps1-game-graphics-lora-for-sdxl", weight_name="PS1Redmond-PS1Game-Playstation1Graphics.safetensors")
|
21 |
+
lora_activation_words = "playstation 1 graphics, PS1 Game, "
|
22 |
|
23 |
MAX_SEED = np.iinfo(np.int32).max
|
24 |
MAX_IMAGE_SIZE = 1024
|
25 |
|
26 |
+
def infer(conditioning, pooled, neg_conditioning, neg_pooled, height, width, num_inference_steps, guidance_scale, seed, randomize_seed, lora_weight):
|
27 |
+
if randomize_seed:
|
28 |
+
seed = random.randint(0, MAX_SEED)
|
29 |
|
30 |
+
generator = torch.Generator().manual_seed(seed)
|
31 |
|
32 |
+
image = pipe(
|
33 |
+
prompt_embeds=conditioning,
|
34 |
+
pooled_prompt_embeds=pooled,
|
35 |
+
negative_prompt_embeds=neg_conditioning,
|
36 |
+
negative_pooled_prompt_embeds=neg_pooled,
|
37 |
+
height=height,
|
38 |
+
width=width,
|
39 |
+
num_inference_steps=num_inference_steps,
|
40 |
+
guidance_scale=guidance_scale,
|
41 |
+
generator=generator,
|
42 |
+
cross_attention_kwargs={"scale": lora_weight}
|
43 |
+
).images[0]
|
44 |
|
45 |
+
return image
|
46 |
+
|
47 |
+
def get_embeds(prompt, negative_prompt, height, width, num_inference_steps, guidance_scale, seed, randomize_seed, lora_weight):
|
48 |
+
|
49 |
+
compel = Compel(
|
50 |
+
tokenizer=[pipe.tokenizer, pipe.tokenizer_2] ,
|
51 |
+
text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
|
52 |
+
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
53 |
+
requires_pooled=[False, True]
|
54 |
+
)
|
55 |
+
|
56 |
+
prompt = lora_activation_words + prompt
|
57 |
|
58 |
+
conditioning, pooled = compel(prompt)
|
59 |
+
neg_conditioning, neg_pooled = compel(negative_prompt)
|
60 |
+
|
61 |
+
image = infer(conditioning, pooled, neg_conditioning, neg_pooled, height, width, num_inference_steps, guidance_scale, seed, randomize_seed, lora_weight)
|
62 |
+
|
63 |
+
return image
|
64 |
|
65 |
css="""
|
66 |
#col-container {
|
|
|
69 |
}
|
70 |
"""
|
71 |
|
|
|
|
|
|
|
|
|
|
|
72 |
with gr.Blocks(css=css) as demo:
|
73 |
|
74 |
with gr.Column(elem_id="col-container"):
|
75 |
gr.Markdown(f"""
|
76 |
# Text-to-Image Gradio Template
|
77 |
+
Currently running on {device.upper()}.
|
78 |
""")
|
79 |
|
80 |
with gr.Row():
|
|
|
97 |
label="Negative prompt",
|
98 |
max_lines=1,
|
99 |
placeholder="Enter a negative prompt",
|
100 |
+
visible=True,
|
101 |
)
|
102 |
|
103 |
seed = gr.Slider(
|
|
|
117 |
minimum=256,
|
118 |
maximum=MAX_IMAGE_SIZE,
|
119 |
step=32,
|
120 |
+
value=1024,
|
121 |
)
|
122 |
|
123 |
height = gr.Slider(
|
|
|
125 |
minimum=256,
|
126 |
maximum=MAX_IMAGE_SIZE,
|
127 |
step=32,
|
128 |
+
value=1024,
|
129 |
)
|
130 |
|
131 |
with gr.Row():
|
|
|
135 |
minimum=0.0,
|
136 |
maximum=10.0,
|
137 |
step=0.1,
|
138 |
+
value=7.5,
|
139 |
)
|
140 |
|
141 |
num_inference_steps = gr.Slider(
|
142 |
label="Number of inference steps",
|
143 |
minimum=1,
|
144 |
+
maximum=100,
|
145 |
step=1,
|
146 |
+
value=30,
|
147 |
+
)
|
148 |
+
|
149 |
+
with gr.Row():
|
150 |
+
|
151 |
+
lora_weight = gr.Slider(
|
152 |
+
label="LoRA weight",
|
153 |
+
minimum=0.0,
|
154 |
+
maximum=5.0,
|
155 |
+
step=0.01,
|
156 |
+
value=1,
|
157 |
)
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
run_button.click(
|
160 |
+
fn = get_embeds,
|
161 |
+
inputs = [prompt, negative_prompt, height, width, num_inference_steps, guidance_scale, seed, randomize_seed, lora_weight],
|
162 |
outputs = [result]
|
163 |
)
|
164 |
|
165 |
+
demo.launch(debug=True)
|