insideman commited on
Commit
6ce42a9
·
verified ·
1 Parent(s): 4b786d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -38
app.py CHANGED
@@ -1,54 +1,52 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- import PIL.Image
4
- import io
5
- import base64
6
 
7
- # Initialize the Hugging Face client
8
- HF_TOKEN = "your_token_here" # Replace with your token
 
 
 
 
 
 
 
 
9
 
10
- client = InferenceClient(
11
- model="Kwai-Kolors/Kolors-Virtual-Try-On",
12
- token=HF_TOKEN
13
- )
 
 
14
 
15
  def virtual_try_on(person_image, garment_image):
16
  """
17
- Process the virtual try-on request
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
- # Convert images to bytes
26
- person_bytes = io.BytesIO()
27
- garment_bytes = io.BytesIO()
28
- person_image.save(person_bytes, format='PNG')
29
- garment_image.save(garment_bytes, format='PNG')
30
-
31
- # Convert bytes to base64
32
- person_b64 = base64.b64encode(person_bytes.getvalue()).decode('utf-8')
33
- garment_b64 = base64.b64encode(garment_bytes.getvalue()).decode('utf-8')
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
- # Convert response to image
46
- result_image = PIL.Image.open(io.BytesIO(response))
47
  return result_image, "Success"
 
48
  except Exception as e:
49
  return None, f"Error: {str(e)}"
50
 
51
- # Create Gradio interface
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 API",
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