| import torch |
| import gradio as gr |
| from PIL import Image |
| from diffusers import AutoPipelineForImage2Image |
|
|
| MODEL_ID = "stabilityai/stable-diffusion-xl-base-1.0" |
|
|
| device = "cuda" if torch.cuda.is_available() else "cpu" |
| dtype = torch.float16 if device == "cuda" else torch.float32 |
|
|
| pipe = AutoPipelineForImage2Image.from_pretrained( |
| MODEL_ID, |
| torch_dtype=dtype, |
| use_safetensors=True, |
| variant="fp16" if device == "cuda" else None, |
| ) |
| pipe = pipe.to(device) |
|
|
| def build_prompt(style, mood, output_style): |
| base = "realistic Indonesian Muslim woman, modest fashion styling, natural proportions, clean composition" |
|
|
| style_map = { |
| "Casual Muslimah": "young Indonesian Muslim woman, simple elegant hijab look", |
| "Elegant Modest": "elegant Indonesian Muslim woman, refined modest fashion look", |
| "Daily Hijab Look": "natural daily hijab fashion styling", |
| "Premium Boutique": "premium boutique modest fashion presentation", |
| } |
|
|
| mood_map = { |
| "Studio White": "clean white studio background, soft commercial lighting", |
| "Soft Indoor Cafe": "cozy indoor cafe setting, soft daylight", |
| "Minimalist Home": "modern minimalist home interior, natural light", |
| "Boutique Display": "elegant boutique fashion display background", |
| } |
|
|
| output_map = { |
| "Clean Catalog Promo": "ecommerce catalog promo image", |
| "Lifestyle Content": "social media lifestyle fashion image", |
| "Soft Premium Campaign": "premium campaign style fashion visual", |
| } |
|
|
| return f"{base}, {style_map[style]}, {mood_map[mood]}, {output_map[output_style]}" |
|
|
| def generate(image, style, mood, output_style, strength, guidance): |
| if image is None: |
| raise gr.Error("Upload foto produk dulu.") |
|
|
| image = image.convert("RGB").resize((1024, 1024)) |
| prompt = build_prompt(style, mood, output_style) |
|
|
| result = pipe( |
| prompt=prompt, |
| image=image, |
| strength=strength, |
| guidance_scale=guidance, |
| ).images[0] |
|
|
| return result, prompt |
|
|
| with gr.Blocks() as demo: |
| gr.Markdown("# 🛍️ Ecommerce Image Generator") |
| gr.Markdown("Upload hijab product photo to create modest fashion promo preview.") |
|
|
| with gr.Row(): |
| image_input = gr.Image(type="pil", label="Upload Product Photo") |
| image_output = gr.Image(label="Generated Preview") |
|
|
| with gr.Row(): |
| style = gr.Dropdown( |
| ["Casual Muslimah","Elegant Modest","Daily Hijab Look","Premium Boutique"], |
| value="Casual Muslimah", |
| label="Model Style" |
| ) |
|
|
| mood = gr.Dropdown( |
| ["Studio White","Soft Indoor Cafe","Minimalist Home","Boutique Display"], |
| value="Studio White", |
| label="Background Mood" |
| ) |
|
|
| output_style = gr.Dropdown( |
| ["Clean Catalog Promo","Lifestyle Content","Soft Premium Campaign"], |
| value="Clean Catalog Promo", |
| label="Output Style" |
| ) |
|
|
| strength = gr.Slider(0.2,0.8,value=0.45,step=0.05,label="Transformation Strength") |
| guidance = gr.Slider(4,10,value=7,step=0.5,label="Guidance Scale") |
|
|
| btn = gr.Button("Generate Preview") |
|
|
| prompt_box = gr.Textbox(label="Auto Prompt Used") |
|
|
| btn.click( |
| fn=generate, |
| inputs=[image_input,style,mood,output_style,strength,guidance], |
| outputs=[image_output,prompt_box] |
| ) |
|
|
| demo.launch() |