tss / app /main.py
Deploy Bot
Deploy backend to HF Spaces
77d2609
"""
Tibetan Learning Platform - FastAPI Backend
Main application entry point
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from app.routers import tts, translate, learn
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan events"""
# Startup: Load TTS model
from app.services.tts_service import tts_service
print("πŸ”„ Loading TTS model...")
await tts_service.load_model()
print("βœ… TTS model loaded successfully!")
yield
# Shutdown: Cleanup
print("πŸ‘‹ Shutting down...")
app = FastAPI(
title="Tibetan Learning Platform API",
description="API for Tibetan TTS, Translation, and Learning features",
version="1.0.0",
lifespan=lifespan
)
# CORS middleware for frontend
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:3000",
"http://127.0.0.1:3000",
"*", # Allow all origins for HF Spaces deployment
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers
app.include_router(tts.router, prefix="/api/tts", tags=["TTS"])
app.include_router(translate.router, prefix="/api/translate", tags=["Translation"])
app.include_router(learn.router, prefix="/api/learn", tags=["Learning"])
@app.get("/")
async def root():
"""Health check endpoint"""
return {
"status": "ok",
"message": "Tibetan Learning Platform API",
"version": "1.0.0"
}
@app.get("/api/health")
async def health_check():
"""Detailed health check"""
from app.services.tts_service import tts_service
return {
"status": "healthy",
"tts_model_loaded": tts_service.is_loaded(),
"device": tts_service.get_device()
}
if __name__ == "__main__":
import uvicorn
uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)