medical / app.py
Dama03's picture
ggg
b0a502a
#!/usr/bin/env python3
"""
Medical AI Assistant - FastAPI Only Entry Point
Simplified for backend integration
"""
import os
import sys
import logging
from pathlib import Path
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def setup_environment():
"""Setup environment variables for FastAPI deployment"""
# Set environment variables for optimal performance
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
os.environ.setdefault("HF_HOME", "/tmp/huggingface")
os.environ.setdefault("TRANSFORMERS_CACHE", "/tmp/transformers")
# FastAPI specific
os.environ.setdefault("HOST", "0.0.0.0")
os.environ.setdefault("PORT", "8000")
logger.info("βœ… Environment configured for FastAPI Medical AI")
def main():
"""Main application entry point"""
try:
logger.info("🩺 Starting Medical AI Assistant - FastAPI Edition")
# Setup environment
setup_environment()
# Import and run FastAPI app
from fastapi_app import app
import uvicorn
# Get port from environment or use default
port = int(os.getenv("PORT", 8000))
host = os.getenv("HOST", "0.0.0.0")
logger.info(f"πŸš€ Starting FastAPI server on {host}:{port}")
logger.info(f"πŸ“š API Documentation will be available at: http://{host}:{port}/docs")
logger.info(f"πŸ”„ Alternative docs at: http://{host}:{port}/redoc")
# Launch the FastAPI application
uvicorn.run(
app,
host=host,
port=port,
log_level="info",
reload=False, # Set to True for development
access_log=True
)
except KeyboardInterrupt:
logger.info("πŸ‘‹ Application stopped by user")
except Exception as e:
logger.error(f"❌ Critical error in main application: {str(e)}", exc_info=True)
sys.exit(1)
# For direct import (Hugging Face Spaces compatibility)
try:
from fastapi_app import app
logger.info("βœ… FastAPI app imported successfully")
except ImportError as e:
logger.error(f"❌ Failed to import FastAPI app: {str(e)}")
# Create minimal fallback app
from fastapi import FastAPI
app = FastAPI(title="Medical AI - Error", description="Failed to load main application")
@app.get("/")
async def error_root():
return {"error": "Medical AI Assistant failed to load properly"}
if __name__ == "__main__":
main()