Update README.md
Browse files
README.md
CHANGED
|
@@ -1,54 +1,48 @@
|
|
| 1 |
-
---
|
| 2 |
-
library_name: diffusers
|
| 3 |
-
pipeline_tag: text-to-image
|
| 4 |
-
tags:
|
| 5 |
-
- safety
|
| 6 |
-
- classifier-guidance
|
| 7 |
-
- stable-diffusion
|
| 8 |
-
- plug-and-play
|
| 9 |
-
license: apache-2.0
|
| 10 |
-
---
|
| 11 |
-
|
| 12 |
-
# Safe Diffusion Guidance (SDG) —
|
| 13 |
-
|
| 14 |
-
**Safe Diffusion Guidance (SDG)** is a *classifier-guided denoising* layer that steers the sampling trajectory away from unsafe content **without retraining** the base model.
|
| 15 |
-
It works **standalone** with SD 1.4 / 1.5 / 2.1
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
safety_scale=5.0, # strength: ~2–8 (Light→Strong)
|
| 50 |
-
mid_fraction=1.0, # guide fraction of steps: 0.5, 0.8, 1.0
|
| 51 |
-
safe_class_index=3 # index of 'safe' in [gore,hate,medical,safe,sexual]
|
| 52 |
-
).images[0]
|
| 53 |
-
|
| 54 |
-
img.save("sdg_safe_output.png")
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: diffusers
|
| 3 |
+
pipeline_tag: text-to-image
|
| 4 |
+
tags:
|
| 5 |
+
- safety
|
| 6 |
+
- classifier-guidance
|
| 7 |
+
- stable-diffusion
|
| 8 |
+
- plug-and-play
|
| 9 |
+
license: apache-2.0
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Safe Diffusion Guidance (SDG) —
|
| 13 |
+
|
| 14 |
+
**Safe Diffusion Guidance (SDG)** is a *classifier-guided denoising* layer that steers the sampling trajectory away from unsafe content **without retraining** the base model.
|
| 15 |
+
It works **standalone** with SD 1.4 / 1.5 / 2.1.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
## Quickstart (SD 1.5)
|
| 19 |
+
|
| 20 |
+
```python
|
| 21 |
+
import torch
|
| 22 |
+
from diffusers import StableDiffusionPipeline
|
| 23 |
+
|
| 24 |
+
# 1) Load base SD pipeline (disable default safety checker)
|
| 25 |
+
base = StableDiffusionPipeline.from_pretrained(
|
| 26 |
+
"runwayml/stable-diffusion-v1-5",
|
| 27 |
+
torch_dtype=torch.float16,
|
| 28 |
+
safety_checker=None
|
| 29 |
+
).to("cuda")
|
| 30 |
+
|
| 31 |
+
# 2) Load SDG custom pipeline from Hub (this repo)
|
| 32 |
+
sdg = StableDiffusionPipeline.from_pretrained(
|
| 33 |
+
"basimazam/safe-diffusion-guidance",
|
| 34 |
+
custom_pipeline="safe_diffusion_guidance",
|
| 35 |
+
torch_dtype=torch.float16
|
| 36 |
+
).to("cuda")
|
| 37 |
+
|
| 38 |
+
img = sdg(
|
| 39 |
+
base_pipe=base,
|
| 40 |
+
prompt="portrait photograph, studio light, 85mm, realistic",
|
| 41 |
+
num_inference_steps=50,
|
| 42 |
+
guidance_scale=7.5,
|
| 43 |
+
safety_scale=5.0,
|
| 44 |
+
mid_fraction=1.0,
|
| 45 |
+
safe_class_index=3
|
| 46 |
+
).images[0]
|
| 47 |
+
|
| 48 |
+
img.save("sdg_safe_output.png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|