X-iZhang commited on
Commit
b277e29
·
verified ·
1 Parent(s): f9018e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -31
app.py CHANGED
@@ -1,72 +1,88 @@
1
  import os
 
 
 
2
  import torch
3
  import gradio as gr
4
  import time
5
- from ccd import ccd_eval, run_eval
6
- from libra.eval.run_libra import load_model
7
 
8
  # =========================================
9
  # Safe Libra Hook (CPU fallback + dtype fix)
 
10
  # =========================================
11
- import torch
12
  import libra.model.builder as builder
13
  import libra.eval.run_libra as run_libra
14
 
15
- # 保存原始函数
16
- _original_load_pretrained_model = builder.load_pretrained_model
17
 
18
  def safe_load_pretrained_model(model_path, model_base=None, model_name=None, **kwargs):
19
  print("[INFO] Hook activated: safe_load_pretrained_model()")
20
 
21
- # ---- 关键修复 1:补全 model_name,避免 .lower() on None ----
22
  if model_name is None:
23
  model_name = model_path
24
 
25
- # ---- 关键修复 2:强制以 CPU 参数调用原函数,彻底绕开 CUDA 初始化 ----
26
- # 同时把 device_map 也设置为 cpu(避免传 'auto' 被塞进 {"": "auto"})
27
- kwargs = dict(kwargs) # 避免原 dict 被上层复用
28
- kwargs.setdefault("device", "cpu")
29
- kwargs.setdefault("device_map", "cpu")
30
 
31
- # 注意:原函数内部仍会把 torch_dtype 设为 float16(除非 4/8bit),
32
- # 但是我们可以在返回后统一上调为 float32。
33
- tokenizer, model, image_processor, context_len = _original_load_pretrained_model(
34
- model_path, model_base, model_name, **kwargs
35
- )
36
 
37
- # ---- 关键修复 3:CPU 环境统一上调到 float32,稳定运行 ----
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  if not torch.cuda.is_available():
39
  try:
40
- # 语言模型主体
41
  model.to(dtype=torch.float32)
42
  except Exception as e:
43
  print(f"[WARN] Could not upcast LM to float32: {e}")
44
-
45
  try:
46
- # 视觉塔
47
  vt = model.get_vision_tower()
48
- vt.to(device="cpu", dtype=torch.float32)
49
- print("[INFO] Vision tower moved to cpu (float32).")
50
  except Exception as e:
51
  print(f"[WARN] Could not move vision_tower to cpu/float32: {e}")
52
  else:
53
- # 若有 GPU,保持原来的 float16 + cuda(无需额外处理)
54
- print("[INFO] GPU available — default CUDA fp16 path is kept.")
55
 
56
  return tokenizer, model, image_processor, context_len
57
 
58
  # 将 builder 的加载函数替换为安全版
59
- builder.load_pretrained_model = safe_load_pretrained_model
 
60
 
61
- # 同时替换 run_libra.load_model,并把本地名也重绑定,确保后续调用走安全版
62
  def safe_load_model(model_path, model_base=None, model_name=None):
63
- print("[INFO] Hook activated: safe_load_model()")
64
  if model_name is None:
65
  model_name = model_path
66
  return safe_load_pretrained_model(model_path, model_base, model_name)
67
 
68
  run_libra.load_model = safe_load_model
69
- load_model = safe_load_model # 让 app.py 后续的 load_model() 使用安全版
 
 
 
70
 
71
  # =========================================
72
  # Global Configuration
@@ -371,14 +387,16 @@ def main():
371
 
372
 
373
  # Log that Gradio is starting (helpful when stdout/stderr are captured)
 
374
  try:
375
- with open('/workspace/CCD/callback.log', 'a', encoding='utf-8') as f:
 
376
  f.write(f"\n=== GRADIO START ===\nstarted_at: {time.strftime('%Y-%m-%d %H:%M:%S')}\n\n")
377
  except Exception:
378
  pass
379
 
380
- # Bind to 0.0.0.0 so the server is reachable from host/container and set an explicit port
381
- demo.launch(share=True)
382
 
383
 
384
  if __name__ == "__main__":
 
1
  import os
2
+ # Force CPU-only in this process by hiding CUDA devices (set before importing heavy libs)
3
+ os.environ.setdefault('CUDA_VISIBLE_DEVICES', '')
4
+
5
  import torch
6
  import gradio as gr
