Spaces:
Sleeping
Sleeping
Update vton.py
Browse files
vton.py
CHANGED
|
@@ -2,6 +2,10 @@ import replicate
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def virtual_try_on(garm_img_data, human_img_data, garment_des):
|
| 7 |
"""
|
|
@@ -34,24 +38,41 @@ def virtual_try_on(garm_img_data, human_img_data, garment_des):
|
|
| 34 |
input=input_data
|
| 35 |
)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
if not output
|
| 39 |
-
raise ValueError("
|
| 40 |
-
|
| 41 |
-
output_url = output[0]
|
| 42 |
-
|
| 43 |
-
# Validate URL scheme
|
| 44 |
-
if not output_url.startswith(('http://', 'https://')):
|
| 45 |
-
output_url = 'https://' + output_url
|
| 46 |
-
|
| 47 |
-
# Download the image from the URL
|
| 48 |
-
response = requests.get(output_url)
|
| 49 |
-
response.raise_for_status() # Raise an exception for bad status codes
|
| 50 |
|
| 51 |
# Save the output image
|
| 52 |
output_path = "output.jpg"
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
print(f"Virtual try-on complete! Output saved to {output_path}")
|
| 57 |
return output_path
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
import base64
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
|
| 10 |
def virtual_try_on(garm_img_data, human_img_data, garment_des):
|
| 11 |
"""
|
|
|
|
| 38 |
input=input_data
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# Validate and process the model output
|
| 42 |
+
if not output:
|
| 43 |
+
raise ValueError("Empty response received from the model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Save the output image
|
| 46 |
output_path = "output.jpg"
|
| 47 |
+
|
| 48 |
+
# Handle FileOutput type from Replicate
|
| 49 |
+
if hasattr(output, 'save'):
|
| 50 |
+
# If output is a FileOutput object, save it directly
|
| 51 |
+
output.save(output_path)
|
| 52 |
+
else:
|
| 53 |
+
# Handle URL-based response (fallback to existing logic)
|
| 54 |
+
if isinstance(output, str):
|
| 55 |
+
output_url = output
|
| 56 |
+
elif isinstance(output, list) and output:
|
| 57 |
+
output_url = output[0]
|
| 58 |
+
else:
|
| 59 |
+
# Instead of raising an error, try to get the content directly
|
| 60 |
+
try:
|
| 61 |
+
with open(output_path, "wb") as file:
|
| 62 |
+
file.write(output.read())
|
| 63 |
+
return output_path
|
| 64 |
+
except Exception as e:
|
| 65 |
+
raise ValueError(f"Unable to process model output: {str(e)}")
|
| 66 |
+
|
| 67 |
+
# Validate URL scheme
|
| 68 |
+
if not output_url.startswith(('http://', 'https://')) and not output_url.startswith('http'):
|
| 69 |
+
output_url = 'https://' + output_url.lstrip('/')
|
| 70 |
+
# Download the image from the URL
|
| 71 |
+
response = requests.get(output_url)
|
| 72 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
| 73 |
+
|
| 74 |
+
with open(output_path, "wb") as file:
|
| 75 |
+
file.write(response.content)
|
| 76 |
|
| 77 |
print(f"Virtual try-on complete! Output saved to {output_path}")
|
| 78 |
return output_path
|