zhang0319 commited on
Commit
dd6729b
·
verified ·
1 Parent(s): 2fdb0c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -2
app.py CHANGED
@@ -32,10 +32,24 @@ def clip_and_norm(img: np.ndarray, vmin: float, vmax: float) -> np.ndarray:
32
  x = (x - vmin) / max(vmax - vmin, 1e-6)
33
  return x.astype(np.float32)
34
 
35
- def load_nii(file) -> np.ndarray:
 
 
 
36
  if file is None:
37
  return None
38
- img = nib.load(file.name)
 
 
 
 
 
 
 
 
 
 
 
39
  return img.get_fdata().astype(np.float32)
40
 
41
  def clean_report_text(raw: str) -> str:
 
32
  x = (x - vmin) / max(vmax - vmin, 1e-6)
33
  return x.astype(np.float32)
34
 
35
+ def load_nii(file) -> np.ndarray | None:
36
+ """
37
+ 兼容 Gradio 4/5:File 可能是字符串路径、{'path': ...} 字典,或旧式带 .name 的对象。
38
+ """
39
  if file is None:
40
  return None
41
+
42
+ if isinstance(file, (str, os.PathLike)):
43
+ path = str(file)
44
+ elif isinstance(file, dict) and "path" in file:
45
+ path = file["path"]
46
+ else:
47
+ path = getattr(file, "name", None)
48
+
49
+ if not path or not os.path.exists(path):
50
+ return None
51
+
52
+ img = nib.load(path)
53
  return img.get_fdata().astype(np.float32)
54
 
55
  def clean_report_text(raw: str) -> str: