gaur3009 commited on
Commit
d26fcbb
·
verified ·
1 Parent(s): 862e2c0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +203 -0
app.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ if randomize_seed:
23
+ seed = random.randint(0, MAX_SEED)
24
+
25
+ generator = torch.Generator().manual_seed(seed)
26
+
27
+ front_prompt = f"front view of {prompt_part1} {color} colored plain {dress_type} with {front_design} design, {prompt_part5}"
28
+ front_image = pipe(
29
+ prompt=front_prompt,
30
+ negative_prompt=negative_prompt,
31
+ guidance_scale=guidance_scale,
32
+ num_inference_steps=num_inference_steps,
33
+ width=width,
34
+ height=height,
35
+ generator=generator
36
+ ).images[0]
37
+
38
+ back_prompt = f"back view of {prompt_part1} {color} colored plain {dress_type} with {back_design} design, {prompt_part5}"
39
+ back_image = pipe(
40
+ prompt=back_prompt,
41
+ negative_prompt=negative_prompt,
42
+ guidance_scale=guidance_scale,
43
+ num_inference_steps=num_inference_steps,
44
+ width=width,
45
+ height=height,
46
+ generator=generator
47
+ ).images[0]
48
+
49
+ return front_image, back_image
50
+
51
+ examples = [
52
+ ["red", "t-shirt", "yellow stripes", "polka dots"],
53
+ ["blue", "hoodie", "minimalist", "abstract art"],
54
+ ["red", "sweat shirt", "geometric design", "plain"],
55
+ ]
56
+
57
+ css = """
58
+ #col-container {
59
+ margin: 0 auto;
60
+ max-width: 520px;
61
+ }
62
+ """
63
+
64
+ if torch.cuda.is_available():
65
+ power_device = "GPU"
66
+ else:
67
+ power_device = "CPU"
68
+
69
+ with gr.Blocks(css=css) as demo:
70
+
71
+ with gr.Column(elem_id="col-container"):
72
+ gr.Markdown(f"""
73
+ # Text-to-Image Gradio Template
74
+ Currently running on {power_device}.
75
+ """)
76
+
77
+ with gr.Row():
78
+
79
+ prompt_part1 = gr.Textbox(
80
+ value="a single",
81
+ label="Prompt Part 1",
82
+ show_label=False,
83
+ interactive=False,
84
+ container=False,
85
+ elem_id="prompt_part1",
86
+ visible=False,
87
+ )
88
+
89
+ prompt_part2 = gr.Textbox(
90
+ label="color",
91
+ show_label=False,
92
+ max_lines=1,
93
+ placeholder="color (e.g., color category)",
94
+ container=False,
95
+ )
96
+
97
+ prompt_part3 = gr.Textbox(
98
+ label="dress_type",
99
+ show_label=False,
100
+ max_lines=1,
101
+ placeholder="dress_type (e.g., t-shirt, sweatshirt, shirt, hoodie)",
102
+ container=False,
103
+ )
104
+
105
+ prompt_part4_front = gr.Textbox(
106
+ label="front design",
107
+ show_label=False,
108
+ max_lines=1,
109
+ placeholder="front design",
110
+ container=False,
111
+ )
112
+
113
+ prompt_part4_back = gr.Textbox(
114
+ label="back design",
115
+ show_label=False,
116
+ max_lines=1,
117
+ placeholder="back design",
118
+ container=False,
119
+ )
120
+
121
+ prompt_part5 = gr.Textbox(
122
+ value="hanging on the plain wall",
123
+ label="Prompt Part 5",
124
+ show_label=False,
125
+ interactive=False,
126
+ container=False,
127
+ elem_id="prompt_part5",
128
+ visible=False,
129
+ )
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.Accordion("Advanced Settings", open=False):
138
+
139
+ negative_prompt = gr.Textbox(
140
+ label="Negative prompt",
141
+ max_lines=1,
142
+ placeholder="Enter a negative prompt",
143
+ visible=False,
144
+ )
145
+
146
+ seed = gr.Slider(
147
+ label="Seed",
148
+ minimum=0,
149
+ maximum=MAX_SEED,
150
+ step=1,
151
+ value=0,
152
+ )
153
+
154
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
155
+
156
+ with gr.Row():
157
+
158
+ width = gr.Slider(
159
+ label="Width",
160
+ minimum=256,
161
+ maximum=MAX_IMAGE_SIZE,
162
+ step=32,
163
+ value=512,
164
+ )
165
+
166
+ height = gr.Slider(
167
+ label="Height",
168
+ minimum=256,
169
+ maximum=MAX_IMAGE_SIZE,
170
+ step=32,
171
+ value=512,
172
+ )
173
+
174
+ with gr.Row():
175
+
176
+ guidance_scale = gr.Slider(
177
+ label="Guidance scale",
178
+ minimum=0.0,
179
+ maximum=10.0,
180
+ step=0.1,
181
+ value=0.0,
182
+ )
183
+
184
+ num_inference_steps = gr.Slider(
185
+ label="Number of inference steps",
186
+ minimum=1,
187
+ maximum=12,
188
+ step=1,
189
+ value=2,
190
+ )
191
+
192
+ gr.Examples(
193
+ examples=examples,
194
+ inputs=[prompt_part2, prompt_part3, prompt_part4_front, prompt_part4_back]
195
+ )
196
+
197
+ run_button.click(
198
+ fn=infer,
199
+ 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],
200
+ outputs=[front_result, back_result]
201
+ )
202
+
203
+ demo.queue().launch()