File size: 3,209 Bytes
f332e2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f67b2
f332e2d
 
c466884
d616c3c
 
 
c466884
7e3bf1f
09014af
 
c466884
c6fe27b
7e3bf1f
09014af
 
7e3bf1f
 
b7f67b2
f332e2d
 
b7f67b2
f332e2d
 
 
 
 
 
225e4cd
 
f332e2d
 
 
0ea6c0b
f332e2d
0ea6c0b
 
 
 
 
f332e2d
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
import requests
import os
import uuid
import shutil
from tempfile import NamedTemporaryFile

app = FastAPI()

# Mount a static directory to serve files
STATIC_DIR = "/app/static"
os.makedirs(STATIC_DIR, exist_ok=True)
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")

# API endpoint for ChangeClothesAI
CHANGE_CLOTHES_API = os.environ.get('URL')

@app.post("/try-on/")
async def try_on(
    human_img: UploadFile = File(...),
    garment_img: UploadFile = File(...),
    garment_desc: str = Form("dresses"),
    category: str = Form("dresses")
):
    try:
        # Generate unique filenames
        human_filename = f"{uuid.uuid4()}_{human_img.filename}"
        garment_filename = f"{uuid.uuid4()}_{garment_img.filename}"

        # Save files temporarily in the static directory
        human_path = os.path.join(STATIC_DIR, human_filename)
        garment_path = os.path.join(STATIC_DIR, garment_filename)

        try:
            # Save human image
            with open(human_path, "wb") as f:
                shutil.copyfileobj(human_img.file, f)
            # Save garment image
            with open(garment_path, "wb") as f:
                shutil.copyfileobj(garment_img.file, f)

            # Generate public URLs (assuming the Space is hosted at a public URL)
            base_url = "https://tejani-tryapi.hf.space"
            human_url = f"{base_url}/static/{human_filename}"
            garment_url = f"{base_url}/static/{garment_filename}"

            print(human_url)
            print(garment_url)

            # Prepare headers and data for the original API
            headers = {
                "accept": "*/*",
                "f": str(uuid.uuid4()).replace("-", ""),
            }

            data = {
                "humanImg": human_url,
                "garment": garment_url,
                "garmentDesc": garment_desc,
                "category": category,
            }
            
            # Call the ChangeClothesAI API
            response = requests.post(CHANGE_CLOTHES_API, headers=headers, data=data, timeout=60)

            # Check if the API call was successful
            if response.status_code != 200:
                raise HTTPException(status_code=response.status_code, detail="Error calling ChangeClothesAI API")

            # Delete temporary files
            #os.remove(human_path)
            #os.remove(garment_path)

            return response.json()

        #except Exception as e:
            # Clean up in case of any errors
            #if os.path.exists(human_path):
                #os.remove(human_path)
            #if os.path.exists(garment_path):
                #os.remove(garment_path)
            #raise HTTPException(status_code=500, detail=str(e))'''

        finally:
            human_img.file.close()
            garment_img.file.close()

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing files: {str(e)}")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)