Spaces:
Running
Running
fragger246
commited on
Commit
•
a35722a
1
Parent(s):
fca7077
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
-
from PIL import Image
|
4 |
import numpy as np
|
5 |
-
import
|
6 |
-
from diffusers import
|
|
|
7 |
|
8 |
-
# Setup the model
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
model_id = "s3nh/artwork-arcane-stable-diffusion"
|
11 |
-
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32)
|
12 |
-
pipe = pipe.to(device)
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
return image
|
19 |
|
20 |
-
# Remove background from the generated design
|
21 |
-
def remove_background(design_image):
|
22 |
-
design_np = np.array(design_image)
|
23 |
-
gray = cv2.cvtColor(design_np, cv2.COLOR_BGR2GRAY)
|
24 |
-
_, alpha = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
|
25 |
-
b, g, r = cv2.split(design_np)
|
26 |
-
rgba = [b, g, r, alpha]
|
27 |
-
design_np = cv2.merge(rgba, 4)
|
28 |
-
return design_np
|
29 |
-
|
30 |
-
# Blend design with T-shirt mockup
|
31 |
-
def blend_design_with_mockup(mockup, design):
|
32 |
-
mockup_np = np.array(mockup)
|
33 |
-
design_np = np.array(design)
|
34 |
-
|
35 |
-
# Ensure the design is RGBA
|
36 |
-
if design_np.shape[2] == 3:
|
37 |
-
alpha = np.ones((design_np.shape[0], design_np.shape[1], 1), dtype=design_np.dtype) * 255
|
38 |
-
design_np = np.concatenate((design_np, alpha), axis=2)
|
39 |
-
|
40 |
-
design_resized = cv2.resize(design_np, (mockup_np.shape[1] // 4, mockup_np.shape[0] // 4)) # Adjust size as needed
|
41 |
-
|
42 |
-
y_offset = (mockup_np.shape[0] - design_resized.shape[0]) // 2
|
43 |
-
x_offset = (mockup_np.shape[1] - design_resized.shape[1]) // 2
|
44 |
-
|
45 |
-
y1, y2 = y_offset, y_offset + design_resized.shape[0]
|
46 |
-
x1, x2 = x_offset, x_offset + design_resized.shape[1]
|
47 |
-
|
48 |
-
alpha_s = design_resized[:, :, 3] / 255.0
|
49 |
-
alpha_l = 1.0 - alpha_s
|
50 |
-
|
51 |
-
for c in range(0, 3):
|
52 |
-
mockup_np[y1:y2, x1:x2, c] = (alpha_s * design_resized[:, :, c] +
|
53 |
-
alpha_l * mockup_np[y1:y2, x1:x2, c])
|
54 |
-
|
55 |
-
result_image = Image.fromarray(mockup_np)
|
56 |
-
return result_image
|
57 |
-
|
58 |
-
# T-shirt mockup generator with Gradio interface
|
59 |
examples = [
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
]
|
64 |
|
65 |
css = """
|
@@ -69,47 +51,134 @@ css = """
|
|
69 |
}
|
70 |
"""
|
71 |
|
|
|
|
|
|
|
|
|
|
|
72 |
with gr.Blocks(css=css) as demo:
|
|
|
73 |
with gr.Column(elem_id="col-container"):
|
74 |
-
gr.Markdown("""
|
75 |
-
#
|
|
|
76 |
""")
|
77 |
-
|
78 |
with gr.Row():
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
83 |
)
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
gr.Examples(
|
90 |
examples=examples,
|
91 |
-
inputs=[
|
92 |
)
|
93 |
|
94 |
-
def generate_tshirt_mockup(text):
|
95 |
-
# Generate T-shirt design
|
96 |
-
design_image = generate_tshirt_design(text)
|
97 |
-
|
98 |
-
# Remove background from design image
|
99 |
-
design_np = remove_background(design_image)
|
100 |
-
|
101 |
-
# Load blank T-shirt mockup template image
|
102 |
-
mockup_template = Image.open("OIP (3).jpeg")
|
103 |
-
|
104 |
-
# Blend design with mockup
|
105 |
-
result_image = blend_design_with_mockup(mockup_template, Image.fromarray(design_np))
|
106 |
-
|
107 |
-
return result_image
|
108 |
-
|
109 |
run_button.click(
|
110 |
-
fn=
|
111 |
-
inputs=[
|
112 |
outputs=[result]
|
113 |
)
|
114 |
|
115 |
-
demo.queue().launch()
|
|
|
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"{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 = """
|
|
|
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()
|