edyoshikun Claude Opus 4.7 (1M context) commited on
Commit
9135e03
·
1 Parent(s): 6d1b025

Migrate to ZeroGPU + VisCy v0.5.0a1

Browse files

- Remove Dockerfile/runtime.txt; switch to native Gradio SDK for ZeroGPU.
- README: python_version 3.12, hardware zero-a10g.
- requirements.txt: install viscy-data/models/transforms/utils and cytoland
from mehta-lab/VisCy monorepo, pinned to alpha tag v0.5.0a1
(modular-viscy-staging @ 393abdf). Add spaces for @spaces.GPU.
- app.py: import VSUNet from cytoland.engine (new monorepo location), load
checkpoint to CPU, decorate predict() with @spaces.GPU, pick device inside
the decorated call.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files changed (5) hide show
  1. Dockerfile +0 -30
  2. README.md +2 -1
  3. app.py +17 -18
  4. requirements.txt +9 -3
  5. runtime.txt +0 -1
Dockerfile DELETED
@@ -1,30 +0,0 @@
1
- FROM python:3.11-slim
2
-
3
- WORKDIR /code
4
-
5
- # Install system dependencies
6
- RUN apt-get update && apt-get install -y \
7
- git \
8
- git-lfs \
9
- ffmpeg \
10
- libsm6 \
11
- libxext6 \
12
- cmake \
13
- rsync \
14
- libgl1-mesa-glx \
15
- && rm -rf /var/lib/apt/lists/* \
16
- && git lfs install
17
-
18
- # Copy requirements and install Python dependencies
19
- COPY requirements.txt .
20
- RUN pip install --no-cache-dir --upgrade pip && \
21
- pip install --no-cache-dir -r requirements.txt
22
-
23
- # Copy the rest of the application
24
- COPY . .
25
-
26
- # Expose the port Gradio runs on
27
- EXPOSE 7860
28
-
29
- # Command to run the application
30
- CMD ["python", "app.py"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -8,7 +8,8 @@ sdk_version: 5.27.1
8
  app_file: app.py
9
  pinned: true
10
  license: bsd-3-clause
11
- python_version: 3.11
 
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
8
  app_file: app.py
9
  pinned: true
10
  license: bsd-3-clause
11
+ python_version: "3.12"
12
+ hardware: zero-a10g
13
  ---
14
 
15
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
 
2
  import torch
3
- from viscy.translation.engine import VSUNet
4
  from huggingface_hub import hf_hub_download
5
  from numpy.typing import ArrayLike
6
  import numpy as np
@@ -14,26 +15,19 @@ class VSGradio:
14
  def __init__(self, model_config, model_ckpt_path):
15
  self.model_config = model_config
16
  self.model_ckpt_path = model_ckpt_path
17
- self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
- print(f"Using device: {self.device}")
19
  self.model = None
20
  self.load_model()
21
 
22
  def load_model(self):
23
- try:
24
- # Load the model checkpoint and move it to the correct device (GPU or CPU)
25
- print(f"Loading model from checkpoint: {self.model_ckpt_path}")
26
- self.model = VSUNet.load_from_checkpoint(
27
- self.model_ckpt_path,
28
- architecture="UNeXt2_2D",
29
- model_config=self.model_config,
30
- )
31
- self.model.to(self.device)
32
- self.model.eval()
33
- print("Model loaded successfully and set to evaluation mode")
34
- except Exception as e:
35
- print(f"Error loading model: {e}")
36
- raise
37
 
38
  def normalize_fov(self, input: ArrayLike):
39
  "Normalizing the fov with zero mean and unit variance"
@@ -52,12 +46,17 @@ class VSGradio:
52
  new_width = int(width * scale_factor)
53
  return resize(inp, (new_height, new_width), anti_aliasing=True)
54
 
 
55
  def predict(self, inp, scaling_factor: float):
56
  try:
57
  if inp is None:
58
  print("Error: Input image is None")
59
  return None, None
60
 
 
 
 
 
61
  # Normalize the input and convert to tensor
62
  inp = self.normalize_fov(inp)
63
  original_shape = inp.shape
@@ -68,7 +67,7 @@ class VSGradio:
68
 
69
  test_dict = dict(
70
  index=None,
71
- source=inp.unsqueeze(0).unsqueeze(0).unsqueeze(0).to(self.device),
72
  )
73
 
74
  with torch.inference_mode():
 
1
  import gradio as gr
2
+ import spaces
3
  import torch
4
+ from cytoland.engine import VSUNet
5
  from huggingface_hub import hf_hub_download
6
  from numpy.typing import ArrayLike
7
  import numpy as np
 
15
  def __init__(self, model_config, model_ckpt_path):
16
  self.model_config = model_config
17
  self.model_ckpt_path = model_ckpt_path
 
 
18
  self.model = None
19
  self.load_model()
20
 
21
  def load_model(self):
22
+ print(f"Loading model from checkpoint: {self.model_ckpt_path}")
23
+ self.model = VSUNet.load_from_checkpoint(
24
+ self.model_ckpt_path,
25
+ architecture="UNeXt2_2D",
26
+ model_config=self.model_config,
27
+ map_location="cpu",
28
+ )
29
+ self.model.eval()
30
+ print("Model loaded on CPU; will move to GPU on demand via @spaces.GPU.")
 
 
 
 
 
31
 
32
  def normalize_fov(self, input: ArrayLike):
33
  "Normalizing the fov with zero mean and unit variance"
 
46
  new_width = int(width * scale_factor)
47
  return resize(inp, (new_height, new_width), anti_aliasing=True)
48
 
49
+ @spaces.GPU
50
  def predict(self, inp, scaling_factor: float):
51
  try:
52
  if inp is None:
53
  print("Error: Input image is None")
54
  return None, None
55
 
56
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
57
+ print(f"Running inference on device: {device}")
58
+ self.model.to(device)
59
+
60
  # Normalize the input and convert to tensor
61
  inp = self.normalize_fov(inp)
62
  original_shape = inp.shape
 
67
 
68
  test_dict = dict(
69
  index=None,
70
+ source=inp.unsqueeze(0).unsqueeze(0).unsqueeze(0).to(device),
71
  )
72
 
73
  with torch.inference_mode():
requirements.txt CHANGED
@@ -1,6 +1,12 @@
1
- # Requires Python 3.11 or higher
2
- git+https://github.com/mehta-lab/VisCy.git@v0.3.0
 
 
 
 
 
 
 
3
  gradio==5.27.1
4
  scikit-image
5
  cmap
6
- pydantic==2.11.3
 
1
+ # Requires Python 3.12 or higher
2
+ # VisCy monorepo (mehta-lab/VisCy) pinned to alpha tag v0.5.0a1
3
+ # (points at modular-viscy-staging @ 393abdf7c62a4b72abdfca49bf1a210f5d5e5ec9)
4
+ viscy-data @ git+https://github.com/mehta-lab/VisCy.git@v0.5.0a1#subdirectory=packages/viscy-data
5
+ viscy-models @ git+https://github.com/mehta-lab/VisCy.git@v0.5.0a1#subdirectory=packages/viscy-models
6
+ viscy-transforms @ git+https://github.com/mehta-lab/VisCy.git@v0.5.0a1#subdirectory=packages/viscy-transforms
7
+ viscy-utils @ git+https://github.com/mehta-lab/VisCy.git@v0.5.0a1#subdirectory=packages/viscy-utils
8
+ cytoland @ git+https://github.com/mehta-lab/VisCy.git@v0.5.0a1#subdirectory=applications/cytoland
9
+ spaces
10
  gradio==5.27.1
11
  scikit-image
12
  cmap
 
runtime.txt DELETED
@@ -1 +0,0 @@
1
- python-3.11