aexyb commited on
Commit
28e8756
Β·
verified Β·
1 Parent(s): 9cd4b27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +264 -98
app.py CHANGED
@@ -1,132 +1,298 @@
 
 
 
 
 
 
1
  import os
2
- import subprocess
3
  import sys
4
  import urllib.request
5
  from pathlib import Path
6
  from datetime import datetime
7
 
8
- # Directories
9
  BASE = Path.cwd()
10
  MODELS = BASE / "models"
11
  OUTPUTS = BASE / "outputs"
12
- COMFYUI = BASE / "ComfyUI"
13
 
14
  MODEL_FILE = "flux.2-klein-4b-Q8_0.gguf"
15
  MODEL_URL = f"https://huggingface.co/unsloth/FLUX.2-klein-4B-GGUF/resolve/main/{MODEL_FILE}"
16
 
 
 
 
17
  def setup():
18
- print("Installing ComfyUI...")
19
- if not (COMFYUI / "main.py").exists():
20
- subprocess.run(["git", "clone", "https://github.com/comfyanonymous/ComfyUI.git", str(COMFYUI)], check=True)
21
- subprocess.run([sys.executable, "-m", "pip", "install", "-r", str(COMFYUI / "requirements.txt")], check=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- print("Installing GGUF node...")
24
- nodes = COMFYUI / "custom_nodes" / "ComfyUI-GGUF"
25
- if not nodes.exists():
26
- subprocess.run(["git", "clone", "https://github.com/city96/ComfyUI-GGUF.git", str(nodes)], check=True)
27
- req = nodes / "requirements.txt"
28
- if req.exists():
29
- subprocess.run([sys.executable, "-m", "pip", "install", "-r", str(req)], check=True)
30
 
31
- print(f"Downloading {MODEL_FILE}...")
32
- MODELS.mkdir(exist_ok=True)
33
  model_path = MODELS / MODEL_FILE
34
- if not model_path.exists():
35
- urllib.request.urlretrieve(MODEL_URL, model_path)
36
 
37
- comfy_models = COMFYUI / "models" / "checkpoints"
38
- comfy_models.mkdir(parents=True, exist_ok=True)
39
- import shutil
40
- shutil.copy2(model_path, comfy_models / MODEL_FILE)
41
-
42
- print("Done!")
43
 
44
- def generate(prompt, neg="", w=1024, h=1024, steps=20, cfg=7.0, seed=-1):
45
- import json
46
- import time
47
- import requests
48
-
49
- if seed < 0:
50
- seed = int(time.time() * 1000) % (2**32)
51
-
52
- wf = {
53
- "1": {"class_type": "CLIPTextEncode", "inputs": {"text": prompt, "clip": ["4", 0]}},
54
- "2": {"class_type": "CLIPTextEncode", "inputs": {"text": neg or "blurry", "clip": ["4", 0]}},
55
- "3": {"class_type": "VAEDecode", "inputs": {"samples": ["6", 0], "vae": ["5", 0]}},
56
- "4": {"class_type": "GGUFCLIPLoader", "inputs": {"clip_name": "t5xxl_fp16.safetensors", "type": "l"}},
57
- "5": {"class_type": "GGUFAutoencoders", "inputs": {"vae_name": "ae.safetensors"}},
58
- "6": {"class_type": "KSampler", "inputs": {
59
- "model": ["7", 0], "positive": ["1", 0], "negative": ["2", 0],
60
- "latent_image": ["8", 0], "seed": seed, "steps": steps,
61
- "cfg": cfg, "sampler_name": "euler", "scheduler": "normal", "denoise": 1.0
62
- }},
63
- "7": {"class_type": "GGUFLoader", "inputs": {"unet_name": MODEL_FILE, "weight_dtype": "fp16_e4m3fn"}},
64
- "8": {"class_type": "EmptyLatentImage", "inputs": {"width": w, "height": h, "batch_size": 1}},
65
- "9": {"class_type": "SaveImage", "inputs": {"filename_prefix": "output", "images": ["3", 0]}}
66
- }
67
-
68
- print("Starting ComfyUI...")
69
- proc = subprocess.Popen(
70
- [sys.executable, str(COMFYUI / "main.py"), "--listen", "127.0.0.1", "--port", "8188"],
71
- cwd=str(COMFYUI), stdout=subprocess.PIPE, stderr=subprocess.PIPE
72
- )
73
-
74
- for _ in range(30):
75
- try:
76
- requests.get("http://127.0.0.1:8188/system_stats")
77
- break
78
- except:
79
- time.sleep(1)
80
- else:
81
- proc.kill()
82
- return None, "ComfyUI failed to start"
83
-
84
  try:
85
- resp = requests.post("http://127.0.0.1:8188/prompt", json={"prompt": wf})
86
- pid = resp.json()["prompt_id"]
87
-
88
- while True:
89
- time.sleep(1)
90
- hist = requests.get(f"http://127.0.0.1:8188/history/{pid}").json()
91
- if pid in hist:
92
- out = hist[pid]["outputs"].get("9", {}).get("images", [])
93
- if out:
94
- src = Path(COMFYUI) / "output" / out[0]["filename"]
95
- OUTPUT.mkdir(exist_ok=True)
96
- dst = OUTPUT / f"{datetime.now().strftime('%H%M%S')}.png"
97
- shutil.copy2(src, dst)
98
- return str(dst), "βœ… Done!"
99
- break
100
- return None, "No output"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  except Exception as e:
102
- return None, f"Error: {e}"
103
- finally:
104
- proc.terminate()
105
- proc.wait()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
- setup()
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  import gradio as gr
110
 
111
- with gr.Blocks(title="FLUX.2-klein-4B") as demo:
112
- gr.Markdown("# 🎨 FLUX.2-klein-4B Q8_0\n### unsloth/FLUX.2-klein-4B-GGUF")
 
 
 
 
 
 
 
 
 
113
 
114
  with gr.Row():
115
  with gr.Column(scale=2):
116
- prompt = gr.Textbox(label="Prompt", lines=3, value="a red fox in snow, detailed, 8k")
117
- neg = gr.Textbox(label="Negative", lines=2, value="")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  with gr.Column():
119
- w = gr.Slider(512, 1024, 1024, 64, label="Width")
120
- h = gr.Slider(512, 1024, 1024, 64, label="Height")
121
- steps = gr.Slider(1, 50, 20, 1, label="Steps")
122
- cfg = gr.Slider(1, 20, 7.0, 0.5, label="CFG")
123
- seed = gr.Number(-1, label="Seed (-1=random)")
 
 
 
 
 
 
 
 
 
124
 
125
- gen = gr.Button("πŸš€ Generate", variant="primary")
126
- img = gr.Image(label="Output")
127
- status = gr.Textbox(label="Status")
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
- gen.click(generate, [prompt, neg, w, h, steps, cfg, seed], [img, status])
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  if __name__ == "__main__":
132
  demo.launch(server_name="0.0.0.0", port=7860)
 
1
+ """
2
+ FLUX.2-klein-4B Q8_0 Generator
3
+ Lightweight version - NO ComfyUI needed!
4
+ Uses: diffusers + gguf direct loading
5
+ """
6
+
7
  import os
 
8
  import sys
9
  import urllib.request
10
  from pathlib import Path
11
  from datetime import datetime
12
 
13
+ # Paths
14
  BASE = Path.cwd()
15
  MODELS = BASE / "models"
16
  OUTPUTS = BASE / "outputs"
 
17
 
18
  MODEL_FILE = "flux.2-klein-4b-Q8_0.gguf"
19
  MODEL_URL = f"https://huggingface.co/unsloth/FLUX.2-klein-4B-GGUF/resolve/main/{MODEL_FILE}"
20
 
21
+ def log(msg):
22
+ print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}")
23
+
24
  def setup():
