Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,12 @@ import requests
|
|
5 |
import os
|
6 |
import uuid
|
7 |
import shutil
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
@@ -15,7 +20,7 @@ os.makedirs(STATIC_DIR, exist_ok=True)
|
|
15 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
16 |
|
17 |
# API endpoint for ChangeClothesAI
|
18 |
-
CHANGE_CLOTHES_API = os.environ.get('
|
19 |
|
20 |
@app.post("/try-on/")
|
21 |
async def try_on(
|
@@ -33,63 +38,79 @@ async def try_on(
|
|
33 |
human_path = os.path.join(STATIC_DIR, human_filename)
|
34 |
garment_path = os.path.join(STATIC_DIR, garment_filename)
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
try:
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
return response.json()
|
77 |
-
|
78 |
-
#except Exception as e:
|
79 |
-
# Clean up in case of any errors
|
80 |
-
#if os.path.exists(human_path):
|
81 |
-
#os.remove(human_path)
|
82 |
-
#if os.path.exists(garment_path):
|
83 |
-
#os.remove(garment_path)
|
84 |
-
#raise HTTPException(status_code=500, detail=str(e))'''
|
85 |
-
|
86 |
-
finally:
|
87 |
-
human_img.file.close()
|
88 |
-
garment_img.file.close()
|
89 |
|
90 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
raise HTTPException(status_code=500, detail=f"Error processing files: {str(e)}")
|
92 |
|
|
|
|
|
|
|
|
|
93 |
if __name__ == "__main__":
|
94 |
import uvicorn
|
95 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
5 |
import os
|
6 |
import uuid
|
7 |
import shutil
|
8 |
+
import logging
|
9 |
+
import time
|
10 |
+
|
11 |
+
# Set up logging
|
12 |
+
logging.basicConfig(level=logging.INFO)
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
|
15 |
app = FastAPI()
|
16 |
|
|
|
20 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
21 |
|
22 |
# API endpoint for ChangeClothesAI
|
23 |
+
CHANGE_CLOTHES_API = os.environ.get('CHANGE_CLOTHES_API', 'https://changeclothesai.online/api/try-on/edge')
|
24 |
|
25 |
@app.post("/try-on/")
|
26 |
async def try_on(
|
|
|
38 |
human_path = os.path.join(STATIC_DIR, human_filename)
|
39 |
garment_path = os.path.join(STATIC_DIR, garment_filename)
|
40 |
|
41 |
+
logger.info(f"Saving human image to: {human_path}")
|
42 |
+
with open(human_path, "wb") as f:
|
43 |
+
shutil.copyfileobj(human_img.file, f)
|
44 |
+
logger.info(f"Human image saved: {os.path.exists(human_path)}")
|
45 |
+
|
46 |
+
logger.info(f"Saving garment image to: {garment_path}")
|
47 |
+
with open(garment_path, "wb") as f:
|
48 |
+
shutil.copyfileobj(garment_img.file, f)
|
49 |
+
logger.info(f"Garment image saved: {os.path.exists(garment_path)}")
|
50 |
+
|
51 |
+
# Generate public URLs
|
52 |
+
base_url = "https://tejani-tryapi.hf.space"
|
53 |
+
human_url = f"{base_url}/static/{human_filename}"
|
54 |
+
garment_url = f"{base_url}/static/{garment_filename}"
|
55 |
+
logger.info(f"Human URL: {human_url}")
|
56 |
+
logger.info(f"Garment URL: {garment_url}")
|
57 |
+
|
58 |
+
# Test URL accessibility
|
59 |
+
time.sleep(1) # Ensure files are available
|
60 |
try:
|
61 |
+
url_check = requests.head(human_url, timeout=5)
|
62 |
+
logger.info(f"Human URL status: {url_check.status_code}")
|
63 |
+
if url_check.status_code != 200:
|
64 |
+
raise HTTPException(status_code=500, detail=f"Human image URL inaccessible: {human_url}")
|
65 |
+
url_check = requests.head(garment_url, timeout=5)
|
66 |
+
logger.info(f"Garment URL status: {url_check.status_code}")
|
67 |
+
if url_check.status_code != 200:
|
68 |
+
raise HTTPException(status_code=500, detail=f"Garment image URL inaccessible: {garment_url}")
|
69 |
+
except requests.RequestException as e:
|
70 |
+
raise HTTPException(status_code=500, detail=f"Error accessing URLs: {str(e)}")
|
71 |
+
|
72 |
+
# Prepare headers, cookies, and data for the API
|
73 |
+
headers = {
|
74 |
+
"accept": "*/*",
|
75 |
+
"f": str(uuid.uuid4()).replace("-", ""),
|
76 |
+
}
|
77 |
+
cookies = {} # Add empty cookies to match the first script
|
78 |
+
data = {
|
79 |
+
"humanImg": human_url,
|
80 |
+
"garment": garment_url,
|
81 |
+
"garmentDesc": garment_desc,
|
82 |
+
"category": category,
|
83 |
+
}
|
84 |
+
|
85 |
+
# Call the ChangeClothesAI API
|
86 |
+
logger.info(f"Calling API: {CHANGE_CLOTHES_API}")
|
87 |
+
response = requests.post(CHANGE_CLOTHES_API, headers=headers, cookies=cookies, data=data, timeout=60)
|
88 |
+
logger.info(f"API Response: {response.status_code}, {response.text}")
|
89 |
+
|
90 |
+
# Check if the API call was successful
|
91 |
+
if response.status_code != 200:
|
92 |
+
raise HTTPException(status_code=response.status_code, detail=f"Error calling ChangeClothesAI API: {response.text}")
|
93 |
+
|
94 |
+
# Delete temporary files
|
95 |
+
os.remove(human_path)
|
96 |
+
os.remove(garment_path)
|
97 |
+
logger.info("Temporary files deleted")
|
98 |
+
|
99 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
except Exception as e:
|
102 |
+
# Clean up in case of errors
|
103 |
+
if os.path.exists(human_path):
|
104 |
+
os.remove(human_path)
|
105 |
+
if os.path.exists(garment_path):
|
106 |
+
os.remove(garment_path)
|
107 |
+
logger.error(f"Error processing request: {str(e)}")
|
108 |
raise HTTPException(status_code=500, detail=f"Error processing files: {str(e)}")
|
109 |
|
110 |
+
finally:
|
111 |
+
human_img.file.close()
|
112 |
+
garment_img.file.close()
|
113 |
+
|
114 |
if __name__ == "__main__":
|
115 |
import uvicorn
|
116 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|