aexyb commited on
Commit
f519f03
Β·
verified Β·
1 Parent(s): 3386a0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +323 -1087
app.py CHANGED
@@ -1,1161 +1,397 @@
 
 
 
 
 
 
 
1
  import os
2
  import sys
3
  import subprocess
4
  import urllib.request
5
- import urllib.error
6
- import shutil
7
- import time
8
- import json
9
- import re
10
  from pathlib import Path
11
  from datetime import datetime
12
- from typing import Optional, Tuple, List, Dict
13
-
14
- # ═══════════════════════════════════════════════════════════════
15
- # SECTION 1: CONFIGURATION & CONSTANTS
16
- # ═══════════════════════════════════════════════════════════════
17
 
18
- class Config:
19
- """Centralized configuration"""
20
-
21
- # Directories
22
- BASE_DIR = Path.cwd()
23
- MODELS_DIR = BASE_DIR / "models"
24
- OUTPUTS_DIR = BASE_DIR / "outputs"
25
- SD_DIR = BASE_DIR / "stable-diffusion.cpp"
26
- CACHE_DIR = BASE_DIR / ".cache"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- # Model definitions (filename -> list of download sources)
29
- MODELS = {
30
- "bonsai-flux2-klein-ternary-q2_k.gguf": {
31
- "description": "Flux Diffusion Model (Q2_K Quantized)",
32
- "expected_size_mb": 2500, # ~2.5 GB
33
- "required": True,
34
- "sources": [
35
- # Source 1: HuggingFace mirror (try without auth first)
36
- {
37
- "url": "https://huggingface.co/bonsai-models/flux.1-klein-gguf/resolve/main/bonsai-flux2-klein-ternary-q2_k.gguf",
38
- "method": "direct",
39
- "priority": 1
40
- },
41
- # Source 2: Alternative HF location
42
- {
43
- "url": "https://huggingface.co/comfyanonymous/flux.1-klein-gguf/resolve/main/bonsai-flux2-klein-ternary-q2_k.gguf",
44
- "method": "direct",
45
- "priority": 2
46
- },
47
- # Source 3: Try different model name pattern
48
- {
49
- "url": "https://huggingface.co/bonsai-models/flux.1-klein-gguf/resolve/main/flux1-klein-Q2_K.gguf",
50
- "method": "direct",
51
- "priority": 3
52
- }
53
- ]
54
- },
55
- "ae.safetensors": {
56
- "description": "VAE Decoder (FLUX.1)",
57
- "expected_size_mb": 335,
58
- "required": True,
59
- "sources": [
60
- {
61
- "url": "https://huggingface.co/black-forest-labs/FLUX.1-dev/resolve/main/ae.safetensors",
62
- "method": "direct",
63
- "priority": 1
64
- },
65
- # Alternative VAE (compatible)
66
- {
67
- "url": "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors",
68
- "method": "direct",
69
- "priority": 2,
70
- "rename_to": "flux2-vae.safetensors" # Will rename after download
71
- }
72
- ]
73
- },
74
- "qwen2.5-1.5b-instruct-q2_k.gguf": {
75
- "description": "Text Encoder LLM (Qwen 1.5B)",
76
- "expected_size_mb": 718,
77
- "required": True,
78
- "sources": [
79
- {
80
- "url": "https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q2_k.gguf",
81
- "method": "direct",
82
- "priority": 1
83
- },
84
- # Smaller alternative if needed
85
- {
86
- "url": "https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q2_k.gguf",
87
- "method": "direct",
88
- "priority": 2,
89
- "rename_to": "qwen_3_4b.gguf"
90
- }
91
- ]
92
- }
93
- }
94
 
95
- # Build settings
96
- SD_REPO_URL = "https://github.com/leejet/stable-diffusion.git"
97
- SD_BUILD_JOBS = os.cpu_count() or 2
98
 
99
- # Generation defaults
100
- DEFAULT_CFG = 1.0
101
- DEFAULT_STEPS = 4
102
- DEFAULT_WIDTH = 1024
103
- DEFAULT_HEIGHT = 1024
104
- TIMEOUT_SECONDS = 600 # 10 minutes
105
 
106
- # Retry settings
107
- MAX_RETRIES = 3
108
- RETRY_DELAY = 5 # seconds
109
-
110
 
111
- # Global state
112
- sd_cli_path: Optional[str] = None
113
- setup_complete = False
114
 
 
 
 
115
 
116
- # ═══════════════════════════════════════════════════════════════
117
- # SECTION 2: UTILITY FUNCTIONS
118
- # ═══════════════════════════════════════════════════════════════
119
-
120
- class Logger:
121
- """Custom logger with timestamps and formatting"""
122
-
123
- @staticmethod
124
- def header(text: str):
125
- print(f"\n{'═'*70}")
126
- print(f" {text}")
127
- print(f"{'═'*70}\n")
128
 
129
- @staticmethod
130
- def info(text: str):
131
- timestamp = datetime.now().strftime("%H:%M:%S")
132
- print(f"[{timestamp}] ℹ️ {text}")
133
 
134
- @staticmethod
135
- def success(text: str):
136
- timestamp = datetime.now().strftime("%H:%M:%S")
137
- print(f"[{timestamp}] βœ… {text}")
138
-
139
- @staticmethod
140
- def warning(text: str):
141
- timestamp = datetime.now().strftime("%H:%M:%S")
142
- print(f"[{timestamp}] ⚠️ {text}")
143
-
144
- @staticmethod
145
- def error(text: str):
146
- timestamp = datetime.now().strftime("%H:%M:%S")
147
- print(f"[{timestamp}] ❌ {text}")
148
 
149
- @staticmethod
150
- def step(step_num: int, total: int, text: str):
151
- print(f"\n[Step {step_num}/{total}] πŸ“ {text}\n")
152
-
153
-
154
- def run_shell_command(
155
- command: List[str] | str,
156
- description: str,
157
- timeout: int = 300,
158
- cwd: Optional[Path] = None,
159
- env: Optional[Dict] = None
160
- ) -> Tuple[bool, str, str]:
161
- """
162
- Execute shell command with comprehensive error handling
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
- Returns:
165
- Tuple of (success: bool, stdout: str, stderr: str)
166
- """
167
- Logger.info(f"{description}")
168
- Logger.debug(f"Command: {' '.join(command) if isinstance(command, list) else command}")
169
 
170
- try:
171
- result = subprocess.run(
172
- command,
173
- shell=isinstance(command, str),
174
- capture_output=True,
175
- text=True,
176
- timeout=timeout,
177
- cwd=str(cwd) if cwd else None,
178
- env=env
179
- )
180
-
181
- if result.returncode == 0:
182
- Logger.success(f"{description} - OK")
183
- return True, result.stdout, result.stderr
184
- else:
185
- Logger.error(f"{description} - FAILED (code {result.returncode})")
186
- if result.stderr:
187
- Logger.error(f"Error output:\n{result.stderr[:500]}")
188
- return False, result.stdout, result.stderr
189
-
190
- except subprocess.TimeoutExpired:
191
- Logger.error(f"{description} - TIMEOUT after {timeout}s")
192
- return False, "", "Command timed out"
193
- except Exception as e:
194
- Logger.error(f"{description} - EXCEPTION: {e}")
195
- return False, "", str(e)
196
-
197
-
198
- def download_file_with_progress(
199
- url: str,
200
- destination: Path,
201
- description: str = "Downloading"
202
- ) -> bool:
203
- """
204
- Download file with progress reporting and retry logic
205
- """
206
- for attempt in range(Config.MAX_RETRIES):
207
  try:
