File size: 1,670 Bytes
6be194e
 
f332e2d
6be194e
a842a46
6be194e
f332e2d
6be194e
 
 
 
 
 
 
 
f332e2d
6be194e
 
 
 
 
 
f332e2d
6be194e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f332e2d
6be194e
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import requests
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

# Add CORS middleware to allow requests from Hugging Face Spaces frontend
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Adjust for production to specific origins
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Define the request model
class TryOnRequest(BaseModel):
    humanImg: str
    garment: str
    garmentDesc: str
    category: str

# Endpoint to proxy the request to the original API
@app.post("/try-on")
async def try_on(request: TryOnRequest):
    url = "https://changeclothesai.online/api/try-on/edge"
    
    headers = {
        "accept": "*/*",
        "f": "sdfdsfsKaVgUoxa5j1jzcFtziPx",
    }
    
    data = {
        "humanImg": request.humanImg,
        "garment": request.garment,
        "garmentDesc": request.garmentDesc,
        "category": request.category
    }
    
    try:
        response = requests.post(url, headers=headers, cookies={}, data=data)
        response.raise_for_status()  # Raise exception for bad status codes
        
        return {
            "status_code": response.status_code,
            "response": response.json() if response.headers.get('content-type') == 'application/json' else response.text
        }
    except requests.exceptions.RequestException as e:
        raise HTTPException(status_code=500, detail=f"Error forwarding request: {str(e)}")

# Health check endpoint for Hugging Face Spaces
@app.get("/")
async def root():
    return {"message": "FastAPI proxy for try-on API is running"}