File size: 3,050 Bytes
52b4ed7
0ae46fb
abad335
e4c0a6a
52b4ed7
 
 
af9efda
ffcfd50
52b4ed7
0ae46fb
 
63e92ef
 
 
 
 
 
 
af9efda
 
 
 
 
 
fce8688
 
 
ffcfd50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fce8688
ffcfd50
c67b4e7
fce8688
63e92ef
0ae46fb
4967d8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""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)