25
+ """Download model only - no heavy installs!"""
26
+ try:
27
+ log(f"Creating directories...")
28
+ MODELS.mkdir(exist_ok=True)
29
+ OUTPUTS.mkdir(exist_ok=True)
30
+
31
+ model_path = MODELS / MODEL_FILE
32
+
33
+ if not model_path.exists():
34
+ log(f"⬇️ Downloading {MODEL_FILE} from HuggingFace...")
35
+ log(f" URL: {MODEL_URL}")
36
+
37
+ # Download with progress
38
+ def progress(count, block_size, total_size):
39
+ percent = count * block_size * 100 // total_size if total_size > 0 else 0
40
+ mb = count * block_size / (1024*1024)
41
+ total_mb = total_size / (1024*1024)
42
+ print(f"\r ⏳ {percent}% ({mb:.1f}/{total_mb:.1f} MB)", end="", flush=True)
43
+
44
+ urllib.request.urlretrieve(MODEL_URL, model_path, progress)
45
+
46
+ size_mb = model_path.stat().st_size / (1024*1024)
47
+ print(f"\n βœ… Downloaded ({size_mb:.1f} MB)")
48
+ else:
49
+ size_mb = model_path.stat().st_size / (1024*1024)
50
+ log(f"βœ… Model exists ({size_mb:.1f} MB)")
51
+
52
+ log("βœ… Setup complete!")
53
+ return True
54
+
55
+ except Exception as e:
56
+ log(f"❌ Error: {e}")
57
+ return False
58
+
59
+
60
+ def generate_image(prompt, neg_prompt="", width=1024, height=1024, steps=20, cfg=7.0, seed=-1, progress=None):
61
+ """
62
+ Generate image using GGUF model with diffusers
63
+ """
64
 
