| 1 | # Copyright (C) 2025 AIDC-AI |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | # See the License for the specific language governing permissions and |
| 11 | # limitations under the License. |
| 12 | |
| 13 | """ |
| 14 | Pixelle-Video FastAPI Application |
| 15 | |
| 16 | Main FastAPI app with all routers and middleware. |
| 17 | |
| 18 | Run this script to start the FastAPI server: |
| 19 | uv run python api/app.py |
| 20 | |
| 21 | Or with custom settings: |
| 22 | uv run python api/app.py --host 0.0.0.0 --port 8080 --reload |
| 23 | """ |
| 24 | |
| 25 | import sys |
| 26 | from pathlib import Path |
| 27 | |
| 28 | # Add project root to sys.path for module imports |
| 29 | # This ensures imports work correctly in both development and packaged environments |
| 30 | _script_dir = Path(__file__).resolve().parent |
| 31 | _project_root = _script_dir.parent |
| 32 | if str(_project_root) not in sys.path: |
| 33 | sys.path.insert(0, str(_project_root)) |
| 34 | |
| 35 | import argparse |
| 36 | from contextlib import asynccontextmanager |
| 37 | from fastapi import FastAPI |
| 38 | from fastapi.middleware.cors import CORSMiddleware |
| 39 | from loguru import logger |
| 40 | |
| 41 | from api.config import api_config |
| 42 | from api.tasks import task_manager |
| 43 | from api.dependencies import shutdown_pixelle_video |
| 44 | |
| 45 | # Import routers |
| 46 | from api.routers import ( |
| 47 | health_router, |
| 48 | llm_router, |
| 49 | tts_router, |
| 50 | image_router, |
| 51 | content_router, |
| 52 | video_router, |
| 53 | tasks_router, |
| 54 | files_router, |
| 55 | resources_router, |
| 56 | frame_router, |
| 57 | ) |
| 58 | |
| 59 | |
| 60 | @asynccontextmanager |
| 61 | async def lifespan(app: FastAPI): |
| 62 | """ |
| 63 | Application lifespan manager |
| 64 | |
| 65 | Handles startup and shutdown events. |
| 66 | """ |
| 67 | # Startup |
| 68 | logger.info("🚀 Starting Pixelle-Video API...") |
| 69 | await task_manager.start() |
| 70 | logger.info("✅ Pixelle-Video API started successfully\n") |
| 71 | |
| 72 | yield |
| 73 | |
| 74 | # Shutdown |
| 75 | logger.info("🛑 Shutting down Pixelle-Video API...") |
| 76 | await task_manager.stop() |
| 77 | await shutdown_pixelle_video() |
| 78 | logger.info("✅ Pixelle-Video API shutdown complete") |
| 79 | |
| 80 | |
| 81 | # Create FastAPI app |
| 82 | app = FastAPI( |
| 83 | title="Pixelle-Video API", |
| 84 | description=""" |
| 85 | ## Pixelle-Video - AI Video Generation Platform API |
| 86 | |
| 87 | ### Features |
| 88 | - 🤖 **LLM**: Large language model integration |
| 89 | - 🔊 **TTS**: Text-to-speech synthesis |
| 90 | - 🎨 **Image**: AI image generation |
| 91 | - 📝 **Content**: Automated content generation |
| 92 | - 🎬 **Video**: End-to-end video generation |
| 93 | |
| 94 | ### Video Generation Modes |
| 95 | - **Sync**: `/api/video/generate/sync` - For small videos (< 30s) |
| 96 | - **Async**: `/api/video/generate/async` - For large videos with task tracking |
| 97 | |
| 98 | ### Getting Started |
| 99 | 1. Check health: `GET /health` |
| 100 | 2. Generate narrations: `POST /api/content/narration` |
| 101 | 3. Generate video: `POST /api/video/generate/sync` or `/async` |
| 102 | 4. Track task progress: `GET /api/tasks/{task_id}` |
| 103 | """, |
| 104 | version="0.1.0", |
| 105 | docs_url=api_config.docs_url, |
| 106 | redoc_url=api_config.redoc_url, |
| 107 | openapi_url=api_config.openapi_url, |
| 108 | lifespan=lifespan, |
| 109 | ) |
| 110 | |
| 111 | # Add CORS middleware |
| 112 | if api_config.cors_enabled: |
| 113 | app.add_middleware( |
| 114 | CORSMiddleware, |
| 115 | allow_origins=api_config.cors_origins, |
| 116 | allow_credentials=True, |
| 117 | allow_methods=["*"], |
| 118 | allow_headers=["*"], |
| 119 | ) |
| 120 | logger.info(f"CORS enabled for origins: {api_config.cors_origins}") |
| 121 | |
| 122 | # Include routers |
| 123 | # Health check (no prefix) |
| 124 | app.include_router(health_router) |
| 125 | |
| 126 | # API routers (with /api prefix) |
| 127 | app.include_router(llm_router, prefix=api_config.api_prefix) |
| 128 | app.include_router(tts_router, prefix=api_config.api_prefix) |
| 129 | app.include_router(image_router, prefix=api_config.api_prefix) |
| 130 | app.include_router(content_router, prefix=api_config.api_prefix) |
| 131 | app.include_router(video_router, prefix=api_config.api_prefix) |
| 132 | app.include_router(tasks_router, prefix=api_config.api_prefix) |
| 133 | app.include_router(files_router, prefix=api_config.api_prefix) |
| 134 | app.include_router(resources_router, prefix=api_config.api_prefix) |
| 135 | app.include_router(frame_router, prefix=api_config.api_prefix) |
| 136 | |
| 137 | |
| 138 | @app.get("/") |
| 139 | async def root(): |
| 140 | """Root endpoint with API information""" |
| 141 | return { |
| 142 | "service": "Pixelle-Video API", |
| 143 | "version": "0.1.0", |
| 144 | "docs": api_config.docs_url, |
| 145 | "health": "/health", |
| 146 | "api": { |
| 147 | "llm": f"{api_config.api_prefix}/llm", |
| 148 | "tts": f"{api_config.api_prefix}/tts", |
| 149 | "image": f"{api_config.api_prefix}/image", |
| 150 | "content": f"{api_config.api_prefix}/content", |
| 151 | "video": f"{api_config.api_prefix}/video", |
| 152 | "tasks": f"{api_config.api_prefix}/tasks", |
| 153 | "files": f"{api_config.api_prefix}/files", |
| 154 | "resources": f"{api_config.api_prefix}/resources", |
| 155 | "frame": f"{api_config.api_prefix}/frame", |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | if __name__ == "__main__": |
| 161 | import uvicorn |
| 162 | |
| 163 | # Parse command line arguments |
| 164 | parser = argparse.ArgumentParser(description="Start Pixelle-Video API Server") |
| 165 | parser.add_argument("--host", default="0.0.0.0", help="Host to bind to") |
| 166 | parser.add_argument("--port", type=int, default=8000, help="Port to bind to") |
| 167 | parser.add_argument("--reload", action="store_true", help="Enable auto-reload") |
| 168 | |
| 169 | args = parser.parse_args() |
| 170 | |
| 171 | # Print startup banner |
| 172 | print(f""" |
| 173 | ╔══════════════════════════════════════════════════════════════╗ |
| 174 | ║ Pixelle-Video API Server ║ |
| 175 | ╚══════════════════════════════════════════════════════════════╝ |
| 176 | |
| 177 | Starting server at http://{args.host}:{args.port} |
| 178 | API Docs: http://{args.host}:{args.port}/docs |
| 179 | ReDoc: http://{args.host}:{args.port}/redoc |
| 180 | |
| 181 | Press Ctrl+C to stop the server |
| 182 | """) |
| 183 | |
| 184 | # Start server |
| 185 | uvicorn.run( |
| 186 | "api.app:app", |
| 187 | host=args.host, |
| 188 | port=args.port, |
| 189 | reload=args.reload, |
| 190 | ) |
| 191 | |
| 192 |