from __future__ import annotations import sys from functools import lru_cache from pathlib import Path from typing import Any from fastapi import FastAPI from fastapi.responses import HTMLResponse from pydantic import BaseModel, Field PROJECT_ROOT = Path(__file__).resolve().parents[1] MODELS_DIR = PROJECT_ROOT / "src" / "models" if str(MODELS_DIR) not in sys.path: sys.path.append(str(MODELS_DIR)) if str(PROJECT_ROOT) not in sys.path: sys.path.append(str(PROJECT_ROOT)) from chatbot_pipeline import ChatbotPipeline app = FastAPI( title="Mental Health Support Chatbot", description="Integrated language, emotion, intent, RAG, guardrail, and response-generation API.", version="1.0.0", ) class ChatRequest(BaseModel): message: str = Field(..., min_length=1) source: str = Field("both", pattern="^(both|cci|amod)$") top_k: int = Field(8, ge=1, le=10) history: list[dict[str, str]] = Field(default_factory=list) class ChatResponse(BaseModel): response: str state: dict[str, Any] @lru_cache(maxsize=1) def get_pipeline() -> ChatbotPipeline: return ChatbotPipeline() @app.get("/health") def health() -> dict[str, str]: return {"status": "ok"} @app.post("/chat", response_model=ChatResponse) def chat(request: ChatRequest) -> ChatResponse: pipeline = get_pipeline() pipeline.retrieval_source = request.source pipeline.top_k = request.top_k output = pipeline.run(request.message, history=request.history) return ChatResponse(response=output["response"], state=output["state"]) @app.get("/", response_class=HTMLResponse) def home() -> str: return PRODUCTION_PAGE @app.get("/developer", response_class=HTMLResponse) def developer() -> str: return DEVELOPER_PAGE PRODUCTION_PAGE = r"""
{}