Nekochu commited on
Commit
b107f30
·
1 Parent(s): b7cce06

CLI mode, CFG slider 1.0-1.5, better neg prompt, lazy example, MCP, local model paths

Browse files
Files changed (2) hide show
  1. .gitignore +2 -0
  2. app.py +170 -102
.gitignore CHANGED
@@ -1 +1,3 @@
1
  models/
 
 
 
1
  models/
2
+ *.png
3
+ test.*
app.py CHANGED
@@ -1,42 +1,39 @@
1
- """Z-Anime 6B Image Generation (CPU) via sd-cli binary"""
2
 
3
- import os, time, subprocess, tempfile, threading, mmap
4
- from PIL import Image
5
- import gradio as gr
 
 
6
 
7
  # ---------------------------------------------------------------------------
8
- # Model paths (downloaded at build time)
9
  # ---------------------------------------------------------------------------
10
- DIFFUSION = "/app/models/z-anime-4step-q5_0.gguf"
11
- LLM = "/app/models/qwen3_4b_iq4xs.gguf"
12
- VAE = "/app/models/ae.safetensors"
13
-
14
- # Warm up page cache — read all model files so --mmap loads from RAM
15
- print("[init] Preloading models into page cache...")
16
- t0 = time.time()
17
- for model_path in [DIFFUSION, LLM, VAE]:
18
- if os.path.exists(model_path):
19
- sz = os.path.getsize(model_path)
20
- with open(model_path, "rb") as f:
21
- mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
22
- mm.read()
23
- mm.close()
24
- print(f" {os.path.basename(model_path)}: {sz / 1e9:.2f} GB cached")
25
- print(f"[init] Page cache warm in {time.time() - t0:.1f}s")
26
 
27
  RESOLUTIONS = ["512x512", "768x512", "512x768"]
28
  STEPS = 4
29
- CFG = 1.0
30
  TIMEOUT = 10800
31
 
32
  _active_proc = None
33
  _proc_lock = threading.Lock()
34
 
35
  # ---------------------------------------------------------------------------
36
- # Inference
37
  # ---------------------------------------------------------------------------
38
 
39
- def generate(prompt, negative_prompt, resolution, seed):
 
40
  """Generate an anime image using Z-Anime 6B model.
41
 
42
  Args:
@@ -44,24 +41,29 @@ def generate(prompt, negative_prompt, resolution, seed):
44
  negative_prompt: Things to avoid in the generated image.
45
  resolution: Image resolution (512x512, 768x512, or 512x768).
46
  seed: Random seed (-1 for random).
 
 
47
 
48
  Returns:
49
- tuple: Generated image and status message.
50
  """
51
  global _active_proc
52
 
53
  if not prompt or not prompt.strip():
54
- raise gr.Error("Please enter a prompt.")
55
 
56
  prompt = prompt.strip()[:500]
57
  w, h = (int(x) for x in resolution.split("x"))
58
- seed = int(seed or -1) if seed is not None else -1
 
59
 
60
- with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
 
61
  output_path = f.name
 
62
 
63
  cmd = [
64
- "/app/sd-cli",
65
  "--diffusion-model", DIFFUSION,
66
  "--llm", LLM,
67
  "--vae", VAE,
@@ -70,7 +72,7 @@ def generate(prompt, negative_prompt, resolution, seed):
70
  "-W", str(w),
71
  "-H", str(h),
72
  "--steps", str(STEPS),
73
- "--cfg-scale", str(CFG),
74
  "--sampling-method", "euler_a",
75
  "-o", output_path,
76
  "--diffusion-fa",
@@ -78,97 +80,163 @@ def generate(prompt, negative_prompt, resolution, seed):
78
  "--vae-tiling",
79
  "--vae-conv-direct",
80
  "--tensor-type-rules", "^vae=f32",
81
- # "--offload-to-cpu", # causes disk swapping with --mmap, slower
82
- # "--mmap", # use with preload only if --offload-to-cpu is off
83
  "-v",
84
  ]
