esab commited on
Commit
b5f32bf
·
verified ·
1 Parent(s): 7db0ee1

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +3 -22
  2. app.py +40 -109
  3. requirements.txt +1 -2
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🔬
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
@@ -14,29 +14,10 @@ license: apache-2.0
14
 
15
  Generate synthetic peripheral blood cell images using a fine-tuned Stable Diffusion 2.1 model.
16
 
17
- ## Features
18
-
19
- - Generate 8 different blood cell types
20
- - Uses detailed morphological V2 prompts
21
- - FID Score: 79.39 (vs 17,092 real images)
22
- - Customizable generation parameters
23
-
24
  ## Supported Cell Types
25
 
26
- 1. **Neutrophil** - Segmented nucleus, azurophilic granules
27
- 2. **Lymphocyte** - Small, high N/C ratio, round nucleus
28
- 3. **Monocyte** - Large, kidney-shaped nucleus, vacuoles
29
- 4. **Eosinophil** - Bilobed nucleus, eosinophilic granules
30
- 5. **Basophil** - Segmented nucleus, basophilic granules
31
- 6. **Platelet** - Small, anucleate fragments
32
- 7. **Erythroblast** - Nucleated RBC precursor
33
- 8. **Immature Granulocyte (IG)** - Large, fine chromatin
34
 
35
  ## Model
36
 
