gaur3009 commited on
Commit
3265912
·
verified ·
1 Parent(s): 807b811

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +196 -0
app.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ front_prompt = f"{prompt_part1} {color} colored plain {dress_type} with {design} design in front, {prompt_part5}"
23
+ back_prompt = f"{prompt_part1} {color} colored plain {dress_type} with {design} design in back, {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",
54
+ "blue, hoodie, minimalist",
55
+ "red, sweat shirt, geometric design",
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 = gr.Textbox(
107
+ label="design",
108
+ show_label=False,
109
+ max_lines=1,
110
+ placeholder="design",
111
+ container=False,
112
+ )
113
+
114
+ prompt_part5 = gr.Textbox(
115
+ value="hanging on the plain wall",
116
+ label="Prompt Part 5",
117
+ show_label=False,
118
+ interactive=False,
119
+ container=False,
120
+ elem_id="prompt_part5",
121
+ visible=False,
122
+ )
123
+
124
+
125
+ run_button = gr.Button("Run", scale=0)
126
+
127
+ front_result = gr.Image(label="Front View Result", show_label=False)
128
+ back_result = gr.Image(label="Back View Result", show_label=False)
129
+
130
+ with gr.Accordion("Advanced Settings", open=False):
131
+
132
+ negative_prompt = gr.Textbox(
133
+ label="Negative prompt",
134
+ max_lines=1,
135
+ placeholder="Enter a negative prompt",
136
+ visible=False,
137
+ )
138
+
139
+ seed = gr.Slider(
140
+ label="Seed",
141
+ minimum=0,
142
+ maximum=MAX_SEED,
143
+ step=1,
144
+ value=0,
145
+ )
146
+
147
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
148
+
149
+ with gr.Row():
150
+
151
+ width = gr.Slider(
152
+ label="Width",
153
+ minimum=256,
154
+ maximum=MAX_IMAGE_SIZE,
155
+ step=32,
156
+ value=512,
157
+ )
158
+
159
+ height = gr.Slider(
160
+ label="Height",
161
+ minimum=256,
162
+ maximum=MAX_IMAGE_SIZE,
163
+ step=32,
164
+ value=512,
165
+ )
166
+
167
+ with gr.Row():
168
+
169
+ guidance_scale = gr.Slider(
170
+ label="Guidance scale",
171
+ minimum=0.0,
172
+ maximum=10.0,
173
+ step=0.1,
174
+ value=0.0,
175
+ )
176
+
177
+ num_inference_steps = gr.Slider(
178
+ label="Number of inference steps",
179
+ minimum=1,
180
+ maximum=12,
181
+ step=1,
182
+ value=2,
183
+ )
184
+
185
+ gr.Examples(
186
+ examples=examples,
187
+ inputs=[prompt_part2]
188
+ )
189
+
190
+ run_button.click(
191
+ fn=infer,
192
+ inputs=[prompt_part1, prompt_part2, prompt_part3, prompt_part4, prompt_part5, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
193
+ outputs=[front_result, back_result]
194
+ )
195
+
196
+ demo.queue().launch()