HNTAI / app.py
sachinchandrankallar's picture
Update .gitignore to include additional files and directories for macOS, Linux, and application-specific configurations. Modify .huggingface.yaml to enhance Docker build settings and hardware requirements. Refactor app.py to remove legacy code and improve error handling. Remove deprecated files related to comprehensive streaming fixes, deployment scripts, and optimized Docker configurations. Update Dockerfile.prod to extend Gunicorn timeout for better performance. Enhance health endpoints and model management with improved logging and error handling. Consolidate routes and simplify architecture for better maintainability.
af75202
"""
Hugging Face Spaces entry point.
This file serves as the main entry point for Hugging Face Spaces deployment.
It imports and exposes the FastAPI app from the ai_med_extract package.
"""
import os
import sys
import logging
# Configure logging for Hugging Face Spaces
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
# Add the services/ai-service/src directory to the Python path
current_dir = os.path.dirname(os.path.abspath(__file__))
src_dir = os.path.join(current_dir, "services", "ai-service", "src")
if src_dir not in sys.path:
sys.path.insert(0, src_dir)
# Detect and set Hugging Face Spaces environment
if os.getenv("SPACE_ID") or os.getenv("SPACE_AUTHOR_NAME"):
os.environ.setdefault("HF_SPACES", "true")
logging.info("Detected Hugging Face Spaces environment")
# Set environment variables for Hugging Face Spaces
os.environ.setdefault("FAST_MODE", "true")
os.environ.setdefault("PRELOAD_SMALL_MODELS", "false")
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "max_split_size_mb:128")
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
os.environ.setdefault("OMP_NUM_THREADS", "1")
os.environ.setdefault("MKL_NUM_THREADS", "1")
os.environ.setdefault("DATABASE_URL", "")
# Import and create the app
try:
from ai_med_extract.app import create_app, initialize_agents # type: ignore
logging.info("Creating FastAPI application for HF Spaces...")
app = create_app(initialize=False)
initialize_agents(app, preload_small_models=False)
logging.info("Application initialized successfully")
except Exception as e:
logging.error(f"Failed to initialize application: {e}")
import traceback
logging.error(traceback.format_exc())
# Create minimal fallback app
from fastapi import FastAPI
app = FastAPI(title="Medical AI Service (fallback)")
@app.get("/")
async def root():
return {"message": "Medical AI Service - Fallback mode", "error": str(e)}
@app.get("/health")
async def health():
return {"status": "degraded", "message": "Initialization failed", "error": str(e)}
# Export the app for Hugging Face Spaces
__all__ = ["app"]