MedLLM-Agent / app.py
Y Phung Nguyen
Fix UI public
4967d8e
"""Main entry point for MedLLM Agent"""
import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"
from logger import logger
from config import DEFAULT_MEDICAL_MODEL
import config
from models import initialize_medical_model, initialize_tts_model, initialize_whisper_model, WHISPER_AVAILABLE
from client import MCP_AVAILABLE
from ui import create_demo
if __name__ == "__main__":
# Note: Models are loaded on-demand when first needed (lazy loading)
# This avoids CUDA initialization in the main process, which is not allowed
# in ZeroGPU's stateless environment. Models will be loaded when stream_chat
# is called (which has the GPU decorator).
logger.info("App starting - models will be loaded on-demand when first needed")
logger.info(f"Default medical model: {DEFAULT_MEDICAL_MODEL}")
# TTS and ASR models also use GPU decorator, so skip preloading
logger.info("TTS and ASR models will be loaded on-demand if needed")
if WHISPER_AVAILABLE:
logger.info("Whisper ASR library (transformers) is available")
else:
logger.warning("Whisper ASR library not available - install with: pip install transformers torchaudio")
# Check Gemini MCP availability
if MCP_AVAILABLE:
logger.info("✅ Gemini MCP SDK is available")
if config.GEMINI_API_KEY:
logger.info(f"✅ GEMINI_API_KEY is set: {config.GEMINI_API_KEY[:10]}...{config.GEMINI_API_KEY[-4:]}")
# Test MCP connection asynchronously (don't block startup)
try:
import asyncio
from client import test_mcp_connection
try:
loop = asyncio.get_event_loop()
if loop.is_running():
# If loop is running, schedule test in background
logger.info("ℹ️ Testing MCP connection in background...")
else:
# Test synchronously
result = loop.run_until_complete(test_mcp_connection())
if result:
logger.info("✅ MCP connection test passed - Gemini MCP is ready!")
else:
logger.warning("⚠️ MCP connection test failed - will use fallback methods")
except Exception as e:
logger.warning(f"Could not test MCP connection: {e}")
except Exception as e:
logger.debug(f"MCP connection test skipped: {e}")
else:
logger.warning("⚠️ GEMINI_API_KEY not set - Gemini MCP features will not work")
logger.warning(" Set it in Hugging Face Space secrets or environment variables")
else:
logger.info("ℹ️ Gemini MCP SDK not available - app will use fallback methods (direct API calls)")
logger.info(" This is normal and the app will continue to work. MCP is optional.")
logger.info("App initialization complete!")
demo = create_demo()
demo.launch(share=True)