Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
)
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def virtual_try_on(person_image, garment_image):
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
Args:
|
| 19 |
-
person_image: PIL Image of the person
|
| 20 |
-
garment_image: PIL Image of the garment
|
| 21 |
-
Returns:
|
| 22 |
-
PIL Image of the result
|
| 23 |
"""
|
| 24 |
try:
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# Make API request
|
| 36 |
-
response = client.post(
|
| 37 |
-
json={
|
| 38 |
-
"inputs": [
|
| 39 |
-
{"image": person_b64},
|
| 40 |
-
{"image": garment_b64}
|
| 41 |
-
]
|
| 42 |
-
}
|
| 43 |
)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
result_image =
|
| 47 |
return result_image, "Success"
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
return None, f"Error: {str(e)}"
|
| 50 |
|
| 51 |
-
#
|
| 52 |
demo = gr.Interface(
|
| 53 |
fn=virtual_try_on,
|
| 54 |
inputs=[
|
|
@@ -59,7 +57,7 @@ demo = gr.Interface(
|
|
| 59 |
gr.Image(type="pil", label="Result"),
|
| 60 |
gr.Text(label="Status")
|
| 61 |
],
|
| 62 |
-
title="Virtual Try-On
|
| 63 |
description="Upload a person image and a garment image to see how the garment would look on the person."
|
| 64 |
)
|
| 65 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
def load_model():
|
| 8 |
+
controlnet = ControlNetModel.from_pretrained("Kwai-Kolors/Kolors-Virtual-Try-On")
|
| 9 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 10 |
+
"Kwai-Kolors/Kolors-Virtual-Try-On",
|
| 11 |
+
controlnet=controlnet,
|
| 12 |
+
torch_dtype=torch.float16
|
| 13 |
+
)
|
| 14 |
+
if torch.cuda.is_available():
|
| 15 |
+
pipe = pipe.to("cuda")
|
| 16 |
+
return pipe
|
| 17 |
|
| 18 |
+
# Model'i global olarak yükle
|
| 19 |
+
try:
|
| 20 |
+
model = load_model()
|
| 21 |
+
print("Model başarıyla yüklendi!")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Model yüklenirken hata: {str(e)}")
|
| 24 |
|
| 25 |
def virtual_try_on(person_image, garment_image):
|
| 26 |
"""
|
| 27 |
+
Virtual try-on process
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
+
# Resimleri uygun formata dönüştür
|
| 31 |
+
if person_image is None or garment_image is None:
|
| 32 |
+
return None, "Error: Both images are required"
|
| 33 |
+
|
| 34 |
+
# Model inference
|
| 35 |
+
output = model(
|
| 36 |
+
person_image,
|
| 37 |
+
garment_image,
|
| 38 |
+
num_inference_steps=30,
|
| 39 |
+
guidance_scale=7.5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Sonuç resmini al
|
| 43 |
+
result_image = output.images[0]
|
| 44 |
return result_image, "Success"
|
| 45 |
+
|
| 46 |
except Exception as e:
|
| 47 |
return None, f"Error: {str(e)}"
|
| 48 |
|
| 49 |
+
# Gradio arayüzü
|
| 50 |
demo = gr.Interface(
|
| 51 |
fn=virtual_try_on,
|
| 52 |
inputs=[
|
|
|
|
| 57 |
gr.Image(type="pil", label="Result"),
|
| 58 |
gr.Text(label="Status")
|
| 59 |
],
|
| 60 |
+
title="Virtual Try-On",
|
| 61 |
description="Upload a person image and a garment image to see how the garment would look on the person."
|
| 62 |
)
|
| 63 |
|