85
  if seed >= 0:
86
  cmd += ["-s", str(seed)]
87
 
88
- print(f"[gen] {w}x{h} steps={STEPS} seed={seed} prompt={prompt[:80]}")
89
  t0 = time.time()
90
 
 
 
 
 
91
  try:
92
- proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
 
 
93
  with _proc_lock:
94
- _active_proc = proc
 
95
 
96
- try:
97
- stdout, stderr = proc.communicate(timeout=TIMEOUT)
98
- except subprocess.TimeoutExpired:
99
- proc.kill()
100
- proc.wait()
101
- raise
102
 
103
- elapsed = time.time() - t0
 
 
 
 
104
 
105
- with _proc_lock:
106
- _active_proc = None
107
 
108
- if proc.returncode != 0:
109
- err = stderr.decode(errors="replace")[-500:] if stderr else "Unknown error"
110
- if proc.returncode == -9:
111
- raise gr.Error("Out of memory (killed by OS). Try 512x512.")
112
- raise gr.Error(f"sd-cli failed (code {proc.returncode}): {err}")
113
 
114
- if not os.path.exists(output_path) or os.path.getsize(output_path) == 0:
115
- raise gr.Error("No output image generated")
116
 
117
- img = Image.open(output_path)
118
- status = f"Generated in {elapsed:.1f}s ({w}x{h}, {STEPS} steps)"
119
- print(f"[gen] {status}")
120
- return img, status
121
 
122
- except subprocess.TimeoutExpired:
123
- with _proc_lock:
124
- _active_proc = None
125
- raise gr.Error(f"Generation timed out ({TIMEOUT//60} min limit)")
126
- except gr.Error:
127
- with _proc_lock:
128
- _active_proc = None
129
- raise
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  except Exception as e:
131
- with _proc_lock:
132
- _active_proc = None
133
- raise gr.Error(f"Error: {e}")
134
 
135
  # ---------------------------------------------------------------------------
136
- # Gradio UI
137
  # ---------------------------------------------------------------------------
138
- with gr.Blocks(title="Z-Anime (CPU)") as demo:
139
- gr.Markdown(
140
- "**[Z-Anime 6B](https://huggingface.co/SeeSee21/Z-Anime)** S3-DiT Q5_0 GGUF "
141
- "(distill 4-step) via [sd.cpp](https://github.com/leejet/stable-diffusion.cpp) | "
142
- "~30 min at 512x512 on free CPU"
143
- )
144
- with gr.Row():
145
- with gr.Column():
146
- prompt_input = gr.Textbox(label="Prompt", lines=3,
147
- placeholder="anime girl with silver hair, fantasy armor, dramatic lighting")
148
- neg_input = gr.Textbox(label="Negative Prompt", lines=2,
149
- value="lowres, bad anatomy, bad hands, text, error, worst quality, blurry")
150
- with gr.Row():
151
- res_input = gr.Dropdown(choices=RESOLUTIONS, value="512x512",
152
- label="Resolution")
153
- seed_input = gr.Number(value=-1, label="Seed (-1=random)", precision=0)
154
- gen_btn = gr.Button("Generate (4 steps, CFG 1)", variant="primary", size="lg")
155
- with gr.Column():
156
- output_img = gr.Image(type="pil", label="Output")
157
- status_box = gr.Textbox(label="Status", interactive=False)
158
-
159
- gen_btn.click(fn=generate,
160
- inputs=[prompt_input, neg_input, res_input, seed_input],
161
- outputs=[output_img, status_box],
162
- concurrency_limit=1,
163
- api_name="generate")
164
-
165
- def _on_unload():
166
- with _proc_lock:
167
- proc = _active_proc
168
- if proc and proc.poll() is None:
169
- print("[cleanup] User disconnected, killing sd-cli process")
170
- proc.kill()
171
 
