vimalk78's picture
Add complete Python backend with AI-powered crossword generation
38c016b
raw
history blame
4.46 kB
"""
FastAPI backend for crossword puzzle generator with vector similarity search.
"""
import os
import logging
import time
from datetime import datetime
from contextlib import asynccontextmanager
from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import uvicorn
from dotenv import load_dotenv
from src.routes.api import router as api_router
from src.services.vector_search import VectorSearchService
# Load environment variables
load_dotenv()
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def log_with_timestamp(message):
"""Helper to log with precise timestamp."""
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
logger.info(f"[{timestamp}] {message}")
# Global vector search service instance
vector_service = None
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Initialize and cleanup application resources."""
global vector_service
# Startup
startup_time = time.time()
log_with_timestamp("πŸš€ Initializing Python backend with vector search...")
# Initialize vector search service
try:
service_start = time.time()
log_with_timestamp("πŸ”§ Creating VectorSearchService instance...")
vector_service = VectorSearchService()
log_with_timestamp("⚑ Starting vector search initialization...")
await vector_service.initialize()
init_time = time.time() - service_start
log_with_timestamp(f"βœ… Vector search service initialized in {init_time:.2f}s")
except Exception as e:
logger.error(f"❌ Failed to initialize vector search service: {e}")
# Continue without vector search (will fallback to static words)
# Make vector service available to routes
app.state.vector_service = vector_service
yield
# Shutdown
logger.info("πŸ›‘ Shutting down Python backend...")
if vector_service:
await vector_service.cleanup()
# Create FastAPI app
app = FastAPI(
title="Crossword Puzzle Generator API",
description="Python backend with AI-powered vector similarity search",
version="2.0.0",
lifespan=lifespan
)
# CORS configuration
cors_origins = []
if os.getenv("NODE_ENV") == "production":
# Production: same origin
cors_origins = ["*"] # HuggingFace Spaces
else:
# Development: allow dev servers
cors_origins = [
"http://localhost:5173", # Vite dev server
"http://localhost:3000", # Alternative dev server
"http://localhost:7860", # Local production test
]
app.add_middleware(
CORSMiddleware,
allow_origins=cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include API routes
app.include_router(api_router, prefix="/api")
# Serve static files (frontend)
static_path = Path(__file__).parent / "public"
if static_path.exists():
app.mount("/assets", StaticFiles(directory=static_path / "assets"), name="assets")
@app.get("/")
async def serve_frontend():
"""Serve the React frontend."""
index_path = static_path / "index.html"
if index_path.exists():
return FileResponse(index_path)
else:
raise HTTPException(status_code=404, detail="Frontend not found")
@app.get("/{full_path:path}")
async def serve_spa_routes(full_path: str):
"""Serve React SPA routes."""
# For any non-API route, serve the React app
if not full_path.startswith("api/"):
index_path = static_path / "index.html"
if index_path.exists():
return FileResponse(index_path)
raise HTTPException(status_code=404, detail="Not found")
@app.get("/health")
async def health_check():
"""Health check endpoint."""
return {
"status": "healthy",
"backend": "python",
"vector_search": vector_service.is_initialized if vector_service else False
}
if __name__ == "__main__":
port = int(os.getenv("PORT", 7860))
host = "0.0.0.0" if os.getenv("NODE_ENV") == "production" else "127.0.0.1"
logger.info(f"🐍 Starting Python backend on {host}:{port}")
uvicorn.run(
"app:app",
host=host,
port=port,
reload=os.getenv("NODE_ENV") != "production"
)