insideman commited on
Commit
2d93d36
·
verified ·
1 Parent(s): 0779147

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import PIL.Image
4
  import io
 
5
 
6
  client = InferenceClient(
7
  model="Kwai-Kolors/Kolors-Virtual-Try-On"
@@ -17,24 +18,33 @@ def virtual_try_on(person_image, garment_image):
17
  PIL Image of the result
18
  """
19
  try:
20
- # Convert images to bytes
21
  person_bytes = io.BytesIO()
22
  garment_bytes = io.BytesIO()
23
  person_image.save(person_bytes, format='PNG')
24
  garment_image.save(garment_bytes, format='PNG')
25
 
 
 
 
26
  # Make API request
27
  response = client.post(
28
  json={
29
  "inputs": [
30
- {"image": person_bytes.getvalue()},
31
- {"image": garment_bytes.getvalue()}
32
  ]
33
  }
34
  )
35
 
 
 
 
 
 
 
36
  # Convert response to image
37
- result_image = PIL.Image.open(io.BytesIO(response))
38
  return result_image, "Success"
39
  except Exception as e:
40
  return None, f"Error: {str(e)}"
 
2
  from huggingface_hub import InferenceClient
3
  import PIL.Image
4
  import io
5
+ import base64
6
 
7
  client = InferenceClient(
8
  model="Kwai-Kolors/Kolors-Virtual-Try-On"
 
18
  PIL Image of the result
19
  """
20
  try:
21
+ # Convert images to base64
22
  person_bytes = io.BytesIO()
23
  garment_bytes = io.BytesIO()
24
  person_image.save(person_bytes, format='PNG')
25
  garment_image.save(garment_bytes, format='PNG')
26
 
27
+ person_base64 = base64.b64encode(person_bytes.getvalue()).decode('utf-8')
28
+ garment_base64 = base64.b64encode(garment_bytes.getvalue()).decode('utf-8')
29
+
30
  # Make API request
31
  response = client.post(
32
  json={
33
  "inputs": [
34
+ {"image": person_base64},
35
+ {"image": garment_base64}
36
  ]
37
  }
38
  )
39
 
40
+ # Eğer response bytes ise doğrudan kullan, değilse base64'ten decode et
41
+ if isinstance(response, bytes):
42
+ result_bytes = response
43
+ else:
44
+ result_bytes = base64.b64decode(response)
45
+
46
  # Convert response to image
47
+ result_image = PIL.Image.open(io.BytesIO(result_bytes))
48
  return result_image, "Success"
49
  except Exception as e:
50
  return None, f"Error: {str(e)}"