Prakhar54-byte commited on
Commit
be96fd0
·
verified ·
1 Parent(s): 5880862

Deploy build-751b390

Browse files
Files changed (2) hide show
  1. backend/main.py +8 -9
  2. frontend/app.py +2 -1
backend/main.py CHANGED
@@ -50,7 +50,7 @@ LEGACY_MODEL_DIR = BASE_DIR / "models"
50
  REALWORLD_MODEL_DIR = BASE_DIR / "models" / "realworld_efficientnet_b0"
51
  CHESTMNIST_MODEL_DIR = BASE_DIR / "models" / "chestmnist_mobilenetv3"
52
 
53
- PROFILE = os.getenv("PNEUMOOPS_PROFILE", "realworld").lower()
54
  _DEFAULT_MODEL_DIR = {
55
  "realworld": REALWORLD_MODEL_DIR,
56
  "chestmnist": CHESTMNIST_MODEL_DIR,
@@ -576,13 +576,9 @@ def generate_cam_overlay(image: Image.Image, cam_tensor: torch.Tensor) -> str:
576
  cam = np.maximum(cam, 0)
577
  cam = cam - np.min(cam)
578
  cam_max = np.max(cam)
579
-
580
  if cam_max > 1e-12:
581
  cam = cam / cam_max
582
-
583
- cam_img = Image.fromarray(np.uint8(255 * cam)).resize(
584
- image.size, Image.Resampling.BILINEAR
585
- )
586
  cam_resized = np.array(cam_img) / 255.0
587
  colormap = cm.get_cmap("jet")(cam_resized)[:, :, :3]
588
  heatmap = np.uint8(255 * colormap)
@@ -598,6 +594,7 @@ def generate_cam_overlay(image: Image.Image, cam_tensor: torch.Tensor) -> str:
598
  image.save(buf, format="PNG")
599
  return base64.b64encode(buf.getvalue()).decode("utf-8")
600
 
 
601
  def run_pytorch_inference(image: Image.Image) -> dict[str, Any]:
602
  if PYTORCH_MODEL is None:
603
  raise RuntimeError("PyTorch checkpoint is missing.")
@@ -674,6 +671,7 @@ def run_pytorch_inference(image: Image.Image) -> dict[str, Any]:
674
  **postprocess_probabilities(probabilities),
675
  }
676
 
 
677
  def run_onnx_inference(image: Image.Image) -> dict[str, Any]:
678
  if ONNX_SESSION is None:
679
  raise RuntimeError("ONNX artifact is missing.")
@@ -1051,9 +1049,9 @@ async def infer_raw(file: UploadFile = File(...)):
1051
 
1052
  try:
1053
  # Dynamically add frontend dir to path so app.py can be imported
1054
- _frontend_dir = str(BASE_DIR / "frontend")
1055
- if _frontend_dir not in sys.path:
1056
- sys.path.insert(0, _frontend_dir)
1057
 
1058
  import gradio as gr
1059
  from frontend.app import demo as gradio_demo # the gr.Blocks() object
@@ -1078,3 +1076,4 @@ if __name__ == "__main__":
1078
 
1079
  port = int(os.getenv("PORT", "7860"))
1080
  uvicorn.run(app, host="0.0.0.0", port=port)
 
 
50
  REALWORLD_MODEL_DIR = BASE_DIR / "models" / "realworld_efficientnet_b0"
51
  CHESTMNIST_MODEL_DIR = BASE_DIR / "models" / "chestmnist_mobilenetv3"
52
 
53
+ PROFILE = os.getenv("PNEUMOOPS_PROFILE", "chestmnist").lower()
54
  _DEFAULT_MODEL_DIR = {
55
  "realworld": REALWORLD_MODEL_DIR,
56
  "chestmnist": CHESTMNIST_MODEL_DIR,
 
576
  cam = np.maximum(cam, 0)
577
  cam = cam - np.min(cam)
578
  cam_max = np.max(cam)
 
579
  if cam_max > 1e-12:
580
  cam = cam / cam_max
581
+ cam_img = Image.fromarray(np.uint8(255 * cam)).resize(image.size, Image.Resampling.BILINEAR)
 
 
 
582
  cam_resized = np.array(cam_img) / 255.0
583
  colormap = cm.get_cmap("jet")(cam_resized)[:, :, :3]
584
  heatmap = np.uint8(255 * colormap)
 
594
  image.save(buf, format="PNG")
595
  return base64.b64encode(buf.getvalue()).decode("utf-8")
596
 
597
+
598
  def run_pytorch_inference(image: Image.Image) -> dict[str, Any]:
599
  if PYTORCH_MODEL is None:
600
  raise RuntimeError("PyTorch checkpoint is missing.")
 
671
  **postprocess_probabilities(probabilities),
672
  }
673
 
674
+
675
  def run_onnx_inference(image: Image.Image) -> dict[str, Any]:
676
  if ONNX_SESSION is None:
677
  raise RuntimeError("ONNX artifact is missing.")
 
1049
 
1050
  try:
1051
  # Dynamically add frontend dir to path so app.py can be imported
1052
+ _root_dir = str(BASE_DIR)
1053
+ if _root_dir not in sys.path:
1054
+ sys.path.insert(0, _root_dir)
1055
 
1056
  import gradio as gr
1057
  from frontend.app import demo as gradio_demo # the gr.Blocks() object
 
1076
 
1077
  port = int(os.getenv("PORT", "7860"))
1078
  uvicorn.run(app, host="0.0.0.0", port=port)
1079
+
frontend/app.py CHANGED
@@ -5,7 +5,8 @@ import requests
5
  import gradio as gr
6
  from PIL import Image
7
 
8
- BACKEND_URL = os.getenv("BACKEND_PREDICT_URL", "http://127.0.0.1:7860/predict")
 
9
 
10
  def call_backend(image: Image.Image, api_url: str) -> dict:
11
  buf = io.BytesIO()
 
5
  import gradio as gr
6
  from PIL import Image
7
 
8
+ PORT = os.getenv("PORT", "7860")
9
+ BACKEND_URL = os.getenv("BACKEND_PREDICT_URL", f"http://127.0.0.1:{PORT}/predict")
10
 
11
  def call_backend(image: Image.Image, api_url: str) -> dict:
12
  buf = io.BytesIO()