File size: 1,722 Bytes
db3aaf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import InferenceClient
import PIL.Image
import io

# Initialize the Hugging Face client
HF_TOKEN = "your_token_here"  # Replace with your token

client = InferenceClient(
    model="Kwai-Kolors/Kolors-Virtual-Try-On",
    token=HF_TOKEN
)

def virtual_try_on(person_image, garment_image):
    """
    Process the virtual try-on request
    Args:
        person_image: PIL Image of the person
        garment_image: PIL Image of the garment
    Returns:
        PIL Image of the result
    """
    try:
        # Convert images to bytes
        person_bytes = io.BytesIO()
        garment_bytes = io.BytesIO()
        person_image.save(person_bytes, format='PNG')
        garment_image.save(garment_bytes, format='PNG')
        
        # Make API request
        response = client.post(
            json={
                "inputs": [
                    {"image": person_bytes.getvalue()},
                    {"image": garment_bytes.getvalue()}
                ]
            }
        )
        
        # Convert response to image
        result_image = PIL.Image.open(io.BytesIO(response))
        return result_image, "Success"
    except Exception as e:
        return None, f"Error: {str(e)}"

# Create Gradio interface
demo = gr.Interface(
    fn=virtual_try_on,
    inputs=[
        gr.Image(type="pil", label="Person Image"),
        gr.Image(type="pil", label="Garment Image")
    ],
    outputs=[
        gr.Image(type="pil", label="Result"),
        gr.Text(label="Status")
    ],
    title="Virtual Try-On API",
    description="Upload a person image and a garment image to see how the garment would look on the person."
)

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