Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,54 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
|
|
|
| 3 |
from optimum.intel import OVZImagePipeline
|
| 4 |
|
| 5 |
-
# =====
|
| 6 |
-
pipe =
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def generate_image(prompt, height, width, steps, seed):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
generator
|
| 17 |
|
| 18 |
image = pipe(
|
| 19 |
prompt=prompt,
|
|
@@ -31,24 +66,28 @@ def generate_image(prompt, height, width, steps, seed):
|
|
| 31 |
default_prompt = """Cinematic portrait of a Japanese girl as Shinobu Kocho, realistic facial features, gradient purple compound eyes, intricate butterfly hair ornament, wearing silk haori with butterfly wing patterns, soft moonlight, hyper-realistic, depth of field, purple butterfly particles, 8k resolution, ethereal lighting"""
|
| 32 |
|
| 33 |
|
| 34 |
-
# =====
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
-
gr.Markdown("# 🚀 Z-Image Turbo (OpenVINO CPU)")
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
with gr.Column():
|
| 40 |
-
prompt = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
height = gr.Slider(256, 1024, value=512, step=64, label="Height")
|
| 43 |
width = gr.Slider(256, 1024, value=512, step=64, label="Width")
|
| 44 |
|
| 45 |
-
steps = gr.Slider(1, 20, value=9, step=1, label="
|
| 46 |
seed = gr.Number(value=-1, label="Seed (-1 = random)")
|
| 47 |
|
| 48 |
run_btn = gr.Button("Generate")
|
| 49 |
|
| 50 |
with gr.Column():
|
| 51 |
-
output = gr.Image(label="
|
| 52 |
|
| 53 |
run_btn.click(
|
| 54 |
fn=generate_image,
|
|
@@ -56,6 +95,10 @@ with gr.Blocks() as demo:
|
|
| 56 |
outputs=output,
|
| 57 |
)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# ===== Launch =====
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# ===== CPU / Thread 最佳化(關鍵)=====
|
| 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 |
+
|
| 11 |
+
# 避免 thread oversubscription
|
| 12 |
+
os.environ["OMP_WAIT_POLICY"] = "PASSIVE"
|
| 13 |
+
os.environ["KMP_BLOCKTIME"] = "0"
|
| 14 |
+
|
| 15 |
import torch
|
| 16 |
+
import gradio as gr
|
| 17 |
from optimum.intel import OVZImagePipeline
|
| 18 |
|
| 19 |
+
# ===== Global objects(避免重複初始化)=====
|
| 20 |
+
pipe = None
|
| 21 |
+
generator = torch.Generator("cpu")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# ===== Load model(lazy + only once)=====
|
| 25 |
+
def load_model():
|
| 26 |
+
global pipe
|
| 27 |
|
| 28 |
+
if pipe is None:
|
| 29 |
+
pipe = OVZImagePipeline.from_pretrained(
|
| 30 |
+
"hsuwill000/Z-Image-Turbo-ov",
|
| 31 |
+
device="cpu"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# ===== Warmup(避免第一次慢)=====
|
| 35 |
+
pipe(
|
| 36 |
+
prompt="warmup",
|
| 37 |
+
height=512,
|
| 38 |
+
width=512,
|
| 39 |
+
num_inference_steps=1,
|
| 40 |
+
guidance_scale=0.0,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
return pipe
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# ===== Inference =====
|
| 47 |
def generate_image(prompt, height, width, steps, seed):
|
| 48 |
+
pipe = load_model()
|
| 49 |
+
|
| 50 |
+
if seed != -1:
|
| 51 |
+
generator.manual_seed(int(seed))
|
| 52 |
|
| 53 |
image = pipe(
|
| 54 |
prompt=prompt,
|
|
|
|
| 66 |
default_prompt = """Cinematic portrait of a Japanese girl as Shinobu Kocho, realistic facial features, gradient purple compound eyes, intricate butterfly hair ornament, wearing silk haori with butterfly wing patterns, soft moonlight, hyper-realistic, depth of field, purple butterfly particles, 8k resolution, ethereal lighting"""
|
| 67 |
|
| 68 |
|
| 69 |
+
# ===== UI =====
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("## 🚀 Z-Image Turbo (OpenVINO CPU Optimized)")
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
with gr.Column():
|
| 75 |
+
prompt = gr.Textbox(
|
| 76 |
+
label="Prompt",
|
| 77 |
+
value=default_prompt,
|
| 78 |
+
lines=4
|
| 79 |
+
)
|
| 80 |
|
| 81 |
height = gr.Slider(256, 1024, value=512, step=64, label="Height")
|
| 82 |
width = gr.Slider(256, 1024, value=512, step=64, label="Width")
|
| 83 |
|
| 84 |
+
steps = gr.Slider(1, 20, value=9, step=1, label="Steps")
|
| 85 |
seed = gr.Number(value=-1, label="Seed (-1 = random)")
|
| 86 |
|
| 87 |
run_btn = gr.Button("Generate")
|
| 88 |
|
| 89 |
with gr.Column():
|
| 90 |
+
output = gr.Image(label="Result")
|
| 91 |
|
| 92 |
run_btn.click(
|
| 93 |
fn=generate_image,
|
|
|
|
| 95 |
outputs=output,
|
| 96 |
)
|
| 97 |
|
| 98 |
+
|
| 99 |
+
# ===== Queue(最小化延遲)=====
|
| 100 |
+
demo.queue(concurrency_count=1, max_size=2)
|
| 101 |
+
|
| 102 |
# ===== Launch =====
|
| 103 |
if __name__ == "__main__":
|
| 104 |
demo.launch()
|