Pass HF token to FLUX loader
Browse files
src/nexus_visual_weaver/hf_runtime.py
CHANGED
|
@@ -23,6 +23,7 @@ class HFGenerationResult:
|
|
| 23 |
width: int = 1024
|
| 24 |
height: int = 1024
|
| 25 |
steps: int = 4
|
|
|
|
| 26 |
|
| 27 |
def to_dict(self) -> dict[str, Any]:
|
| 28 |
return asdict(self)
|
|
@@ -49,6 +50,10 @@ def _short_error(exc: BaseException) -> str:
|
|
| 49 |
return f"{exc.__class__.__name__}: {text}"
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def generate_flux_image(prompt: str, *, seed: int = 0, width: int = 1024, height: int = 1024, steps: int = 4) -> HFGenerationResult:
|
| 53 |
if not hf_runtime_enabled():
|
| 54 |
return HFGenerationResult(
|
|
@@ -59,6 +64,7 @@ def generate_flux_image(prompt: str, *, seed: int = 0, width: int = 1024, height
|
|
| 59 |
width=width,
|
| 60 |
height=height,
|
| 61 |
steps=steps,
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
started = time.perf_counter()
|
|
@@ -74,6 +80,7 @@ def generate_flux_image(prompt: str, *, seed: int = 0, width: int = 1024, height
|
|
| 74 |
width=width,
|
| 75 |
height=height,
|
| 76 |
steps=steps,
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
if not torch.cuda.is_available():
|
|
@@ -85,11 +92,13 @@ def generate_flux_image(prompt: str, *, seed: int = 0, width: int = 1024, height
|
|
| 85 |
width=width,
|
| 86 |
height=height,
|
| 87 |
steps=steps,
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
try:
|
| 91 |
dtype = torch.bfloat16
|
| 92 |
-
|
|
|
|
| 93 |
pipe.enable_model_cpu_offload()
|
| 94 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 95 |
image = pipe(
|
|
@@ -112,6 +121,7 @@ def generate_flux_image(prompt: str, *, seed: int = 0, width: int = 1024, height
|
|
| 112 |
width=width,
|
| 113 |
height=height,
|
| 114 |
steps=steps,
|
|
|
|
| 115 |
)
|
| 116 |
except Exception as exc: # pragma: no cover - exercised on HF Space with gated/runtime conditions.
|
| 117 |
return HFGenerationResult(
|
|
@@ -123,4 +133,5 @@ def generate_flux_image(prompt: str, *, seed: int = 0, width: int = 1024, height
|
|
| 123 |
width=width,
|
| 124 |
height=height,
|
| 125 |
steps=steps,
|
|
|
|
| 126 |
)
|
|
|
|
| 23 |
width: int = 1024
|
| 24 |
height: int = 1024
|
| 25 |
steps: int = 4
|
| 26 |
+
hf_token_present: bool = False
|
| 27 |
|
| 28 |
def to_dict(self) -> dict[str, Any]:
|
| 29 |
return asdict(self)
|
|
|
|
| 50 |
return f"{exc.__class__.__name__}: {text}"
|
| 51 |
|
| 52 |
|
| 53 |
+
def _hf_token() -> str | None:
|
| 54 |
+
return os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
def generate_flux_image(prompt: str, *, seed: int = 0, width: int = 1024, height: int = 1024, steps: int = 4) -> HFGenerationResult:
|
| 58 |
if not hf_runtime_enabled():
|
| 59 |
return HFGenerationResult(
|
|
|
|
| 64 |
width=width,
|
| 65 |
height=height,
|
| 66 |
steps=steps,
|
| 67 |
+
hf_token_present=bool(_hf_token()),
|
| 68 |
)
|
| 69 |
|
| 70 |
started = time.perf_counter()
|
|
|
|
| 80 |
width=width,
|
| 81 |
height=height,
|
| 82 |
steps=steps,
|
| 83 |
+
hf_token_present=bool(_hf_token()),
|
| 84 |
)
|
| 85 |
|
| 86 |
if not torch.cuda.is_available():
|
|
|
|
| 92 |
width=width,
|
| 93 |
height=height,
|
| 94 |
steps=steps,
|
| 95 |
+
hf_token_present=bool(_hf_token()),
|
| 96 |
)
|
| 97 |
|
| 98 |
try:
|
| 99 |
dtype = torch.bfloat16
|
| 100 |
+
token = _hf_token()
|
| 101 |
+
pipe = Flux2KleinPipeline.from_pretrained(FLUX_REPO_ID, torch_dtype=dtype, token=token)
|
| 102 |
pipe.enable_model_cpu_offload()
|
| 103 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 104 |
image = pipe(
|
|
|
|
| 121 |
width=width,
|
| 122 |
height=height,
|
| 123 |
steps=steps,
|
| 124 |
+
hf_token_present=bool(token),
|
| 125 |
)
|
| 126 |
except Exception as exc: # pragma: no cover - exercised on HF Space with gated/runtime conditions.
|
| 127 |
return HFGenerationResult(
|
|
|
|
| 133 |
width=width,
|
| 134 |
height=height,
|
| 135 |
steps=steps,
|
| 136 |
+
hf_token_present=bool(_hf_token()),
|
| 137 |
)
|