37
- This Space uses [esab/pbcell-sd21-v2](https://huggingface.co/esab/pbcell-sd21-v2), fine-tuned on the PBC dataset.
38
-
39
- ## Note
40
-
41
- Running on free CPU tier - generation takes ~2-3 minutes per image.
42
- For faster generation, duplicate this Space and select a GPU runtime.
 
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 4.19.2
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
14
 
15
  Generate synthetic peripheral blood cell images using a fine-tuned Stable Diffusion 2.1 model.
16
 
 
 
 
 
 
 
 
17
  ## Supported Cell Types
18
 
19
+ Neutrophil, Lymphocyte, Monocyte, Eosinophil, Basophil, Platelet, Erythroblast, Immature Granulocyte (IG)
 
 
 
 
 
 
 
20
 
21
  ## Model
22
 
23
+ [esab/pbcell-sd21-v2](https://huggingface.co/esab/pbcell-sd21-v2) - FID Score: 79.39
 
 
 
 
 
app.py CHANGED
@@ -1,12 +1,9 @@
1
  """
2
  PB Cell Generator - Synthetic Blood Cell Image Generation
3
- Using fine-tuned Stable Diffusion 2.1 with V2 morphological captions.
4
  """
5
 
6
  import os
7
- import torch
8
  import gradio as gr
9
- from PIL import Image
10
 
11
  # Cell type configurations with V2 prompts
12
  CELL_TYPES = {
@@ -22,152 +19,86 @@ CELL_TYPES = {
22
 
23
  CELL_TYPE_LIST = list(CELL_TYPES.keys())
24
 
25
- # Model configuration
26
- MODEL_REPO = "esab/pbcell-sd21-v2"
27
- BASE_MODEL = "sd2-community/stable-diffusion-2-1"
28
-
29
- # Global pipeline variable
30
  pipe = None
31
 
32
 
33
  def load_pipeline():
34
- """Load the fine-tuned pipeline."""
35
  global pipe
36
  if pipe is not None:
37
  return pipe
38
 
 
39
  from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler, UNet2DConditionModel
40
 
41
- # Get HF token from environment (set as Space secret)
42
  hf_token = os.environ.get("HF_TOKEN")
43
-
44
- # Determine device and dtype
45
  device = "cuda" if torch.cuda.is_available() else "cpu"
46
  dtype = torch.float16 if torch.cuda.is_available() else torch.float32
47
 
48
- print(f"Loading pipeline on {device} with {dtype}...")
49
 
50
- # Load base pipeline
51
  pipe = StableDiffusionPipeline.from_pretrained(
52
- BASE_MODEL,
53
  torch_dtype=dtype,
54
  token=hf_token,
55
  )
56
 
57
- # Load fine-tuned UNet
58
  unet = UNet2DConditionModel.from_pretrained(
59
- MODEL_REPO,
60
  subfolder="unet",
61
  torch_dtype=dtype,
62
  token=hf_token,
63
  )
64
  pipe.unet = unet
65
-
66
- # Use DPM solver for faster inference
67
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
68
-
69
  pipe = pipe.to(device)
70
 
71
- # Enable memory optimizations
72
  if device == "cuda":
73
  pipe.enable_attention_slicing()
74
 
75
- print("Pipeline loaded successfully!")
76
  return pipe
77
 
78
 
79
- def generate_cell(
80
- cell_type: str,
81
- custom_prompt: str,
82
- guidance_scale: float,
83
- num_steps: int,
84
- seed: int,
85
- ) -> Image.Image:
86
- """Generate a blood cell image."""
87
- pipe = load_pipeline()
88
-
89
- # Use custom prompt if provided, otherwise use preset
90
- if custom_prompt and custom_prompt.strip():
91
- prompt = custom_prompt
92
- else:
93
- prompt = CELL_TYPES.get(cell_type, CELL_TYPES["Neutrophil"])
94
-
95
- # Set seed for reproducibility
96
- generator = torch.Generator(device=pipe.device)
97
- if seed >= 0:
98
- generator.manual_seed(int(seed))
99
- else:
100
- generator.manual_seed(torch.randint(0, 2**32, (1,)).item())
101
-
102
- # Generate image
103
- result = pipe(
104
  prompt=prompt,
105
  height=512,
106
  width=512,
107
- num_inference_steps=int(num_steps),
108
- guidance_scale=float(guidance_scale),
109
- generator=generator,
110
  )
111
 
112
  return result.images[0]
113
 
114
 
115
- # Create simple Gradio interface
116
- demo = gr.Interface(
117
- fn=generate_cell,
118
- inputs=[
119
- gr.Dropdown(
120
- choices=CELL_TYPE_LIST,
121
- value="Neutrophil",
122
- label="Cell Type",
123
- info="Select a blood cell type to generate"
124
- ),
125
- gr.Textbox(
126
- label="Custom Prompt (optional)",
127
- placeholder="Leave empty to use the default V2 prompt...",
128
- lines=2,
129
- ),
130
- gr.Slider(
131
- minimum=1.0,
132
- maximum=20.0,
133
- value=8.5,
134
- step=0.5,
135
- label="Guidance Scale (CFG)"
136
- ),
137
- gr.Slider(
138
- minimum=10,
139
- maximum=50,
140
- value=20,
141
- step=5,
142
- label="Inference Steps"
143
- ),
144
- gr.Number(
145
- value=42,
146
- label="Seed (-1 for random)",
147
- precision=0
148
- ),
149
- ],
150
- outputs=gr.Image(type="pil", label="Generated Cell"),
151
- title="PB Cell Generator",
152
- description="""
153
- Generate synthetic peripheral blood cell images using a fine-tuned Stable Diffusion 2.1 model.
154
-
155
- **Model:** [esab/pbcell-sd21-v2](https://huggingface.co/esab/pbcell-sd21-v2) | **FID Score:** 79.39
156
-
157
- Select a cell type or write a custom prompt to generate blood cell images.
158
-
159
- **Supported Cell Types:** Neutrophil, Lymphocyte, Monocyte, Eosinophil, Basophil, Platelet, Erythroblast, Immature Granulocyte (IG)
160
-
161
- **Note:** Running on CPU - generation takes ~2-3 minutes. For faster results, duplicate this Space with a GPU.
162
- """,
163
- examples=[
164
- ["Neutrophil", "", 8.5, 20, 42],
165
- ["Lymphocyte", "", 8.5, 20, 123],
166
- ["Eosinophil", "", 8.5, 20, 456],
167
- ["Monocyte", "", 8.5, 20, 789],
168
- ],
169
- cache_examples=False,
170
- )
171
-
172
- if __name__ == "__main__":
173
- demo.launch()
 
1
  """
2
  PB Cell Generator - Synthetic Blood Cell Image Generation
 
3
  """
4
 
5
  import os
 
6
  import gradio as gr
 
7
 
8
  # Cell type configurations with V2 prompts
9
  CELL_TYPES = {
 
19
 
20
  CELL_TYPE_LIST = list(CELL_TYPES.keys())
21
 
22
+ # Global pipeline
 
 
 
 
23
  pipe = None
24
 
25
 
26
  def load_pipeline():
 
27
  global pipe
28
  if pipe is not None:
29
  return pipe
30
 
31
+ import torch
32
  from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler, UNet2DConditionModel
33
 
 
34
  hf_token = os.environ.get("HF_TOKEN")
 
 
35
  device = "cuda" if torch.cuda.is_available() else "cpu"
36
  dtype = torch.float16 if torch.cuda.is_available() else torch.float32
37
 
38
+ print(f"Loading on {device}...")
39
 
 
40
  pipe = StableDiffusionPipeline.from_pretrained(
41
+ "sd2-community/stable-diffusion-2-1",
42
  torch_dtype=dtype,
43
  token=hf_token,
44
  )
45
 
 
46
  unet = UNet2DConditionModel.from_pretrained(
47
+ "esab/pbcell-sd21-v2",
48
  subfolder="unet",
49
  torch_dtype=dtype,
50
  token=hf_token,
51
  )
52
  pipe.unet = unet
 
 
53
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
 
54
  pipe = pipe.to(device)
55
 
 
56
  if device == "cuda":
57
  pipe.enable_attention_slicing()
58
 
59
+ print("Loaded!")
60
  return pipe
61
 
62
 
63
+ def generate(cell_type, custom_prompt, cfg, steps, seed):
64
+ import torch
65
+
66
+ pipeline = load_pipeline()
67
+
68
+ prompt = custom_prompt.strip() if custom_prompt and custom_prompt.strip() else CELL_TYPES.get(cell_type, CELL_TYPES["Neutrophil"])
69
+
70
+ gen = torch.Generator(device=pipeline.device)
71
+ if int(seed) >= 0:
72
+ gen.manual_seed(int(seed))
73
+
74
+ result = pipeline(
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  prompt=prompt,
76
  height=512,
77
  width=512,
78
+ num_inference_steps=int(steps),
79
+ guidance_scale=float(cfg),
80
+ generator=gen,
81
  )
82
 
83
  return result.images[0]
84
 
85
 
86
+ with gr.Blocks() as demo:
87
+ gr.Markdown("# PB Cell Generator")
88
+ gr.Markdown("Generate synthetic blood cell images. Model: [esab/pbcell-sd21-v2](https://huggingface.co/esab/pbcell-sd21-v2) | FID: 79.39")
89
+
90
+ with gr.Row():
91
+ with gr.Column():
92
+ cell_dropdown = gr.Dropdown(choices=CELL_TYPE_LIST, value="Neutrophil", label="Cell Type")
93
+ custom_box = gr.Textbox(label="Custom Prompt (optional)", placeholder="Leave empty for default V2 prompt")
94
+ cfg_slider = gr.Slider(1, 20, value=8.5, step=0.5, label="Guidance Scale")
95
+ steps_slider = gr.Slider(10, 50, value=20, step=5, label="Steps")
96
+ seed_box = gr.Number(value=42, label="Seed (-1 = random)")
97
+ btn = gr.Button("Generate", variant="primary")
98
+
99
+ with gr.Column():
100
+ output_img = gr.Image(label="Generated Cell")
101
+
102
+ btn.click(fn=generate, inputs=[cell_dropdown, custom_box, cfg_slider, steps_slider, seed_box], outputs=output_img)
103
+
104
+ demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,6 +1,5 @@
1
  torch
2
- diffusers>=0.25.0
3
  transformers
4
  accelerate
5
  safetensors
6
- Pillow
 
1
  torch
2
+ diffusers==0.25.1
3
  transformers
4
  accelerate
5
  safetensors