Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from tempfile import NamedTemporaryFile
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
# Mount a static directory to serve files
|
13 |
+
STATIC_DIR = "/app/static"
|
14 |
+
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(
|
22 |
+
human_img: UploadFile = File(...),
|
23 |
+
garment_img: UploadFile = File(...),
|
24 |
+
garment_desc: str = Form("dresses"),
|
25 |
+
category: str = Form("dresses")
|
26 |
+
):
|
27 |
+
try:
|
28 |
+
# Generate unique filenames
|
29 |
+
human_filename = f"{uuid.uuid4()}_{human_img.filename}"
|
30 |
+
garment_filename = f"{uuid.uuid4()}_{garment_img.filename}"
|
31 |
+
|
32 |
+
# Save files temporarily in the static directory
|
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 |
+
# Replace YOUR_SPACE_URL with the actual Hugging Face Space URL
|
46 |
+
base_url = "https://tejani-tryapi.hf.space" # e.g., https://your-username-your-space.hf.space
|
47 |
+
human_url = f"{base_url}/static/{human_filename}"
|
48 |
+
garment_url = f"{base_url}/static/{garment_filename}"
|
49 |
+
|
50 |
+
# Call the ChangeClothesAI API
|
51 |
+
response = requests.post(CHANGE_CLOTHES_API, headers=headers, data=data)
|
52 |
+
|
53 |
+
# Check if the API call was successful
|
54 |
+
if response.status_code != 200:
|
55 |
+
raise HTTPException(status_code=response.status_code, detail="Error calling ChangeClothesAI API")
|
56 |
+
|
57 |
+
# Delete temporary files
|
58 |
+
os.remove(human_path)
|
59 |
+
os.remove(garment_path)
|
60 |
+
|
61 |
+
return response.json()
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
# Clean up in case of any errors
|
65 |
+
if os.path.exists(human_path):
|
66 |
+
os.remove(human_path)
|
67 |
+
if os.path.exists(garment_path):
|
68 |
+
os.remove(garment_path)
|
69 |
+
raise HTTPException(status_code=500, detail=str(e))
|
70 |
+
|
71 |
+
finally:
|
72 |
+
human_img.file.close()
|
73 |
+
garment_img.file.close()
|
74 |
+
|
75 |
+
except Exception as e:
|
76 |
+
raise HTTPException(status_code=500, detail=f"Error processing files: {str(e)}")
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
import uvicorn
|
80 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|