File size: 7,141 Bytes
6a2ab89
2648824
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5559e0
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
import spaces
import gradio as gr
from PIL import Image
import torch
import io
import base64
import tempfile
import os
from diffusers import AutoPipelineForInpainting, AutoencoderKL
import numpy as np

# Global variables for models
pipeline = None
segment_body = None

def load_models():
    """Load all required models"""
    global pipeline, segment_body
    
    print("πŸ”„ Loading VAE...")
    vae = AutoencoderKL.from_pretrained(
        "madebyollin/sdxl-vae-fp16-fix",
        torch_dtype=torch.float16
    )
    
    print("πŸ”„ Loading inpainting pipeline...")
    pipeline = AutoPipelineForInpainting.from_pretrained(
        "diffusers/stable-diffusion-xl-1.0-inpainting-0.1",
        vae=vae,
        torch_dtype=torch.float16,
        variant="fp16",
        use_safetensors=True
    )
    
    pipeline = pipeline.to("cuda")
    
    print("πŸ”„ Loading IP-Adapter...")
    pipeline.load_ip_adapter(
        "h94/IP-Adapter",
        subfolder="sdxl_models",
        weight_name="ip-adapter_sdxl.bin",
        low_cpu_mem_usage=True
    )
    
    print("πŸ”„ Loading body segmentation...")
    try:
        from SegBody import segment_body as seg_func
        segment_body = seg_func
        print("βœ… Body segmentation loaded!")
    except ImportError:
        print("⚠️ SegBody module not found, segmentation will be disabled")
    
    print("βœ… All models loaded successfully!")

# Load models on startup
load_models()

def create_mask(person_img):
    """Generate body segmentation mask"""
    if segment_body is None:
        # Create a simple fallback mask (full body) if segmentation not available
        return Image.new('L', (512, 512), 255)
    
    try:
        # Try calling segment_body - it might expect a file path or PIL Image
        try:
            # First try with PIL Image directly
            seg_image, mask_img = segment_body(person_img, face=False)
        except (TypeError, AttributeError):
            # If that fails, try with file path
            with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:
                temp_path = tmp_file.name
                person_img.save(temp_path)
            
            seg_image, mask_img = segment_body(temp_path, face=False)
            os.unlink(temp_path)
        
        # Ensure mask is PIL Image and resize
        if isinstance(mask_img, str):
            mask_img = Image.open(mask_img).convert('L')
        mask_img = mask_img.resize((512, 512))
        return mask_img
        
    except Exception as e:
        print(f"⚠️ Segmentation failed: {e}, using full mask")
        return Image.new('L', (512, 512), 255)

@spaces.GPU
def virtual_tryon(
    person_image,
    clothing_image,
    prompt="photorealistic, perfect body, beautiful skin, realistic skin, natural skin",
    negative_prompt="ugly, bad quality, bad anatomy, deformed body, deformed hands, deformed feet, deformed face, deformed clothing, deformed skin, bad skin, leggings, tights, stockings",
    ip_scale=0.8,
    strength=0.99,
    guidance_scale=7.5,
    num_steps=50
):
    """
    Virtual Try-On function
    
    Args:
        person_image: PIL Image of the person
        clothing_image: PIL Image of the clothing
        prompt: Generation prompt
        negative_prompt: Negative prompt
        ip_scale: IP-Adapter influence (0.0-1.0)
        strength: Inpainting strength (0.0-1.0)
        guidance_scale: CFG scale
        num_steps: Number of inference steps
    
    Returns:
        Generated PIL Image
    """
    try:
        if pipeline is None:
            raise ValueError("Models not loaded yet")
        
        print("πŸ“₯ Processing images...")
        # Ensure images are PIL Images and resize
        if isinstance(person_image, np.ndarray):
            person_image = Image.fromarray(person_image)
        if isinstance(clothing_image, np.ndarray):
            clothing_image = Image.fromarray(clothing_image)
            
        person_img = person_image.convert('RGB').resize((512, 512))
        clothing_img = clothing_image.convert('RGB').resize((512, 512))
        
        # Generate body segmentation mask
        print("🎭 Generating segmentation mask...")
        mask_img = create_mask(person_img)
        
        # Set IP-Adapter scale
        pipeline.set_ip_adapter_scale(ip_scale)
        
        # Generate virtual try-on
        print("🎨 Generating virtual try-on...")
        result = pipeline(
            prompt=prompt,
            negative_prompt=negative_prompt,
            image=person_img,
            mask_image=mask_img,
            ip_adapter_image=clothing_img,
            strength=strength,
            guidance_scale=guidance_scale,
            num_inference_steps=num_steps,
        )
        
        generated_image = result.images[0]
        print("βœ… Generation completed!")
        
        return generated_image
    
    except Exception as e:
        print(f"❌ Error: {str(e)}")
        raise gr.Error(f"Generation failed: {str(e)}")

# Create Gradio interface with simpler structure for better compatibility
demo = gr.Interface(
    fn=virtual_tryon,
    inputs=[
        gr.Image(label="Person Image", type="pil"),
        gr.Image(label="Clothing Image", type="pil"),
        gr.Textbox(
            label="Prompt",
            value="photorealistic, perfect body, beautiful skin, realistic skin, natural skin",
            lines=2
        ),
        gr.Textbox(
            label="Negative Prompt",
            value="ugly, bad quality, bad anatomy, deformed body, deformed hands, deformed feet, deformed face, deformed clothing, deformed skin, bad skin, leggings, tights, stockings",
            lines=2
        ),
        gr.Slider(
            minimum=0.0,
            maximum=1.0,
            value=0.8,
            step=0.05,
            label="IP-Adapter Scale (clothing influence)"
        ),
        gr.Slider(
            minimum=0.0,
            maximum=1.0,
            value=0.99,
            step=0.01,
            label="Inpainting Strength"
        ),
        gr.Slider(
            minimum=1.0,
            maximum=20.0,
            value=7.5,
            step=0.5,
            label="Guidance Scale"
        ),
        gr.Slider(
            minimum=20,
            maximum=100,
            value=50,
            step=5,
            label="Number of Steps"
        )
    ],
    outputs=gr.Image(label="Result", type="pil"),
    title="πŸ‘• Virtual Try-On",
    description="""
    Upload a photo of yourself and a clothing item to see how it looks on you!
    
    **Powered by Stable Diffusion XL + IP-Adapter + ZeroGPU**
    
    ### Tips for Best Results:
    - Use clear, well-lit photos
    - Person should be facing forward
    - Clothing image should show the item clearly
    - Higher steps = better quality but slower
    - Adjust IP-Adapter scale to control how much the clothing influences the result
    """,
    examples=None,
    cache_examples=False,
    allow_flagging="never"
)

# Launch the app
if __name__ == "__main__":
    demo.queue(max_size=20)
    demo.launch(server_name="0.0.0.0", server_port=7860, share=True)