tejani commited on
Commit
90e74e8
·
verified ·
1 Parent(s): c309c9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -116
app.py CHANGED
@@ -1,123 +1,22 @@
1
- from fastapi import FastAPI, File, UploadFile, HTTPException, Form
2
- from fastapi.responses import FileResponse
3
- from fastapi.staticfiles import StaticFiles
4
  import requests
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
 
17
- # Mount a static directory to serve files
18
- STATIC_DIR = "/app/static"
19
- os.makedirs(STATIC_DIR, exist_ok=True)
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(
27
- human_img: UploadFile = File(...),
28
- garment_img: UploadFile = File(...),
29
- garment_desc: str = Form(""), # Match Trinket
30
- category: str = Form("upper_body") # Match Trinket
31
- ):
32
- try:
33
- # Generate unique filenames
34
- human_filename = f"{uuid.uuid4()}_{human_img.filename}"
35
- garment_filename = f"{uuid.uuid4()}_{garment_img.filename}"
36
 
37
- # Save files temporarily in the static directory
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" # Verify this is correct
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
- # Skip URL verification for debugging
59
- # try:
60
- # url_check = requests.head(human_url, timeout=10)
61
- # logger.info(f"Human URL status: {url_check.status_code}")
62
- # if url_check.status_code != 200:
63
- # raise HTTPException(status_code=500, detail=f"Human image URL inaccessible: {human_url}")
64
- # url_check = requests.head(garment_url, timeout=10)
65
- # logger.info(f"Garment URL status: {url_check.status_code}")
66
- # if url_check.status_code != 200:
67
- # raise HTTPException(status_code=500, detail=f"Garment image URL inaccessible: {garment_url}")
68
- # except requests.RequestException as e:
69
- # raise HTTPException(status_code=500, detail=f"Error accessing URLs: {str(e)}")
70
-
71
- # Prepare headers, cookies, and data for the API
72
- headers = {
73
- "accept": "*/*",
74
- "f": "sdfdsfsKaVgUoxa5j1jzcFtziPx", # Match Trinket
75
- }
76
- cookies = {}
77
- data = {
78
- "humanImg": human_url,
79
- "garment": garment_url,
80
- "garmentDesc": garment_desc,
81
- "category": category,
82
- }
83
-
84
- # Test with Trinket URLs (uncomment to test)
85
- # data = {
86
- # "humanImg": "https://images.pexels.com/photos/1391498/pexels-photo-1391498.jpeg",
87
- # "garment": "https://r2.changeclothesai.online/1752560324456537514.jpeg",
88
- # "garmentDesc": "",
89
- # "category": "upper_body"
90
- # }
91
-
92
- # Call the ChangeClothesAI API
93
- logger.info(f"Sending API request: URL={CHANGE_CLOTHES_API}, Headers={headers}, Data={data}")
94
- response = requests.post(CHANGE_CLOTHES_API, headers=headers, cookies=cookies, data=data, timeout=60)
95
- logger.info(f"API Response: Status={response.status_code}, Body={response.text}, Headers={response.headers}")
96
-
97
- # Check if the API call was successful
98
- if response.status_code != 200:
99
- raise HTTPException(status_code=response.status_code, detail=f"Error calling ChangeClothesAI API: {response.text}")
100
-
101
- # Delete temporary files (commented out for debugging)
102
- # os.remove(human_path)
103
- # os.remove(garment_path)
104
- # logger.info("Temporary files deleted")
105
-
106
- return response.json()
107
-
108
- except Exception as e:
109
- # Clean up in case of errors (commented out for debugging)
110
- # if os.path.exists(human_path):
111
- # os.remove(human_path)
112
- # if os.path.exists(garment_path):
113
- # os.remove(garment_path)
114
- logger.error(f"Error processing request: {str(e)}")
115
- raise HTTPException(status_code=500, detail=f"Error processing files: {str(e)}")
116
-
117
- finally:
118
- human_img.file.close()
119
- garment_img.file.close()
120
-
121
- if __name__ == "__main__":
122
- import uvicorn
123
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
1
  import requests
 
 
 
 
 
2
 
3
+ url = "https://changeclothesai.online/api/try-on/edge"
 
 
4
 
5
+ cookies = {}
6
 
7
+ headers = {
8
+ "accept": "*/*",
9
+ "f": "sdfdsfsKaVgUoxa5j1jzcFtziPx",
10
+ }
11
 
12
+ data = {
13
+ "humanImg": "https://jallenjia-change-clothes-ai.hf.space/file=/tmp/gradio/f71ebb0af5153ab8d2a76722918738f803045719/model_3.png",
14
+ "garment": "https://jallenjia-change-clothes-ai.hf.space/file=/tmp/gradio/01b0bf774b8b20130cdb650fda3f2232b571a90f/Girl%20outfits%203.jpg",
15
+ "garmentDesc": "",
16
+ "category": "upper_body"
17
+ }
18
 
19
+ response = requests.post(url, headers=headers, cookies=cookies, data=data)
 
 
 
 
 
 
 
 
 
 
20
 
21
+ print(response.status_code)
22
+ print(response.text)