thera-guide-ai / app.py
Said Lfagrouche
Prepare for Hugging Face Spaces deployment with simplified configuration
e62f11c
raw
history blame
2.56 kB
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import os
from dotenv import load_dotenv
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
# Initialize FastAPI app
app = FastAPI(title="Mental Health Counselor API")
# Initialize global storage
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
os.makedirs(DATA_DIR, exist_ok=True)
os.makedirs(os.path.join(DATA_DIR, "users"), exist_ok=True)
os.makedirs(os.path.join(DATA_DIR, "sessions"), exist_ok=True)
os.makedirs(os.path.join(DATA_DIR, "conversations"), exist_ok=True)
os.makedirs(os.path.join(DATA_DIR, "feedback"), exist_ok=True)
# Simple health check route
@app.get("/")
async def root():
return {
"status": "ok",
"message": "Mental Health Counselor API is running",
"api_version": "1.0.0",
"backend_info": "FastAPI on Hugging Face Spaces"
}
# Health check endpoint
@app.get("/health")
async def health_check():
return {"status": "ok", "message": "Mental Health Counselor API is running"}
# Metadata endpoint
@app.get("/metadata")
async def get_metadata():
return {
"api_version": "1.0.0",
"endpoints": [
"/",
"/health",
"/metadata"
],
"provider": "Mental Health Counselor API on Hugging Face Spaces",
"deployment_type": "Hugging Face Spaces Docker",
"description": "This API provides functionality for a mental health counseling application.",
"frontend": "Deployed separately on Vercel"
}
# Try to import the full API if available
try:
import api_mental_health
# If the import succeeds, mount all routes from the API
logger.info("Successfully imported full API module")
# Include all routes from the API
app.include_router(api_mental_health.app, prefix="")
# Add a status endpoint for the full API
@app.get("/full-api-status")
async def full_api_status():
return {
"status": "active",
"message": "Full API module is active and all routes are mounted",
"routes_count": len(api_mental_health.app.routes)
}
except ImportError as e:
logger.warning(f"Could not import full API module: {e}")
@app.get("/full-api-status")
async def full_api_status():
return {
"status": "unavailable",
"message": "Full API module could not be imported",
"error": str(e)
}