7
  import time
 
 
8
 
9
  # =========================================
10
  # Safe Libra Hook (CPU fallback + dtype fix)
11
+ # This hook must run before any heavyweight libra model-loading occurs.
12
  # =========================================
 
13
  import libra.model.builder as builder
14
  import libra.eval.run_libra as run_libra
15
 
16
+ # 保存原始函数(如果存在)
17
+ _original_load_pretrained_model = getattr(builder, 'load_pretrained_model', None)
18
 
19
  def safe_load_pretrained_model(model_path, model_base=None, model_name=None, **kwargs):
20
  print("[INFO] Hook activated: safe_load_pretrained_model()")
21
 
22
+ # 补全 model_name,避免 .lower() on None
23
  if model_name is None:
24
  model_name = model_path
25
 
26
+ # 强制以 CPU 参数调用原函数,尽量避免 CUDA 初始化
27
+ kwargs = dict(kwargs)
28
+ kwargs.setdefault('device', 'cpu')
29
+ kwargs.setdefault('device_map', 'cpu')
 
30
 
31
+ if _original_load_pretrained_model is None:
32
+ raise RuntimeError('Original load_pretrained_model not found in builder')
 
 
 
33
 
34
+ # Try calling the original with our kwargs; if it doesn't accept them, fall back.
35
+ try:
36
+ tokenizer, model, image_processor, context_len = _original_load_pretrained_model(
37
+ model_path, model_base, model_name, **kwargs
38
+ )
39
+ except TypeError as te:
40
+ # Some implementations don't accept device/device_map kwargs. Retry without them.
41
+ print(f"[WARN] original load_pretrained_model rejected kwargs: {te} — retrying without device kwargs")
42
+ try:
43
+ tokenizer, model, image_processor, context_len = _original_load_pretrained_model(
44
+ model_path, model_base, model_name
45
+ )
46
+ except Exception as e:
47
+ print(f"[ERROR] load_pretrained_model failed on retry: {e}")
48
+ raise
49
+ except Exception:
50
+ # propagate other errors
51
+ raise
52
+
53
+ # 在 CPU 情况下尝试把模型和视觉塔上调到 float32,减少 CPU 上的兼容问题
54
  if not torch.cuda.is_available():
55
  try:
 
56
  model.to(dtype=torch.float32)
57
  except Exception as e:
58
  print(f"[WARN] Could not upcast LM to float32: {e}")
 
59
  try:
 
60
  vt = model.get_vision_tower()
61
+ vt.to(device='cpu', dtype=torch.float32)
62
+ print('[INFO] Vision tower moved to cpu (float32).')
63
  except Exception as e:
64
  print(f"[WARN] Could not move vision_tower to cpu/float32: {e}")
65
  else:
66
+ print('[INFO] GPU available keeping original device/dtype behavior.')
 
67
 
68
  return tokenizer, model, image_processor, context_len
69
 
70
  # 将 builder 的加载函数替换为安全版
71
+ if _original_load_pretrained_model is not None:
72
+ builder.load_pretrained_model = safe_load_pretrained_model
73
 
74
+ # 同时替换 run_libra.load_model
75
  def safe_load_model(model_path, model_base=None, model_name=None):
76
+ print('[INFO] Hook activated: safe_load_model()')
77
  if model_name is None:
78
  model_name = model_path
79
  return safe_load_pretrained_model(model_path, model_base, model_name)
80
 
81
  run_libra.load_model = safe_load_model
82
+
83
+ # 现在导入 CCD 与其他被 hook 的符号(导入放在 hook 之后以确保生效)
84
+ from ccd import ccd_eval, run_eval
85
+ from libra.eval.run_libra import load_model
86
 
87
  # =========================================
88
  # Global Configuration
 
387
 
388
 
389
  # Log that Gradio is starting (helpful when stdout/stderr are captured)
390
+ # write startup log to local file in repository (avoid permission issues on Spaces)
391
  try:
392
+ os.makedirs('logs', exist_ok=True)
393
+ with open('logs/callback.log', 'a', encoding='utf-8') as f:
394
  f.write(f"\n=== GRADIO START ===\nstarted_at: {time.strftime('%Y-%m-%d %H:%M:%S')}\n\n")
395
  except Exception:
396
  pass
397
 
398
+
399
+ demo.launch()
400
 
401
 
402
  if __name__ == "__main__":