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 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import gradio as gr
import numpy as np
import cv2
from PIL import Image, ImageDraw, ImageFont
import requests
import json
import random
import base64
import io
import os
from diffusers import StableDiffusionInpaintPipeline, StableDiffusionPipeline
import torch

# Initialize AI models
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe_inpaint = None
pipe_txt2img = None

# Base model photos (plain people to put outfits on)
MODEL_AVATARS = {
    "Fashion Model A": "https://i.postimg.cc/mD60gXJ6/male-model.png",
    "Fashion Model B": "https://i.postimg.cc/ZYWqF1bn/female-model.png",
    "Fashion Model C": "https://i.postimg.cc/Gh0bPtgg/female-model2.png",
    "Casual Model A": "https://i.postimg.cc/T2b6PG2V/male-model2.png"
}

def add_watermark(img):
    """Add watermark to the image"""
    if isinstance(img, np.ndarray):
        h, w, _ = img.shape
        img = cv2.putText(img, 'AI Outfit Designer', (int(0.02*w), h-10), 
                         cv2.FONT_HERSHEY_SIMPLEX, 0.6, (128, 128, 128), 1, cv2.LINE_AA)
    return img

def initialize_models():
    global pipe_inpaint, pipe_txt2img
    try:
        # Load inpainting model for outfit editing
        pipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(
            "runwayml/stable-diffusion-inpainting",
            torch_dtype=torch.float16 if device == "cuda" else torch.float32,
            safety_checker=None,
            requires_safety_checker=False
        ).to(device)
        
        # Load text-to-image model for outfit generation
        pipe_txt2img = StableDiffusionPipeline.from_pretrained(
            "runwayml/stable-diffusion-v1-5",
            torch_dtype=torch.float16 if device == "cuda" else torch.float32,
            safety_checker=None,
            requires_safety_checker=False
        ).to(device)
        
        if device == "cuda":
            pipe_inpaint.enable_attention_slicing()
            pipe_txt2img.enable_attention_slicing()
            
        return True
    except Exception as e:
        print(f"Error loading models: {e}")
        return False

