Spaces:
Sleeping
Sleeping
File size: 2,152 Bytes
0187d9a b77c0a2 0187d9a fe6002b 5fa8be0 0187d9a de4b55b 0187d9a b77c0a2 bf7f5ee fe6002b b77c0a2 fe6002b b77c0a2 0187d9a fe6002b b77c0a2 8600f2c edf9ade 4e4ac38 5fa8be0 b77c0a2 4e4ac38 668c994 0187d9a b77c0a2 0187d9a b77c0a2 ea1dcd3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import sys
import os
from fastapi import FastAPI
import uvicorn
import traceback
from contextlib import asynccontextmanager
from fastapi.middleware.cors import CORSMiddleware
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)
sys.path.append(os.path.join(current_dir, "meisai-check-ai"))
from routes import auth, predict, health
from services.sentence_transformer_service import sentence_transformer_service
from utils import create_directories
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan context manager for startup and shutdown events"""
try:
# Load models and data ONCE at startup
sentence_transformer_service.load_model_data()
except Exception as e:
print(f"Error during startup: {e}")
traceback.print_exc()
yield # App chạy tại đây
print("Shutting down application")
# Initialize FastAPI
app = FastAPI(
title="MeisaiCheck API",
description="API for MeisaiCheck AI System",
version="1.0",
lifespan=lifespan,
openapi_tags=[
{
"name": "Health",
"description": "Health check endpoints",
},
{
"name": "Authentication",
"description": "User authentication and token management",
},
{
"name": "AI Model",
"description": "AI model endpoints for prediction and embedding",
},
],
# Removed root_path since HF Spaces already handles it
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
expose_headers=["*"], # Expose all headers
)
# Include Routers
app.include_router(health.router, tags=["Health"])
app.include_router(auth.router, tags=["Authentication"])
app.include_router(predict.router, tags=["AI Model"])
@app.get("/", tags=["Health"])
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
create_directories()
uvicorn.run(app, host="0.0.0.0", port=7860)
|