CLAT_Guide / backend /main.py
ashutoshneemval's picture
deploy: Support dynamic PORT environment variable for Railway
74a7197
Raw History Blame Contribute Delete
3.1 kB
import logging
import os
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from config import settings
from database.connection import engine, Base
from auth.routes import router as auth_router
from rag.routes import router as rag_router
from ai.routes import router as ai_router
from ai.nemotron import nemotron_client
from rag.service import rag_service
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
handlers=[logging.StreamHandler()]
)
logger = logging.getLogger("lexai.main")
# Auto-create DB tables (simplified migration for dev/proto)
try:
logger.info("Initializing database tables...")
Base.metadata.create_all(bind=engine)
logger.info("Database tables initialized successfully.")
except Exception as e:
logger.error(f"Failed to initialize database tables: {e}")
# Initialize FastAPI App
app = FastAPI(
title=settings.APP_NAME,
description="Ecosystem for CLAT preparation built with FastAPI, ChromaDB RAG, and NVIDIA Nemotron 120B.",
version="1.0.0"
)
# CORS configuration
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Adjust in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include Routers
app.include_router(auth_router)
app.include_router(rag_router)
app.include_router(ai_router)
class MessageRequest(BaseModel):
message: str
@app.get("/", response_class=HTMLResponse)
def read_root():
# Serve index.html dynamically
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates", "index.html")
if not os.path.exists(filepath):
return HTMLResponse("<h1>LexAI is online! Web Interface template is missing.</h1>")
with open(filepath, "r", encoding="utf-8") as f:
return HTMLResponse(content=f.read())
@app.post("/chat/message")
def chat_message_public(request: MessageRequest):
"""Public chatbot endpoint utilized by the frontend study guide website."""
try:
# Retrieve context from vector db (default public query)
retrieved_chunks = rag_service.retrieve_context(
query=request.message,
user_id=0, # 0 indicates public/general database
top_k=3
)
context_text = ""
if retrieved_chunks:
context_text = "\n---\n".join([chunk["text"] for chunk in retrieved_chunks])
# Call NVIDIA Nemotron doubt solver
ai_response = nemotron_client.generate_response(
prompt=request.message,
context=context_text
)
return {"response": ai_response}
except Exception as e:
logger.error(f"Public /chat/message failed: {e}")
return {"response": f"Sorry, I encountered an internal error: {str(e)}"}
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8000))
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=settings.DEBUG)