172
- demo.unload(_on_unload)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
- demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True, theme="NoCrypt/miku", mcp_server=True)
 
 
 
 
 
1
+ """Z-Anime 6B Image Generation (CPU/GPU) via sd-cli binary
2
 
3
+ CLI: python app.py "prompt" --seed 42 --cfg 1.0
4
+ GUI: python app.py --gradio
5
+ """
6
+
7
+ import os, sys, time, subprocess, tempfile, threading, argparse
8
 
9
  # ---------------------------------------------------------------------------
10
+ # Paths auto-detect local vs Docker
11
  # ---------------------------------------------------------------------------
12
+ _LOCAL_MODELS = os.path.join(os.path.dirname(__file__), "models")
13
+ _DOCKER_MODELS = "/app/models"
14
+ MODELS_DIR = _LOCAL_MODELS if os.path.isdir(_LOCAL_MODELS) else _DOCKER_MODELS
15
+
16
+ _LOCAL_SDCLI = os.path.join(os.path.dirname(__file__), "..", "sd-cpp-tools", "build", "bin", "Release", "sd-cli.exe")
17
+ _DOCKER_SDCLI = "/app/sd-cli"
18
+ SD_CLI = _LOCAL_SDCLI if os.path.isfile(_LOCAL_SDCLI) else _DOCKER_SDCLI
19
+
20
+ DIFFUSION = os.path.join(MODELS_DIR, "z-anime-distill-4step-q5_0.gguf")
21
+ LLM = os.path.join(MODELS_DIR, "qwen3_4b_iq4xs.gguf")
22
+ VAE = os.path.join(MODELS_DIR, "ae.safetensors")
 
 
 
 
 
23
 
24
  RESOLUTIONS = ["512x512", "768x512", "512x768"]
25
  STEPS = 4
 
26
  TIMEOUT = 10800
27
 
28
  _active_proc = None
29
  _proc_lock = threading.Lock()
30
 
31
  # ---------------------------------------------------------------------------
32
+ # Core generation (shared by CLI and GUI)
33
  # ---------------------------------------------------------------------------
34
 
35
+ def generate_image(prompt, negative_prompt="", resolution="512x512",
36
+ seed=-1, cfg=1.0, output_path=None):
37
  """Generate an anime image using Z-Anime 6B model.
38
 
39
  Args:
 
41
  negative_prompt: Things to avoid in the generated image.
42
  resolution: Image resolution (512x512, 768x512, or 512x768).
43
  seed: Random seed (-1 for random).
44
+ cfg: CFG scale (1.0 recommended for distill, higher = slower).
45
+ output_path: Where to save the image (auto if None).
46
 
47
  Returns:
48
+ tuple: (output_path, status_message)
49
  """
50
  global _active_proc
51
 
52
  if not prompt or not prompt.strip():
53
+ raise ValueError("Please enter a prompt.")
54
 
55
  prompt = prompt.strip()[:500]
56
  w, h = (int(x) for x in resolution.split("x"))
57
+ seed = int(seed or -1)
58
+ cfg = float(cfg)
59
 
60
+ if output_path is None:
61
+ f = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
62
  output_path = f.name
63
+ f.close()
64
 
65
  cmd = [
66
+ SD_CLI,
67
  "--diffusion-model", DIFFUSION,
68
  "--llm", LLM,
69
  "--vae", VAE,
 
72
  "-W", str(w),
73
  "-H", str(h),
74
  "--steps", str(STEPS),
75
+ "--cfg-scale", str(cfg),
76
  "--sampling-method", "euler_a",
77
  "-o", output_path,
78
  "--diffusion-fa",
 
80
  "--vae-tiling",
81
  "--vae-conv-direct",
82
  "--tensor-type-rules", "^vae=f32",
 
 
83
  "-v",
84
  ]
85
  if seed >= 0:
86
  cmd += ["-s", str(seed)]
