Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,97 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
-
from transformers import pipeline
|
| 5 |
-
from torchvision import models, transforms
|
| 6 |
|
| 7 |
-
|
| 8 |
-
text_to_image_pipeline = pipeline("text-to-image-generation", model="CompVis/stable-diffusion-v1-4")
|
| 9 |
-
segmentation_model = models.segmentation.deeplabv3_resnet101(pretrained=True)
|
| 10 |
-
segmentation_model.eval()
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
input_tensor = preprocess(image)
|
| 23 |
-
input_batch = input_tensor.unsqueeze(0)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
output_predictions = output.argmax(0)
|
| 28 |
-
mask = output_predictions.byte().cpu().numpy()
|
| 29 |
-
|
| 30 |
-
return mask
|
| 31 |
-
|
| 32 |
-
# Function to generate base image
|
| 33 |
-
def generate_base_image(base_prompt_part1, base_prompt_color, base_prompt_clothing):
|
| 34 |
-
# Combine the parts to create the full base prompt
|
| 35 |
-
base_prompt = f"{base_prompt_part1} {base_prompt_color} {base_prompt_clothing}"
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
return
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
generated_design = generated_design.resize(base_image.size)
|
| 54 |
-
|
| 55 |
-
# Paste the design onto the clothing area
|
| 56 |
-
clothing_area = Image.composite(generated_design, base_image, Image.fromarray(clothing_mask*255))
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
final_image = generate_and_paste_design(base_image, design_prompt)
|
| 73 |
-
|
| 74 |
-
return final_image
|
| 75 |
|
| 76 |
-
|
| 77 |
-
fn=full_process,
|
| 78 |
-
inputs=[base_prompt_part1_input, base_prompt_color_input, base_prompt_clothing_input, design_prompt_input],
|
| 79 |
-
outputs=output_image,
|
| 80 |
-
title="Design and Paste on Clothing",
|
| 81 |
-
description="Generate a base clothing image from the given prompts and paste the generated design onto it."
|
| 82 |
-
).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 |
+
try:
|
| 30 |
+
image = pipe(
|
| 31 |
+
prompt=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 |
+
print("Image generated successfully.") # Debug: Confirm image generation
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Error generating image: {e}")
|
| 42 |
+
return None
|
| 43 |
|
| 44 |
+
return image
|
| 45 |
|
| 46 |
+
examples = [
|
| 47 |
+
["red", "t-shirt", "yellow stripes"],
|
| 48 |
+
["blue", "hoodie", "minimalist"],
|
| 49 |
+
["red", "sweatshirt", "geometric design"],
|
| 50 |
+
]
|
| 51 |
|
| 52 |
+
css = """
|
| 53 |
+
#col-container {
|
| 54 |
+
margin: 0 auto;
|
| 55 |
+
max-width: 520px;
|
| 56 |
+
}
|
| 57 |
+
"""
|
| 58 |
|
| 59 |
+
power_device = "GPU" if torch.cuda.is_available() else "CPU"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
with gr.Blocks(css=css) as demo:
|
| 62 |
+
with gr.Column(elem_id="col-container"):
|
| 63 |
+
gr.Markdown(f"""
|
| 64 |
+
# Text-to-Image Gradio Template
|
| 65 |
+
Currently running on {power_device}.
|
| 66 |
+
""")
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
prompt_part1 = gr.Textbox(value="a single", label="Prompt Part 1", show_label=False, interactive=False, container=False, elem_id="prompt_part1", visible=False)
|
| 70 |
+
prompt_part2 = gr.Textbox(label="color", show_label=False, max_lines=1, placeholder="color (e.g., color category)", container=False)
|
| 71 |
+
prompt_part3 = gr.Textbox(label="dress_type", show_label=False, max_lines=1, placeholder="dress_type (e.g., t-shirt, sweatshirt, shirt, hoodie)", container=False)
|
| 72 |
+
prompt_part4 = gr.Textbox(label="design", show_label=False, max_lines=1, placeholder="design", container=False)
|
| 73 |
+
prompt_part5 = gr.Textbox(value="hanging on the plain grey wall", label="Prompt Part 5", show_label=False, interactive=False, container=False, elem_id="prompt_part5", visible=False)
|
| 74 |
+
run_button = gr.Button("Run", scale=0)
|
| 75 |
+
|
| 76 |
+
result = gr.Image(label="Result", show_label=False)
|
| 77 |
|
| 78 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 79 |
+
negative_prompt = gr.Textbox(label="Negative prompt", max_lines=1, placeholder="Enter a negative prompt", visible=False)
|
| 80 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 81 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 82 |
+
with gr.Row():
|
| 83 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
|
| 84 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
|
| 85 |
+
with gr.Row():
|
| 86 |
+
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=0.0)
|
| 87 |
+
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=12, step=1, value=2)
|
| 88 |
+
|
| 89 |
+
gr.Examples(examples=examples, inputs=[prompt_part2, prompt_part3, prompt_part4])
|
| 90 |
|
| 91 |
+
run_button.click(
|
| 92 |
+
fn=infer,
|
| 93 |
+
inputs=[prompt_part1, prompt_part2, prompt_part3, prompt_part4, prompt_part5, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 94 |
+
outputs=[result]
|
| 95 |
+
)
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
demo.queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|