Spaces:
Running
on
Zero
Running
on
Zero
Y Phung Nguyen
commited on
Commit
Β·
5c0f22e
1
Parent(s):
63e92ef
Upd MedSwin loader with GPU
Browse files
ui.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
"""Gradio UI setup"""
|
| 2 |
import time
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from config import TITLE, DESCRIPTION, CSS, MEDSWIN_MODELS, DEFAULT_MEDICAL_MODEL
|
| 5 |
from indexing import create_or_update_index
|
| 6 |
from pipeline import stream_chat
|
| 7 |
from voice import transcribe_audio, generate_speech
|
| 8 |
from models import initialize_medical_model, is_model_loaded, get_model_loading_state, set_model_loading_state
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
def create_demo():
|
|
@@ -258,6 +260,29 @@ def create_demo():
|
|
| 258 |
outputs=[agentic_thoughts_box, show_thoughts_state]
|
| 259 |
)
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
def load_model_and_update_status(model_name):
|
| 262 |
"""Load model and update status, return status text and whether model is ready"""
|
| 263 |
try:
|
|
@@ -270,14 +295,9 @@ def create_demo():
|
|
| 270 |
elif state == "error":
|
| 271 |
return "β Error loading model. Please try again.", False
|
| 272 |
|
| 273 |
-
#
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
initialize_medical_model(model_name)
|
| 277 |
-
return "β
The model has been loaded successfully", True
|
| 278 |
-
except Exception as e:
|
| 279 |
-
set_model_loading_state(model_name, "error")
|
| 280 |
-
return f"β Error loading model: {str(e)[:100]}", False
|
| 281 |
except Exception as e:
|
| 282 |
return f"β Error: {str(e)[:100]}", False
|
| 283 |
|
|
@@ -293,6 +313,29 @@ def create_demo():
|
|
| 293 |
else:
|
| 294 |
return "β οΈ Model not loaded. Click to load or it will load on first use.", False
|
| 295 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
# Initialize status on load
|
| 297 |
def init_model_status():
|
| 298 |
status_text, is_ready = check_model_status(DEFAULT_MEDICAL_MODEL)
|
|
@@ -314,9 +357,9 @@ def create_demo():
|
|
| 314 |
outputs=[model_status, submit_button, message_input]
|
| 315 |
)
|
| 316 |
|
| 317 |
-
#
|
| 318 |
demo.load(
|
| 319 |
-
fn=
|
| 320 |
outputs=[model_status]
|
| 321 |
)
|
| 322 |
|
|
|
|
| 1 |
"""Gradio UI setup"""
|
| 2 |
import time
|
| 3 |
import gradio as gr
|
| 4 |
+
import spaces
|
| 5 |
from config import TITLE, DESCRIPTION, CSS, MEDSWIN_MODELS, DEFAULT_MEDICAL_MODEL
|
| 6 |
from indexing import create_or_update_index
|
| 7 |
from pipeline import stream_chat
|
| 8 |
from voice import transcribe_audio, generate_speech
|
| 9 |
from models import initialize_medical_model, is_model_loaded, get_model_loading_state, set_model_loading_state
|
| 10 |
+
from logger import logger
|
| 11 |
|
| 12 |
|
| 13 |
def create_demo():
|
|
|
|
| 260 |
outputs=[agentic_thoughts_box, show_thoughts_state]
|
| 261 |
)
|
| 262 |
|
| 263 |
+
# GPU-decorated function to load any model (for user selection)
|
| 264 |
+
@spaces.GPU(max_duration=120)
|
| 265 |
+
def load_model_with_gpu(model_name):
|
| 266 |
+
"""Load medical model (GPU-decorated for ZeroGPU compatibility)"""
|
| 267 |
+
try:
|
| 268 |
+
if not is_model_loaded(model_name):
|
| 269 |
+
logger.info(f"Loading medical model: {model_name}...")
|
| 270 |
+
set_model_loading_state(model_name, "loading")
|
| 271 |
+
try:
|
| 272 |
+
initialize_medical_model(model_name)
|
| 273 |
+
logger.info(f"β
Medical model {model_name} loaded successfully!")
|
| 274 |
+
return "β
The model has been loaded successfully", True
|
| 275 |
+
except Exception as e:
|
| 276 |
+
logger.error(f"Failed to load medical model {model_name}: {e}")
|
| 277 |
+
set_model_loading_state(model_name, "error")
|
| 278 |
+
return f"β Error loading model: {str(e)[:100]}", False
|
| 279 |
+
else:
|
| 280 |
+
logger.info(f"Medical model {model_name} is already loaded")
|
| 281 |
+
return "β
The model has been loaded successfully", True
|
| 282 |
+
except Exception as e:
|
| 283 |
+
logger.error(f"Error loading model {model_name}: {e}")
|
| 284 |
+
return f"β Error: {str(e)[:100]}", False
|
| 285 |
+
|
| 286 |
def load_model_and_update_status(model_name):
|
| 287 |
"""Load model and update status, return status text and whether model is ready"""
|
| 288 |
try:
|
|
|
|
| 295 |
elif state == "error":
|
| 296 |
return "β Error loading model. Please try again.", False
|
| 297 |
|
| 298 |
+
# Use GPU-decorated function to load the model
|
| 299 |
+
status_text, is_ready = load_model_with_gpu(model_name)
|
| 300 |
+
return status_text, is_ready
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
except Exception as e:
|
| 302 |
return f"β Error: {str(e)[:100]}", False
|
| 303 |
|
|
|
|
| 313 |
else:
|
| 314 |
return "β οΈ Model not loaded. Click to load or it will load on first use.", False
|
| 315 |
|
| 316 |
+
# GPU-decorated function to load model on startup
|
| 317 |
+
@spaces.GPU(max_duration=120)
|
| 318 |
+
def load_default_model_on_startup():
|
| 319 |
+
"""Load default medical model on startup (GPU-decorated for ZeroGPU compatibility)"""
|
| 320 |
+
try:
|
| 321 |
+
if not is_model_loaded(DEFAULT_MEDICAL_MODEL):
|
| 322 |
+
logger.info(f"Loading default medical model on startup: {DEFAULT_MEDICAL_MODEL}...")
|
| 323 |
+
set_model_loading_state(DEFAULT_MEDICAL_MODEL, "loading")
|
| 324 |
+
try:
|
| 325 |
+
initialize_medical_model(DEFAULT_MEDICAL_MODEL)
|
| 326 |
+
logger.info(f"β
Default medical model {DEFAULT_MEDICAL_MODEL} loaded successfully on startup!")
|
| 327 |
+
return f"β
{DEFAULT_MEDICAL_MODEL} loaded successfully"
|
| 328 |
+
except Exception as e:
|
| 329 |
+
logger.error(f"Failed to load default medical model on startup: {e}")
|
| 330 |
+
set_model_loading_state(DEFAULT_MEDICAL_MODEL, "error")
|
| 331 |
+
return f"β Error loading model: {str(e)[:100]}"
|
| 332 |
+
else:
|
| 333 |
+
logger.info(f"Default medical model {DEFAULT_MEDICAL_MODEL} is already loaded")
|
| 334 |
+
return f"β
{DEFAULT_MEDICAL_MODEL} is ready"
|
| 335 |
+
except Exception as e:
|
| 336 |
+
logger.error(f"Error in model loading startup: {e}")
|
| 337 |
+
return f"β οΈ Startup loading error: {str(e)[:100]}"
|
| 338 |
+
|
| 339 |
# Initialize status on load
|
| 340 |
def init_model_status():
|
| 341 |
status_text, is_ready = check_model_status(DEFAULT_MEDICAL_MODEL)
|
|
|
|
| 357 |
outputs=[model_status, submit_button, message_input]
|
| 358 |
)
|
| 359 |
|
| 360 |
+
# Load default model on startup (GPU-decorated function)
|
| 361 |
demo.load(
|
| 362 |
+
fn=load_default_model_on_startup,
|
| 363 |
outputs=[model_status]
|
| 364 |
)
|
| 365 |
|