87
 
88
+ print(f"[gen] {w}x{h} steps={STEPS} cfg={cfg} seed={seed} prompt={prompt[:80]}")
89
  t0 = time.time()
90
 
91
+ proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
92
+ with _proc_lock:
93
+ _active_proc = proc
94
+
95
  try:
96
+ stdout, stderr = proc.communicate(timeout=TIMEOUT)
97
+ except subprocess.TimeoutExpired:
98
+ proc.kill()
99
+ proc.wait()
100
  with _proc_lock:
101
+ _active_proc = None
102
+ raise RuntimeError(f"Generation timed out ({TIMEOUT // 60} min limit)")
103
 
104
+ elapsed = time.time() - t0
105
+ with _proc_lock:
106
+ _active_proc = None
 
 
 
107
 
108
+ if proc.returncode != 0:
109
+ err = stderr.decode(errors="replace")[-500:] if stderr else "Unknown error"
110
+ if proc.returncode == -9:
111
+ raise RuntimeError("Out of memory (killed by OS). Try 512x512.")
112
+ raise RuntimeError(f"sd-cli failed (code {proc.returncode}): {err}")
113
 
114
+ if not os.path.exists(output_path) or os.path.getsize(output_path) == 0:
115
+ raise RuntimeError("No output image generated")
116
 
117
+ status = f"Generated in {elapsed:.1f}s ({w}x{h}, {STEPS} steps, cfg {cfg})"
118
+ print(f"[gen] {status}")
119
+ return output_path, status
 
 
120
 
 
 
121
 
122
+ # ---------------------------------------------------------------------------
123
+ # CLI mode
124
+ # ---------------------------------------------------------------------------
 
125
 
126
+ def cli_main():
127
+ parser = argparse.ArgumentParser(description="Z-Anime 6B Image Generation")
128
+ parser.add_argument("prompt", help="Text prompt for image generation")
129
+ parser.add_argument("-n", "--negative", default="lowres, bad anatomy, bad hands, text, error, worst quality, blurry",
130
+ help="Negative prompt")
131
+ parser.add_argument("-r", "--resolution", default="512x512", choices=RESOLUTIONS)
132
+ parser.add_argument("-s", "--seed", type=int, default=-1, help="Random seed (-1=random)")
133
+ parser.add_argument("-c", "--cfg", type=float, default=1.0, help="CFG scale (1.0 recommended)")
134
+ parser.add_argument("-o", "--output", default=None, help="Output file path")
135
+ args = parser.parse_args()
136
+
137
+ if args.output is None:
138
+ args.output = f"z-anime_seed{args.seed}_cfg{args.cfg}.png"
139
+
140
+ try:
141
+ path, status = generate_image(
142
+ prompt=args.prompt,
143
+ negative_prompt=args.negative,
144
+ resolution=args.resolution,
145
+ seed=args.seed,
146
+ cfg=args.cfg,
147
+ output_path=args.output,
148
+ )
149
+ print(f" Output: {path}")
150
  except Exception as e:
151
+ print(f"ERROR: {e}", file=sys.stderr)
152
+ sys.exit(1)
153
+
154
 
155
  # ---------------------------------------------------------------------------
156
+ # Gradio GUI mode
157
  # ---------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
