Spaces:
Paused
Paused
Create inference.py
Browse files- inference.py +63 -0
inference.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# ===== CPU optimization =====
|
| 4 |
+
CPU_THREADS = str(os.cpu_count() or 2)
|
| 5 |
+
|
| 6 |
+
os.environ["OMP_NUM_THREADS"] = CPU_THREADS
|
| 7 |
+
os.environ["OPENVINO_NUM_THREADS"] = CPU_THREADS
|
| 8 |
+
os.environ["MKL_NUM_THREADS"] = CPU_THREADS
|
| 9 |
+
os.environ["NUMEXPR_NUM_THREADS"] = CPU_THREADS
|
| 10 |
+
os.environ["OMP_WAIT_POLICY"] = "PASSIVE"
|
| 11 |
+
os.environ["KMP_BLOCKTIME"] = "0"
|
| 12 |
+
|
| 13 |
+
import torch
|
| 14 |
+
from optimum.intel import OVZImagePipeline
|
| 15 |
+
|
| 16 |
+
# ===== Global =====
|
| 17 |
+
pipe = None
|
| 18 |
+
generator = torch.Generator("cpu")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def load_model():
|
| 22 |
+
global pipe
|
| 23 |
+
|
| 24 |
+
if pipe is None:
|
| 25 |
+
pipe = OVZImagePipeline.from_pretrained(
|
| 26 |
+
"hsuwill000/Z-Image-Turbo-ov",
|
| 27 |
+
device="cpu"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# warmup
|
| 31 |
+
pipe(
|
| 32 |
+
prompt="warmup",
|
| 33 |
+
height=512,
|
| 34 |
+
width=512,
|
| 35 |
+
num_inference_steps=1,
|
| 36 |
+
guidance_scale=0.0,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
return pipe
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def generate(
|
| 43 |
+
prompt: str,
|
| 44 |
+
height: int = 512,
|
| 45 |
+
width: int = 512,
|
| 46 |
+
steps: int = 9,
|
| 47 |
+
seed: int = -1,
|
| 48 |
+
):
|
| 49 |
+
pipe = load_model()
|
| 50 |
+
|
| 51 |
+
if seed != -1:
|
| 52 |
+
generator.manual_seed(int(seed))
|
| 53 |
+
|
| 54 |
+
image = pipe(
|
| 55 |
+
prompt=prompt,
|
| 56 |
+
height=height,
|
| 57 |
+
width=width,
|
| 58 |
+
num_inference_steps=steps,
|
| 59 |
+
guidance_scale=0.0,
|
| 60 |
+
generator=generator,
|
| 61 |
+
).images[0]
|
| 62 |
+
|
| 63 |
+
return image
|