File size: 5,094 Bytes
91934f6 5cb75e6 91934f6 | 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | from fastapi import FastAPI, Request, Depends, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import os
from pathlib import Path
from seed_plans import seed_plans
from router.image import background_remove
from router.voice import clone
from router.try_on import try_clothes
from router.lip_sync import lip_sync
import torch
from transformers import AutoModelForImageSegmentation
from torchvision import transforms
from starlette.middleware.base import BaseHTTPMiddleware
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create FastAPI app
app = FastAPI(
title="AI Image Processing API",
description="API for image processing with AI models",
version="1.0.0",
debug=True,
)
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import Response
import traceback
class CustomCORSMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
if request.method == "OPTIONS":
response = Response()
else:
try:
response = await call_next(request)
except Exception as e:
response = Response(
content=f"Internal Server Error: {str(e)}", status_code=500
)
# Get the origin from the request
origin = request.headers.get("origin")
# Allow specific origins (not wildcard) when using credentials
allowed_origins = [
"http://localhost:3000",
"http://localhost:3001",
"http://localhost:5173",
"http://localhost:8080",
]
if origin in allowed_origins:
response.headers["Access-Control-Allow-Origin"] = (
origin # Specific origin, not "*"
)
response.headers["Access-Control-Allow-Credentials"] = (
"true" # Must be string "true"
)
response.headers["Access-Control-Allow-Methods"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
response.headers["Access-Control-Max-Age"] = "86400"
return response
app.add_middleware(CustomCORSMiddleware)
# Add CORS middleware for Next.js frontend
app.add_middleware(
CORSMiddleware,
# allow_origins=["http://localhost:3000"], # NextJS default port
# allow_credentials=True,
allow_origins=["*"], # Allow all origins
allow_credentials=True, # Must be False when using "*"
allow_methods=["*"],
allow_headers=["*"],
)
# Global variables
birefnet = None
birefnet_lite = None
transform_image = None
# Include routers
# app.include_router(ai_image_router)
app.include_router(background_remove.router)
app.include_router(clone.router)
app.include_router(try_clothes.router)
# Create temp directory for file storage if it doesn't exist
temp_dir = Path("./temp")
temp_dir.mkdir(exist_ok=True)
@app.on_event("startup")
def startup_event():
"""Create database tables if they don't exist."""
print("DB URL:", os.getenv("DATABASE_URL"))
try_clothes.initialize_models()
lip_sync.load_model("wav2lip_checkpoints/wav2lip_gan.pth")
logger.info("Loading BiRefNet models...")
torch.set_float32_matmul_precision("medium")
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load both models and store in app state
app.state.birefnet = AutoModelForImageSegmentation.from_pretrained(
"ZhengPeng7/BiRefNet", trust_remote_code=True
)
app.state.birefnet.to(device)
app.state.birefnet_lite = AutoModelForImageSegmentation.from_pretrained(
"ZhengPeng7/BiRefNet_lite", trust_remote_code=True
)
app.state.birefnet_lite.to(device)
app.state.transform_image = transforms.Compose(
[
transforms.Resize((768, 768)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
)
logger.info("Models loaded successfully")
@app.get("/")
async def root():
return {
"message": "Voice Processing API",
"version": "1.0.0",
"endpoints": {
"voice_cloning": "/voice/clone",
"speech_enhancement": "/voice/speech-enhancement",
"speech_separation": "/voice/speech-separation",
"target_speaker_extraction": "/voice/target-speaker-extraction",
"speech_super_resolution": "/voice/speech-super-resolution",
"health_check": "/voice/health",
"cleanup": "/voice/cleanup",
"download": "/voice/download/{filename}",
},
}
@app.get("/health")
async def health_check():
return {"status": "healthy"}
if __name__ == "__main__":
# Start server with hot reload during development
# uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
reload=True,
reload_dirs=["./ai-api"],
log_level="debug", # Add this line for debug logs
)
|