aeon12 commited on
Commit
a39f434
·
verified ·
1 Parent(s): 4ed6413

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -1
app.py CHANGED
@@ -46,8 +46,55 @@ def get_base64_from_image(image):
46
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
47
 
48
  def generate_tryon(cloth_input, user_input, background):
49
- # ... (keep the existing generate_tryon function)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def tryon_interface(cloth_selected, cloth_upload, user_selected, user_upload, scene_selection, custom_scene):
52
  cloth = cloth_selected if cloth_selected else cloth_upload
53
  user = user_selected if user_selected else user_upload
 
46
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
47
 
48
  def generate_tryon(cloth_input, user_input, background):
49
+ if isinstance(cloth_input, str): # URL selected
50
+ cloth_base64 = get_base64_from_url(cloth_input)
51
+ else: # Image uploaded
52
+ cloth_base64 = get_base64_from_image(cloth_input)
53
+
54
+ if isinstance(user_input, str): # URL selected
55
+ user_base64 = get_base64_from_url(user_input)
56
+ else: # Image uploaded
57
+ user_base64 = get_base64_from_image(user_input)
58
+
59
+ input_data = {
60
+ "user_image": user_base64,
61
+ "product_image": cloth_base64,
62
+ "background": background
63
+ }
64
+
65
+ response = requests.post(
66
+ f"{API_URL}/run",
67
+ headers={
68
+ "Authorization": f"Bearer {API_KEY}",
69
+ "Content-Type": "application/json"
70
+ },
71
+ data=json.dumps({"input": input_data})
72
+ )
73
 
74
+ if not response.ok:
75
+ error_text = response.text
76
+ raise Exception(f"Failed to upload image: {response.status_code} {response.reason} - {error_text}")
77
+
78
+ job_id = response.json()['id']
79
+
80
+ while True:
81
+ status_response = requests.get(
82
+ f"{API_URL}/status/{job_id}",
83
+ headers={"Authorization": f"Bearer {API_KEY}"}
84
+ )
85
+
86
+ if not status_response.ok:
87
+ raise Exception(f"Status check failed: {status_response.status_code} {status_response.reason} - {status_response.text}")
88
+
89
+ status_data = status_response.json()
90
+ if status_data['status'] == 'COMPLETED':
91
+ output_base64 = status_data['output']['output']
92
+ output_image = Image.open(io.BytesIO(base64.b64decode(output_base64)))
93
+ return output_image
94
+ elif status_data['status'] == 'FAILED':
95
+ raise Exception(f"Job processing failed: {status_data}")
96
+
97
+ time.sleep(2)
98
  def tryon_interface(cloth_selected, cloth_upload, user_selected, user_upload, scene_selection, custom_scene):
99
  cloth = cloth_selected if cloth_selected else cloth_upload
100
  user = user_selected if user_selected else user_upload