add error display
Browse files
app.py
CHANGED
|
@@ -49,6 +49,7 @@ def url_to_base64(url):
|
|
| 49 |
print(f"Error converting URL to base64: {str(e)}")
|
| 50 |
return None
|
| 51 |
|
|
|
|
| 52 |
def run_viton(model_image_path: str = None,
|
| 53 |
garment_image_path: str = None,
|
| 54 |
model_url: str = None,
|
|
@@ -59,53 +60,43 @@ def run_viton(model_image_path: str = None,
|
|
| 59 |
):
|
| 60 |
"""
|
| 61 |
Run the Virtual Try-On model with provided images path or URLs.
|
| 62 |
-
|
| 63 |
-
Args:
|
| 64 |
-
model_image_path (str): Path to the model image file.
|
| 65 |
-
garment_image_path (str): Path to the garment image file.
|
| 66 |
-
model_url (str): URL of the model image.
|
| 67 |
-
garment_url (str): URL of the garment image.
|
| 68 |
-
n_steps (int): Number of steps for the model.
|
| 69 |
-
image_scale (float): Scale for the generated images.
|
| 70 |
-
seed (int): Random seed for reproducibility.
|
| 71 |
-
|
| 72 |
-
Returns:
|
| 73 |
-
list: List of generated images in base64 format.
|
| 74 |
"""
|
| 75 |
if not model_image_path and not model_url:
|
| 76 |
-
|
| 77 |
-
return []
|
| 78 |
if not garment_image_path and not garment_url:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
try:
|
| 82 |
api_url = os.environ.get("SERVER_URL")
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
model_b64 = None
|
| 87 |
-
garment_b64 = None
|
| 88 |
|
| 89 |
# Handle model image
|
|
|
|
| 90 |
if model_url and model_url.strip():
|
| 91 |
print(f"Using model URL: {model_url}")
|
| 92 |
model_b64 = url_to_base64(model_url.strip())
|
|
|
|
|
|
|
| 93 |
elif model_image_path:
|
| 94 |
print(f"Using model file: {model_image_path}")
|
| 95 |
model_b64 = image_to_base64(model_image_path)
|
| 96 |
|
| 97 |
# Handle garment image
|
|
|
|
| 98 |
if garment_url and garment_url.strip():
|
| 99 |
print(f"Using garment URL: {garment_url}")
|
| 100 |
garment_b64 = url_to_base64(garment_url.strip())
|
|
|
|
|
|
|
| 101 |
elif garment_image_path:
|
| 102 |
print(f"Using garment file: {garment_image_path}")
|
| 103 |
garment_b64 = image_to_base64(garment_image_path)
|
| 104 |
|
| 105 |
-
# Check if we have both images
|
| 106 |
if not model_b64 or not garment_b64:
|
| 107 |
-
|
| 108 |
-
return []
|
| 109 |
|
| 110 |
# Prepare request
|
| 111 |
request_data = {
|
|
@@ -128,25 +119,28 @@ def run_viton(model_image_path: str = None,
|
|
| 128 |
if response.status_code == 200:
|
| 129 |
result = response.json()
|
| 130 |
if result.get("error"):
|
| 131 |
-
|
| 132 |
-
return []
|
| 133 |
|
| 134 |
generated_images = []
|
| 135 |
for i, img_b64 in enumerate(result.get("images_base64", [])):
|
| 136 |
output_path = f"ootd_output_{i}.png"
|
| 137 |
-
img = base64_to_image(img_b64, output_path)
|
| 138 |
generated_images.append(img)
|
| 139 |
|
|
|
|
|
|
|
|
|
|
| 140 |
print(f"Successfully generated {len(generated_images)} images")
|
| 141 |
return generated_images
|
| 142 |
else:
|
| 143 |
-
|
| 144 |
-
return []
|
| 145 |
|
|
|
|
|
|
|
| 146 |
except Exception as e:
|
| 147 |
-
print(f"Exception occurred: {str(e)}")
|
| 148 |
-
|
| 149 |
-
|
| 150 |
def run_new_garment(model_image_path: str = None,
|
| 151 |
garment_prompt: str = None,
|
| 152 |
model_url: str = None,
|
|
@@ -155,45 +149,33 @@ def run_new_garment(model_image_path: str = None,
|
|
| 155 |
seed=-1
|
| 156 |
):
|
| 157 |
"""
|
| 158 |
-
Run the Virtual Try-On model with provided model image and garment
|
| 159 |
-
based on description of the garment obtained from the user
|
| 160 |
-
|
| 161 |
-
Args:
|
| 162 |
-
model_image_path (str): Path to the model image file.
|
| 163 |
-
garment_prompt (str): Description of the garment.
|
| 164 |
-
model_url (str): URL of the model image.
|
| 165 |
-
n_steps (int): Number of steps for the model.
|
| 166 |
-
image_scale (float): Scale for the generated images.
|
| 167 |
-
seed (int): Random seed for reproducibility.
|
| 168 |
-
|
| 169 |
-
Returns:
|
| 170 |
-
list: List of generated images in base64 format.
|
| 171 |
"""
|
| 172 |
if not model_image_path and not model_url:
|
| 173 |
-
|
| 174 |
-
return []
|
| 175 |
if not garment_prompt or not garment_prompt.strip():
|
| 176 |
-
|
| 177 |
-
|
| 178 |
try:
|
| 179 |
api_url = os.environ.get("SERVER_URL")
|
| 180 |
-
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
model_b64 = None
|
| 184 |
|
| 185 |
# Handle model image
|
|
|
|
| 186 |
if model_url and model_url.strip():
|
| 187 |
print(f"Using model URL: {model_url}")
|
| 188 |
model_b64 = url_to_base64(model_url.strip())
|
|
|
|
|
|
|
| 189 |
elif model_image_path:
|
| 190 |
print(f"Using model file: {model_image_path}")
|
| 191 |
model_b64 = image_to_base64(model_image_path)
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
print("Error: Missing model or garment description")
|
| 196 |
-
return []
|
| 197 |
|
| 198 |
# Prepare request
|
| 199 |
request_data = {
|
|
@@ -216,24 +198,27 @@ def run_new_garment(model_image_path: str = None,
|
|
| 216 |
if response.status_code == 200:
|
| 217 |
result = response.json()
|
| 218 |
if result.get("error"):
|
| 219 |
-
|
| 220 |
-
return []
|
| 221 |
|
| 222 |
generated_images = []
|
| 223 |
for i, img_b64 in enumerate(result.get("images_base64", [])):
|
| 224 |
output_path = f"flux_output_{i}.png"
|
| 225 |
-
img = base64_to_image(img_b64, output_path)
|
| 226 |
generated_images.append(img)
|
| 227 |
|
|
|
|
|
|
|
|
|
|
| 228 |
print(f"Successfully generated {len(generated_images)} images")
|
| 229 |
return generated_images
|
| 230 |
else:
|
| 231 |
-
|
| 232 |
-
return [] # Fix: was missing 'return'
|
| 233 |
|
|
|
|
|
|
|
| 234 |
except Exception as e:
|
| 235 |
-
print(f"Exception occurred: {str(e)}")
|
| 236 |
-
|
| 237 |
|
| 238 |
block = gr.Blocks().queue()
|
| 239 |
with block:
|
|
|
|
| 49 |
print(f"Error converting URL to base64: {str(e)}")
|
| 50 |
return None
|
| 51 |
|
| 52 |
+
|
| 53 |
def run_viton(model_image_path: str = None,
|
| 54 |
garment_image_path: str = None,
|
| 55 |
model_url: str = None,
|
|
|
|
| 60 |
):
|
| 61 |
"""
|
| 62 |
Run the Virtual Try-On model with provided images path or URLs.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
"""
|
| 64 |
if not model_image_path and not model_url:
|
| 65 |
+
raise gr.Error("β Please provide either a model image file or URL")
|
|
|
|
| 66 |
if not garment_image_path and not garment_url:
|
| 67 |
+
raise gr.Error("β Please provide either a garment image file or URL")
|
| 68 |
+
|
| 69 |
try:
|
| 70 |
api_url = os.environ.get("SERVER_URL")
|
| 71 |
+
if not api_url:
|
| 72 |
+
raise gr.Error("β SERVER_URL not configured in environment variables")
|
| 73 |
|
| 74 |
+
print(f"Using API URL: {api_url}")
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Handle model image
|
| 77 |
+
model_b64 = None
|
| 78 |
if model_url and model_url.strip():
|
| 79 |
print(f"Using model URL: {model_url}")
|
| 80 |
model_b64 = url_to_base64(model_url.strip())
|
| 81 |
+
if not model_b64:
|
| 82 |
+
raise gr.Error("β Failed to load model image from URL. Please check the URL is valid.")
|
| 83 |
elif model_image_path:
|
| 84 |
print(f"Using model file: {model_image_path}")
|
| 85 |
model_b64 = image_to_base64(model_image_path)
|
| 86 |
|
| 87 |
# Handle garment image
|
| 88 |
+
garment_b64 = None
|
| 89 |
if garment_url and garment_url.strip():
|
| 90 |
print(f"Using garment URL: {garment_url}")
|
| 91 |
garment_b64 = url_to_base64(garment_url.strip())
|
| 92 |
+
if not garment_b64:
|
| 93 |
+
raise gr.Error("β Failed to load garment image from URL. Please check the URL is valid.")
|
| 94 |
elif garment_image_path:
|
| 95 |
print(f"Using garment file: {garment_image_path}")
|
| 96 |
garment_b64 = image_to_base64(garment_image_path)
|
| 97 |
|
|
|
|
| 98 |
if not model_b64 or not garment_b64:
|
| 99 |
+
raise gr.Error("β Failed to process images. Please try again.")
|
|
|
|
| 100 |
|
| 101 |
# Prepare request
|
| 102 |
request_data = {
|
|
|
|
| 119 |
if response.status_code == 200:
|
| 120 |
result = response.json()
|
| 121 |
if result.get("error"):
|
| 122 |
+
raise gr.Error(f"β Server error: {result['error']}")
|
|
|
|
| 123 |
|
| 124 |
generated_images = []
|
| 125 |
for i, img_b64 in enumerate(result.get("images_base64", [])):
|
| 126 |
output_path = f"ootd_output_{i}.png"
|
| 127 |
+
img = base64_to_image(img_b64, output_path)
|
| 128 |
generated_images.append(img)
|
| 129 |
|
| 130 |
+
if not generated_images:
|
| 131 |
+
raise gr.Error("β No images were generated. Please try again.")
|
| 132 |
+
|
| 133 |
print(f"Successfully generated {len(generated_images)} images")
|
| 134 |
return generated_images
|
| 135 |
else:
|
| 136 |
+
raise gr.Error(f"β Request failed with status code: {response.status_code}")
|
|
|
|
| 137 |
|
| 138 |
+
except gr.Error:
|
| 139 |
+
raise # Re-raise Gradio errors
|
| 140 |
except Exception as e:
|
| 141 |
+
print(f"Exception occurred: {str(e)}")
|
| 142 |
+
raise gr.Error(f"β An unexpected error occurred: {str(e)}")
|
| 143 |
+
|
| 144 |
def run_new_garment(model_image_path: str = None,
|
| 145 |
garment_prompt: str = None,
|
| 146 |
model_url: str = None,
|
|
|
|
| 149 |
seed=-1
|
| 150 |
):
|
| 151 |
"""
|
| 152 |
+
Run the Virtual Try-On model with provided model image and garment prompt.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
"""
|
| 154 |
if not model_image_path and not model_url:
|
| 155 |
+
raise gr.Error("β Please provide either a model image file or URL")
|
|
|
|
| 156 |
if not garment_prompt or not garment_prompt.strip():
|
| 157 |
+
raise gr.Error("β Please provide a garment description")
|
| 158 |
+
|
| 159 |
try:
|
| 160 |
api_url = os.environ.get("SERVER_URL")
|
| 161 |
+
if not api_url:
|
| 162 |
+
raise gr.Error("β SERVER_URL not configured in environment variables")
|
| 163 |
|
| 164 |
+
print(f"Using API URL: {api_url}")
|
|
|
|
| 165 |
|
| 166 |
# Handle model image
|
| 167 |
+
model_b64 = None
|
| 168 |
if model_url and model_url.strip():
|
| 169 |
print(f"Using model URL: {model_url}")
|
| 170 |
model_b64 = url_to_base64(model_url.strip())
|
| 171 |
+
if not model_b64:
|
| 172 |
+
raise gr.Error("β Failed to load model image from URL. Please check the URL is valid.")
|
| 173 |
elif model_image_path:
|
| 174 |
print(f"Using model file: {model_image_path}")
|
| 175 |
model_b64 = image_to_base64(model_image_path)
|
| 176 |
|
| 177 |
+
if not model_b64:
|
| 178 |
+
raise gr.Error("β Failed to process model image. Please try again.")
|
|
|
|
|
|
|
| 179 |
|
| 180 |
# Prepare request
|
| 181 |
request_data = {
|
|
|
|
| 198 |
if response.status_code == 200:
|
| 199 |
result = response.json()
|
| 200 |
if result.get("error"):
|
| 201 |
+
raise gr.Error(f"β Server error: {result['error']}")
|
|
|
|
| 202 |
|
| 203 |
generated_images = []
|
| 204 |
for i, img_b64 in enumerate(result.get("images_base64", [])):
|
| 205 |
output_path = f"flux_output_{i}.png"
|
| 206 |
+
img = base64_to_image(img_b64, output_path)
|
| 207 |
generated_images.append(img)
|
| 208 |
|
| 209 |
+
if not generated_images:
|
| 210 |
+
raise gr.Error("β No images were generated. Please try again.")
|
| 211 |
+
|
| 212 |
print(f"Successfully generated {len(generated_images)} images")
|
| 213 |
return generated_images
|
| 214 |
else:
|
| 215 |
+
raise gr.Error(f"β Request failed with status code: {response.status_code}")
|
|
|
|
| 216 |
|
| 217 |
+
except gr.Error:
|
| 218 |
+
raise # Re-raise Gradio errors
|
| 219 |
except Exception as e:
|
| 220 |
+
print(f"Exception occurred: {str(e)}")
|
| 221 |
+
raise gr.Error(f"β An unexpected error occurred: {str(e)}")
|
| 222 |
|
| 223 |
block = gr.Blocks().queue()
|
| 224 |
with block:
|