|
import gradio as gr |
|
import torch |
|
from PIL import Image |
|
import numpy as np |
|
import random |
|
import cv2 |
|
from diffusers import DiffusionPipeline, StableDiffusionPipeline |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
if torch.cuda.is_available(): |
|
torch.cuda.max_memory_allocated(device=device) |
|
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True) |
|
pipe.enable_xformers_memory_efficient_attention() |
|
pipe = pipe.to(device) |
|
else: |
|
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True) |
|
pipe = pipe.to(device) |
|
|
|
MAX_SEED = np.iinfo(np.int32).max |
|
MAX_IMAGE_SIZE = 1024 |
|
|
|
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps): |
|
if randomize_seed: |
|
seed = random.randint(0, MAX_SEED) |
|
|
|
generator = torch.Generator().manual_seed(seed) |
|
|
|
image = pipe( |
|
prompt=prompt, |
|
negative_prompt=negative_prompt, |
|
guidance_scale=guidance_scale, |
|
num_inference_steps=num_inference_steps, |
|
width=width, |
|
height=height, |
|
generator=generator |
|
).images[0] |
|
|
|
return image |
|
|
|
|
|
def generate_tshirt_design(style, color, graphics, text=None): |
|
prompt = f"T-shirt design, style: {style}, color: {color}, graphics: {graphics}" |
|
if text: |
|
prompt += f", text: {text}" |
|
image = pipe(prompt).images[0] |
|
return image |
|
|
|
|
|
examples = [ |
|
["Casual", "White", "Logo: MyBrand", None], |
|
["Formal", "Black", "Text: Hello World", "Custom text"], |
|
["Sports", "Red", "Graphic: Team logo", None], |
|
] |
|
|
|
css = """ |
|
#col-container { |
|
margin: 0 auto; |
|
max-width: 520px; |
|
} |
|
""" |
|
|
|
if torch.cuda.is_available(): |
|
power_device = "GPU" |
|
else: |
|
power_device = "CPU" |
|
|
|
with gr.Blocks(css=css) as demo: |
|
with gr.Column(elem_id="col-container"): |
|
gr.Markdown(f""" |
|
# T-shirt Mockup Generator with Rookus AI |
|
Currently running on {power_device}. |
|
""") |
|
|
|
with gr.Row(): |
|
style = gr.Dropdown( |
|
label="T-shirt Style", |
|
choices=["Casual", "Formal", "Sports"], |
|
value="Casual", |
|
container=False, |
|
) |
|
|
|
run_button = gr.Button("Generate Mockup", scale=0) |
|
|
|
result = gr.Image(label="Mockup", show_label=False) |
|
|
|
with gr.Accordion("Design Options", open=False): |
|
color = gr.Radio( |
|
label="T-shirt Color", |
|
choices=[ |
|
"White", "Black", "Blue", "Red", "Green", "Yellow", "Pink", "Purple", "Orange", "Brown", |
|
"Gray", "Maroon", "Navy", "Teal", "Lime", "Olive", "Cyan", "Magenta", "Beige", "Turquoise", |
|
"Gold", "Silver", "Lavender", "Mint", "Coral", "Indigo" |
|
], |
|
value="White", |
|
) |
|
|
|
graphics = gr.Textbox( |
|
label="Graphics/Logo", |
|
placeholder="Enter graphics or logo details", |
|
visible=True, |
|
) |
|
|
|
text = gr.Textbox( |
|
label="Text (optional)", |
|
placeholder="Enter optional text", |
|
visible=True, |
|
) |
|
|
|
gr.Examples( |
|
examples=examples, |
|
inputs=[style, color, graphics, text] |
|
) |
|
|
|
def generate_tshirt_mockup(style, color, graphics, text=None): |
|
|
|
design_image = generate_tshirt_design(style, color, graphics, text) |
|
|
|
|
|
mockup_template = Image.open("https://th.bing.com/th/id/OIP.oYpJxkyDYCFdF4GJulkFcQHaFj?rs=1&pid=ImgDetMain") |
|
|
|
|
|
design_np = np.array(design_image) |
|
mockup_np = np.array(mockup_template) |
|
|
|
|
|
design_resized = cv2.resize(design_np, (mockup_np.shape[1] // 2, mockup_np.shape[0] // 2)) |
|
|
|
|
|
y_offset = mockup_np.shape[0] // 4 |
|
x_offset = mockup_np.shape[1] // 4 |
|
y1, y2 = y_offset, y_offset + design_resized.shape[0] |
|
x1, x2 = x_offset, x_offset + design_resized.shape[1] |
|
|
|
alpha_s = design_resized[:, :, 3] / 255.0 if design_resized.shape[2] == 4 else np.ones(design_resized.shape[:2]) |
|
alpha_l = 1.0 - alpha_s |
|
|
|
for c in range(0, 3): |
|
mockup_np[y1:y2, x1:x2, c] = (alpha_s * design_resized[:, :, c] + |
|
alpha_l * mockup_np[y1:y2, x1:x2, c]) |
|
|
|
|
|
result_image = Image.fromarray(mockup_np) |
|
|
|
return result_image |
|
|
|
run_button.click( |
|
fn=generate_tshirt_mockup, |
|
inputs=[style, color, graphics, text], |
|
outputs=[result] |
|
) |
|
|
|
demo.queue().launch(share=True) |
|
|