-
Notifications
You must be signed in to change notification settings - Fork 3
/
predict.py
67 lines (59 loc) · 2.07 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Prediction interface for Cog
from cog import BasePredictor, Input, Path
import os
import torch
from typing import List
from diffusers import AutoPipelineForText2Image
MODEL_NAME = "stabilityai/sdxl-turbo"
MODEL_CACHE = "model-cache"
class Predictor(BasePredictor):
def setup(self) -> None:
self.pipe = AutoPipelineForText2Image.from_pretrained(
MODEL_NAME,
cache_dir=MODEL_CACHE,
torch_dtype=torch.float16,
variant="fp16"
).to("cuda")
def predict(
self,
prompt: str = Input(
description="Input prompt",
default="21 years old girl,short cut,beauty,dusk,Ghibli style illustration"
),
negative_prompt: str = Input(
description="Input Negative Prompt",
default="3d, cgi, render, bad quality, normal quality",
),
num_outputs: int = Input(
description="Number of images to output.",
ge=1,
le=4,
default=1,
),
num_inference_steps: int = Input(
description="Number of inference steps",
ge=1, le=4, default=1,
),
seed: int = Input(
description="Random seed. Leave blank to randomize the seed", default=None
),
) -> List[Path]:
"""Run a single prediction on the model"""
if seed is None:
seed = int.from_bytes(os.urandom(3), "big")
print(f"Using seed: {seed}")
generator = torch.Generator("cuda").manual_seed(seed)
common_args = {
"prompt": [prompt] * num_outputs,
"negative_prompt": [negative_prompt] * num_outputs,
"guidance_scale": 0,
"generator": generator,
"num_inference_steps": num_inference_steps,
}
output = self.pipe(**common_args)
output_paths = []
for i, image in enumerate(output.images):
output_path = f"/tmp/out-{i}.png"
image.save(output_path)
output_paths.append(Path(output_path))
return output_paths