208
- Logger.info(f"{description} (attempt {attempt + 1}/{Config.MAX_RETRIES})")
209
- Logger.info(f"URL: {url[:100]}...")
210
 
211
- # Create destination directory
212
- destination.parent.mkdir(parents=True, exist_ok=True)
213
-
214
- # Download with progress hook
215
- def progress_hook(count: int, block_size: int, total_size: int):
216
- if total_size > 0:
217
- downloaded = count * block_size
218
- percent = min(100, (downloaded * 100) // total_size)
219
- mb_downloaded = downloaded / (1024 * 1024)
220
- mb_total = total_size / (1024 * 1024)
221
-
222
- # Update same line for progress
223
- sys.stdout.write(
224
- f"\r Progress: {percent}% "
225
- f"({mb_downloaded:.1f} MB / {mb_total:.1f} MB)"
226
- )
227
- sys.stdout.flush()
228
-
229
- # Add user agent to avoid blocking
230
- request = urllib.request.Request(
231
- url,
232
- headers={'User-Agent': 'Mozilla/5.0 (Space Setup Bot)'}
233
- )
234
-
235
- urllib.request.urlretrieve(request, destination, progress_hook)
236
- sys.stdout.write("\n") # New line after progress
237
-
238
- # Verify download
239
- if destination.exists() and destination.stat().st_size > 0:
240
- size_mb = destination.stat().st_size / (1024 * 1024)
241
- Logger.success(f"Download complete: {destination.name} ({size_mb:.1f} MB)")
242
  return True
243
  else:
244
- Logger.warning("Download completed but file is empty or missing")
245
-
246
- except urllib.error.HTTPError as e:
247
- Logger.warning(f"HTTP Error {e.code}: {e.reason}")
248
- if e.code == 401 or e.code == 403:
249
- Logger.warning("Authentication required or access denied - trying next source")
250
- return False # Don't retry auth errors
251
 
252
- except urllib.error.URLError as e:
253
- Logger.warning(f"URL Error: {e.reason}")
254
-
255
  except Exception as e:
256
- Logger.error(f"Download error: {e}")
257
-
258
- if attempt < Config.MAX_RETRIES - 1:
259
- wait_time = Config.RETRY_DELAY * (attempt + 1)
260
- Logger.info(f"Retrying in {wait_time} seconds...")
261
- time.sleep(wait_time)
262
 
 
 
263
  return False
264
 
265
 
266
- def check_disk_space(required_gb: float = 10.0) -> bool:
267
- """Check if enough disk space available"""
268
- try:
269
- usage = shutil.disk_usage(Config.BASE_DIR)
270
- free_gb = usage.free / (1024**3)
271
-
272
- if free_gb < required_gb:
273
- Logger.error(f"Insufficient disk space! Need {required_gb}GB, have {free_gb:.1f}GB")
274
- return False
275
-
276
- Logger.success(f"Disk space OK: {free_gb:.1f} GB free")
277
- return True
278
- except Exception as e:
279
- Logger.warning(f"Could not check disk space: {e}")
280
- return True # Assume OK if can't check
281
 
282
-
283
- # ═══════════════════════════════════════════════════════════════
284
- # SECTION 3: SETUP FUNCTIONS
285
- # ═══════════════════════════════════════════════════════════════
286
-
287
- def setup_directories() -> None:
288
- """Create necessary directories"""
289
- Logger.step(1, 6, "Creating directory structure")
290
 
291
- for dir_path in [Config.MODELS_DIR, Config.OUTPUTS_DIR, Config.CACHE_DIR]:
292
- dir_path.mkdir(exist_ok=True)
293
- Logger.success(f"Created: {dir_path}")
294
-
295
-
296
- def install_system_dependencies() -> bool:
297
- """Install system packages needed for building"""
298
- Logger.step(2, 6, "Installing system dependencies")
299
 
300
- packages = ["cmake", "build-essential", "git", "wget", "curl"]
301
-
302
- for pkg in packages:
303
- success, _, _ = run_shell_command(
304
- f"apt-get install -y {pkg}",
305
- f"Installing {pkg}",
306
- timeout=120
307
- )
308
-
309
- if not success:
310
- Logger.warning(f"Failed to install {pkg}, may not be critical")
311
-
312
- # Install Python dependencies
313
- success, _, _ = run_shell_command(
314
- [sys.executable, "-m", "pip", "install", "--quiet",
315
- "gradio==4.44.0", "Pillow>=10.0.0"],
316
- "Installing Python packages",
317
- timeout=180
318
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
 
320
- return success
321
 
322
 
323
- def clone_and_build_sd_cpp() -> Optional[str]:
324
- """Clone and compile stable-diffusion.cpp from source"""
325
- Logger.step(3, 6, "Building stable-diffusion.cpp")
326
-
327
- global sd_cli_path
328
-
329
- # Check if already built
330
- possible_binaries = [
331
- Config.SD_DIR / "build" / "bin" / "sd-cli",
332
- Config.SD_DIR / "bin" / "sd-cli",
333
- ]
334
-
335
- for binary in possible_binaries:
336
- if binary.exists():
337
- sd_cli_path = str(binary.absolute())
338
- Logger.success(f"Found existing build: {sd_cli_path}")
339
- return sd_cli_path
340
-
341
- # Clone repository
342
- if not (Config.SD_DIR / "CMakeLists.txt").exists():
343
- Logger.info("Cloning stable-diffusion.cpp repository...")
344
-
345
- success, _, error = run_shell_command(
346
- ["git", "clone", "--depth", "1", "--shallow-submodules",
347
- Config.SD_REPO_URL, str(Config.SD_DIR)],
348
- "Cloning repository (shallow clone for speed)",
349
- timeout=180
350
- )
351
-
352
- if not success:
353
- Logger.error("Failed to clone repository!")
354
- return None
355
-
356
- # Create build directory
357
- build_dir = Config.SD_DIR / "build"
358
- build_dir.mkdir(exist_ok=True)
359
-
360
- # Configure with CMake
361
- Logger.info("Configuring build system...")
362
- cmake_args = [
363
- "cmake", "..",
364
- "-DSD_CPU=ON",
365
- "-DSD_CUDA=OFF",
366
- "-DCMAKE_BUILD_TYPE=Release"
367
- ]
368
-
369
- success, stdout, error = run_shell_command(
370
- cmake_args,
371
- "Running CMake configuration",
372
- timeout=120,
373
- cwd=build_dir
374
- )
375
 
376
- if not success:
377
- Logger.error("CMake configuration failed!")
378
- Logger.error(f"Output: {stdout[:500]}")
379
- Logger.error(f"Errors: {error[:500]}")
380
- return None
381
 
382
- # Build
383
- Logger.info(f"Compiling (using {Config.SD_BUILD_JOBS} CPU cores)...")
384
- Logger.warning("This may take 5-15 minutes...")
385
 
386
- success, stdout, error = run_shell_command(
387
- ["cmake", "--build", ".", "--config", "Release",
388
- f"-j{Config.SD_BUILD_JOBS}"],
389
- "Building project",
390
- timeout=900, # 15 minute timeout
391
- cwd=build_dir
392
  )
393
 
394
- if not success:
395
- Logger.error("Build failed!")
396
- return None
397
-
398
- # Verify binary exists
399
- built_binary = build_dir / "bin" / "sd-cli"
400
- if built_binary.exists():
401
- sd_cli_path = str(built_binary.absolute())
402
-
403
- # Ensure executable
404
- built_binary.chmod(0o755)
405
-
406
- size_mb = built_binary.stat().st_size / (1024 * 1024)
407
- Logger.success(f"Build successful!")
408
- Logger.success(f"Binary: {sd_cli_path}")
409
- Logger.success(f"Size: {size_mb:.1f} MB")
410
-
411
- return sd_cli_path
412
  else:
413
- Logger.error("Build completed but binary not found!")
414
- return None
415
-
416
-
417
- def download_all_models() -> Dict[str, bool]:
418
- """
419
- Download all required models from multiple sources
420
- Returns dict of model_name -> success_status
421
- """
422
- Logger.step(4, 6, "Downloading model files")
423
-
424
- results = {}
425
 
426
- # Check disk space first (need ~8GB for all models)
427
- if not check_disk_space(10.0):
428
- Logger.error("Not enough disk space for models!")
429
- return {name: False for name in Config.MODELS}
430
-
431
- for filename, model_config in Config.MODELS.items():
432
- Logger.header(f"MODEL: {model_config['description']}")
433
-
434
- dest_path = Config.MODELS_DIR / filename
435
-
436
- # Skip if already downloaded
437
- if dest_path.exists():
438
- size_mb = dest_path.stat().st_size / (1024 * 1024)
439
- Logger.success(f"Already exists: {filename} ({size_mb:.1f} MB)")
440
- results[filename] = True
441
- continue
442
-
443
- # Try each source in priority order
444
- downloaded = False
445
- sorted_sources = sorted(
446
- model_config['sources'],
447
- key=lambda x: x['priority']
448
  )
449
 
450
- for source in sorted_sources:
451
- url = source['url']
452
- method = source.get('method', 'direct')
453
- rename_to = source.get('rename_to')
454
-
455
- Logger.info(f"Trying source (priority {source['priority']}): {url[:80]}...")
456
-
457
- if method == "direct":
458
- download_dest = dest_path
459
- if rename_to:
460
- download_dest = Config.MODELS_DIR / rename_to.split('/')[-1]
461
-
462
- success = download_file_with_progress(
463
- url=url,
464
- destination=download_dest,
465
- description=f"Downloading {filename}"
466
- )
467
-
468
- if success and rename_to:
469
- # Rename after successful download
470
- final_dest = Config.MODELS_DIR / rename_to
471
- download_dest.rename(final_dest)
472
- Logger.success(f"Renamed to: {rename_to}")
473
- dest_path = final_dest
474
-
475
- if success:
476
- downloaded = True
477
- break
478
 
479
- if downloaded:
480
- results[filename] = True
481
- else:
482
- results[filename] = False
483
- if model_config.get('required'):
484
- Logger.error(f"FAILED to download required model: {filename}")
485
- Logger.error("\n⚠️ MANUAL UPLOAD REQUIRED!")
486
- Logger.error(f"Please upload '{filename}' to the './models/' directory")
487
- Logger.error("\nOr use the upload helper script provided below.\n")
488
-
489
- return results
490
-
491
-
492
- def verify_setup() -> bool:
493
- """Verify all components are ready"""
494
- Logger.step(5, 6, "Verifying installation")
495
-
496
- issues = []
497
-
498
- # Check sd-cli
499
- if sd_cli_path and Path(sd_cli_path).exists():
500
- Logger.success(f"βœ“ sd-cli found: {sd_cli_path}")
501
- else:
502
- issues.append("stable-diffusion.cpp binary (sd-cli) not found or not built")
503
-
504
- # Check models
505
- for filename, config in Config.MODELS.items():
506
- model_path = Config.MODELS_DIR / filename
507
-
508
- # Also check renamed versions
509
- if not model_path.exists():
510
- # Check if it was downloaded under a different name
511
- for source in config.get('sources', []):
512
- alt_name = source.get('rename_to', '')
513
- if alt_name and (Config.MODELS_DIR / alt_name).exists():
514
- model_path = Config.MODELS_DIR / alt_name
515
- break
516
-
517
- if model_path.exists():
518
- size_mb = model_path.stat().st_size / (1024 * 1024)
519
- expected = config.get('expected_size_mb', 0)
520
 
521
- if expected > 0 and size_mb < expected * 0.5:
522
- issues.append(
523
- f"{filename}: File seems too small ({size_mb:.1f}MB vs ~{expected}MB expected)"
524
- )
525
- else:
526
- Logger.success(f"βœ“ {filename}: {size_mb:.1f} MB")
527
- else:
528
- if config.get('required'):
529
- issues.append(f"Missing required model: {filename}")
530
- else:
531
- Logger.warning(f"Optional model missing: {filename}")
532
-
533
- # Check outputs directory
534
- Config.OUTPUTS_DIR.mkdir(exist_ok=True)
535
- Logger.success(f"βœ“ Output directory ready: {Config.OUTPUTS_DIR}")
536
-
537
- if issues:
538
- Logger.error("\n⚠️ ISSUES DETECTED:")
539
- for i, issue in enumerate(issues, 1):
540
- Logger.error(f" {i}. {issue}")
541
- return False
542
- else:
543
- Logger.success("\nβœ… ALL SYSTEMS OPERATIONAL!")
544
- return True
545
-
546
-
547
- def generate_upload_helper_script() -> None:
548
- """Generate a script to help users manually upload models"""
549
- Logger.step(6, 6, "Generating upload helper tools")
550
-
551
- script_content = '''#!/bin/bash
552
- # ═══════════════════════════════════════════════════════════════
553
- # MODEL UPLOAD HELPER SCRIPT
554
- # Use this if automatic downloads fail
555
- # ═══════════════════════════════════════════════════════════════
556
-
557
- set -e
558
-
559
- echo "╔══════════════════════════════════════════════════════════╗"
560
- echo "β•‘ FLUX MODEL UPLOAD HELPER β•‘"
561
- echo "β•‘ For situations where auto-download fails β•‘"
562
- echo "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•"
563
- echo ""
564
-
565
- mkdir -p models
566
-
567
- echo "πŸ“₯ Option 1: Download using huggingface-cli (recommended)"
568
- echo ""
569
- echo "Run these commands on your LOCAL machine:"
570
- echo ""
571
- echo "# Install huggingface-hub if needed:"
572
- echo "pip install -U huggingface_hub"
573
- echo ""
574
- echo "# Login (optional but recommended for gated repos):"
575
- echo "huggingface-cli login"
576
- echo ""
577
- echo "# Download models:"
578
- echo "huggingface-cli download bonsai-models/flux.1-klein-gguf \\"
579
- echo " bonsai-flux2-klein-ternary-q2_k.gguf --local-dir ./models"
580
- echo ""
581
- echo "huggingface-cli download black-forest-labs/FLUX.1-dev \\"
582
- echo " ae.safetensors --local-dir ./models --local-dir-use-symlinks False"
583
- echo ""
584
- echo "huggingface-cli download Qwen/Qwen2.5-1.5B-Instruct-GGUF \\"
585
- echo " qwen2.5-1.5b-instruct-q2_k.gguf --local-dir ./models"
586
- echo ""
587
- echo "───────────────────────────────────────────────────────"
588
- echo ""
589
- echo "🌐 Option 2: Manual browser download"
590
- echo ""
591
- echo "Visit these URLs in your browser and save to ./models/:"
592
- echo ""
593
- echo "1. Diffusion Model (~2.5 GB):"
594
- echo " https://huggingface.co/bonsai-models/flux.1-klein-gguf/tree/main"
595
- echo " β†’ Download: bonsai-flux2-klein-ternary-q2_k.gguf"
596
- echo ""
597
- echo "2. VAE Decoder (~335 MB):"
598
- echo " https://huggingface.co/black-forest-labs/FLUX.1-dev/tree/main"
599
- echo " β†’ Download: ae.safetensors"
600
- echo " β†’ Rename to: flux2-vae.safetensors (if needed)"
601
- echo ""
602
- echo "3. Text Encoder (~718 MB):"
603
- echo " https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/tree/main"
604
- echo " β†’ Download: qwen2.5-1.5b-instruct-q2_k.gguf"
605
- echo " β†’ Rename to: qwen_3_4b.gguf (if needed)"
606
- echo ""
607
- echo "───────────────────────────────────────────────────────"
608
- echo ""
609
- echo "πŸ“€ After downloading, verify files:"
610
- echo ""
611
- ls -lh models/
612
- echo ""
613
- echo "Total size should be ~3.5-4 GB"
614
- du -sh models/
615
- echo ""
616
- echo "βœ… Done! Now restart your Space."
617
- '''
618
-
619
- script_path = Config.BASE_DIR / "upload_models.sh"
620
- script_path.write_text(script_content)
621
- script_path.chmod(0o755)
622
-
623
- Logger.success(f"Created upload helper: {script_path}")
624
- Logger.info("Run this script locally if auto-download fails")
625
-
626
-
627
- # ═══════════════════════════════════════════════════════════════
628
- # SECTION 4: MAIN SETUP ORCHESTRATOR
629
- # ═══════════════════════════════════════════════════════════════
630
-
631
- def run_setup() -> bool:
632
- """Execute complete setup process"""
633
- global setup_complete
634
-
635
- start_time = time.time()
636
-
637
- Logger.header("FLUX TEXT-TO-IMAGE GENERATOR - AUTO SETUP")
638
- Logger.info(f"Base directory: {Config.BASE_DIR}")
639
- Logger.info(f"CPU cores: {os.cpu_count() or 'unknown'}")
640
- Logger.info(f"Python: {sys.version.split()[0]}")
641
- Logger.info(f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
642
-
643
- # Step 1: Directories
644
- setup_directories()
645
-
646
- # Step 2: System dependencies
647
- install_system_dependencies()
648
-
649
- # Step 3: Build sd-cpp
650
- sd_cli_path = clone_and_build_sd_cpp()
651
-
652
- # Step 4: Download models
653
- model_results = download_all_models()
654
-
655
- # Step 5: Verify
656
- all_ready = verify_setup()
657
-
658
- # Step 6: Helper scripts
659
- generate_upload_helper_script()
660
-
661
- # Summary
662
- elapsed = time.time() - start_time
663
-
664
- Logger.header("SETUP COMPLETE")
665
- Logger.info(f"Total time: {elapsed:.1f} seconds ({elapsed/60:.1f} minutes)")
666
- Logger.info(f"Models downloaded: {sum(model_results.values())}/{len(model_results)}")
667
- Logger.info(f"sd-cli built: {'Yes' if sd_cli_path else 'No'}")
668
- Logger.info(f"System ready: {'Yes' if all_ready else 'No - see issues above'}")
669
-
670
- setup_complete = all_ready
671
- return all_ready
672
-
673
-
674
- # ═══════════════════════════════════════════════════════════════
675
- # SECTION 5: IMAGE GENERATION LOGIC
676
- # ═══════════════════════════════════════════════════════════════
677
-
678
- def generate_image(
679
- prompt: str,
680
- negative_prompt: str = "",
681
- cfg_scale: float = Config.DEFAULT_CFG,
682
- steps: int = Config.DEFAULT_STEPS,
683
- width: int = Config.DEFAULT_WIDTH,
684
- height: int = Config.DEFAULT_HEIGHT,
685
- seed: int = -1,
686
- progress=None
687
- ) -> Tuple[Optional[str], str]:
688
- """
689
- Main image generation function
690
- """
691
-
692
- # Input validation
693
- if not prompt or not prompt.strip():
694
- return None, "❌ Please enter a text prompt!"
695
-
696
- prompt = prompt.strip()
697
-
698
- # Check prerequisites
699
- if not sd_cli_path or not Path(sd_cli_path).exists():
700
- return None, f"""❌ CRITICAL: Inference engine not found!
701
-
702
- The stable-diffusion.cpp binary (sd-cli) was not detected.
703
-
704
- Possible causes:
705
- β€’ Build failed during setup
706
- β€’ Binary was deleted
707
- β€’ Incorrect path configuration
708
-
709
- Solution: Restart the Space to trigger rebuild."""
710
-
711
- # Resolve actual model paths (handle renames)
712
- def get_model_path(filename):
713
- path = Config.MODELS_DIR / filename
714
- if path.exists():
715
- return str(path)
716
 
717
- # Check alternatives
718
- for config in Config.MODELS.values():
719
- for source in config.get('sources', []):
720
- alt = source.get('rename_to', '')
721
- if alt and (Config.MODELS_DIR / alt).exists(): # ← Remove one )
722
- return str(Config.MODELS_DIR / alt)
723
 
724
- return str(path) # Return original even if missing (for better error msg)
725
-
726
- diffusion_model = get_model_path("bonsai-flux2-klein-ternary-q2_k.gguf")
727
- vae_model = get_model_path("ae.safetensors")
728
- llm_model = get_model_path("qwen2.5-1.5b-instruct-q2_k.gguf")
729
-
730
- # Validate models exist
731
- missing = []
732
- for name, path in [("Diffusion", diffusion_model), ("VAE", vae_model), ("LLM", llm_model)]:
733
- if not Path(path).exists():
734
- missing.append(f"β€’ {name}: {path}")
735
-
736
- if missing:
737
- return None, f"""❌ Missing model files!
738
 
739
- The following required models were not found:
740
- {chr(10).join(missing)}
741
 
742
- To fix this:
 
 
743
 
744
- Option A: Restart Space (will retry downloads)
745
 
746
- Option B: Manual upload
747
- 1. Run: bash upload_models.sh (on your local machine)
748
- 2. Upload the ./models/ folder to your Space
749
- 3. Restart Space
750
-
751
- Option C: Use Space Persistent Storage
752
- 1. Go to Space Settings β†’ Storage
753
- 2. Upload model files there
754
- 3. They'll persist across restarts"""
755
-
756
- # Prepare output
757
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
758
- output_filename = f"gen_{timestamp}.png"
759
- output_path = str(Config.OUTPUTS_DIR / output_filename)
760
-
761
- # Build command
762
- cmd = [
763
- sd_cli_path,
764
- "--diffusion-model", diffusion_model,
765
- "--vae", vae_model,
766
- "--llm", llm_model,
767
- "-p", prompt,
768
- "--cfg-scale", str(cfg_scale),
769
- "--steps", str(steps),
770
- "-W", str(width),
771
- "-H", str(height),
772
- "-o", output_path,
773
- "--offload-to-cpu",
774
- "--diffusion-fa",
775
- "--vae-tiling",
776
- ]
777
 
778
- if seed > 0:
779
- cmd.extend(["--seed", str(seed)])
780
 
781
- if negative_prompt.strip():
782
- cmd.extend(["-n", negative_prompt.strip()])
783
-
784
- # Execute generation
785
- gen_start = time.time()
786
-
787
- if progress:
788
- progress(0.05, desc="Initializing generation engine...")
789
 
790
  try:
791
- Logger.info(f"Starting generation...")
792
- Logger.info(f"Prompt: {prompt[:80]}...")
793
- Logger.info(f"Parameters: CFG={cfg_scale}, Steps={steps}, Size={width}x{height}")
794
-
795
- if progress:
796
- progress(0.1, desc="Running inference (CPU mode, please be patient)...")
797
-
798
- result = subprocess.run(
799
- cmd,
800
- capture_output=True,
801
- text=True,
802
- timeout=Config.TIMEOUT_SECONDS,
803
- cwd=str(Config.BASE_DIR)
804
  )
805
 
806
- gen_time = time.time() - gen_start
807
-
808
- if progress:
809
- progress(0.9, desc="Processing output...")
810
-
811
- # Capture outputs
812
- stdout = result.stdout or ""
813
- stderr = result.stderr or ""
814
-
815
- # Log for debugging
816
- if stdout:
817
- Logger.debug(f"SD-CLI stdout:\n{stdout[:800]}")
818
- if stderr:
819
- Logger.debug(f"SD-CLI stderr:\n{stderr[:800]}")
820
-
821
- # Check result
822
- if result.returncode != 0:
823
- error_msg = stderr or stdout or "Unknown error"
824
-
825
- return None, f"""❌ Generation failed!
826
-
827
- Exit code: {result.returncode}
828
- Time elapsed: {gen_time:.1f}s
829
-
830
- Error details:
831
- {error_msg[:1000]}
832
-
833
- Command that failed:
834
- {' '.join(cmd)}
835
-
836
- Common fixes:
837
- β€’ Check model file integrity (re-download if corrupted)
838
- β€’ Reduce resolution (try 512x512)
839
- β€’ Reduce steps (try 2-4)
840
- β€’ Check available RAM (need ~12GB+ free)"""
841
 
842
- # Check for output file
843
- if Path(output_path).exists():
844
- file_size = Path(output_path).stat().st_size / (1024*1024)
845
-
846
- if progress:
847
- progress(1.0, desc=f"βœ… Complete! ({file_size:.1f} MB)")
848
-
849
- status = f"""βœ… Image generated successfully!
850
-
851
- πŸ“Š Statistics:
852
- β”œβ”€ File: {output_filename}
853
- β”œβ”€ Size: {file_size:.2f} MB
854
- β”œβ”€ Resolution: {width} Γ— {height}
855
- β”œβ”€ CFG Scale: {cfg_scale}
856
- β”œβ”€ Steps: {steps}
857
- β”œβ”€ Seed: {'Auto' if seed < 0 else seed}
858
- └─ Time: {gen_time:.1f} seconds
859
-
860
- πŸ’‘ Tip: CPU generation takes 2-8 minutes depending on complexity."""
861
-
862
- return output_path, status
863
  else:
864
- return None, f"""⚠️ Command completed but no image generated!
865
-
866
- Generation ran for {gen_time:.1f}s but no output file found.
867
- This usually means the generation failed silently.
868
-
869
- Debug info:
870
- STDOUT (last 500 chars):
871
- {stdout[-500:] if stdout else '(empty)'}
872
-
873
- STDERR (last 500 chars):
874
- {stderr[-500:] if stderr else '(empty)'}
875
-
876
- Try:
877
- β€’ Using a simpler prompt
878
- β€’ Reducing steps to 2-4
879
- β€’ Lowering resolution to 768x768 or 512x512"""
880
 
881
- except subprocess.TimeoutExpired:
882
- gen_time = time.time() - gen_start
883
- return None, f"""⏰ Generation timed out after {Config.TIMEOUT_SECONDS//60} minutes!
884
-
885
- Elapsed time: {gen_time:.1f}s
886
-
887
- CPU inference is slow. To prevent timeouts:
888
-
889
- Recommendations:
890
- β€’ Use lower resolution (512Γ—512 instead of 1024Γ—1024)
891
- β€’ Fewer steps (2-4 instead of 8+)
892
- β€’ Simpler prompts (fewer details)
893
- β€’ CFG scale closer to 1.0
894
-
895
- Current settings used:
896
- Resolution: {width}Γ—{height}
897
- Steps: {steps}
898
- CFG: {cfg_scale}"""
899
-
900
  except Exception as e:
901
- import traceback
902
- return None, f"""❌ Unexpected error during generation!
903
-
904
- Error type: {type(e).__name__}
905
- Message: {str(e)}
906
-
907
- Traceback:
908
- {traceback.format_exc()}
909
-
910
- This is likely a bug. Please report this error message."""
911
 
912
 
913
- # ═══════════════════════════════════════════════════════════════
914
- # SECTION 6: GRADIO INTERFACE
915
- # ═══════════════════════════════════════════════════════════════
916
-
917
- def create_interface():
918
- """Build Gradio web interface"""
919
-
920
- # Import here to avoid issues if setup fails before import
921
- try:
922
- import gradio as gr
923
- except ImportError:
924
- subprocess.check_call([sys.executable, "-m", "pip", "install", "gradio==4.44.0"])
925
- import gradio as gr
926
-
927
- # Custom CSS
928
- custom_css = """
929
- .gradio-container {
930
- max-width: 1100px !important;
931
- margin: 0 auto;
932
- }
933
-
934
- .main-title {
935
- text-align: center;
936
- margin-bottom: 30px;
937
- padding: 20px;
938
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
939
- border-radius: 12px;
940
- color: white;
941
- }
942
-
943
- .status-box {
944
- font-family: monospace;
945
- white-space: pre-wrap;
946
- line-height: 1.5;
947
- }
948
-
949
- .generate-btn {
950
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
951
- color: white !important;
952
- font-weight: bold !important;
953
- font-size: 16px !important;
954
- padding: 12px 24px !important;
955
- }
956
-
957
- .generate-btn:hover {
958
- transform: translateY(-2px);
959
- box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
960
- }
961
- """
962
 
963
- with gr.Blocks(
964
- title="Flux Text-to-Image Generator (CPU)",
965
- css=custom_css,
966
- theme=gr.themes.Soft()
967
- ) as demo:
968
 
969
- # Header
970
- gr.HTML("""
971
- <div class="main-title">
972
- <h1 style="margin: 0; font-size: 2.5em;">🎨 Flux Text-to-Image</h1>
973
- <p style="margin: 10px 0 0; opacity: 0.9;">
974
- ⚑ CPU-Powered β€’ No GPU Required β€’ stable diffusion.cpp
975
- </p>
976
- </div>
977
- """)
978
-
979
- # Status indicator
980
- setup_status = gr.Textbox(
981
- value=f"Setup Status: {'βœ… Ready' if setup_complete else '⏳ Running initial setup...'}",
982
- label="System Status",
983
- interactive=False,
984
- elem_classes=["status-box"]
985
- )
986
-
987
- # Main content area
988
- with gr.Row():
989
- # Left column: Inputs
990
- with gr.Column(scale=2):
991
- prompt_input = gr.Textbox(
992
- label="✍️ Text Prompt",
993
- placeholder="A red fox in snow, detailed, 8k quality...",
994
- lines=4,
995
- value="a red fox in snow, detailed, 8k"
996
- )
997
-
998
- neg_prompt_input = gr.Textbox(
999
- label="🚫 Negative Prompt (optional)",
1000
- placeholder="blurry, low quality, deformed, ugly...",
1001
- lines=2,
1002
- value=""
1003
- )
1004
-
1005
- # Right column: Parameters
1006
- with gr.Column(scale=1):
1007
- cfg_slider = gr.Slider(
1008
- minimum=1.0,
1009
- maximum=7.0,
1010
- value=Config.DEFAULT_CFG,
1011
- step=0.5,
1012
- label="βš™οΈ CFG Scale",
1013
- info="Lower = faster | Higher = more prompt adherence"
1014
- )
1015
-
1016
- steps_slider = gr.Slider(
1017
- minimum=1,
1018
- maximum=20,
1019
- value=Config.DEFAULT_STEPS,
1020
- step=1,
1021
- label="πŸ”„ Inference Steps",
1022
- info="4 recommended for CPU speed"
1023
- )
1024
-
1025
- width_slider = gr.Slider(
1026
- minimum=256,
1027
- maximum=1024,
1028
- value=Config.DEFAULT_WIDTH,
1029
- step=64,
1030
- label="↔️ Width (pixels)"
1031
- )
1032
-
1033
- height_slider = gr.Slider(
1034
- minimum=256,
1035
- maximum=1024,
1036
- value=Config.DEFAULT_HEIGHT,
1037
- step=64,
1038
- label="↕️ Height (pixels)"
1039
- )
1040
-
1041
- seed_input = gr.Number(
1042
- label="🎲 Seed (-1 = random)",
1043
- value=-1,
1044
- precision=0
1045
- )
1046
-
1047
- # Action buttons
1048
- with gr.Row():
1049
- gen_btn = gr.Button(
1050
- "πŸš€ Generate Image",
1051
- variant="primary",
1052
- elem_classes=["generate-btn"],
1053
- size="lg"
1054
- )
1055
- clear_btn = gr.Button(
1056
- "πŸ—‘οΈ Clear All",
1057
- size="lg"
1058
- )
1059
-
1060
- # Output area
1061
- with gr.Row():
1062
- with gr.Column():
1063
- output_image = gr.Image(
1064
- label="πŸ–ΌοΈ Generated Image",
1065
- type="filepath",
1066
- height=450
1067
- )
1068
-
1069
- with gr.Column():
1070
- status_output = gr.Textbox(
1071
- label="πŸ“Š Status & Details",
1072
- interactive=False,
1073
- lines=14,
1074
- max_lines=25,
1075
- elem_classes=["status-box"]
1076
- )
1077
-
1078
- # Event handlers
1079
- gen_btn.click(
1080
- fn=generate_image,
1081
- inputs=[
1082
- prompt_input,
1083
- neg_prompt_input,
1084
- cfg_slider,
1085
- steps_slider,
1086
- width_slider,
1087
- height_slider,
1088
- seed_input
1089
- ],
1090
- outputs=[output_image, status_output],
1091
- show_progress=True
1092
- )
1093
-
1094
- clear_btn.click(
1095
- fn=lambda: (
1096
- "a red fox in snow, detailed, 8k", # Reset prompt
1097
- "", # Clear negative
1098
- Config.DEFAULT_CFG, # Reset CFG
1099
- Config.DEFAULT_STEPS, # Reset steps
1100
- Config.DEFAULT_WIDTH, # Reset width
1101
- Config.DEFAULT_HEIGHT, # Reset height
1102
- -1, # Random seed
1103
- None, # Clear image
1104
- "" # Clear status
1105
- ),
1106
- outputs=[
1107
- prompt_input,
1108
- neg_prompt_input,
1109
- cfg_slider,
1110
- steps_slider,
1111
- width_slider,
1112
- height_slider,
1113
- seed_input,
1114
- output_image,
1115
- status_output
1116
- ]
1117
- )
1118
-
1119
- # Footer info
1120
- gr.Markdown("""
1121
- ---
1122
- ### πŸ’‘ Tips for CPU Generation
1123
- - **Speed**: Lower resolution (512Γ—512) and fewer steps (2-4) = faster generation
1124
- - **Quality**: Higher CFG (2-4) and more steps (8-12) = better quality but slower
1125
- - **Memory**: Keep total parameters moderate to avoid OOM errors
1126
- - **Patience**: CPU inference typically takes 2-8 minutes per image
1127
-
1128
- ⏱️ First generation may be slower due to model loading into memory.
1129
- """)
1130
 
1131
- return demo
1132
 
1133
 
1134
- # ═══════════════════════════════════════════════════════════════
1135
- # SECTION 7: APPLICATION ENTRY POINT
1136
- # ═══════════════════════════════════════════════════════════════
1137
 
1138
- if __name__ != "__main__":
1139
- # When imported by Gradio/Uvicorn, run setup
1140
- run_setup()
1141
 
1142
- # Import Gradio (after setup ensures deps are installed)
1143
- try:
1144
- import gradio as gr
1145
- except ImportError:
1146
- subprocess.check_call([sys.executable, "-m", "pip", "install", "gradio==4.44.0"])
1147
- import gradio as gr
1148
 
1149
- # Create interface
1150
- demo = create_interface()
1151
 
1152
  if __name__ == "__main__":
1153
- Logger.header("LAUNCHING WEB INTERFACE")
1154
-
1155
- # Launch
1156
- demo.launch(
1157
- server_name="0.0.0.0",
1158
- server_port=7860,
1159
- share=False,
1160
- show_error=True
1161
- )
 
1
+ """
2
+ 🎨 FLUX.2-klein-4B Generator
3
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━
4
+ Model: unsloth/FLUX.2-klein-4B-GGUF (Q8_0)
5
+ Backend: ComfyUI + ComfyUI-GGUF
6
+ """
7
+
8
  import os
9
  import sys
10
  import subprocess
11
  import urllib.request
 
 
 
 
 
12
  from pathlib import Path
13
  from datetime import datetime
 
 
 
 
 
14
 
15
+ # ══════════════════════════════════════════════════════════
16
+ # CONFIGURATION
17
+ # ══════════════════════════════════════════════════════════
18
+
19
+ BASE_DIR = Path.cwd()
20
+ MODELS_DIR = BASE_DIR / "models"
21
+ OUTPUTS_DIR = BASE_DIR / "outputs"
22
+ COMFYUI_DIR = BASE_DIR / "ComfyUI"
23
+
24
+ # THE ONE MODEL YOU WANT
25
+ MODEL_NAME = "flux.2-klein-4b-Q8_0.gguf"
26
+ MODEL_HF_REPO = "unsloth/FLUX.2-klein-4B-GGUF"
27
+ MODEL_HF_FILE = "flux.2-klein-4b-Q8_0.gguf"
28
+
29
+ # Paths
30
+ MODEL_PATH = MODELS_DIR / MODEL_NAME
31
+ COMFY_PATH = COMFYUI_DIR / "main.py"
32
+ COMFY_MODELS_DIR = COMFYUI_DIR / "models" / "checkpoints"
33
+
34
+ # ══════════════════════════════════════════════════════════
35
+ # STEP 1: INSTALL COMFYUI
36
+ # ══════════════════════════════════════════════════════════
37
+
38
+ def install_comfyui():
39
+ """Install ComfyUI"""
40
+ print("\n" + "="*60)
41
+ print("πŸ“¦ Installing ComfyUI...")
42
+ print("="*60)
43
+
44
+ if COMFY_PATH.exists():
45
+ print("βœ… ComfyUI already installed")
46
+ return True
47
 
48
+ # Clone ComfyUI
49
+ subprocess.run([
50
+ "git", "clone",
51
+ "https://github.com/comfyanonymous/ComfyUI.git",
52
+ str(COMFYUI_DIR)
53
+ ], check=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ # Install requirements
56
+ pip_path = sys.executable
57
+ req_file = COMFYUI_DIR / "requirements.txt"
58
 
59
+ if req_file.exists():
60
+ subprocess.run([
61
+ pip_path, "-m", "pip", "install", "-r", str(req_file)
62
+ ], check=True)
 
 
63
 
64
+ print("βœ… ComfyUI installed")
65
+ return True
 
 
66
 
 
 
 
67
 
68
+ # ══════════════════════════════════════════════════════════
69
+ # STEP 2: INSTALL COMFYUI-GGUF NODE
70
+ # ══════════════════════════════════════════════════════════
71
 
72
+ def install_gguf_node():
73
+ """Install ComfyUI-GGUF custom node"""
74
+ print("\n" + "="*60)
75
+ print("πŸ”§ Installing ComfyUI-GGUF...")
76
+ print("="*60)
 
 
 
 
 
 
 
77
 
78
+ nodes_dir = COMFYUI_DIR / "custom_nodes"
79
+ gguf_dir = nodes_dir / "ComfyUI-GGUF"
 
 
80
 
81
+ if gguf_dir.exists():
82
+ print("βœ… ComfyUI-GGUF already installed")
83
+ return True
 
 
 
 
 
 
 
 
 
 
 
84
 
85
+ # Clone the node
86
+ subprocess.run([
87
+ "git", "clone",
88
+ "https://github.com/city96/ComfyUI-GGUF.git",
89
+ str(gguf_dir)
90
+ ], check=True)
91
+
92
+ # Install node requirements
93
+ req_file = gguf_dir / "requirements.txt"
94
+ if req_file.exists():
95
+ subprocess.run([
96
+ sys.executable, "-m", "pip", "install", "-r", str(req_file)
97
+ ], check=True)
98
+
99
+ print("βœ… ComfyUI-GGUF installed")
100
+ return True
101
+
102
+
103
+ # ══════════════════════════════════════════════════════════
104
+ # STEP 3: DOWNLOAD MODEL
105
+ # ══════════════════════════════════════════════════════════
106
+
107
+ def download_model():
108
+ """Download the single model file"""
109
+ print("\n" + "="*60)
110
+ print(f"πŸ“₯ Downloading {MODEL_NAME}...")
111
+ print("="*60)
112
+
113
+ MODELS_DIR.mkdir(exist_ok=True)
114
+ COMFY_MODELS_DIR.mkdir(parents=True, exist_ok=True)
115
+
116
+ # Check if already downloaded
117
+ if MODEL_PATH.exists():
118
+ size_mb = MODEL_PATH.stat().st_size / (1024*1024)
119
+ print(f"βœ… Model already exists ({size_mb:.1f} MB)")
120
+
121
+ # Copy to ComfyUI models dir too
122
+ comfy_model_path = COMFY_MODELS_DIR / MODEL_NAME
123
+ if not comfy_model_path.exists():
124
+ import shutil
125
+ shutil.copy2(MODEL_PATH, comfy_model_path)
126
+ print(f"βœ… Copied to ComfyUI directory")
127
+
128
+ return True
129
 
130
+ # Try multiple download methods
131
+ urls_to_try = [
132
+ f"https://huggingface.co/{MODEL_HF_REPO}/resolve/main/{MODEL_HF_FILE}",
133
+ f"https://huggingface.co/{MODEL_HF_REPO}/resolve/main/{MODEL_NAME}",
134
+ ]
135
 
136
+ for url in urls_to_try:
137
+ print(f"\nTrying: {url}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  try:
139
+ urllib.request.urlretrieve(url, MODEL_PATH)
 
140
 
141
+ if MODEL_PATH.exists() and MODEL_PATH.stat().st_size > 1000000: # > 1MB
142
+ size_mb = MODEL_PATH.stat().st_size / (1024*1024)
143
+ print(f"βœ… Downloaded successfully ({size_mb:.1f} MB)")
144
+
145
+ # Copy to ComfyUI
146
+ import shutil
147
+ shutil.copy2(MODEL_PATH, COMFY_MODELS_DIR / MODEL_NAME)
148
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  return True
150
  else:
151
+ print("❌ File too small, trying next source...")
152
+ MODEL_PATH.unlink(missing_ok=True)
 
 
 
 
 
153
 
 
 
 
154
  except Exception as e:
155
+ print(f"❌ Failed: {e}")
 
 
 
 
 
156
 
157
+ print("\n⚠️ Auto-download failed!")
158
+ print(f" Please manually upload {MODEL_NAME} to ./models/")
159
  return False
160
 
161
 
162
+ # ══════════════════════════════════════════════════════════
163
+ # STEP 4: CREATE WORKFLOW
164
+ # ══════════════════════════════════════════════════════════
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
+ def create_workflow(prompt, negative_prompt="", width=1024, height=1024, steps=20, cfg=7.0, seed=-1):
167
+ """Create ComfyUI workflow for GGUF Flux model"""
 
 
 
 
 
 
168
 
169
+ if seed == -1:
170
+ seed = int(datetime.now().timestamp() * 1000) % (2**32)
 
 
 
 
 
 
171
 
172
+ workflow = {
173
+ "1": {
174
+ "class_type": "CLIPTextEncode",
175
+ "inputs": {
176
+ "text": prompt,
177
+ "clip": ["4", 0]
178
+ }
179
+ },
180
+ "2": {
181
+ "class_type": "CLIPTextEncode",
182
+ "inputs": {
183
+ "text": negative_prompt or "blurry, bad quality",
184
+ "clip": ["4", 0]
185
+ }
186
+ },
187
+ "3": {
188
+ "class_type": "VAEDecode",
189
+ "inputs": {
190
+ "samples": ["6", 0],
191
+ "vae": ["5", 0]
192
+ }
193
+ },
194
+ "4": {
195
+ "class_type": "GGUFCLIPLoader",
196
+ "inputs": {
197
+ "clip_name": "t5xxl_fp16.safetensors",
198
+ "type": "l"
199
+ }
200
+ },
201
+ "5": {
202
+ "class_type": "GGUFAutoencoders",
203
+ "inputs": {
204
+ "vae_name": "ae.safetensors"
205
+ }
206
+ },
207
+ "6": {
208
+ "class_type": "KSampler",
209
+ "inputs": {
210
+ "model": ["7", 0],
211
+ "positive": ["1", 0],
212
+ "negative": ["2", 0],
213
+ "latent_image": ["8", 0],
214
+ "seed": seed,
215
+ "steps": steps,
216
+ "cfg": cfg,
217
+ "sampler_name": "euler",
218
+ "scheduler": "normal",
219
+ "denoise": 1.0
220
+ }
221
+ },
222
+ "7": {
223
+ "class_type": "GGUFLoader",
224
+ "inputs": {
225
+ "unet_name": MODEL_NAME,
226
+ "weight_dtype": "fp16_e4m3fn"
227
+ }
228
+ },
229
+ "8": {
230
+ "class_type": "EmptyLatentImage",
231
+ "inputs": {
232
+ "width": width,
233
+ "height": height,
234
+ "batch_size": 1
235
+ }
236
+ },
237
+ "9": {
238
+ "class_type": "SaveImage",
239
+ "inputs": {
240
+ "filename_prefix": "flux_output",
241
+ "images": ["3", 0]
242
+ }
243
+ }
244
+ }
245
 
246
+ return workflow
247
 
248
 
249
+ # ══════════════════════════════════════════════════════════
250
+ # STEP 5: RUN GENERATION
251
+ # ══════════════════════════════════════════════════════════
252
+
253
+ def generate_with_comfyui(workflow):
254
+ """Send workflow to ComfyUI API and get result"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
 
256
+ import json
257
+ import time
258
+ import requests
 
 
259
 
260
+ # Start ComfyUI in background if not running
261
+ print("πŸš€ Starting ComfyUI...")
 
262
 
263
+ process = subprocess.Popen(
264
+ [sys.executable, str(COMFY_PATH), "--listen", "127.0.0.1", "--port", "8188"],
265
+ cwd=str(COMFYUI_DIR),
266
+ stdout=subprocess.PIPE,
267
+ stderr=subprocess.PIPE
 
268
  )
269
 
270
+ # Wait for ComfyUI to start
271
+ max_wait = 30
272
+ for i in range(max_wait):
273
+ try:
274
+ requests.get("http://127.0.0.1:8188/system_stats")
275
+ break
276
+ except:
277
+ time.sleep(1)
 
 
 
 
 
 
 
 
 
 
278
  else:
279
+ process.kill()
280
+ raise Exception("ComfyUI failed to start")
 
 
 
 
 
 
 
 
 
 
281
 
282
+ try:
283
+ # Queue prompt
284
+ response = requests.post(
285
+ "http://127.0.0.1:8188/prompt",
286
+ json={"prompt": workflow}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  )
288
 
289
+ prompt_id = response.json()["prompt_id"]
290
+ print(f"Queued: {prompt_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291
 
292
+ # Wait for completion
293
+ while True:
294
+ time.sleep(1)
295
+ history = requests.get(f"http://127.0.0.1:8188/history/{prompt_id}").json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
 
297
+ if prompt_id in history:
298
+ outputs = history[prompt_id]["outputs"]
299
+ if "9" in outputs: # SaveImage node
300
+ images = outputs["9"]["images"]
301
+ if images:
302
+ img_info = images[0]
303
+ img_path = os.path.join(
304
+ COMFYUI_DIR, "output", img_info["filename"]
305
+ )
306
+
307
+ # Copy to our output dir
308
+ OUTPUTS_DIR.mkdir(exist_ok=True)
309
+ final_path = OUTPUTS_DIR / f"output_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
310
+ import shutil
311
+ shutil.copy2(img_path, final_path)
312
+
313
+ return str(final_path)
314
+ break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
 
316
+ return None
 
 
 
 
 
317
 
318
+ finally:
319
+ process.terminate()
320
+ process.wait()
 
 
 
 
 
 
 
 
 
 
 
321
 
 
 
322
 
323
+ # ══════════════════════════════════════════════════════════
324
+ # GRADIO INTERFACE
325
+ # ══════════════════════════════════════════════════════════
326
 
327
+ import gradio as gr
328
 
329
+ def generate_image(prompt, neg_prompt, width, height, steps, cfg, seed):
330
+ """Main generation function for Gradio"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
 
332
+ if not prompt.strip():
333
+ return None, "❌ Enter a prompt!"
334
 
335
+ if not MODEL_PATH.exists():
336
+ return None, f"❌ Model not found: {MODEL_PATH}"
 
 
 
 
 
 
337
 
338
  try:
339
+ workflow = create_workflow(
340
+ prompt=prompt,
341
+ negative_prompt=neg_prompt,
342
+ width=int(width),
343
+ height=int(height),
344
+ steps=int(steps),
345
+ cfg=float(cfg),
346
+ seed=int(seed) if seed > 0 else -1
 
 
 
 
 
347
  )
348
 
349
+ result = generate_with_comfyui(workflow)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
 
351
+ if result:
352
+ return result, "βœ… Done!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
  else:
354
+ return None, "❌ Generation failed"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  except Exception as e:
357
+ return None, f"❌ Error: {str(e)}"
 
 
 
 
 
 
 
 
 
358
 
359
 
360
+ with gr.Blocks(title="FLUX.2-klein-4B") as demo:
361
+ gr.Markdown("# 🎨 FLUX.2-klein-4B (Q8_0)\n### Powered by ComfyUI-GGUF")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
 
363
+ with gr.Row():
364
+ with gr.Column():
365
+ prompt = gr.Textbox(label="Prompt", lines=3, value="a red fox in snow, detailed, 8k")
366
+ neg = gr.Textbox(label="Negative Prompt", lines=2, value="")
 
367
 
368
+ with gr.Column():
369
+ width = gr.Slider(512, 1024, value=1024, step=64, label="Width")
370
+ height = gr.Slider(512, 1024, value=1024, step=64, label="Height")
371
+ steps = gr.Slider(1, 50, value=20, step=1, label="Steps")
372
+ cfg = gr.Slider(1, 20, value=7.0, step=0.5, label="CFG")
373
+ seed = gr.Number(value=-1, label="Seed (-1=random)")
374
+
375
+ gen = gr.Button("Generate", variant="primary")
376
+ output = gr.Image(label="Output")
377
+ status = gr.Textbox(label="Status")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
 
379
+ gen.click(generate_image, [prompt, neg, width, height, steps, cfg, seed], [output, status])
380
 
381
 
382
+ # ══════════════════════════════════════════════════════════
383
+ # RUN SETUP ON STARTUP
384
+ # ══════════════════════════════════════════════════════════
385
 
386
+ print("\n" + "🎯"*30)
387
+ print("SETTING UP FLUX.2-KLEIN-4B GENERATOR")
388
+ print("🎯"*30)
389
 
390
+ install_comfyui()
391
+ install_gguf_node()
392
+ download_model()
 
 
 
393
 
394
+ print("\nβœ… Setup complete! Launching interface...")
 
395
 
396
  if __name__ == "__main__":
397
+ demo.launch(server_name="0.0.0.0", port=7860)