gaur3009 commited on
Commit
d0ae4b7
·
verified ·
1 Parent(s): 38b1b9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +207 -3
app.py CHANGED
@@ -50,9 +50,213 @@ def infer(prompt_part1, color, dress_type, front_design, back_design, prompt_par
50
  return front_image, back_image
51
 
52
  examples = [
53
- "red, t-shirt, yellow stripes, polka dots",
54
- "blue, hoodie, minimalist, abstract art",
55
- "red, sweat shirt, geometric design, plain",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  ]
57
 
58
  css = """
 
50
  return front_image, back_image
51
 
52
  examples = [
53
+ ["red", "t-shirt", "yellow stripes", "polka dots"],
54
+ ["blue", "hoodie", "minimalist", "abstract art"],
55
+ ["red", "sweat shirt", "geometric design", "plain"],
56
+ ]
57
+
58
+ css = """
59
+ #col-container {
60
+ margin: 0 auto;
61
+ max-width: 520px;
62
+ }
63
+ """
64
+
65
+ if torch.cuda.is_available():
66
+ power_device = "GPU"
67
+ else:
68
+ power_device = "CPU"
69
+
70
+ with gr.Blocks(css=css) as demo:
71
+
72
+ with gr.Column(elem_id="col-container"):
73
+ gr.Markdown(f"""
74
+ # Text-to-Image Gradio Template
75
+ Currently running on {power_device}.
76
+ """)
77
+
78
+ with gr.Row():
79
+
80
+ prompt_part1 = gr.Textbox(
81
+ value="a single",
82
+ label="Prompt Part 1",
83
+ show_label=False,
84
+ interactive=False,
85
+ container=False,
86
+ elem_id="prompt_part1",
87
+ visible=False,
88
+ )
89
+
90
+ prompt_part2 = gr.Textbox(
91
+ label="color",
92
+ show_label=False,
93
+ max_lines=1,
94
+ placeholder="color (e.g., color category)",
95
+ container=False,
96
+ )
97
+
98
+ prompt_part3 = gr.Textbox(
99
+ label="dress_type",
100
+ show_label=False,
101
+ max_lines=1,
102
+ placeholder="dress_type (e.g., t-shirt, sweatshirt, shirt, hoodie)",
103
+ container=False,
104
+ )
105
+
106
+ prompt_part4_front = gr.Textbox(
107
+ label="front design",
108
+ show_label=False,
109
+ max_lines=1,
110
+ placeholder="front design",
111
+ container=False,
112
+ )
113
+
114
+ prompt_part4_back = gr.Textbox(
115
+ label="back design",
116
+ show_label=False,
117
+ max_lines=1,
118
+ placeholder="back design",
119
+ container=False,
120
+ )
121
+
122
+ prompt_part5 = gr.Textbox(
123
+ value="hanging on the plain wall",
124
+ label="Prompt Part 5",
125
+ show_label=False,
126
+ interactive=False,
127
+ container=False,
128
+ elem_id="prompt_part5",
129
+ visible=False,
130
+ )
131
+
132
+
133
+ run_button = gr.Button("Run", scale=0)
134
+
135
+ front_result = gr.Image(label="Front View Result", show_label=False)
136
+ back_result = gr.Image(label="Back View Result", show_label=False)
137
+
138
+ with gr.Accordion("Advanced Settings", open=False):
139
+
140
+ negative_prompt = gr.Textbox(
141
+ label="Negative prompt",
142
+ max_lines=1,
143
+ placeholder="Enter a negative prompt",
144
+ visible=False,
145
+ )
146
+
147
+ seed = gr.Slider(
148
+ label="Seed",
149
+ minimum=0,
150
+ maximum=MAX_SEED,
151
+ step=1,
152
+ value=0,
153
+ )
154
+
155
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
156
+
157
+ with gr.Row():
158
+
159
+ width = gr.Slider(
160
+ label="Width",
161
+ minimum=256,
162
+ maximum=MAX_IMAGE_SIZE,
163
+ step=32,
164
+ value=512,
165
+ )
166
+
167
+ height = gr.Slider(
168
+ label="Height",
169
+ minimum=256,
170
+ maximum=MAX_IMAGE_SIZE,
171
+ step=32,
172
+ value=512,
173
+ )
174
+
175
+ with gr.Row():
176
+
177
+ guidance_scale = gr.Slider(
178
+ label="Guidance scale",
179
+ minimum=0.0,
180
+ maximum=10.0,
181
+ step=0.1,
182
+ value=0.0,
183
+ )
184
+
185
+ num_inference_steps = gr.Slider(
186
+ label="Number of inference steps",
187
+ minimum=1,
188
+ maximum=12,
189
+ step=1,
190
+ value=2,
191
+ )
192
+
193
+ gr.Examples(
194
+ examples=examples,
195
+ inputs=[prompt_part2, prompt_part3, prompt_part4_front, prompt_part4_back]
196
+ )
197
+
198
+ run_button.click(
199
+ fn=infer,
200
+ inputs=[prompt_part1, prompt_part2, prompt_part3, prompt_part4_front, prompt_part4_back, prompt_part5, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
201
+ outputs=[front_result, back_result]
202
+ )
203
+
204
+ demo.queue().launch()
205
+ import gradio as gr
206
+ import numpy as np
207
+ import random
208
+ from diffusers import DiffusionPipeline
209
+ import torch
210
+
211
+ device = "cuda" if torch.cuda.is_available() else "cpu"
212
+
213
+ if torch.cuda.is_available():
214
+ torch.cuda.max_memory_allocated(device=device)
215
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
216
+ pipe.enable_xformers_memory_efficient_attention()
217
+ pipe = pipe.to(device)
218
+ else:
219
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
220
+ pipe = pipe.to(device)
221
+
222
+ MAX_SEED = np.iinfo(np.int32).max
223
+ MAX_IMAGE_SIZE = 1024
224
+
225
+ def infer(prompt_part1, color, dress_type, front_design, back_design, prompt_part5, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
226
+ front_prompt = f"front view of {prompt_part1} {color} colored plain {dress_type} with {front_design} design, {prompt_part5}"
227
+ back_prompt = f"back view of {prompt_part1} {color} colored plain {dress_type} with {back_design} design, {prompt_part5}"
228
+
229
+ if randomize_seed:
230
+ seed = random.randint(0, MAX_SEED)
231
+
232
+ generator = torch.Generator().manual_seed(seed)
233
+
234
+ front_image = pipe(
235
+ prompt=front_prompt,
236
+ negative_prompt=negative_prompt,
237
+ guidance_scale=guidance_scale,
238
+ num_inference_steps=num_inference_steps,
239
+ width=width,
240
+ height=height,
241
+ generator=generator
242
+ ).images[0]
243
+
244
+ back_image = pipe(
245
+ prompt=back_prompt,
246
+ negative_prompt=negative_prompt,
247
+ guidance_scale=guidance_scale,
248
+ num_inference_steps=num_inference_steps,
249
+ width=width,
250
+ height=height,
251
+ generator=generator
252
+ ).images[0]
253
+
254
+ return front_image, back_image
255
+
256
+ examples = [
257
+ ["red", "t-shirt", "yellow stripes", "polka dots"],
258
+ ["blue", "hoodie", "minimalist", "abstract art"],
259
+ ["red", "sweat shirt", "geometric design", "plain"],
260
  ]
261
 
262
  css = """