apix/backend/main.py
Khoa.vo 0ef7e5475c
Some checks are pending
CI / build (18.x) (push) Waiting to run
CI / build (20.x) (push) Waiting to run
v3.0.0: Add FastAPI backend
- Add Python FastAPI backend with Pydantic validation
- Port WhiskClient and MetaAIClient to Python
- Create API routers for all endpoints
- Add Swagger/ReDoc documentation at /docs
- Update Dockerfile for multi-service container
- Add lib/api.ts frontend client
- Update README for V3
2026-01-13 07:46:32 +07:00

114 lines
3 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...")
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="1.0.0",
lifespan=lifespan,
docs_url="/docs",
redoc_url="/redoc"
)
# 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)