tejani commited on
Commit
a842a46
·
verified ·
1 Parent(s): 0ea6c0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -54
app.py CHANGED
@@ -5,7 +5,12 @@ import requests
5
  import os
6
  import uuid
7
  import shutil
8
- from tempfile import NamedTemporaryFile
 
 
 
 
 
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('URL')
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
- # Save human image
38
- with open(human_path, "wb") as f:
39
- shutil.copyfileobj(human_img.file, f)
40
- # Save garment image
41
- with open(garment_path, "wb") as f:
42
- shutil.copyfileobj(garment_img.file, f)
43
-
44
- # Generate public URLs (assuming the Space is hosted at a public URL)
45
- base_url = "https://tejani-tryapi.hf.space"
46
- human_url = f"{base_url}/static/{human_filename}"
47
- garment_url = f"{base_url}/static/{garment_filename}"
48
-
49
- print(human_url)
50
- print(garment_url)
51
-
52
- # Prepare headers and data for the original API
53
- headers = {
54
- "accept": "*/*",
55
- "f": str(uuid.uuid4()).replace("-", ""),
56
- }
57
-
58
- data = {
59
- "humanImg": human_url,
60
- "garment": garment_url,
61
- "garmentDesc": garment_desc,
62
- "category": category,
63
- }
64
-
65
- # Call the ChangeClothesAI API
66
- response = requests.post(CHANGE_CLOTHES_API, headers=headers, data=data, timeout=60)
67
-
68
- # Check if the API call was successful
69
- if response.status_code != 200:
70
- raise HTTPException(status_code=response.status_code, detail="Error calling ChangeClothesAI API")
71
-
72
- # Delete temporary files
73
- #os.remove(human_path)
74
- #os.remove(garment_path)
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)