Upload 8 files
Browse files
app.py
CHANGED
|
@@ -366,6 +366,33 @@ class AppState:
|
|
| 366 |
|
| 367 |
print("✓ RF-DETR model loaded")
|
| 368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
def get_text_generator(self, model_size: str = "4B") -> TextGenerator:
|
| 370 |
"""Get or create text generator."""
|
| 371 |
# Determine model ID based on size selection
|
|
@@ -403,8 +430,9 @@ def create_detection_interface():
|
|
| 403 |
return None, "Please upload an image."
|
| 404 |
|
| 405 |
try:
|
| 406 |
-
#
|
| 407 |
-
app_state.
|
|
|
|
| 408 |
|
| 409 |
# Run detection
|
| 410 |
detections = app_state.model.predict(image, threshold=threshold)
|
|
@@ -456,6 +484,7 @@ def create_detection_interface():
|
|
| 456 |
try:
|
| 457 |
generator = app_state.get_text_generator(model_size)
|
| 458 |
hf_token = app_state.config.get('hf_token')
|
|
|
|
| 459 |
llm_description = generator.generate(description, image=annotated, hf_token=hf_token)
|
| 460 |
description = llm_description
|
| 461 |
except Exception as e:
|
|
@@ -560,6 +589,13 @@ def main():
|
|
| 560 |
# Ensure results directory exists
|
| 561 |
os.makedirs(app_state.config.get('results_dir'), exist_ok=True)
|
| 562 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
# Create and launch the interface
|
| 564 |
demo = create_detection_interface()
|
| 565 |
|
|
|
|
| 366 |
|
| 367 |
print("✓ RF-DETR model loaded")
|
| 368 |
|
| 369 |
+
def preload_all_models(self):
|
| 370 |
+
"""Preload both detection and LLM models into VRAM at startup."""
|
| 371 |
+
print("=" * 60)
|
| 372 |
+
print("Preloading all models into VRAM...")
|
| 373 |
+
print("=" * 60)
|
| 374 |
+
|
| 375 |
+
# Load detection model
|
| 376 |
+
print("\n[1/2] Loading RF-DETR detection model...")
|
| 377 |
+
self.load_model()
|
| 378 |
+
|
| 379 |
+
# Load LLM model
|
| 380 |
+
if self.config.get('use_llm'):
|
| 381 |
+
print("\n[2/2] Loading MedGemma LLM model...")
|
| 382 |
+
try:
|
| 383 |
+
model_size = "4B" # Default to 4B model
|
| 384 |
+
generator = self.get_text_generator(model_size)
|
| 385 |
+
hf_token = self.config.get('hf_token')
|
| 386 |
+
generator.load_model(hf_token)
|
| 387 |
+
print("✓ MedGemma model loaded and ready")
|
| 388 |
+
except Exception as e:
|
| 389 |
+
print(f"⚠️ Warning: Could not preload LLM model: {e}")
|
| 390 |
+
print("LLM will be loaded on first use instead")
|
| 391 |
+
|
| 392 |
+
print("\n" + "=" * 60)
|
| 393 |
+
print("✓ All models loaded and ready in VRAM!")
|
| 394 |
+
print("=" * 60 + "\n")
|
| 395 |
+
|
| 396 |
def get_text_generator(self, model_size: str = "4B") -> TextGenerator:
|
| 397 |
"""Get or create text generator."""
|
| 398 |
# Determine model ID based on size selection
|
|
|
|
| 430 |
return None, "Please upload an image."
|
| 431 |
|
| 432 |
try:
|
| 433 |
+
# Models are preloaded at startup, but check just in case
|
| 434 |
+
if app_state.model is None:
|
| 435 |
+
app_state.load_model()
|
| 436 |
|
| 437 |
# Run detection
|
| 438 |
detections = app_state.model.predict(image, threshold=threshold)
|
|
|
|
| 484 |
try:
|
| 485 |
generator = app_state.get_text_generator(model_size)
|
| 486 |
hf_token = app_state.config.get('hf_token')
|
| 487 |
+
# Model is already preloaded, just generate
|
| 488 |
llm_description = generator.generate(description, image=annotated, hf_token=hf_token)
|
| 489 |
description = llm_description
|
| 490 |
except Exception as e:
|
|
|
|
| 589 |
# Ensure results directory exists
|
| 590 |
os.makedirs(app_state.config.get('results_dir'), exist_ok=True)
|
| 591 |
|
| 592 |
+
# Preload all models into VRAM
|
| 593 |
+
try:
|
| 594 |
+
app_state.preload_all_models()
|
| 595 |
+
except Exception as e:
|
| 596 |
+
print(f"⚠️ Warning: Failed to preload models: {e}")
|
| 597 |
+
print("Models will be loaded on first use instead")
|
| 598 |
+
|
| 599 |
# Create and launch the interface
|
| 600 |
demo = create_detection_interface()
|
| 601 |
|