NMundhra commited on
Commit
754d54a
·
1 Parent(s): f325c2c

fix(app): add comprehensive image_input type handling for dictionary/path/PIL objects

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -50,15 +50,28 @@ print("✅ ConvNeXt V2 Model loaded into CPU memory!", flush=True)
50
  # Inference function wrapped in ZeroGPU decorator
51
  def _run_classification(image_input, generate_gradcam=True):
52
  if image_input is None:
53
- return None, "Please upload a valid OCT scan image."
54
 
55
- # Handle image input types (PIL or file path)
56
  if isinstance(image_input, str):
57
  img_path = image_input
58
- else:
 
 
 
 
 
 
59
  temp_dir = Path(tempfile.gettempdir())
60
  img_path = str(temp_dir / "temp_input_scan.png")
61
  image_input.save(img_path)
 
 
 
 
 
 
 
62
 
63
  # Dynamic CUDA transfer inside GPU context
64
  if torch.cuda.is_available():
@@ -115,4 +128,4 @@ if HAS_GRADIO:
115
 
116
  if __name__ == "__main__":
117
  if HAS_GRADIO:
118
- demo.launch()
 
50
  # Inference function wrapped in ZeroGPU decorator
51
  def _run_classification(image_input, generate_gradcam=True):
52
  if image_input is None:
53
+ return {"error": "Please upload a valid OCT scan image."}
54
 
55
+ img_path = None
56
  if isinstance(image_input, str):
57
  img_path = image_input
58
+ elif isinstance(image_input, dict) and "path" in image_input:
59
+ img_path = image_input["path"]
60
+ elif hasattr(image_input, "name") and isinstance(image_input.name, str):
61
+ img_path = image_input.name
62
+ elif hasattr(image_input, "path") and isinstance(image_input.path, str):
63
+ img_path = image_input.path
64
+ elif hasattr(image_input, "save"):
65
  temp_dir = Path(tempfile.gettempdir())
66
  img_path = str(temp_dir / "temp_input_scan.png")
67
  image_input.save(img_path)
68
+ else:
69
+ try:
70
+ temp_dir = Path(tempfile.gettempdir())
71
+ img_path = str(temp_dir / "temp_input_scan.png")
72
+ Image.fromarray(np.array(image_input)).save(img_path)
73
+ except Exception as err:
74
+ return {"error": f"Invalid image format received: {type(image_input)} - {err}"}
75
 
76
  # Dynamic CUDA transfer inside GPU context
77
  if torch.cuda.is_available():
 
128
 
129
  if __name__ == "__main__":
130
  if HAS_GRADIO:
131
+ demo.launch(show_error=True)