X-iZhang commited on
Commit
5b2a27e
·
verified ·
1 Parent(s): 9a186ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py CHANGED
@@ -5,6 +5,38 @@ import time
5
  from ccd import ccd_eval, run_eval
6
  from libra.eval.run_libra import load_model
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # =========================================
10
  # Global Configuration
@@ -201,6 +233,12 @@ def main():
201
  gr.Markdown("""
202
  # 📷 CCD: Mitigating Hallucinations in Radiology MLLMs via Clinical Contrastive Decoding
203
  ### [Project Page](https://x-izhang.github.io/CCD/) | [Paper](https://arxiv.org/abs/2509.23379) | [Code](https://github.com/X-iZhang/CCD) | [Models](https://huggingface.co/collections/X-iZhang/libra-6772bfccc6079298a0fa5f8d)
 
 
 
 
 
 
204
  """)
205
 
206
  with gr.Tab("✨ CCD Demo"):
 
5
  from ccd import ccd_eval, run_eval
6
  from libra.eval.run_libra import load_model
7
 
8
+ # =========================================
9
+ # Safe Libra Hook: fallback to CPU if no CUDA
10
+ # =========================================
11
+ import torch
12
+ import libra.model.builder as builder
13
+
14
+ _original_load_pretrained_model = builder.load_pretrained_model
15
+
16
+ def safe_load_pretrained_model(model_path, model_base=None, model_name=None, **kwargs):
17
+ print("[INFO] Applying safe device fallback hook for Libra...")
18
+ tokenizer, model, image_processor, context_len = _original_load_pretrained_model(
19
+ model_path, model_base, model_name, **kwargs
20
+ )
21
+
22
+ if torch.cuda.is_available():
23
+ device, dtype = "cuda", torch.float16
24
+ print("[INFO] GPU detected: using float16 precision.")
25
+ else:
26
+ device, dtype = "cpu", torch.float32
27
+ print("[WARN] No GPU detected. Running Libra on CPU with float32.")
28
+
29
+ try:
30
+ vision_tower = model.get_vision_tower()
31
+ vision_tower.to(device=device, dtype=dtype)
32
+ print(f"[INFO] Vision tower moved to {device} ({dtype})")
33
+ except Exception as e:
34
+ print(f"[WARN] Could not move vision tower: {e}")
35
+
36
+ return tokenizer, model, image_processor, context_len
37
+
38
+ builder.load_pretrained_model = safe_load_pretrained_model
39
+
40
 
41
  # =========================================
42
  # Global Configuration
 
233
  gr.Markdown("""
234
  # 📷 CCD: Mitigating Hallucinations in Radiology MLLMs via Clinical Contrastive Decoding
235
  ### [Project Page](https://x-izhang.github.io/CCD/) | [Paper](https://arxiv.org/abs/2509.23379) | [Code](https://github.com/X-iZhang/CCD) | [Models](https://huggingface.co/collections/X-iZhang/libra-6772bfccc6079298a0fa5f8d)
236
+
237
+ **🚨 Performance Warning**
238
+
239
+ The demo is currently running on **CPU**, and a single inference takes approximately **500 seconds**.
240
+ To achieve optimal performance and significantly reduce inference time, **GPU** is required for effective operation.
241
+ For more details, please refer to the [launch demo locally](https://github.com/X-iZhang/CCD#gradio-web-interface).
242
  """)
243
 
244
  with gr.Tab("✨ CCD Demo"):