Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,59 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import FluxPipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import spaces
|
| 6 |
|
| 7 |
+
@spaces.GPU
|
| 8 |
+
def load_flux_model():
|
| 9 |
+
pipe = FluxPipeline.from_pretrained(
|
| 10 |
+
"black-forest-labs/FLUX.1-dev",
|
| 11 |
+
torch_dtype=torch.bfloat16
|
| 12 |
+
).to("cuda")
|
| 13 |
+
return pipe
|
| 14 |
|
| 15 |
+
flux_pipe = load_flux_model()
|
| 16 |
+
|
| 17 |
+
def generate_design(images, apparel_type, guidance_scale=7.0, steps=30, num_outputs=1):
|
| 18 |
+
if not images:
|
| 19 |
+
raise gr.Error("Please upload at least one image.")
|
| 20 |
+
|
| 21 |
+
# Create a reference embedding by averaging CLIP image embeddings
|
| 22 |
+
# This gives the model a style/content reference
|
| 23 |
+
ref_embeds = []
|
| 24 |
+
for img in images:
|
| 25 |
+
img = img.convert("RGB")
|
| 26 |
+
embed = flux_pipe.encode_image(img)
|
| 27 |
+
ref_embeds.append(embed)
|
| 28 |
+
combined_embed = torch.mean(torch.stack(ref_embeds), dim=0)
|
| 29 |
+
|
| 30 |
+
prompt = f"Design a {apparel_type} inspired by the given references, fashionable, photorealistic, detailed fabric textures."
|
| 31 |
+
|
| 32 |
+
outputs = flux_pipe(
|
| 33 |
+
prompt=prompt,
|
| 34 |
+
image_embeds=combined_embed, # use custom conditioning
|
| 35 |
+
guidance_scale=guidance_scale,
|
| 36 |
+
num_inference_steps=steps,
|
| 37 |
+
num_images_per_prompt=num_outputs
|
| 38 |
+
).images
|
| 39 |
+
|
| 40 |
+
return outputs
|
| 41 |
+
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("# Fashion Design Generator 👗✨")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
images = gr.Image(type="pil", label="Upload reference images", tool="editor", image_mode="RGB", sources=["upload"], elem_id="multi_upload", interactive=True, value=None, every=1, container=True, show_label=True, mirror_webcam=False, height=300, allow_multiple=True)
|
| 48 |
+
apparel_type = gr.Textbox(label="Type of Apparel", placeholder="e.g., summer dress, jacket, kurta")
|
| 49 |
+
guidance = gr.Slider(1, 15, 7.0, 0.1, label="Guidance Scale")
|
| 50 |
+
steps = gr.Slider(10, 50, 30, 1, label="Steps")
|
| 51 |
+
num_outputs = gr.Slider(1, 4, 1, 1, label="Number of Designs")
|
| 52 |
+
btn = gr.Button("Generate Designs", variant="primary")
|
| 53 |
+
with gr.Column():
|
| 54 |
+
gallery = gr.Gallery(label="Generated Designs", height=300)
|
| 55 |
+
|
| 56 |
+
btn.click(generate_design, inputs=[images, apparel_type, guidance, steps, num_outputs], outputs=gallery)
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
demo.launch()
|