Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import requests
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
@@ -16,40 +19,73 @@ app.add_middleware(
|
|
| 16 |
|
| 17 |
# Define the request model
|
| 18 |
class TryOnRequest(BaseModel):
|
| 19 |
-
humanImg: str
|
| 20 |
-
garment: str
|
| 21 |
garmentDesc: str
|
| 22 |
category: str
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
}
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
"category": request.category
|
| 39 |
-
}
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
response = requests.post(url, headers=headers, cookies={}, data=data)
|
| 43 |
-
response.raise_for_status()
|
| 44 |
|
| 45 |
return {
|
| 46 |
"status_code": response.status_code,
|
| 47 |
-
"response": response.json() if response.headers.get('content-type') == 'application/json' else response.text
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
except requests.exceptions.RequestException as e:
|
| 50 |
raise HTTPException(status_code=500, detail=f"Error forwarding request: {str(e)}")
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Health check endpoint for Hugging Face Spaces
|
| 53 |
@app.get("/")
|
| 54 |
async def root():
|
| 55 |
-
return {"message": "FastAPI proxy for try-on API is running"}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import requests
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
import os
|
| 6 |
+
import uuid
|
| 7 |
+
from pathlib import Path
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 19 |
|
| 20 |
# Define the request model
|
| 21 |
class TryOnRequest(BaseModel):
|
|
|
|
|
|
|
| 22 |
garmentDesc: str
|
| 23 |
category: str
|
| 24 |
|
| 25 |
+
# Base directory for temporary file storage
|
| 26 |
+
UPLOAD_DIR = Path("/tmp/gradio")
|
| 27 |
+
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
| 28 |
+
|
| 29 |
+
# Helper function to save file and generate public URL
|
| 30 |
+
async def save_file_and_get_url(file: UploadFile) -> str:
|
| 31 |
+
# Generate unique filename
|
| 32 |
+
file_extension = file.filename.split(".")[-1]
|
| 33 |
+
unique_filename = f"{uuid.uuid4()}.{file_extension}"
|
| 34 |
+
file_path = UPLOAD_DIR / unique_filename
|
| 35 |
|
| 36 |
+
# Save file
|
| 37 |
+
with file_path.open("wb") as buffer:
|
| 38 |
+
content = await file.read()
|
| 39 |
+
buffer.write(content)
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Generate public URL (Hugging Face Spaces format)
|
| 42 |
+
public_url = f"https://{os.getenv('SPACE_ID', 'your-username-space-name')}.hf.space/file={str(file_path)}"
|
| 43 |
+
return public_url
|
| 44 |
+
|
| 45 |
+
# Updated endpoint to handle file uploads and proxy the request
|
| 46 |
+
@app.post("/try-on")
|
| 47 |
+
async def try_on(
|
| 48 |
+
human_img: UploadFile = File(...),
|
| 49 |
+
garment: UploadFile = File(...),
|
| 50 |
+
garment_desc: str = "",
|
| 51 |
+
category: str = "upper_body"
|
| 52 |
+
):
|
| 53 |
try:
|
| 54 |
+
# Save files and get public URLs
|
| 55 |
+
human_img_url = await save_file_and_get_url(human_img)
|
| 56 |
+
garment_url = await save_file_and_get_url(garment)
|
| 57 |
+
|
| 58 |
+
# Original API endpoint
|
| 59 |
+
url = "https://changeclothesai.online/api/try-on/edge"
|
| 60 |
+
|
| 61 |
+
headers = {
|
| 62 |
+
"accept": "*/*",
|
| 63 |
+
"f": "sdfdsdfKaVgUoxa5j1jzcFtziPx",
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
data = {
|
| 67 |
+
"humanImg": human_img_url,
|
| 68 |
+
"garment": garment_url,
|
| 69 |
+
"garmentDesc": garment_desc,
|
| 70 |
+
"category": category
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
# Forward request to the original API
|
| 74 |
response = requests.post(url, headers=headers, cookies={}, data=data)
|
| 75 |
+
response.raise_for_status()
|
| 76 |
|
| 77 |
return {
|
| 78 |
"status_code": response.status_code,
|
| 79 |
+
"response": response.json() if response.headers.get('content-type') == 'application/json' else response.text,
|
| 80 |
+
"human_img_url": human_img_url,
|
| 81 |
+
"garment_url": garment_url
|
| 82 |
}
|
| 83 |
except requests.exceptions.RequestException as e:
|
| 84 |
raise HTTPException(status_code=500, detail=f"Error forwarding request: {str(e)}")
|
| 85 |
+
except Exception as e:
|
| 86 |
+
raise HTTPException(status_code=500, detail=f"Error processing files: {str(e)}")
|
| 87 |
|
| 88 |
# Health check endpoint for Hugging Face Spaces
|
| 89 |
@app.get("/")
|
| 90 |
async def root():
|
| 91 |
+
return {"message": "FastAPI proxy for try-on API with file upload is running"}
|