Spaces:
Runtime error
Runtime error
create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import PIL.Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# Initialize the Hugging Face client
|
| 7 |
+
HF_TOKEN = "your_token_here" # Replace with your token
|
| 8 |
+
|
| 9 |
+
client = InferenceClient(
|
| 10 |
+
model="Kwai-Kolors/Kolors-Virtual-Try-On",
|
| 11 |
+
token=HF_TOKEN
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def virtual_try_on(person_image, garment_image):
|
| 15 |
+
"""
|
| 16 |
+
Process the virtual try-on request
|
| 17 |
+
Args:
|
| 18 |
+
person_image: PIL Image of the person
|
| 19 |
+
garment_image: PIL Image of the garment
|
| 20 |
+
Returns:
|
| 21 |
+
PIL Image of the result
|
| 22 |
+
"""
|
| 23 |
+
try:
|
| 24 |
+
# Convert images to bytes
|
| 25 |
+
person_bytes = io.BytesIO()
|
| 26 |
+
garment_bytes = io.BytesIO()
|
| 27 |
+
person_image.save(person_bytes, format='PNG')
|
| 28 |
+
garment_image.save(garment_bytes, format='PNG')
|
| 29 |
+
|
| 30 |
+
# Make API request
|
| 31 |
+
response = client.post(
|
| 32 |
+
json={
|
| 33 |
+
"inputs": [
|
| 34 |
+
{"image": person_bytes.getvalue()},
|
| 35 |
+
{"image": garment_bytes.getvalue()}
|
| 36 |
+
]
|
| 37 |
+
}
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Convert response to image
|
| 41 |
+
result_image = PIL.Image.open(io.BytesIO(response))
|
| 42 |
+
return result_image, "Success"
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return None, f"Error: {str(e)}"
|
| 45 |
+
|
| 46 |
+
# Create Gradio interface
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
+
fn=virtual_try_on,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Image(type="pil", label="Person Image"),
|
| 51 |
+
gr.Image(type="pil", label="Garment Image")
|
| 52 |
+
],
|
| 53 |
+
outputs=[
|
| 54 |
+
gr.Image(type="pil", label="Result"),
|
| 55 |
+
gr.Text(label="Status")
|
| 56 |
+
],
|
| 57 |
+
title="Virtual Try-On API",
|
| 58 |
+
description="Upload a person image and a garment image to see how the garment would look on the person."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
demo.launch()
|