Files changed (1) hide show
  1. handler.py +16 -5
handler.py CHANGED
@@ -463,14 +463,20 @@ def initialize_model():
463
  print(f"Failed to initialize model: {e}")
464
  return False
465
 
466
- # Initialize model on import
467
- model_initialized = initialize_model()
468
 
469
  # Main endpoint function for Hugging Face
470
  def query(payload):
471
  """Main endpoint function for Hugging Face inference API"""
 
 
 
472
  if not model_initialized:
473
- return {"error": "Model not initialized"}
 
 
 
474
 
475
  try:
476
  # Extract parameters from payload
@@ -506,13 +512,18 @@ def health_check():
506
  "cuda_available": torch.cuda.is_available(),
507
  "llava_available": LLAVA_AVAILABLE,
508
  "transformers_available": TRANSFORMERS_AVAILABLE,
509
- "cv2_available": CV2_AVAILABLE
 
510
  }
511
 
512
  def get_model_info():
513
  """Get model information"""
514
  if not model_initialized:
515
- return {"error": "Model not initialized"}
 
 
 
 
516
 
517
  return {
518
  "model_path": args.model_path if args else "Unknown",
 
463
  print(f"Failed to initialize model: {e}")
464
  return False
465
 
466
+ # Don't initialize model on import - do it lazily
467
+ model_initialized = False
468
 
469
  # Main endpoint function for Hugging Face
470
  def query(payload):
471
  """Main endpoint function for Hugging Face inference API"""
472
+ global model_initialized
473
+
474
+ # Lazy initialization - initialize model on first call
475
  if not model_initialized:
476
+ print("Initializing model on first query...")
477
+ model_initialized = initialize_model()
478
+ if not model_initialized:
479
+ return {"error": "Model initialization failed"}
480
 
481
  try:
482
  # Extract parameters from payload
 
512
  "cuda_available": torch.cuda.is_available(),
513
  "llava_available": LLAVA_AVAILABLE,
514
  "transformers_available": TRANSFORMERS_AVAILABLE,
515
+ "cv2_available": CV2_AVAILABLE,
516
+ "lazy_loading": True # Model will be loaded on first query
517
  }
518
 
519
  def get_model_info():
520
  """Get model information"""
521
  if not model_initialized:
522
+ return {
523
+ "error": "Model not initialized yet",
524
+ "lazy_loading": True,
525
+ "note": "Model will be loaded on first query"
526
+ }
527
 
528
  return {
529
  "model_path": args.model_path if args else "Unknown",