File size: 3,223 Bytes
c09e844
 
 
 
 
 
 
 
4714e76
 
c09e844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""

Main FastAPI Application

Sentiment Rating Prediction System

"""
import os

# OPTIONAL: Set HuggingFace cache directory (only for local dev)
# Comment this out for production to use default cache
# if not os.getenv("RENDER"):  # Only for local development
#     os.environ['HF_HOME'] = 'G:/huggingface_cache'

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

from app.database import engine, Base
from app.routers import auth, prediction, dashboard

# ============================================
# DATABASE AUTO-MIGRATION
# ============================================
# This creates all tables automatically on first deploy
# Critical for PostgreSQL on Render (no manual migrations needed)
print("πŸ”„ Creating database tables...")
Base.metadata.create_all(bind=engine)
print("βœ… Database tables created successfully!")

# ============================================
# INITIALIZE FASTAPI APP
# ============================================
app = FastAPI(
    title="Vietnamese Product Rating Prediction API",
    description="ML-powered sentiment analysis for Vietnamese product reviews (1-5 stars)",
    version="1.0.0",
    docs_url="/docs",  # Swagger UI
    redoc_url="/redoc"  # ReDoc
)

# ============================================
# CORS MIDDLEWARE
# ============================================
# For production, restrict origins to your actual domain
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # TODO: Restrict in production
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ============================================
# STATIC FILES & TEMPLATES
# ============================================
app.mount("/static", StaticFiles(directory="app/static"), name="static")
templates = Jinja2Templates(directory="app/templates")

# ============================================
# INCLUDE ROUTERS
# ============================================
app.include_router(auth.router, prefix="/api/auth", tags=["Authentication"])
app.include_router(prediction.router, prefix="/api/predict", tags=["Prediction"])
app.include_router(dashboard.router, tags=["Dashboard"])

# ============================================
# ROOT & HEALTH CHECK ENDPOINTS
# ============================================
@app.get("/")
async def root():
    """Root endpoint - API information"""
    return {
        "message": "Vietnamese Product Rating Prediction API",
        "docs": "/docs",
        "health": "/health"
    }

@app.get("/health")
async def health_check():
    """Health check endpoint for monitoring"""
    return {
        "status": "healthy",
        "service": "rating-prediction",
        "version": "1.0.0"
    }

# ============================================
# LOCAL DEVELOPMENT SERVER
# ============================================
if __name__ == "__main__":
    # This only runs when executing: python main.py
    # On Render, gunicorn/uvicorn will be used instead
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)