65
+ if not prompt.strip():
66
+ return None, "❌ Please enter a prompt!"
 
 
 
 
 
67
 
 
 
68
  model_path = MODELS / MODEL_FILE
 
 
69
 
70
+ if not model_path.exists():
71
+ return None, f"""❌ Model not found!
72
+
73
+ Expected: {model_path}
74
+
75
+ Please wait for setup to complete, or manually upload the model file."""
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  try:
78
+ import torch
79
+ from diffusers import FluxPipeline
80
+ from transformers import T5EncoderModel, CLIPTokenizer
81
+
82
+ if progress:
83
+ progress(0.1, desc="Loading model (first time takes ~2 min)...")
84
+
85
+ log("🎨 Loading FLUX GGUF model...")
86
+ log(f" Model: {model_path}")
87
+ log(f" Prompt: {prompt[:50]}...")
88
+
89
+ # Load pipeline with GGUF quantization
90
+ pipe = FluxPipeline.from_single_file(
91
+ str(model_path),
92
+ torch_dtype=torch.bfloat16,
93
+ use_safetensors=True,
94
+ )
95
+
96
+ # CPU offloading for low memory
97
+ pipe.enable_model_cpu_offload()
98
+
99
+ if progress:
100
+ progress(0.3, desc="Generating image...")
101
+
102
+ log(f"πŸ–ΌοΈ Generating ({width}x{height}, {steps} steps)...")
103
+
104
+ # Set seed
105
+ generator = None
106
+ if seed > 0:
107
+ generator = torch.Generator(device="cpu").manual_seed(seed)
108
+
109
+ # Generate
110
+ result = pipe(
111
+ prompt=prompt,
112
+ negative_prompt=neg_prompt or None,
113
+ height=int(height),
114
+ width=int(width),
115
+ num_inference_steps=int(steps),
116
+ guidance_scale=float(cfg),
117
+ generator=generator,
118
+ )
119
+
120
+ if progress:
121
+ progress(0.9, desc="Saving image...")
122
+
123
+ # Save output
124
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
125
+ output_filename = f"output_{timestamp}.png"
126
+ output_path = str(OUTPUTS_DIR / output_filename)
127
+
128
+ OUTPUTS_DIR.mkdir(exist_ok=True)
129
+ result.images[0].save(output_path)
130
+
131
+ file_size = Path(output_path).stat().st_size / (1024*1024)
132
+
133
+ if progress:
134
+ progress(1.0, desc="Done!")
135
+
136
+ status = f"""βœ… Image generated!
137
+
138
+ πŸ“Š Details:
139
+ β€’ File: {output_filename}
140
+ β€’ Size: {file_size:.2f} MB
141
+ β€’ Resolution: {width}Γ—{height}
142
+ β€’ Steps: {steps}
143
+ β€’ CFG: {cfg}
144
+ β€’ Seed: {seed if seed > 0 else 'random'}
145
+
146
+ πŸ’‘ Generated with FLUX.2-klein-4B Q8_0"""
147
+
148
+ log(f"βœ… Saved: {output_path} ({file_size:.1f} MB)")
149
+
150
+ return output_path, status
151
+
152
+ except ImportError as e:
153
+ missing = str(e).split("'")[1] if "'" in str(e) else str(e)
154
+ return None, f"""❌ Missing library: {missing}
155
+
156
+ Installing now... (this may take 1-2 minutes)
157
+
158
+ Run in terminal:
159
+ pip install diffusers transformers accelerate safetensors sentencepiece
160
+
161
+ Or add to requirements.txt:
162
+ diffusers>=0.30.0
163
+ transformers>=4.44.0
164
+ accelerate>=0.33.0
165
+ safetensors>=0.4.5
166
+ sentencepiece>=0.2.0"""
167
+
168
  except Exception as e:
169
+ error_type = type(e).__name__
170
+ error_msg = str(e)[:500]
171
+
172
+ log(f"❌ Generation failed: {error_type}: {error_msg}")
173
+
174
+ return None, f"""❌ Generation failed!
175
+
176
+ Error Type: {error_type}
177
+ Message: {error_msg}
178
+
179
+ Common fixes:
180
+ β€’ Reduce resolution (try 512Γ—512)
181
+ β€’ Reduce steps (try 10-15)
182
+ β€’ Check available RAM (need ~12GB+ free)
183
+
184
+ Full error logged above."""
185
+
186
+
187
+ # ══════════════════════════════════════════════
188
+ # RUN SETUP
189
+ # ══════════════════════════════════════════════
190
 
