SamarpeetGarad commited on
Commit
c82dd0f
·
verified ·
1 Parent(s): 38edfbc

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +51 -4
app.py CHANGED
@@ -4,14 +4,29 @@ Main Gradio Application for MedGemma Impact Challenge
4
 
5
  This application demonstrates a multi-agent system for chest X-ray analysis
6
  using Google's Health AI Developer Foundations (HAI-DEF) models.
 
 
7
  """
8
 
 
9
  import gradio as gr
10
  from PIL import Image
11
  import time
12
  from typing import Optional, Tuple, List, Dict
13
  import json
14
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Import our modules
16
  from orchestrator import RadioFlowOrchestrator, WorkflowResult, create_orchestrator
17
  from utils.visualization import (
@@ -21,18 +36,49 @@ from utils.visualization import (
21
  create_timeline_chart
22
  )
23
 
 
 
 
 
 
 
 
 
 
 
24
  # Global orchestrator instance
25
  orchestrator: Optional[RadioFlowOrchestrator] = None
 
26
 
27
 
28
  def initialize_system():
29
- """Initialize the RadioFlow system."""
30
- global orchestrator
 
31
  if orchestrator is None:
32
- orchestrator = create_orchestrator(demo_mode=True)
33
- return "✅ RadioFlow System Initialized"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
 
 
36
  def process_xray(
37
  image: Optional[Image.Image],
38
  clinical_history: str,
@@ -42,6 +88,7 @@ def process_xray(
42
  ) -> Tuple[str, str, str, str, str, dict, dict, dict]:
43
  """
44
  Process a chest X-ray through the RadioFlow pipeline.
 
45
 
46
  Returns:
47
  Tuple of (report, priority_html, findings_json, metrics, status,
 
4
 
5
  This application demonstrates a multi-agent system for chest X-ray analysis
6
  using Google's Health AI Developer Foundations (HAI-DEF) models.
7
+
8
+ Now with REAL MedGemma inference via MLX (local) or ZeroGPU (HuggingFace).
9
  """
10
 
11
+ import os
12
  import gradio as gr
13
  from PIL import Image
14
  import time
15
  from typing import Optional, Tuple, List, Dict
16
  import json
17
 
18
+ # Try to import spaces for ZeroGPU on HuggingFace
19
+ try:
20
+ import spaces
21
+ SPACES_AVAILABLE = True
22
+ except ImportError:
23
+ SPACES_AVAILABLE = False
24
+ # Create a dummy decorator
25
+ class spaces:
26
+ @staticmethod
27
+ def GPU(func):
28
+ return func
29
+
30
  # Import our modules
31
  from orchestrator import RadioFlowOrchestrator, WorkflowResult, create_orchestrator
32
  from utils.visualization import (
 
36
  create_timeline_chart
37
  )
38
 
39
+ # Check if we're on HuggingFace Spaces with ZeroGPU
40
+ IS_SPACES = os.environ.get("SPACE_ID") is not None
41
+ USE_ZEROGPU = IS_SPACES and os.environ.get("ZEROGPU_ENABLED") == "true"
42
+
43
+ # Determine if we should use demo mode
44
+ # - Local with MLX: Use real model (demo_mode=False)
45
+ # - HuggingFace without GPU: Use demo mode (demo_mode=True)
46
+ # - HuggingFace with ZeroGPU: Use real model (demo_mode=False)
47
+ FORCE_DEMO_MODE = os.environ.get("FORCE_DEMO_MODE", "false").lower() == "true"
48
+
49
  # Global orchestrator instance
50
  orchestrator: Optional[RadioFlowOrchestrator] = None
51
+ engine_status = "Not initialized"
52
 
53
 
54
  def initialize_system():
55
+ """Initialize the RadioFlow system with real MedGemma."""
56
+ global orchestrator, engine_status
57
+
58
  if orchestrator is None:
59
+ # Try to use real model, fall back to demo if needed
60
+ demo_mode = FORCE_DEMO_MODE
61
+
62
+ try:
63
+ # Try to load the MedGemma engine first
64
+ from agents.medgemma_engine import get_engine
65
+ engine = get_engine(force_demo=demo_mode)
66
+ engine_status = f"MedGemma: {engine.backend}"
67
+
68
+ # Only use demo mode if engine is in demo mode
69
+ if engine.backend == "demo":
70
+ demo_mode = True
71
+ except Exception as e:
72
+ print(f"Could not initialize MedGemma engine: {e}")
73
+ engine_status = "Demo mode (engine failed)"
74
+ demo_mode = True
75
+
76
+ orchestrator = create_orchestrator(demo_mode=demo_mode)
77
+
78
+ return f"✅ RadioFlow System Initialized ({engine_status})"
79
 
80
 
81
+ @spaces.GPU(duration=120) # Request GPU for up to 2 minutes per inference
82
  def process_xray(
83
  image: Optional[Image.Image],
84
  clinical_history: str,
 
88
  ) -> Tuple[str, str, str, str, str, dict, dict, dict]:
89
  """
90
  Process a chest X-ray through the RadioFlow pipeline.
91
+ Uses real MedGemma inference with GPU acceleration.
92
 
93
  Returns:
94
  Tuple of (report, priority_html, findings_json, metrics, status,