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"}