121 lines
3.2 KiB
Python
121 lines
3.2 KiB
Python
"""
|
|
KV-Pix FastAPI Backend
|
|
|
|
A secure and intuitive API backend for the KV-Pix image generation application.
|
|
Provides endpoints for:
|
|
- Whisk image and video generation
|
|
- Meta AI image generation
|
|
- Prompt library management
|
|
- Reference image uploads
|
|
- Upload history
|
|
|
|
API Documentation available at /docs (Swagger UI) and /redoc (ReDoc)
|
|
"""
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add backend to path for imports
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
from routers import generate, video, references, meta, prompts, history
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""Startup and shutdown events"""
|
|
print("🚀 KV-Pix FastAPI Backend starting...")
|
|
print("📚 Swagger UI available at: http://localhost:8000/docs")
|
|
print("📖 ReDoc available at: http://localhost:8000/redoc")
|
|
yield
|
|
print("👋 KV-Pix FastAPI Backend shutting down...")
|
|
|
|
|
|
import os
|
|
|
|
# Detect if running behind nginx proxy (via env var)
|
|
# When behind nginx at /api, set root_path so Swagger knows correct URL prefix
|
|
ROOT_PATH = os.getenv("API_ROOT_PATH", "")
|
|
|
|
app = FastAPI(
|
|
title="KV-Pix API",
|
|
description="""
|
|
## KV-Pix Image Generation API
|
|
|
|
A powerful API for AI image generation using multiple providers.
|
|
|
|
### Features
|
|
- **Whisk API**: Google's experimental image generation with reference images
|
|
- **Meta AI**: Meta's Imagine model for creative images
|
|
- **Prompt Library**: Curated prompts with categories
|
|
- **Upload History**: Track and reuse uploaded references
|
|
|
|
### Authentication
|
|
All generation endpoints require provider-specific cookies passed in the request body.
|
|
See the Settings page in the web app for cookie configuration instructions.
|
|
""",
|
|
version="3.2.0",
|
|
lifespan=lifespan,
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
root_path=ROOT_PATH,
|
|
)
|
|
|
|
# CORS middleware - allow Next.js frontend
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[
|
|
"http://localhost:3000",
|
|
"http://127.0.0.1:3000",
|
|
"http://localhost:3001",
|
|
],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include routers
|
|
app.include_router(generate.router)
|
|
app.include_router(video.router)
|
|
app.include_router(references.router)
|
|
app.include_router(meta.router)
|
|
app.include_router(prompts.router)
|
|
app.include_router(history.router)
|
|
|
|
|
|
@app.get("/", tags=["Health"])
|
|
async def root():
|
|
"""Health check endpoint"""
|
|
return {
|
|
"status": "healthy",
|
|
"service": "kv-pix-api",
|
|
"version": "1.0.0",
|
|
"docs": "/docs"
|
|
}
|
|
|
|
|
|
@app.get("/health", tags=["Health"])
|
|
async def health_check():
|
|
"""Detailed health check"""
|
|
return {
|
|
"status": "healthy",
|
|
"endpoints": {
|
|
"generate": "/generate",
|
|
"video": "/video/generate",
|
|
"references": "/references/upload",
|
|
"meta": "/meta/generate",
|
|
"prompts": "/prompts",
|
|
"history": "/history"
|
|
},
|
|
"documentation": {
|
|
"swagger": "/docs",
|
|
"redoc": "/redoc"
|
|
}
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)
|