191
+ print("\n" + "="*60)
192
+ print("🎨 FLUX.2-klein-4B Q8_0 GENERATOR")
193
+ print("="*60 + "\n")
194
+
195
+ setup_success = setup()
196
+
197
+ if setup_success:
198
+ print("\nβœ… Ready to generate images!\n")
199
+ else:
200
+ print("\n⚠️ Setup had issues - check logs above\n")
201
+
202
+ # ══════════════════════════════════════════════
203
+ # GRADIO INTERFACE
204
+ # ══════════════════════════════════════════════
205
 
206
  import gradio as gr
207
 
208
+ with gr.Blocks(
209
+ title="FLUX.2-klein-4B Q8_0",
210
+ theme=gr.themes.Soft()
211
+ ) as demo:
212
+
213
+ gr.Markdown("""
214
+ # 🎨 FLUX.2-klein-4B (Q8_0) Generator
215
+
216
+ ### Model: `unsloth/FLUX.2-klein-4B-GGUF`
217
+ ### Backend: Diffusers (CPU Optimized)
218
+ """)
219
 
220
  with gr.Row():
221
  with gr.Column(scale=2):
222
+ prompt_input = gr.Textbox(
223
+ label="✍️ Prompt",
224
+ placeholder="A red fox in snow, detailed, 8k quality...",
225
+ lines=4,
226
+ value="a red fox in snow, detailed, 8k"
227
+ )
228
+
229
+ neg_input = gr.Textbox(
230
+ label="🚫 Negative Prompt (optional)",
231
+ placeholder="blurry, low quality, deformed...",
232
+ lines=2,
233
+ value=""
234
+ )
235
+
236
+ with gr.Column(scale=1):
237
+ width_slider = gr.Slider(256, 1024, 1024, step=64, label="↔️ Width")
238
+ height_slider = gr.Slider(256, 1024, 1024, step=64, label="↕️ Height")
239
+ steps_slider = gr.Slider(1, 50, 20, step=1, label="πŸ”„ Steps")
240
+ cfg_slider = gr.Slider(1.0, 20.0, 7.0, step=0.5, label="βš™οΈ CFG Scale")
241
+ seed_input = gr.Number(-1, label="🎲 Seed (-1=random)")
242
+
243
+ with gr.Row():
244
+ gen_btn = gr.Button("πŸš€ Generate Image", variant="primary", size="lg")
245
+ clear_btn = gr.Button("πŸ—‘οΈ Clear", size="lg")
246
+
247
+ with gr.Row():
248
+ with gr.Column():
249
+ output_image = gr.Image(label="πŸ–ΌοΈ Output", height=450, type="filepath")
250
+
251
  with gr.Column():
252
+ status_output = gr.Textbox(
253
+ label="πŸ“Š Status & Info",
254
+ lines=12,
255
+ max_lines=20,
256
+ interactive=False
257
+ )
258
+
259
+ gen_btn.click(
260
+ fn=generate_image,
261
+ inputs=[prompt_input, neg_input, width_slider, height_slider,
262
+ steps_slider, cfg_slider, seed_input],
263
+ outputs=[output_image, status_output],
264
+ show_progress=True
265
+ )
266
 
267
+ clear_btn.click(
268
+ fn=lambda: (
269
+ "a red fox in snow, detailed, 8k", # Reset prompt
270
+ "", # Clear negative
271
+ 1024, # Default width
272
+ 1024, # Default height
273
+ 20, # Default steps
274
+ 7.0, # Default CFG
275
+ -1, # Random seed
276
+ None, # Clear image
277
+ "" # Clear status
278
+ ),
279
+ outputs=[prompt_input, neg_input, width_slider, height_slider,
280
+ steps_slider, cfg_slider, seed_input, output_image, status_output]
281
+ )
282
 
283
+ gr.Markdown("""
284
+ ---
285
+ ### πŸ’‘ Tips for CPU Generation
286
+ - **Faster**: Lower resolution (512Γ—512) + fewer steps (10-15)
287
+ - **Better Quality**: Higher CFG (7-12) + more steps (25-30)
288
+ - **First Run**: Slow (~3-5 min) while model loads into memory
289
+ - **Subsequent Runs**: Faster (~1-3 min each)
290
+ """)
291
+
292
+
293
+ # ══════════════════════════════════════════════
294
+ # LAUNCH
295
+ # ══════════════════════════════════════════════
296
 
297
  if __name__ == "__main__":
298
  demo.launch(server_name="0.0.0.0", port=7860)