def create_placeholder_image(text="Model Image", size=(400, 600)):
    """Create placeholder image for models"""
    img = Image.new('RGB', size, color='lightgray')
    draw = ImageDraw.Draw(img)
    
    try:
        font = ImageFont.truetype("arial.ttf", 20)
    except:
        font = ImageFont.load_default()
    
    bbox = draw.textbbox((0, 0), text, font=font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    
    x = (size[0] - text_width) // 2
    y = (size[1] - text_height) // 2
    
    draw.text((x, y), text, fill='black', font=font)
    return np.array(img)

def load_model_image_from_url(url):
    """Load model image from URL"""
    try:
        response = requests.get(url, timeout=10)
        if response.status_code == 200:
            image = Image.open(io.BytesIO(response.content))
            # Convert to RGB if needed
            if image.mode != 'RGB':
                image = image.convert('RGB')
            # Resize to standard size
            image = image.resize((400, 600), Image.Resampling.LANCZOS)
            return np.array(image)
        else:
            return create_placeholder_image("Failed to load model")
    except Exception as e:
        print(f"Error loading model image: {e}")
        return create_placeholder_image("Model loading error")

def get_outfit_tryon_result(model_image, garment_top, garment_bottom, style_prompt="", selected_model=None):
    """Generate outfit try-on result"""
    try:
        # Determine which model to use
        if model_image is not None:
            # Use uploaded image
            base_model = model_image
            model_source = "uploaded photo"
        elif selected_model and selected_model in MODEL_AVATARS:
            # Load selected model from URL
            base_model = load_model_image_from_url(MODEL_AVATARS[selected_model])
            model_source = selected_model
        else:
            return create_placeholder_image("Please upload a photo or select a model")
        
        # If no garments provided, generate based on style
        if garment_top is None and garment_bottom is None and not style_prompt:
            return create_placeholder_image("Please upload garments or describe a style")
        
        if garment_top is None and garment_bottom is None and style_prompt:
            # Generate outfit based on text description
            if pipe_txt2img:
                prompt = f"professional fashion photo of a person wearing {style_prompt}, high quality, detailed clothing, same person as reference"
                negative_prompt = "blurry, low quality, distorted, nude, different person"
                
                generated_image = pipe_txt2img(
                    prompt=prompt,
                    negative_prompt=negative_prompt,
                    num_inference_steps=25,
                    guidance_scale=7.5,
                    width=400,
                    height=600
                ).images[0]
                
                result = np.array(generated_image)
            else:
                # Create styled placeholder showing the base model with text overlay
                result = create_styled_placeholder(base_model, style_prompt)
        else:
            # Apply uploaded garments to the base model
            result = simulate_tryon(base_model, garment_top, garment_bottom, model_source)
        
        # Add watermark
        final_result = add_watermark(result)
        return final_result
        
    except Exception as e:
        print(f"Error in outfit generation: {e}")
        return create_placeholder_image("Generation Error")

def create_styled_placeholder(base_model, style_text):
    """Create a styled placeholder showing base model with style text"""
    if isinstance(base_model, np.ndarray):
        result = base_model.copy()
    else:
        result = create_placeholder_image("Base Model")
    
    # Add style overlay
    img_pil = Image.fromarray(result)
    draw = ImageDraw.Draw(img_pil)
    
    try:
        font = ImageFont.truetype("arial.ttf", 18)
        small_font = ImageFont.truetype("arial.ttf", 14)
    except:
        font = ImageFont.load_default()
        small_font = ImageFont.load_default()
    
    # Add semi-transparent background for text
    overlay = Image.new('RGBA', img_pil.size, (0,0,0,0))
    overlay_draw = ImageDraw.Draw(overlay)
    overlay_draw.rectangle([10, 10, 390, 80], fill=(0,0,0,128))
    
    img_pil = Image.alpha_composite(img_pil.convert('RGBA'), overlay).convert('RGB')
    draw = ImageDraw.Draw(img_pil)
    
    # Add text
    draw.text((20, 20), "Generated Style:", fill='white', font=small_font)
    draw.text((20, 45), style_text[:40] + "..." if len(style_text) > 40 else style_text, 
              fill='yellow', font=font)
    
    return np.array(img_pil)

def simulate_tryon(model_img, garment_top, garment_bottom, model_source=""):
    """Simulate outfit try-on with base model and garments"""
    if isinstance(model_img, np.ndarray):
        result = model_img.copy()
    else:
        result = create_placeholder_image("Base Model")
    
    # Convert to PIL for drawing
    img_pil = Image.fromarray(result)
    draw = ImageDraw.Draw(img_pil)
    
    try:
        font = ImageFont.truetype("arial.ttf", 16)
        small_font = ImageFont.truetype("arial.ttf", 12)
    except:
        font = ImageFont.load_default()
        small_font = ImageFont.load_default()
    
    # Add overlay showing what's being applied
    overlay = Image.new('RGBA', img_pil.size, (0,0,0,0))
    overlay_draw = ImageDraw.Draw(overlay)
    overlay_draw.rectangle([10, 10, 390, 100], fill=(0,100,0,128))
    
    img_pil = Image.alpha_composite(img_pil.convert('RGBA'), overlay).convert('RGB')
    draw = ImageDraw.Draw(img_pil)
    
    y_pos = 20
    draw.text((20, y_pos), f"Base Model: {model_source}", fill='white', font=small_font)
    y_pos += 25
    
    if garment_top is not None:
        draw.text((20, y_pos), "βœ“ Top garment applied", fill='lightgreen', font=font)
        y_pos += 25
    if garment_bottom is not None:
        draw.text((20, y_pos), "βœ“ Bottom garment applied", fill='lightgreen', font=font)
    
    return np.array(img_pil)

def edit_outfit_with_text(current_image, edit_prompt, strength=0.7):
    """Edit the current outfit using text prompt"""
    if current_image is None:
        return None, "Please generate an outfit first!"
    
    if not edit_prompt.strip():
        return current_image, "Please enter an edit prompt!"
    
    try:
        # Convert to PIL if numpy array
        if isinstance(current_image, np.ndarray):
            image = Image.fromarray(current_image)
        else:
            image = current_image
            
        # Resize if needed
        image = image.resize((400, 600))
        
        # Create a simple mask for clothing area
        mask = Image.new('L', (400, 600), 0)
        mask_draw = ImageDraw.Draw(mask)
        mask_draw.rectangle([50, 150, 350, 450], fill=255)  # Clothing area
        
        if pipe_inpaint:
            prompt = f"person wearing {edit_prompt}, high quality fashion photo"
            negative_prompt = "blurry, low quality, distorted, nude"
            
            edited_image = pipe_inpaint(
                prompt=prompt,
                image=image,
                mask_image=mask,
                negative_prompt=negative_prompt,
                num_inference_steps=20,
                strength=strength,
                guidance_scale=7.5
            ).images[0]
            
            return np.array(edited_image), f"βœ… Outfit edited: {edit_prompt}"
        else:
            # Fallback: add text overlay
            draw = ImageDraw.Draw(image)
            draw.text((10, 10), f"Edit: {edit_prompt}", fill='red')
            return np.array(image), f"βœ… Edit applied: {edit_prompt}"
            
    except Exception as e:
        return current_image, f"❌ Error editing: {str(e)}"

# Initialize models
print("Loading AI models...")
models_loaded = initialize_models()
print(f"Models loaded: {models_loaded}")

# Create the main interface
with gr.Blocks(css=".output-image, .input-image, .image-preview {height: 400px !important}", 
               title="AI Outfit Designer") as demo:
    
    # Header
    gr.HTML("""
    <div style="display: flex; justify-content: center; align-items: center; text-align: center;">
        <div>
            <h1>πŸ‘— AI Outfit Designer & Virtual Try-On πŸ‘”</h1>
            <h4>v1.0 - Upload your own photo or choose from our models</h4>
            <div style="display: flex; justify-content: center; align-items: center; text-align: center;">
                <span style="background: #4CAF50; color: white; padding: 5px 10px; border-radius: 15px; margin: 5px;">
                    ✨ AI-Powered
                </span>
                <span style="background: #2196F3; color: white; padding: 5px 10px; border-radius: 15px; margin: 5px;">
                    🎨 Text Editing
                </span>
                <span style="background: #FF9800; color: white; padding: 5px 10px; border-radius: 15px; margin: 5px;">
                    πŸ‘— Virtual Try-On
                </span>
            </div>
        </div>
    </div>
    """)
    
    with gr.Row():
        # Left Column - Model Selection
        with gr.Column():
            gr.HTML("""
            <div style="text-align: center;">
                <h3>πŸ‘€ Step 1: Choose Your Base Model</h3>
                <p>Upload your own photo or select a base model to dress up</p>
            </div>
            """)
            
            model_image = gr.Image(
                sources=['upload', 'clipboard'], 
                type="numpy", 
                label="Upload Your Photo (Optional)",
                height=400
            )
            
            # Model selection dropdown
            model_selector = gr.Dropdown(
                choices=list(MODEL_AVATARS.keys()),
                value="Fashion Model A",
                label="Or Choose a Base Model",
                interactive=True
            )
            
            # Example models gallery (shows actual base model images)
            gr.Markdown("### πŸ“Έ Available Base Models")
            
            # Create a function to show model preview
            def show_model_preview(model_name):
                if model_name in MODEL_AVATARS:
                    return load_model_image_from_url(MODEL_AVATARS[model_name])
                return create_placeholder_image("Select a model")
            
            model_preview = gr.Image(label="Selected Base Model Preview", height=300, interactive=False)
            
            # Update preview when dropdown changes
            model_selector.change(fn=show_model_preview, inputs=[model_selector], outputs=[model_preview])
        
        # Middle Column - Garments and Style
        with gr.Column():
            gr.HTML("""
            <div style="text-align: center;">
                <h3>πŸ‘• Step 2: Choose Style or Upload Garments</h3>
                <p>Upload specific garments or describe your desired style</p>
            </div>
            """)
            
            # Style prompt (new feature)
            style_prompt = gr.Textbox(
                label="Describe Your Desired Style",
                placeholder="e.g., 'casual jeans and t-shirt', 'elegant black dress', 'business suit'",
                lines=2
            )
            
            with gr.Row():
                garment_top = gr.Image(
                    sources='upload', 
                    type="numpy", 
                    label="Top Garment (Optional)",
                    height=200
                )
                garment_bottom = gr.Image(
                    sources='upload', 
                    type="numpy", 
                    label="Bottom Garment (Optional)",
                    height=200
                )
            
            # Quick style buttons
            gr.Markdown("### 🎯 Quick Style Options")
            with gr.Row():
                casual_btn = gr.Button("πŸ‘• Casual", size="sm")
                business_btn = gr.Button("πŸ’Ό Business", size="sm")
                formal_btn = gr.Button("🎩 Formal", size="sm")
                trendy_btn = gr.Button("✨ Trendy", size="sm")
            
            generate_btn = gr.Button("🎨 Generate Outfit", variant="primary", size="lg")
        
        # Right Column - Results
        with gr.Column():
            gr.HTML("""
            <div style="text-align: center;">
                <h3>✨ Your Generated Outfit</h3>
            </div>
            """)
            
            result_image = gr.Image(label="Generated Outfit", height=400)
            
            generation_status = gr.Markdown("Upload a photo or select a model, then click Generate!")
    
    # Separator
    gr.HTML("<hr style='margin: 30px 0; border: 1px solid #ddd;'>")
    
    # Bottom Section - Text Editor
    gr.HTML("""
    <div style="text-align: center; margin: 20px;">
        <h2>✏️ Step 3: Edit Your Outfit with AI</h2>
        <p>Use natural language to modify your generated outfit</p>
    </div>
    """)
    
    with gr.Row():
        with gr.Column():
            edit_prompt = gr.Textbox(
                label="Describe Your Edits",
                placeholder="e.g., 'change shirt to red', 'add a jacket', 'make it more colorful'",
                lines=3
            )
            
            with gr.Row():
                edit_strength = gr.Slider(
                    minimum=0.3,
                    maximum=1.0,
                    value=0.7,
                    step=0.1,
                    label="Edit Strength"
                )
            
            edit_btn = gr.Button("πŸ–ŒοΈ Apply Edit", variant="secondary", size="lg")
            
            # Quick edit options
            gr.Markdown("### ⚑ Quick Edits")
            with gr.Row():
                add_jacket_btn = gr.Button("Add Jacket", size="sm")
                change_color_btn = gr.Button("Change Colors", size="sm")
                make_formal_btn = gr.Button("Make Formal", size="sm")
                add_accessories_btn = gr.Button("Add Accessories", size="sm")
        
        with gr.Column():
            edited_result = gr.Image(label="Edited Outfit", height=400)
            edit_status = gr.Markdown("Generate an outfit first, then describe your edits!")
    
    # Examples Section
    gr.HTML("<hr style='margin: 30px 0; border: 1px solid #ddd;'>")
    gr.Markdown("## πŸ“š Examples & Tips")
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("""
            ### 🎯 Style Prompt Examples:
            - "Casual weekend outfit with jeans"
            - "Professional business attire" 
            - "Elegant evening dress for dinner"
            - "Sporty athleisure wear"
            - "Vintage 1950s inspired look"
            """)
        
        with gr.Column():
            gr.Markdown("""
            ### ✏️ Edit Prompt Examples:
            - "Change the shirt to a red color"
            - "Add a denim jacket over the outfit"
            - "Make the dress shorter and more casual"
            - "Add gold jewelry and accessories"
            - "Change to a winter coat and boots"
            """)
    
    # Store current image for editing
    current_generated = gr.State(None)
    
    # Event Handlers
    def generate_outfit(model_img, garment1, garment2, style_text, selected_model):
        result = get_outfit_tryon_result(model_img, garment1, garment2, style_text, selected_model)
        
        if model_img is not None:
            model_info = "uploaded photo"
        elif selected_model:
            model_info = selected_model
        else:
            model_info = "no model selected"
            
        status = f"βœ… Generated outfit on {model_info}"
        if style_text:
            status += f" with style: {style_text}"
        if garment1 is not None or garment2 is not None:
            status += f" using uploaded garments"
            
        return result, result, status
    
    def apply_edit(current_img, edit_text, strength):
        if current_img is None:
            return None, "❌ Please generate an outfit first!"
        
        edited_img, status = edit_outfit_with_text(current_img, edit_text, strength)
        return edited_img, status
    
    # Connect events
    generate_btn.click(
        fn=generate_outfit,
        inputs=[model_image, garment_top, garment_bottom, style_prompt, model_selector],
        outputs=[result_image, current_generated, generation_status]
    )
    
    edit_btn.click(
        fn=apply_edit,
        inputs=[current_generated, edit_prompt, edit_strength],
        outputs=[edited_result, edit_status]
    )
    
    # Quick style buttons
    casual_btn.click(lambda: "casual jeans and t-shirt outfit", outputs=[style_prompt])
    business_btn.click(lambda: "professional business suit", outputs=[style_prompt])
    formal_btn.click(lambda: "elegant formal evening wear", outputs=[style_prompt])
    trendy_btn.click(lambda: "trendy fashionable modern outfit", outputs=[style_prompt])
    
    # Quick edit buttons
    add_jacket_btn.click(lambda: "add a stylish jacket", outputs=[edit_prompt])
    change_color_btn.click(lambda: "change colors to be more vibrant", outputs=[edit_prompt])
    make_formal_btn.click(lambda: "make the outfit more formal and elegant", outputs=[edit_prompt])
    add_accessories_btn.click(lambda: "add fashionable accessories and jewelry", outputs=[edit_prompt])

if __name__ == "__main__":
    demo.launch()