-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathmain.py
67 lines (57 loc) · 1.76 KB
/
main.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
import base64
import io
import os
from typing import Optional
import torch
from diffusers import StableDiffusionXLPipeline
from onediffx import compile_pipe, save_pipe, load_pipe
from pydantic import BaseModel
pipe = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
variant="fp16",
use_safetensors=True,
)
pipe.to("cuda")
pipe = compile_pipe(pipe)
if not os.path.isdir("/persistent-storage/cached_pipe"):
##Run before saving
image = pipe(
prompt="street style, detailed, raw photo, woman, face, shot on CineStill 800T",
height=1024,
width=1024,
num_inference_steps=30,
output_type="pil",
).images
print("Pipe compiled:", pipe)
save_pipe(pipe, dir="/persistent-storage/cached_pipe")
else:
print("Loading:", pipe)
pipe = load_pipe(pipe, dir="/persistent-storage/cached_pipe")
print("Pipe loaded:", pipe)
class Item(BaseModel):
prompt: str
height: Optional[int] = 512
width: Optional[int] = 512
num_inference_steps: Optional[int] = 30
def predict(prompt, height=512, width=512, num_inference_steps=30):
item = Item(
prompt=prompt,
height=height,
width=width,
num_inference_steps=num_inference_steps,
)
# run once to trigger compilation
images = pipe(
prompt=item.prompt,
height=item.height,
width=item.width,
num_inference_steps=item.num_inference_steps,
output_type="pil",
).images
finished_images = []
for image in images:
buffered = io.BytesIO()
image.save(buffered, format="PNG")
finished_images.append(base64.b64encode(buffered.getvalue()).decode("utf-8"))
return {"images": finished_images}