scribbyotx
commited on
Commit
•
470726d
1
Parent(s):
7a003e9
Upload 5 files
Browse files- .gitattributes +1 -0
- README.md +17 -3
- cog.yaml +22 -0
- output.0.png +3 -0
- predict.py +169 -0
- preprocessor_config.json +20 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
output.0.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# artificialguybr/Nebul.Redmond Cog model
|
2 |
+
|
3 |
+
This is an implementation of [artificialguybr/Nebul.Redmond](https://huggingface.co/artificialguybr/NebulRedmond) as a Cog model. [Cog packages machine learning models as standard containers.](https://github.com/replicate/cog)
|
4 |
+
|
5 |
+
First, download the pre-trained weights:
|
6 |
+
|
7 |
+
cog run script/download-weights
|
8 |
+
|
9 |
+
Then, you can run predictions:
|
10 |
+
|
11 |
+
cog predict -i prompt="masterpiece, high quality, ultra good, this is the good stuff, best prompt ever, portrait of a woman, freckles, ginger"
|
12 |
+
|
13 |
+
## Example Output
|
14 |
+
|
15 |
+
Example output for prompt: "masterpiece, high quality, ultra good, this is the good stuff, best prompt ever, portrait of a woman, freckles, ginger"
|
16 |
+
|
17 |
+
![alt text](output.0.png)
|
cog.yaml
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Configuration for Cog ⚙️
|
2 |
+
# Reference: https://github.com/replicate/cog/blob/main/docs/yaml.md
|
3 |
+
|
4 |
+
build:
|
5 |
+
gpu: true
|
6 |
+
cuda: "11.8"
|
7 |
+
python_version: "3.11"
|
8 |
+
system_packages:
|
9 |
+
- "libgl1-mesa-glx"
|
10 |
+
- "libsm6"
|
11 |
+
- "libxext6"
|
12 |
+
python_packages:
|
13 |
+
- "torch==2.1.0"
|
14 |
+
- "torchvision"
|
15 |
+
- "diffusers==0.23.0"
|
16 |
+
- "transformers==4.35.0"
|
17 |
+
- "accelerate==0.24.0"
|
18 |
+
- "invisible-watermark==0.2.0"
|
19 |
+
- "omegaconf"
|
20 |
+
|
21 |
+
# predict.py defines how predictions are run on your model
|
22 |
+
predict: "predict.py:Predictor"
|
output.0.png
ADDED
Git LFS Details
|
predict.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Prediction interface for Cog ⚙️
|
2 |
+
# https://github.com/replicate/cog/blob/main/docs/python.md
|
3 |
+
|
4 |
+
from cog import BasePredictor, Input, Path
|
5 |
+
import os
|
6 |
+
import time
|
7 |
+
import torch
|
8 |
+
import numpy as np
|
9 |
+
from typing import List
|
10 |
+
from transformers import CLIPImageProcessor
|
11 |
+
from diffusers import (
|
12 |
+
StableDiffusionXLPipeline,
|
13 |
+
DPMSolverMultistepScheduler,
|
14 |
+
DDIMScheduler,
|
15 |
+
HeunDiscreteScheduler,
|
16 |
+
EulerAncestralDiscreteScheduler,
|
17 |
+
EulerDiscreteScheduler,
|
18 |
+
PNDMScheduler
|
19 |
+
)
|
20 |
+
from diffusers.pipelines.stable_diffusion.safety_checker import (
|
21 |
+
StableDiffusionSafetyChecker,
|
22 |
+
)
|
23 |
+
|
24 |
+
class KarrasDPM:
|
25 |
+
def from_config(config):
|
26 |
+
return DPMSolverMultistepScheduler.from_config(config, use_karras_sigmas=True)
|
27 |
+
|
28 |
+
SCHEDULERS = {
|
29 |
+
"DDIM": DDIMScheduler,
|
30 |
+
"DPMSolverMultistep": DPMSolverMultistepScheduler,
|
31 |
+
"HeunDiscrete": HeunDiscreteScheduler,
|
32 |
+
"KarrasDPM": KarrasDPM,
|
33 |
+
"K_EULER_ANCESTRAL": EulerAncestralDiscreteScheduler,
|
34 |
+
"K_EULER": EulerDiscreteScheduler,
|
35 |
+
"PNDM": PNDMScheduler,
|
36 |
+
}
|
37 |
+
|
38 |
+
MODEL_NAME = "artificialguybr/NebulRedmond"
|
39 |
+
MODEL_CACHE = "model-cache"
|
40 |
+
SAFETY_CACHE = "safety-cache"
|
41 |
+
FEATURE_EXTRACTOR = "feature-extractor"
|
42 |
+
|
43 |
+
class Predictor(BasePredictor):
|
44 |
+
def setup(self) -> None:
|
45 |
+
"""Load the model into memory to make running multiple predictions efficient"""
|
46 |
+
start = time.time()
|
47 |
+
print("Loading safety checker...")
|
48 |
+
self.safety_checker = StableDiffusionSafetyChecker.from_pretrained(
|
49 |
+
SAFETY_CACHE, torch_dtype=torch.float16
|
50 |
+
).to("cuda")
|
51 |
+
self.feature_extractor = CLIPImageProcessor.from_pretrained(FEATURE_EXTRACTOR)
|
52 |
+
print("Loading txt2img model")
|
53 |
+
self.pipe = StableDiffusionXLPipeline.from_pretrained(
|
54 |
+
MODEL_NAME,
|
55 |
+
torch_dtype=torch.float16,
|
56 |
+
use_safetensors=True,
|
57 |
+
cache_dir=MODEL_CACHE
|
58 |
+
).to('cuda')
|
59 |
+
print("setup took: ", time.time() - start)
|
60 |
+
|
61 |
+
def run_safety_checker(self, image):
|
62 |
+
safety_checker_input = self.feature_extractor(image, return_tensors="pt").to(
|
63 |
+
"cuda"
|
64 |
+
)
|
65 |
+
np_image = [np.array(val) for val in image]
|
66 |
+
image, has_nsfw_concept = self.safety_checker(
|
67 |
+
images=np_image,
|
68 |
+
clip_input=safety_checker_input.pixel_values.to(torch.float16),
|
69 |
+
)
|
70 |
+
return image, has_nsfw_concept
|
71 |
+
|
72 |
+
@torch.inference_mode()
|
73 |
+
def predict(
|
74 |
+
self,
|
75 |
+
prompt: str = Input(
|
76 |
+
description="Input prompt",
|
77 |
+
default="An astronaut riding a rainbow unicorn",
|
78 |
+
),
|
79 |
+
negative_prompt: str = Input(
|
80 |
+
description="Input Negative Prompt",
|
81 |
+
default="",
|
82 |
+
),
|
83 |
+
width: int = Input(
|
84 |
+
description="Width of output image",
|
85 |
+
default=1024,
|
86 |
+
),
|
87 |
+
height: int = Input(
|
88 |
+
description="Height of output image",
|
89 |
+
default=1024,
|
90 |
+
),
|
91 |
+
num_outputs: int = Input(
|
92 |
+
description="Number of images to output.",
|
93 |
+
ge=1,
|
94 |
+
le=4,
|
95 |
+
default=1,
|
96 |
+
),
|
97 |
+
scheduler: str = Input(
|
98 |
+
description="scheduler",
|
99 |
+
choices=SCHEDULERS.keys(),
|
100 |
+
default="K_EULER",
|
101 |
+
),
|
102 |
+
num_inference_steps: int = Input(
|
103 |
+
description="Number of denoising steps", ge=1, le=100, default=40
|
104 |
+
),
|
105 |
+
guidance_scale: float = Input(
|
106 |
+
description="Scale for classifier-free guidance", ge=1, le=20, default=7.5
|
107 |
+
),
|
108 |
+
seed: int = Input(
|
109 |
+
description="Random seed. Leave blank to randomize the seed", default=None
|
110 |
+
),
|
111 |
+
apply_watermark: bool = Input(
|
112 |
+
description="Applies a watermark to enable determining if an image is generated in downstream applications. If you have other provisions for generating or deploying images safely, you can use this to disable watermarking.",
|
113 |
+
default=True,
|
114 |
+
),
|
115 |
+
disable_safety_checker: bool = Input(
|
116 |
+
description="Disable safety checker for generated images. This feature is only available through the API. See [https://replicate.com/docs/how-does-replicate-work#safety](https://replicate.com/docs/how-does-replicate-work#safety)",
|
117 |
+
default=False
|
118 |
+
)
|
119 |
+
) -> List[Path]:
|
120 |
+
"""Run a single prediction on the model."""
|
121 |
+
if seed is None:
|
122 |
+
seed = int.from_bytes(os.urandom(3), "big")
|
123 |
+
print(f"Using seed: {seed}")
|
124 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
125 |
+
|
126 |
+
pipe = self.pipe
|
127 |
+
pipe.scheduler = SCHEDULERS[scheduler].from_config(pipe.scheduler.config)
|
128 |
+
|
129 |
+
# toggles watermark for this prediction
|
130 |
+
if not apply_watermark:
|
131 |
+
watermark_cache = pipe.watermark
|
132 |
+
pipe.watermark = None
|
133 |
+
|
134 |
+
sdxl_kwargs = {}
|
135 |
+
sdxl_kwargs["width"] = width
|
136 |
+
sdxl_kwargs["height"] = height
|
137 |
+
|
138 |
+
common_args = {
|
139 |
+
"prompt": [prompt] * num_outputs,
|
140 |
+
"negative_prompt": [negative_prompt] * num_outputs,
|
141 |
+
"guidance_scale": guidance_scale,
|
142 |
+
"generator": generator,
|
143 |
+
"num_inference_steps": num_inference_steps,
|
144 |
+
}
|
145 |
+
|
146 |
+
output = pipe(**common_args, **sdxl_kwargs)
|
147 |
+
|
148 |
+
if not apply_watermark:
|
149 |
+
pipe.watermark = watermark_cache
|
150 |
+
|
151 |
+
if not disable_safety_checker:
|
152 |
+
_, has_nsfw_content = self.run_safety_checker(output.images)
|
153 |
+
|
154 |
+
output_paths = []
|
155 |
+
for i, image in enumerate(output.images):
|
156 |
+
if not disable_safety_checker:
|
157 |
+
if has_nsfw_content[i]:
|
158 |
+
print(f"NSFW content detected in image {i}")
|
159 |
+
continue
|
160 |
+
output_path = f"/tmp/out-{i}.png"
|
161 |
+
image.save(output_path)
|
162 |
+
output_paths.append(Path(output_path))
|
163 |
+
|
164 |
+
if len(output_paths) == 0:
|
165 |
+
raise Exception(
|
166 |
+
f"NSFW content detected. Try running it again, or try a different prompt."
|
167 |
+
)
|
168 |
+
|
169 |
+
return output_paths
|
preprocessor_config.json
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"crop_size": 224,
|
3 |
+
"do_center_crop": true,
|
4 |
+
"do_convert_rgb": true,
|
5 |
+
"do_normalize": true,
|
6 |
+
"do_resize": true,
|
7 |
+
"feature_extractor_type": "CLIPFeatureExtractor",
|
8 |
+
"image_mean": [
|
9 |
+
0.48145466,
|
10 |
+
0.4578275,
|
11 |
+
0.40821073
|
12 |
+
],
|
13 |
+
"image_std": [
|
14 |
+
0.26862954,
|
15 |
+
0.26130258,
|
16 |
+
0.27577711
|
17 |
+
],
|
18 |
+
"resample": 3,
|
19 |
+
"size": 224
|
20 |
+
}
|