Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
from diffusers import DiffusionPipeline
|
5 |
+
import torch
|
6 |
+
|
7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
+
|
9 |
+
if torch.cuda.is_available():
|
10 |
+
torch.cuda.max_memory_allocated(device=device)
|
11 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
12 |
+
pipe.enable_xformers_memory_efficient_attention()
|
13 |
+
pipe = pipe.to(device)
|
14 |
+
else:
|
15 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
16 |
+
pipe = pipe.to(device)
|
17 |
+
|
18 |
+
MAX_SEED = np.iinfo(np.int32).max
|
19 |
+
MAX_IMAGE_SIZE = 1024
|
20 |
+
|
21 |
+
def infer(prompt_part1, color, dress_type, design, prompt_part5, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
22 |
+
prompt = f"Create fornt view and back view seperately of {prompt_part1} {color} colored plain {dress_type} with {design} design, {prompt_part5}"
|
23 |
+
|
24 |
+
if randomize_seed:
|
25 |
+
seed = random.randint(0, MAX_SEED)
|
26 |
+
|
27 |
+
generator = torch.Generator().manual_seed(seed)
|
28 |
+
|
29 |
+
image = pipe(
|
30 |
+
prompt=prompt,
|
31 |
+
negative_prompt=negative_prompt,
|
32 |
+
guidance_scale=guidance_scale,
|
33 |
+
num_inference_steps=num_inference_steps,
|
34 |
+
width=width,
|
35 |
+
height=height,
|
36 |
+
generator=generator
|
37 |
+
).images[0]
|
38 |
+
|
39 |
+
return image
|
40 |
+
|
41 |
+
examples = [
|
42 |
+
"red, t-shirt, yellow stripes",
|
43 |
+
"blue, hoodie, minimalist",
|
44 |
+
"red, sweat shirt, geometric design",
|
45 |
+
]
|
46 |
+
|
47 |
+
css = """
|
48 |
+
#col-container {
|
49 |
+
margin: 0 auto;
|
50 |
+
max-width: 520px;
|
51 |
+
}
|
52 |
+
"""
|
53 |
+
|
54 |
+
if torch.cuda.is_available():
|
55 |
+
power_device = "GPU"
|
56 |
+
else:
|
57 |
+
power_device = "CPU"
|
58 |
+
|
59 |
+
with gr.Blocks(css=css) as demo:
|
60 |
+
|
61 |
+
with gr.Column(elem_id="col-container"):
|
62 |
+
gr.Markdown(f"""
|
63 |
+
# Text-to-Image Gradio Template
|
64 |
+
Currently running on {power_device}.
|
65 |
+
""")
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
|
69 |
+
prompt_part1 = gr.Textbox(
|
70 |
+
value="a single",
|
71 |
+
label="Prompt Part 1",
|
72 |
+
show_label=False,
|
73 |
+
interactive=False,
|
74 |
+
container=False,
|
75 |
+
elem_id="prompt_part1",
|
76 |
+
visible=False,
|
77 |
+
)
|
78 |
+
|
79 |
+
prompt_part2 = gr.Textbox(
|
80 |
+
label="color",
|
81 |
+
show_label=False,
|
82 |
+
max_lines=1,
|
83 |
+
placeholder="color (e.g., color category)",
|
84 |
+
container=False,
|
85 |
+
)
|
86 |
+
|
87 |
+
prompt_part3 = gr.Textbox(
|
88 |
+
label="dress_type",
|
89 |
+
show_label=False,
|
90 |
+
max_lines=1,
|
91 |
+
placeholder="dress_type (e.g., t-shirt, sweatshirt, shirt, hoodie)",
|
92 |
+
container=False,
|
93 |
+
)
|
94 |
+
|
95 |
+
prompt_part4 = gr.Textbox(
|
96 |
+
label="design",
|
97 |
+
show_label=False,
|
98 |
+
max_lines=1,
|
99 |
+
placeholder="design",
|
100 |
+
container=False,
|
101 |
+
)
|
102 |
+
|
103 |
+
prompt_part5 = gr.Textbox(
|
104 |
+
value="hanging on the plain wall",
|
105 |
+
label="Prompt Part 5",
|
106 |
+
show_label=False,
|
107 |
+
interactive=False,
|
108 |
+
container=False,
|
109 |
+
elem_id="prompt_part5",
|
110 |
+
visible=False,
|
111 |
+
)
|
112 |
+
|
113 |
+
|
114 |
+
run_button = gr.Button("Run", scale=0)
|
115 |
+
|
116 |
+
result = gr.Image(label="Result", show_label=False)
|
117 |
+
|
118 |
+
with gr.Accordion("Advanced Settings", open=False):
|
119 |
+
|
120 |
+
negative_prompt = gr.Textbox(
|
121 |
+
label="Negative prompt",
|
122 |
+
max_lines=1,
|
123 |
+
placeholder="Enter a negative prompt",
|
124 |
+
visible=False,
|
125 |
+
)
|
126 |
+
|
127 |
+
seed = gr.Slider(
|
128 |
+
label="Seed",
|
129 |
+
minimum=0,
|
130 |
+
maximum=MAX_SEED,
|
131 |
+
step=1,
|
132 |
+
value=0,
|
133 |
+
)
|
134 |
+
|
135 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
136 |
+
|
137 |
+
with gr.Row():
|
138 |
+
|
139 |
+
width = gr.Slider(
|
140 |
+
label="Width",
|
141 |
+
minimum=256,
|
142 |
+
maximum=MAX_IMAGE_SIZE,
|
143 |
+
step=32,
|
144 |
+
value=512,
|
145 |
+
)
|
146 |
+
|
147 |
+
height = gr.Slider(
|
148 |
+
label="Height",
|
149 |
+
minimum=256,
|
150 |
+
maximum=MAX_IMAGE_SIZE,
|
151 |
+
step=32,
|
152 |
+
value=512,
|
153 |
+
)
|
154 |
+
|
155 |
+
with gr.Row():
|
156 |
+
|
157 |
+
guidance_scale = gr.Slider(
|
158 |
+
label="Guidance scale",
|
159 |
+
minimum=0.0,
|
160 |
+
maximum=10.0,
|
161 |
+
step=0.1,
|
162 |
+
value=0.0,
|
163 |
+
)
|
164 |
+
|
165 |
+
num_inference_steps = gr.Slider(
|
166 |
+
label="Number of inference steps",
|
167 |
+
minimum=1,
|
168 |
+
maximum=12,
|
169 |
+
step=1,
|
170 |
+
value=2,
|
171 |
+
)
|
172 |
+
|
173 |
+
gr.Examples(
|
174 |
+
examples=examples,
|
175 |
+
inputs=[prompt_part2]
|
176 |
+
)
|
177 |
+
|
178 |
+
run_button.click(
|
179 |
+
fn=infer,
|
180 |
+
inputs=[prompt_part1, prompt_part2, prompt_part3, prompt_part4, prompt_part5, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
181 |
+
outputs=[result]
|
182 |
+
)
|
183 |
+
|
184 |
+
demo.queue().launch()
|