gaur3009 commited on
Commit
408242e
·
verified ·
1 Parent(s): 4fa2f44

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +223 -0
app.py ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, front_design, back_design, prompt_part5, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
22
+ front_prompt = f"front view of {prompt_part1} {color} colored plain {dress_type} with {front_design} design, {prompt_part5}"
23
+ back_prompt = f"back view of {prompt_part1} {color} colored plain {dress_type} with {back_design} design, {prompt_part5}"
24
+
25
+ if randomize_seed:
26
+ seed = random.randint(0, MAX_SEED)
27
+
28
+ generator = torch.Generator().manual_seed(seed)
29
+
30
+ front_image = pipe(
31
+ prompt=front_prompt,
32
+ negative_prompt=negative_prompt,
33
+ guidance_scale=guidance_scale,
34
+ num_inference_steps=num_inference_steps,
35
+ width=width,
36
+ height=height,
37
+ generator=generator
38
+ ).images[0]
39
+
40
+ back_image = pipe(
41
+ prompt=back_prompt,
42
+ negative_prompt=negative_prompt,
43
+ guidance_scale=guidance_scale,
44
+ num_inference_steps=num_inference_steps,
45
+ width=width,
46
+ height=height,
47
+ generator=generator
48
+ ).images[0]
49
+
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
+ run_button = gr.Button("Run", scale=0)
133
+
134
+ front_result = gr.Image(label="Front View Result", show_label=False)
135
+ back_result = gr.Image(label="Back View Result", show_label=False)
136
+
137
+ with gr.Row():
138
+ edit_button = gr.Button("Edit Front Design")
139
+ edit_front_design = gr.Image(interactive=True, tool="editor")
140
+
141
+ with gr.Row():
142
+ edit_button_back = gr.Button("Edit Back Design")
143
+ edit_back_design = gr.Image(interactive=True, tool="editor")
144
+
145
+ with gr.Accordion("Advanced Settings", open=False):
146
+
147
+ negative_prompt = gr.Textbox(
148
+ label="Negative prompt",
149
+ max_lines=1,
150
+ placeholder="Enter a negative prompt",
151
+ visible=False,
152
+ )
153
+
154
+ seed = gr.Slider(
155
+ label="Seed",
156
+ minimum=0,
157
+ maximum=MAX_SEED,
158
+ step=1,
159
+ value=0,
160
+ )
161
+
162
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
163
+
164
+ with gr.Row():
165
+
166
+ width = gr.Slider(
167
+ label="Width",
168
+ minimum=256,
169
+ maximum=MAX_IMAGE_SIZE,
170
+ step=32,
171
+ value=512,
172
+ )
173
+
174
+ height = gr.Slider(
175
+ label="Height",
176
+ minimum=256,
177
+ maximum=MAX_IMAGE_SIZE,
178
+ step=32,
179
+ value=512,
180
+ )
181
+
182
+ with gr.Row():
183
+
184
+ guidance_scale = gr.Slider(
185
+ label="Guidance scale",
186
+ minimum=0.0,
187
+ maximum=10.0,
188
+ step=0.1,
189
+ value=0.0,
190
+ )
191
+
192
+ num_inference_steps = gr.Slider(
193
+ label="Number of inference steps",
194
+ minimum=1,
195
+ maximum=12,
196
+ step=1,
197
+ value=2,
198
+ )
199
+
200
+ gr.Examples(
201
+ examples=examples,
202
+ inputs=[prompt_part2, prompt_part3, prompt_part4_front, prompt_part4_back]
203
+ )
204
+
205
+ run_button.click(
206
+ fn=infer,
207
+ 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],
208
+ outputs=[front_result, back_result]
209
+ )
210
+
211
+ edit_button.click(
212
+ fn=lambda x: x,
213
+ inputs=[front_result],
214
+ outputs=[edit_front_design],
215
+ )
216
+
217
+ edit_button_back.click(
218
+ fn=lambda x: x,
219
+ inputs=[back_result],
220
+ outputs=[edit_back_design],
221
+ )
222
+
223
+ demo.queue().launch()