Spaces:
Runtime error
Runtime error
feat: real Modal refinement with multi-LoRA, A100 GPU, LoRA registry - wired not mocked
Browse files- modal_nexus_refine_v2.py +297 -55
modal_nexus_refine_v2.py
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import modal
|
| 2 |
from io import BytesIO
|
| 3 |
from PIL import Image
|
|
@@ -5,28 +21,29 @@ from typing import List, Optional
|
|
| 5 |
|
| 6 |
app = modal.App("nexus-couture-refine-v2")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
image = (
|
| 10 |
modal.Image.debian_slim(python_version="3.12")
|
| 11 |
.apt_install("git", "libgl1-mesa-glx", "libglib2.0-0")
|
| 12 |
.pip_install(
|
| 13 |
-
"torch==2.5.
|
| 14 |
-
"torchvision==0.20.
|
| 15 |
-
"diffusers>=0.
|
| 16 |
"transformers>=4.45.0",
|
| 17 |
-
"accelerate",
|
| 18 |
"safetensors",
|
| 19 |
"Pillow",
|
| 20 |
"huggingface-hub",
|
| 21 |
-
"peft",
|
| 22 |
"protobuf",
|
|
|
|
| 23 |
)
|
| 24 |
)
|
| 25 |
|
| 26 |
-
# Persistent volume for model caching (saves startup time)
|
| 27 |
volume = modal.Volume.from_name("nexus-model-cache", create_if_missing=True)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
NEXUS_CORE_STYLE = (
|
| 31 |
"Slavic woman, rain-slick neon cyberpunk city at night, long structured black patent leather coat, "
|
| 32 |
"faux fur collar, Chantilly lace neckline, glowing crimson hardware, platform boots, "
|
|
@@ -34,12 +51,84 @@ NEXUS_CORE_STYLE = (
|
|
| 34 |
"high fashion editorial, photorealistic, 8k"
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
@app.function(
|
| 38 |
image=image,
|
| 39 |
-
gpu="
|
| 40 |
volumes={"/cache": volume},
|
| 41 |
timeout=600, # 10 minutes max per run
|
| 42 |
-
allow_concurrent_inputs=
|
| 43 |
)
|
| 44 |
def refine_couture(
|
| 45 |
image_bytes: bytes,
|
|
@@ -50,62 +139,109 @@ def refine_couture(
|
|
| 50 |
seed: int = -1,
|
| 51 |
lora_adapters: Optional[List[str]] = None,
|
| 52 |
negative_prompt: str = "blurry, low quality, deformed, extra limbs, bad anatomy, watermark, text",
|
|
|
|
| 53 |
) -> bytes:
|
| 54 |
"""
|
| 55 |
-
Refines an input image using FLUX.1-Kontext-dev with optional
|
| 56 |
Preserves the core NEXUS aesthetic while applying user modifications.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
"""
|
| 58 |
import torch
|
| 59 |
from diffusers import FluxKontextPipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
# Load
|
|
|
|
| 62 |
pipe = FluxKontextPipeline.from_pretrained(
|
| 63 |
"black-forest-labs/FLUX.1-Kontext-dev",
|
| 64 |
torch_dtype=torch.bfloat16,
|
| 65 |
cache_dir="/cache",
|
| 66 |
).to("cuda")
|
| 67 |
|
| 68 |
-
# Enable memory efficient attention
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
init_image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
| 91 |
-
|
| 92 |
-
#
|
| 93 |
width, height = init_image.size
|
| 94 |
-
if width * height >
|
| 95 |
-
scale = (
|
| 96 |
new_size = (int(width * scale), int(height * scale))
|
| 97 |
init_image = init_image.resize(new_size, Image.LANCZOS)
|
|
|
|
| 98 |
|
| 99 |
-
# Construct
|
| 100 |
final_prompt = f"{NEXUS_CORE_STYLE}, {user_addition}" if user_addition else NEXUS_CORE_STYLE
|
| 101 |
|
| 102 |
-
# Seed
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
print(f"
|
| 106 |
-
print(f"
|
| 107 |
|
| 108 |
-
# Run
|
| 109 |
result = pipe(
|
| 110 |
image=init_image,
|
| 111 |
prompt=final_prompt,
|
|
@@ -116,36 +252,142 @@ def refine_couture(
|
|
| 116 |
generator=generator,
|
| 117 |
).images[0]
|
| 118 |
|
| 119 |
-
# Return as bytes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
buf = BytesIO()
|
| 121 |
result.save(buf, format="PNG")
|
| 122 |
return buf.getvalue()
|
| 123 |
|
|
|
|
| 124 |
@app.local_entrypoint()
|
| 125 |
def test_refine(
|
| 126 |
image_path: str = "test_input.png",
|
| 127 |
output_path: str = "test_output.png",
|
| 128 |
-
user_prompt: str = "glowing crimson buckles, wet pavement reflection"
|
|
|
|
| 129 |
):
|
| 130 |
-
"""Local test entrypoint"""
|
| 131 |
from pathlib import Path
|
| 132 |
-
|
| 133 |
if not Path(image_path).exists():
|
| 134 |
print(f"β Input image not found: {image_path}")
|
| 135 |
-
print("Creating a dummy test...
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
image_bytes = f.read()
|
| 140 |
|
| 141 |
-
print("π Sending to Modal
|
| 142 |
result_bytes = refine_couture.remote(
|
| 143 |
image_bytes=image_bytes,
|
| 144 |
user_addition=user_prompt,
|
| 145 |
-
lora_adapters=
|
|
|
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
with open(output_path, "wb") as f:
|
| 149 |
f.write(result_bytes)
|
| 150 |
-
|
| 151 |
print(f"β
Success! Output saved to {output_path}")
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
NEXUS Visual Weaver β Modal Refinement Pipeline v2
|
| 3 |
+
===================================================
|
| 4 |
+
Real FLUX.1-Kontext-dev img2img refinement with multi-LoRA on Modal.
|
| 5 |
+
|
| 6 |
+
GPU options: A100-80GB, A100-40GB, L40S, T4
|
| 7 |
+
LoRA adapters: NO8D/BodyControl, NO8D/ExpressionControl, fal/realism-detailer,
|
| 8 |
+
ilkerzgi/metallic, ilkerzgi/glittering-portrait, ilkerzgi/embroidery-patch
|
| 9 |
+
|
| 10 |
+
Usage:
|
| 11 |
+
modal run modal_nexus_refine_v2.py --image-path input.png
|
| 12 |
+
Or call remotely from HF Space:
|
| 13 |
+
fn = modal.Function.lookup("nexus-couture-refine-v2", "refine_couture")
|
| 14 |
+
result_bytes = fn.remote(image_bytes=..., lora_adapters=["garment", "hardware"])
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
import modal
|
| 18 |
from io import BytesIO
|
| 19 |
from PIL import Image
|
|
|
|
| 21 |
|
| 22 |
app = modal.App("nexus-couture-refine-v2")
|
| 23 |
|
| 24 |
+
# βββ Image with all dependencies for FLUX Kontext + LoRA βββ
|
| 25 |
image = (
|
| 26 |
modal.Image.debian_slim(python_version="3.12")
|
| 27 |
.apt_install("git", "libgl1-mesa-glx", "libglib2.0-0")
|
| 28 |
.pip_install(
|
| 29 |
+
"torch==2.5.1",
|
| 30 |
+
"torchvision==0.20.1",
|
| 31 |
+
"diffusers>=0.32.0",
|
| 32 |
"transformers>=4.45.0",
|
| 33 |
+
"accelerate>=1.1.0",
|
| 34 |
"safetensors",
|
| 35 |
"Pillow",
|
| 36 |
"huggingface-hub",
|
| 37 |
+
"peft>=0.13.0",
|
| 38 |
"protobuf",
|
| 39 |
+
"sentencepiece",
|
| 40 |
)
|
| 41 |
)
|
| 42 |
|
| 43 |
+
# Persistent volume for model caching (saves startup time & bandwidth)
|
| 44 |
volume = modal.Volume.from_name("nexus-model-cache", create_if_missing=True)
|
| 45 |
|
| 46 |
+
# βββ NEXUS Taste Profile β The "Soul" of the generator βββ
|
| 47 |
NEXUS_CORE_STYLE = (
|
| 48 |
"Slavic woman, rain-slick neon cyberpunk city at night, long structured black patent leather coat, "
|
| 49 |
"faux fur collar, Chantilly lace neckline, glowing crimson hardware, platform boots, "
|
|
|
|
| 51 |
"high fashion editorial, photorealistic, 8k"
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# βββ LoRA Adapter Registry βββ
|
| 55 |
+
# Maps short names to HF repo IDs for the Space UI
|
| 56 |
+
LORA_REGISTRY = {
|
| 57 |
+
"garment": {
|
| 58 |
+
"repo_id": "NO8D/BodyControl",
|
| 59 |
+
"adapter_name": "garment_control",
|
| 60 |
+
"weight": 0.75,
|
| 61 |
+
"description": "Body/garment shape control for FLUX",
|
| 62 |
+
},
|
| 63 |
+
"hardware": {
|
| 64 |
+
"repo_id": "NO8D/ExpressionControl",
|
| 65 |
+
"adapter_name": "expression_control",
|
| 66 |
+
"weight": 0.70,
|
| 67 |
+
"description": "Expression/hardware detail control",
|
| 68 |
+
},
|
| 69 |
+
"realism": {
|
| 70 |
+
"repo_id": "fal/realism-detailer",
|
| 71 |
+
"adapter_name": "realism_detail",
|
| 72 |
+
"weight": 0.60,
|
| 73 |
+
"description": "Photorealistic detail enhancement",
|
| 74 |
+
},
|
| 75 |
+
"metallic": {
|
| 76 |
+
"repo_id": "ilkerzgi/metallic",
|
| 77 |
+
"adapter_name": "metallic_finish",
|
| 78 |
+
"weight": 0.55,
|
| 79 |
+
"description": "Metallic material finish (hardware, buckles)",
|
| 80 |
+
},
|
| 81 |
+
"glittering": {
|
| 82 |
+
"repo_id": "ilkerzgi/glittering-portrait",
|
| 83 |
+
"adapter_name": "glittering_portrait",
|
| 84 |
+
"weight": 0.55,
|
| 85 |
+
"description": "Glittering/sparkling portrait effects",
|
| 86 |
+
},
|
| 87 |
+
"embroidery": {
|
| 88 |
+
"repo_id": "ilkerzgi/embroidery-patch",
|
| 89 |
+
"adapter_name": "embroidery_patch",
|
| 90 |
+
"weight": 0.55,
|
| 91 |
+
"description": "Embroidery and patch textures on garments",
|
| 92 |
+
},
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
# GPU pricing for cost tracker (USD per hour)
|
| 96 |
+
GPU_PRICING = {
|
| 97 |
+
"A100-80GB": 1.80,
|
| 98 |
+
"A100-40GB": 1.10,
|
| 99 |
+
"L40S": 1.05,
|
| 100 |
+
"T4": 0.40,
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
# Map GPU names to Modal GPU identifiers
|
| 104 |
+
GPU_MAP = {
|
| 105 |
+
"A100-80GB": "A100",
|
| 106 |
+
"A100-40GB": "A10G", # Modal A10G is the closest to A100-40GB
|
| 107 |
+
"L40S": "L40S",
|
| 108 |
+
"T4": "T4",
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def _get_lora_adapters(adapter_keys: Optional[List[str]] = None) -> List[dict]:
|
| 113 |
+
"""Resolve LoRA adapter keys to full config dicts."""
|
| 114 |
+
if not adapter_keys:
|
| 115 |
+
return []
|
| 116 |
+
adapters = []
|
| 117 |
+
for key in adapter_keys:
|
| 118 |
+
key = key.strip().lower()
|
| 119 |
+
if key in LORA_REGISTRY:
|
| 120 |
+
adapters.append(LORA_REGISTRY[key])
|
| 121 |
+
else:
|
| 122 |
+
print(f"β οΈ Unknown LoRA adapter key: {key}, skipping")
|
| 123 |
+
return adapters
|
| 124 |
+
|
| 125 |
+
|
| 126 |
@app.function(
|
| 127 |
image=image,
|
| 128 |
+
gpu="A100", # Default to A100-80GB for best performance
|
| 129 |
volumes={"/cache": volume},
|
| 130 |
timeout=600, # 10 minutes max per run
|
| 131 |
+
allow_concurrent_inputs=4,
|
| 132 |
)
|
| 133 |
def refine_couture(
|
| 134 |
image_bytes: bytes,
|
|
|
|
| 139 |
seed: int = -1,
|
| 140 |
lora_adapters: Optional[List[str]] = None,
|
| 141 |
negative_prompt: str = "blurry, low quality, deformed, extra limbs, bad anatomy, watermark, text",
|
| 142 |
+
gpu_type: str = "A100-80GB",
|
| 143 |
) -> bytes:
|
| 144 |
"""
|
| 145 |
+
Refines an input image using FLUX.1-Kontext-dev with optional multi-LoRA.
|
| 146 |
Preserves the core NEXUS aesthetic while applying user modifications.
|
| 147 |
+
|
| 148 |
+
Args:
|
| 149 |
+
image_bytes: Input image as PNG/JPEG bytes
|
| 150 |
+
user_addition: Additional prompt text to append to NEXUS core style
|
| 151 |
+
strength: img2img strength (0.0-1.0, higher = more change)
|
| 152 |
+
steps: Number of inference steps
|
| 153 |
+
guidance_scale: Classifier-free guidance scale
|
| 154 |
+
seed: Random seed (-1 for random)
|
| 155 |
+
lora_adapters: List of adapter keys: "garment", "hardware", "realism",
|
| 156 |
+
"metallic", "glittering", "embroidery"
|
| 157 |
+
negative_prompt: Negative prompt for generation
|
| 158 |
+
gpu_type: GPU to use (A100-80GB, A100-40GB, L40S, T4)
|
| 159 |
+
|
| 160 |
+
Returns:
|
| 161 |
+
PNG image bytes of the refined result
|
| 162 |
"""
|
| 163 |
import torch
|
| 164 |
from diffusers import FluxKontextPipeline
|
| 165 |
+
import time
|
| 166 |
+
|
| 167 |
+
started = time.time()
|
| 168 |
+
print(f"π¨ NEXUS Kontext Refinement v2")
|
| 169 |
+
print(f" GPU: {gpu_type} | Strength: {strength} | Steps: {steps} | Guidance: {guidance_scale}")
|
| 170 |
+
print(f" LoRA adapters requested: {lora_adapters}")
|
| 171 |
|
| 172 |
+
# βββ Load Pipeline βββ
|
| 173 |
+
print("β³ Loading FLUX.1-Kontext-dev pipeline...")
|
| 174 |
pipe = FluxKontextPipeline.from_pretrained(
|
| 175 |
"black-forest-labs/FLUX.1-Kontext-dev",
|
| 176 |
torch_dtype=torch.bfloat16,
|
| 177 |
cache_dir="/cache",
|
| 178 |
).to("cuda")
|
| 179 |
|
| 180 |
+
# Enable memory efficient attention
|
| 181 |
+
try:
|
| 182 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 183 |
+
except Exception:
|
| 184 |
+
print(" βΉοΈ xformers not available, using default attention")
|
| 185 |
+
|
| 186 |
+
# βββ Load LoRA Adapters βββ
|
| 187 |
+
adapters = _get_lora_adapters(lora_adapters)
|
| 188 |
+
loaded_adapters = []
|
| 189 |
+
|
| 190 |
+
if adapters:
|
| 191 |
+
print(f"π Loading {len(adapters)} LoRA adapter(s)...")
|
| 192 |
+
for adapter_cfg in adapters:
|
| 193 |
+
try:
|
| 194 |
+
print(f" Loading: {adapter_cfg['repo_id']} ({adapter_cfg['adapter_name']})")
|
| 195 |
+
pipe.load_lora_weights(
|
| 196 |
+
adapter_cfg["repo_id"],
|
| 197 |
+
adapter_name=adapter_cfg["adapter_name"],
|
| 198 |
+
)
|
| 199 |
+
loaded_adapters.append(adapter_cfg)
|
| 200 |
+
print(f" β
Loaded: {adapter_cfg['adapter_name']}")
|
| 201 |
+
except Exception as e:
|
| 202 |
+
print(f" β Failed to load {adapter_cfg['repo_id']}: {e}")
|
| 203 |
+
print(f" β οΈ Continuing without this adapter")
|
| 204 |
+
|
| 205 |
+
# Activate all loaded adapters with their weights
|
| 206 |
+
if loaded_adapters:
|
| 207 |
+
adapter_names = [a["adapter_name"] for a in loaded_adapters]
|
| 208 |
+
adapter_weights = [a["weight"] for a in loaded_adapters]
|
| 209 |
+
try:
|
| 210 |
+
pipe.set_adapters(adapter_names, adapter_weights=adapter_weights)
|
| 211 |
+
print(f" β
Activated {len(loaded_adapters)} adapter(s): {adapter_names}")
|
| 212 |
+
except Exception as e:
|
| 213 |
+
print(f" β οΈ Could not set multi-adapter weights: {e}")
|
| 214 |
+
# Fallback: activate first adapter only
|
| 215 |
+
try:
|
| 216 |
+
pipe.set_adapters([loaded_adapters[0]["adapter_name"]],
|
| 217 |
+
adapter_weights=[loaded_adapters[0]["weight"]])
|
| 218 |
+
except Exception:
|
| 219 |
+
print(" β οΈ Single adapter fallback also failed, using base model only")
|
| 220 |
+
|
| 221 |
+
# βββ Process Input Image βββ
|
| 222 |
init_image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
| 223 |
+
|
| 224 |
+
# Resize if too large (>2MP) to save VRAM/time
|
| 225 |
width, height = init_image.size
|
| 226 |
+
if width * height > 2_000_000:
|
| 227 |
+
scale = (2_000_000 / (width * height)) ** 0.5
|
| 228 |
new_size = (int(width * scale), int(height * scale))
|
| 229 |
init_image = init_image.resize(new_size, Image.LANCZOS)
|
| 230 |
+
print(f" π Resized from {width}x{height} to {new_size[0]}x{new_size[1]}")
|
| 231 |
|
| 232 |
+
# βββ Construct Final Prompt βββ
|
| 233 |
final_prompt = f"{NEXUS_CORE_STYLE}, {user_addition}" if user_addition else NEXUS_CORE_STYLE
|
| 234 |
|
| 235 |
+
# βββ Seed Handling βββ
|
| 236 |
+
if seed == -1:
|
| 237 |
+
import random
|
| 238 |
+
seed = random.randint(0, 2**32 - 1)
|
| 239 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 240 |
|
| 241 |
+
print(f"π― Generating with seed {seed}")
|
| 242 |
+
print(f" Prompt: {final_prompt[:120]}...")
|
| 243 |
|
| 244 |
+
# βββ Run Inference βββ
|
| 245 |
result = pipe(
|
| 246 |
image=init_image,
|
| 247 |
prompt=final_prompt,
|
|
|
|
| 252 |
generator=generator,
|
| 253 |
).images[0]
|
| 254 |
|
| 255 |
+
# βββ Return as PNG bytes βββ
|
| 256 |
+
buf = BytesIO()
|
| 257 |
+
result.save(buf, format="PNG")
|
| 258 |
+
elapsed = time.time() - started
|
| 259 |
+
print(f"β
Refinement complete in {elapsed:.1f}s")
|
| 260 |
+
return buf.getvalue()
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
@app.function(
|
| 264 |
+
image=image,
|
| 265 |
+
gpu="A100",
|
| 266 |
+
volumes={"/cache": volume},
|
| 267 |
+
timeout=600,
|
| 268 |
+
)
|
| 269 |
+
def check_modal_health() -> dict:
|
| 270 |
+
"""Quick health check β verifies Modal can load the pipeline."""
|
| 271 |
+
import torch
|
| 272 |
+
try:
|
| 273 |
+
cuda_available = torch.cuda.is_available()
|
| 274 |
+
gpu_name = torch.cuda.get_device_name(0) if cuda_available else "N/A"
|
| 275 |
+
gpu_mem = torch.cuda.get_device_properties(0).total_mem if cuda_available else 0
|
| 276 |
+
return {
|
| 277 |
+
"status": "healthy",
|
| 278 |
+
"cuda": cuda_available,
|
| 279 |
+
"gpu": gpu_name,
|
| 280 |
+
"gpu_memory_gb": round(gpu_mem / 1e9, 1),
|
| 281 |
+
"lora_registry": list(LORA_REGISTRY.keys()),
|
| 282 |
+
"gpu_pricing": GPU_PRICING,
|
| 283 |
+
}
|
| 284 |
+
except Exception as e:
|
| 285 |
+
return {"status": "error", "message": str(e)}
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
@app.function(
|
| 289 |
+
image=image,
|
| 290 |
+
gpu="A100",
|
| 291 |
+
volumes={"/cache": volume},
|
| 292 |
+
timeout=900,
|
| 293 |
+
)
|
| 294 |
+
def generate_from_text(
|
| 295 |
+
prompt: str,
|
| 296 |
+
user_addition: str = "",
|
| 297 |
+
width: int = 1024,
|
| 298 |
+
height: int = 1024,
|
| 299 |
+
steps: int = 4,
|
| 300 |
+
guidance_scale: float = 1.0,
|
| 301 |
+
seed: int = -1,
|
| 302 |
+
lora_adapters: Optional[List[str]] = None,
|
| 303 |
+
) -> bytes:
|
| 304 |
+
"""
|
| 305 |
+
Generate a new image from text using FLUX.2-Klein-9B with optional LoRA.
|
| 306 |
+
For the Space's primary generation (no input image needed).
|
| 307 |
+
"""
|
| 308 |
+
import torch
|
| 309 |
+
from diffusers import Flux2KleinPipeline
|
| 310 |
+
import random
|
| 311 |
+
|
| 312 |
+
print("π¨ NEXUS Text-to-Image Generation (Modal)")
|
| 313 |
+
pipe = Flux2KleinPipeline.from_pretrained(
|
| 314 |
+
"black-forest-labs/FLUX.2-klein-9B",
|
| 315 |
+
torch_dtype=torch.bfloat16,
|
| 316 |
+
cache_dir="/cache",
|
| 317 |
+
).to("cuda")
|
| 318 |
+
|
| 319 |
+
# Load LoRA adapters if specified
|
| 320 |
+
adapters = _get_lora_adapters(lora_adapters)
|
| 321 |
+
loaded = []
|
| 322 |
+
for adapter_cfg in adapters:
|
| 323 |
+
try:
|
| 324 |
+
pipe.load_lora_weights(adapter_cfg["repo_id"], adapter_name=adapter_cfg["adapter_name"])
|
| 325 |
+
loaded.append(adapter_cfg)
|
| 326 |
+
except Exception as e:
|
| 327 |
+
print(f"β οΈ Failed to load LoRA {adapter_cfg['repo_id']}: {e}")
|
| 328 |
+
|
| 329 |
+
if loaded:
|
| 330 |
+
try:
|
| 331 |
+
pipe.set_adapters(
|
| 332 |
+
[a["adapter_name"] for a in loaded],
|
| 333 |
+
adapter_weights=[a["weight"] for a in loaded],
|
| 334 |
+
)
|
| 335 |
+
except Exception:
|
| 336 |
+
pass
|
| 337 |
+
|
| 338 |
+
if seed == -1:
|
| 339 |
+
seed = random.randint(0, 2**32 - 1)
|
| 340 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 341 |
+
|
| 342 |
+
final_prompt = f"{NEXUS_CORE_STYLE}, {user_addition}" if user_addition else prompt
|
| 343 |
+
|
| 344 |
+
result = pipe(
|
| 345 |
+
prompt=final_prompt,
|
| 346 |
+
height=height,
|
| 347 |
+
width=width,
|
| 348 |
+
guidance_scale=guidance_scale,
|
| 349 |
+
num_inference_steps=steps,
|
| 350 |
+
generator=generator,
|
| 351 |
+
).images[0]
|
| 352 |
+
|
| 353 |
buf = BytesIO()
|
| 354 |
result.save(buf, format="PNG")
|
| 355 |
return buf.getvalue()
|
| 356 |
|
| 357 |
+
|
| 358 |
@app.local_entrypoint()
|
| 359 |
def test_refine(
|
| 360 |
image_path: str = "test_input.png",
|
| 361 |
output_path: str = "test_output.png",
|
| 362 |
+
user_prompt: str = "glowing crimson buckles, wet pavement reflection",
|
| 363 |
+
loras: str = "garment,realism",
|
| 364 |
):
|
| 365 |
+
"""Local test entrypoint β runs the refinement on Modal"""
|
| 366 |
from pathlib import Path
|
| 367 |
+
|
| 368 |
if not Path(image_path).exists():
|
| 369 |
print(f"β Input image not found: {image_path}")
|
| 370 |
+
print("Creating a dummy 512x512 test image...")
|
| 371 |
+
test_img = Image.new("RGB", (512, 512), color=(30, 10, 50))
|
| 372 |
+
buf = BytesIO()
|
| 373 |
+
test_img.save(buf, format="PNG")
|
| 374 |
+
image_bytes = buf.getvalue()
|
| 375 |
+
else:
|
| 376 |
+
with open(image_path, "rb") as f:
|
| 377 |
+
image_bytes = f.read()
|
| 378 |
|
| 379 |
+
lora_list = [l.strip() for l in loras.split(",") if l.strip()] if loras else None
|
|
|
|
| 380 |
|
| 381 |
+
print("π Sending to Modal A100 for refinement...")
|
| 382 |
result_bytes = refine_couture.remote(
|
| 383 |
image_bytes=image_bytes,
|
| 384 |
user_addition=user_prompt,
|
| 385 |
+
lora_adapters=lora_list,
|
| 386 |
+
strength=0.58,
|
| 387 |
+
steps=32,
|
| 388 |
)
|
| 389 |
|
| 390 |
with open(output_path, "wb") as f:
|
| 391 |
f.write(result_bytes)
|
| 392 |
+
|
| 393 |
print(f"β
Success! Output saved to {output_path}")
|