+ def gradio_main():
160
+ import mmap
161
+ from PIL import Image
162
+ import gradio as gr
163
+
164
+ # Warm up page cache
165
+ print("[init] Preloading models into page cache...")
166
+ t0 = time.time()
167
+ for model_path in [DIFFUSION, LLM, VAE]:
168
+ if os.path.exists(model_path):
169
+ sz = os.path.getsize(model_path)
170
+ with open(model_path, "rb") as f:
171
+ mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
172
+ mm.read()
173
+ mm.close()
174
+ print(f" {os.path.basename(model_path)}: {sz / 1e9:.2f} GB cached")
175
+ print(f"[init] Page cache warm in {time.time() - t0:.1f}s")
176
+
177
+ def gui_generate(prompt, negative_prompt, resolution, cfg, seed):
178
+ try:
179
+ path, status = generate_image(prompt, negative_prompt, resolution,
180
+ int(seed or -1), cfg=float(cfg or 1.0))
181
+ return Image.open(path), status
182
+ except Exception as e:
183
+ raise gr.Error(str(e))
184
+
185
+ with gr.Blocks(title="Z-Anime (CPU)") as demo:
186
+ gr.Markdown(
187
+ "**[Z-Anime 6B](https://huggingface.co/SeeSee21/Z-Anime)** S3-DiT Q5_0 GGUF "
188
+ "(distill 4-step) via [sd.cpp](https://github.com/leejet/stable-diffusion.cpp) | "
189
+ "~30 min at 512x512 on free CPU"
190
+ )
191
+ with gr.Row():
192
+ with gr.Column():
193
+ prompt_input = gr.Textbox(label="Prompt", lines=3,
194
+ placeholder="anime girl with silver hair, fantasy armor, dramatic lighting, beautiful background art, professional anime illustration quality, cinematic composition, detailed shading, high quality anime art.")
195
+ neg_input = gr.Textbox(label="Negative Prompt", lines=2,
196
+ value="worst quality, low quality, lowres, blurry, bad anatomy, deformed hands, extra fingers, fused fingers, missing fingers, bad proportions, wrong proportions, extra limbs, broken limbs, duplicate body parts, asymmetrical eyes, distorted face, warped features, poorly drawn face, mutated, extra eyes, cropped head, cut-off body, bad framing, jpeg artifacts, compression artifacts, watermark, logo, signature, text, error, noisy, oversmoothed, muddy colors, background clutter, censored, 3d, chibi, character doll, sepia, high contrast")
197
+ with gr.Row():
198
+ res_input = gr.Dropdown(choices=RESOLUTIONS, value="512x512", label="Resolution")
199
+ cfg_input = gr.Slider(minimum=1.0, maximum=1.5, value=1.0, step=0.1, label="CFG (1.0 best, max 1.5)")
200
+ seed_input = gr.Number(value=-1, label="Seed (-1=random)", precision=0)
201
+ gen_btn = gr.Button("Generate (4 steps)", variant="primary", size="lg")
202
+ with gr.Column():
203
+ output_img = gr.Image(type="pil", label="Output")
204
+ status_box = gr.Textbox(label="Status", interactive=False)
205
+
206
+ gen_btn.click(fn=gui_generate,
207
+ inputs=[prompt_input, neg_input, res_input, cfg_input, seed_input],
208
+ outputs=[output_img, status_box],
209
+ concurrency_limit=1,
210
+ api_name="generate")
211
+
212
+ gr.Examples(
213
+ examples=[
214
+ ["anime girl with silver hair, fantasy armor, dramatic lighting, beautiful background art, professional anime illustration quality, cinematic composition, detailed shading, high quality anime art."],
215
+ ],
216
+ inputs=[prompt_input],
217
+ cache_examples=True,
218
+ cache_mode="lazy",
219
+ )
220
+
221
+ def _on_unload():
222
+ with _proc_lock:
223
+ proc = _active_proc
224
+ if proc and proc.poll() is None:
225
+ print("[cleanup] User disconnected, killing sd-cli process")
226
+ proc.kill()
227
+
228
+ demo.unload(_on_unload)
229
+
230
+ demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True,
231
+ theme="NoCrypt/miku", mcp_server=True)
232
+
233
+
234
+ # ---------------------------------------------------------------------------
235
+ # Entry point
236
+ # ---------------------------------------------------------------------------
237
 
238
+ if __name__ == "__main__":
239
+ if len(sys.argv) > 1 and sys.argv[1] != "--gradio":
240
+ cli_main()
241
+ else:
242
+ gradio_main()