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|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from io import BytesIO
|
| 3 |
+
import modal
|
| 4 |
+
from huggingface_hub import login
|
| 5 |
+
from fastapi import FastAPI, HTTPException
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
import base64
|
| 8 |
+
import sys
|
| 9 |
+
import requests
|
| 10 |
+
import os
|
| 11 |
+
from safetensors.torch import load_file
|
| 12 |
+
|
| 13 |
+
# Modal setup (same as your original)
|
| 14 |
+
cuda_version = "12.4.0"
|
| 15 |
+
flavor = "devel"
|
| 16 |
+
operating_sys = "ubuntu22.04"
|
| 17 |
+
tag = f"{cuda_version}-{flavor}-{operating_sys}"
|
| 18 |
+
cuda_dev_image = modal.Image.from_registry(
|
| 19 |
+
f"nvidia/cuda:{tag}", add_python="3.11"
|
| 20 |
+
).entrypoint([])
|
| 21 |
+
|
| 22 |
+
diffusers_commit_sha = "81cf3b2f155f1de322079af28f625349ee21ec6b"
|
| 23 |
+
|
| 24 |
+
flux_image = (
|
| 25 |
+
cuda_dev_image.apt_install(
|
| 26 |
+
"git",
|
| 27 |
+
"libglib2.0-0",
|
| 28 |
+
"libsm6",
|
| 29 |
+
"libxrender1",
|
| 30 |
+
"libxext6",
|
| 31 |
+
"ffmpeg",
|
| 32 |
+
"libgl1",
|
| 33 |
+
)
|
| 34 |
+
.pip_install(
|
| 35 |
+
"invisible_watermark==0.2.0",
|
| 36 |
+
"peft==0.10.0",
|
| 37 |
+
"transformers==4.44.0",
|
| 38 |
+
"huggingface_hub[hf_transfer]==0.26.2",
|
| 39 |
+
"accelerate==0.33.0",
|
| 40 |
+
"safetensors==0.4.4",
|
| 41 |
+
"sentencepiece==0.2.0",
|
| 42 |
+
"torch==2.5.0",
|
| 43 |
+
f"git+https://github.com/huggingface/diffusers.git@{diffusers_commit_sha}",
|
| 44 |
+
"numpy<2",
|
| 45 |
+
"fastapi==0.104.1",
|
| 46 |
+
"uvicorn==0.24.0",
|
| 47 |
+
)
|
| 48 |
+
.env({"HF_HUB_ENABLE_HF_TRANSFER": "1", "HF_HUB_CACHE": "/cache"})
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
flux_image = flux_image.env(
|
| 52 |
+
{
|
| 53 |
+
"TORCHINDUCTOR_CACHE_DIR": "/root/.inductor-cache",
|
| 54 |
+
"TORCHINDUCTOR_FX_GRAPH_CACHE": "1",
|
| 55 |
+
}
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
app = modal.App("flux-api-server", image=flux_image, secrets=[modal.Secret.from_name("huggingface-token")])
|
| 59 |
+
|
| 60 |
+
with flux_image.imports():
|
| 61 |
+
import torch
|
| 62 |
+
from diffusers import FluxPipeline
|
| 63 |
+
|
| 64 |
+
MINUTES = 60 # seconds
|
| 65 |
+
VARIANT = "dev"
|
| 66 |
+
NUM_INFERENCE_STEPS = 50
|
| 67 |
+
|
| 68 |
+
class ImageRequest(BaseModel):
|
| 69 |
+
prompt: str
|
| 70 |
+
num_inference_steps: int = 50
|
| 71 |
+
width: int = 1024 # Add width parameter
|
| 72 |
+
height: int = 1024 # Add height parameter
|
| 73 |
+
|
| 74 |
+
class ImageResponse(BaseModel):
|
| 75 |
+
image_base64: str
|
| 76 |
+
generation_time: float
|
| 77 |
+
|
| 78 |
+
@app.cls(
|
| 79 |
+
gpu="H200",
|
| 80 |
+
scaledown_window=20 * MINUTES,
|
| 81 |
+
timeout=60 * MINUTES,
|
| 82 |
+
volumes={
|
| 83 |
+
"/cache": modal.Volume.from_name("hf-hub-cache", create_if_missing=True),
|
| 84 |
+
"/root/.nv": modal.Volume.from_name("nv-cache", create_if_missing=True),
|
| 85 |
+
"/root/.triton": modal.Volume.from_name("triton-cache", create_if_missing=True),
|
| 86 |
+
"/root/.inductor-cache": modal.Volume.from_name(
|
| 87 |
+
"inductor-cache", create_if_missing=True
|
| 88 |
+
),
|
| 89 |
+
},
|
| 90 |
+
)
|
| 91 |
+
class Model:
|
| 92 |
+
compile: bool = modal.parameter(default=False)
|
| 93 |
+
|
| 94 |
+
lora_loaded = False
|
| 95 |
+
lora_path = "/cache/flux.1_lora_flyway_doodle-poster.safetensors"
|
| 96 |
+
lora_url = "https://huggingface.co/RajputVansh/SG161222-DISTILLED-IITI-VANSH-RUHELA/resolve/main/flux.1_lora_flyway_doodle-poster.safetensors?download=true"
|
| 97 |
+
|
| 98 |
+
def download_lora_from_url(self, url, save_path):
|
| 99 |
+
"""Download LoRA with proper error handling"""
|
| 100 |
+
try:
|
| 101 |
+
print(f"π₯ Downloading LoRA from {url}")
|
| 102 |
+
response = requests.get(url, timeout=300) # 5 minute timeout
|
| 103 |
+
response.raise_for_status() # Raise exception for bad status codes
|
| 104 |
+
|
| 105 |
+
with open(save_path, "wb") as f:
|
| 106 |
+
f.write(response.content)
|
| 107 |
+
|
| 108 |
+
print(f"β
LoRA downloaded successfully to {save_path}")
|
| 109 |
+
print(f"π File size: {len(response.content)} bytes")
|
| 110 |
+
return True
|
| 111 |
+
except Exception as e:
|
| 112 |
+
print(f"β LoRA download failed: {str(e)}")
|
| 113 |
+
return False
|
| 114 |
+
|
| 115 |
+
def verify_lora_file(self, lora_path):
|
| 116 |
+
"""Verify that the LoRA file is valid"""
|
| 117 |
+
try:
|
| 118 |
+
if not os.path.exists(lora_path):
|
| 119 |
+
return False, "File does not exist"
|
| 120 |
+
|
| 121 |
+
file_size = os.path.getsize(lora_path)
|
| 122 |
+
if file_size == 0:
|
| 123 |
+
return False, "File is empty"
|
| 124 |
+
|
| 125 |
+
# Try to load the file to verify it's valid
|
| 126 |
+
try:
|
| 127 |
+
load_file(lora_path)
|
| 128 |
+
return True, f"Valid LoRA file ({file_size} bytes)"
|
| 129 |
+
except Exception as e:
|
| 130 |
+
return False, f"Invalid LoRA file: {str(e)}"
|
| 131 |
+
|
| 132 |
+
except Exception as e:
|
| 133 |
+
return False, f"Error verifying file: {str(e)}"
|
| 134 |
+
|
| 135 |
+
@modal.enter()
|
| 136 |
+
def enter(self):
|
| 137 |
+
from huggingface_hub import login
|
| 138 |
+
import os
|
| 139 |
+
|
| 140 |
+
# Login to HuggingFace
|
| 141 |
+
token = os.environ["huggingface_token"]
|
| 142 |
+
login(token)
|
| 143 |
+
|
| 144 |
+
# Download and verify LoRA
|
| 145 |
+
if not os.path.exists(self.lora_path):
|
| 146 |
+
print("π₯ LoRA not found, downloading...")
|
| 147 |
+
download_success = self.download_lora_from_url(self.lora_url, self.lora_path)
|
| 148 |
+
if not download_success:
|
| 149 |
+
print("β Failed to download LoRA, continuing without it")
|
| 150 |
+
self.lora_loaded = False
|
| 151 |
+
else:
|
| 152 |
+
print("π LoRA file found in cache")
|
| 153 |
+
|
| 154 |
+
# Verify LoRA file
|
| 155 |
+
is_valid, message = self.verify_lora_file(self.lora_path)
|
| 156 |
+
print(f"π LoRA verification: {message}")
|
| 157 |
+
|
| 158 |
+
# Load the base model
|
| 159 |
+
from diffusers import FluxPipeline
|
| 160 |
+
import torch
|
| 161 |
+
|
| 162 |
+
print("π Loading Flux model...")
|
| 163 |
+
pipe = FluxPipeline.from_pretrained(
|
| 164 |
+
"black-forest-labs/FLUX.1-dev",
|
| 165 |
+
torch_dtype=torch.bfloat16
|
| 166 |
+
).to("cuda")
|
| 167 |
+
|
| 168 |
+
# Load LoRA if available and valid
|
| 169 |
+
if is_valid:
|
| 170 |
+
try:
|
| 171 |
+
print(f"π Loading LoRA from {self.lora_path}")
|
| 172 |
+
pipe.load_lora_weights(self.lora_path)
|
| 173 |
+
print("β
LoRA successfully loaded!")
|
| 174 |
+
self.lora_loaded = True
|
| 175 |
+
|
| 176 |
+
# Test LoRA by checking if it affects the model
|
| 177 |
+
print("π§ͺ Testing LoRA integration...")
|
| 178 |
+
# You could add a simple test generation here if needed
|
| 179 |
+
|
| 180 |
+
except Exception as e:
|
| 181 |
+
print(f"β LoRA loading failed: {str(e)}")
|
| 182 |
+
self.lora_loaded = False
|
| 183 |
+
else:
|
| 184 |
+
print("β οΈ LoRA not loaded due to verification failure")
|
| 185 |
+
self.lora_loaded = False
|
| 186 |
+
|
| 187 |
+
# Optimize the pipeline
|
| 188 |
+
self.pipe = optimize(pipe, compile=self.compile)
|
| 189 |
+
|
| 190 |
+
print(f"π― Model ready! LoRA status: {'β
Loaded' if self.lora_loaded else 'β Not loaded'}")
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
@modal.method()
|
| 194 |
+
def get_model_status(self) -> dict:
|
| 195 |
+
"""Get detailed model and LoRA status"""
|
| 196 |
+
lora_file_info = {}
|
| 197 |
+
if os.path.exists(self.lora_path):
|
| 198 |
+
try:
|
| 199 |
+
file_size = os.path.getsize(self.lora_path)
|
| 200 |
+
lora_file_info = {
|
| 201 |
+
"exists": True,
|
| 202 |
+
"size_bytes": file_size,
|
| 203 |
+
"size_mb": round(file_size / (1024 * 1024), 2)
|
| 204 |
+
}
|
| 205 |
+
except:
|
| 206 |
+
lora_file_info = {"exists": False}
|
| 207 |
+
else:
|
| 208 |
+
lora_file_info = {"exists": False}
|
| 209 |
+
|
| 210 |
+
return {
|
| 211 |
+
"status": "ready",
|
| 212 |
+
"lora_loaded": self.lora_loaded,
|
| 213 |
+
"lora_path": self.lora_path,
|
| 214 |
+
"model_info": {
|
| 215 |
+
"base_model": "black-forest-labs/FLUX.1-dev",
|
| 216 |
+
"lora_file": lora_file_info,
|
| 217 |
+
"lora_url": self.lora_url
|
| 218 |
+
}
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
@modal.method()
|
| 222 |
+
def inference(self, prompt: str, num_inference_steps: int = 50, width: int = 1024, height: int = 1024) -> dict:
|
| 223 |
+
# Clean and prepare the prompt
|
| 224 |
+
final_prompt = prompt
|
| 225 |
+
|
| 226 |
+
print(f"π¨ Generating image:")
|
| 227 |
+
print(f" Original prompt: {prompt}")
|
| 228 |
+
print(f" Final prompt: {final_prompt}")
|
| 229 |
+
print(f" Dimensions: {width}x{height}")
|
| 230 |
+
print(f" LoRA status: {'β
Active' if self.lora_loaded else 'β Inactive'}")
|
| 231 |
+
|
| 232 |
+
start_time = time.time()
|
| 233 |
+
|
| 234 |
+
out = self.pipe(
|
| 235 |
+
final_prompt,
|
| 236 |
+
output_type="pil",
|
| 237 |
+
num_inference_steps=num_inference_steps,
|
| 238 |
+
width=width,
|
| 239 |
+
height=height,
|
| 240 |
+
max_sequence_length=512
|
| 241 |
+
).images[0]
|
| 242 |
+
|
| 243 |
+
# Convert to base64
|
| 244 |
+
byte_stream = BytesIO()
|
| 245 |
+
out.save(byte_stream, format="PNG")
|
| 246 |
+
image_bytes = byte_stream.getvalue()
|
| 247 |
+
image_base64 = base64.b64encode(image_bytes).decode('utf-8')
|
| 248 |
+
|
| 249 |
+
generation_time = time.time() - start_time
|
| 250 |
+
print(f"β
Generated image in {generation_time:.2f} seconds")
|
| 251 |
+
|
| 252 |
+
return {
|
| 253 |
+
"image_base64": image_base64,
|
| 254 |
+
"generation_time": generation_time,
|
| 255 |
+
"final_prompt": final_prompt,
|
| 256 |
+
"lora_used": self.lora_loaded
|
| 257 |
+
}
|
| 258 |
+
# FastAPI server
|
| 259 |
+
fastapi_app = FastAPI(title="Flux Image Generation API")
|
| 260 |
+
|
| 261 |
+
# Initialize model instance
|
| 262 |
+
model_instance = Model(compile=False)
|
| 263 |
+
|
| 264 |
+
@fastapi_app.post("/generate", response_model=ImageResponse)
|
| 265 |
+
async def generate_image(request: ImageRequest):
|
| 266 |
+
try:
|
| 267 |
+
print(f"Received request: {request.prompt} at {request.width}x{request.height}")
|
| 268 |
+
result = model_instance.inference.remote(
|
| 269 |
+
request.prompt,
|
| 270 |
+
request.num_inference_steps,
|
| 271 |
+
request.width,
|
| 272 |
+
request.height
|
| 273 |
+
)
|
| 274 |
+
return ImageResponse(**result)
|
| 275 |
+
except Exception as e:
|
| 276 |
+
print(f"Error generating image: {str(e)}")
|
| 277 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 278 |
+
|
| 279 |
+
@fastapi_app.get("/health")
|
| 280 |
+
async def health_check():
|
| 281 |
+
return {"status": "healthy", "message": "Flux API server is running"}
|
| 282 |
+
|
| 283 |
+
@app.function(
|
| 284 |
+
image=flux_image.pip_install("fastapi", "uvicorn"),
|
| 285 |
+
keep_warm=1,
|
| 286 |
+
timeout=60 * MINUTES,
|
| 287 |
+
)
|
| 288 |
+
@modal.asgi_app()
|
| 289 |
+
def fastapi_server():
|
| 290 |
+
return fastapi_app
|
| 291 |
+
|
| 292 |
+
def optimize(pipe, compile=True):
|
| 293 |
+
# fuse QKV projections in Transformer and VAE
|
| 294 |
+
pipe.transformer.fuse_qkv_projections()
|
| 295 |
+
pipe.vae.fuse_qkv_projections()
|
| 296 |
+
|
| 297 |
+
# switch memory layout to Torch's preferred, channels_last
|
| 298 |
+
pipe.transformer.to(memory_format=torch.channels_last)
|
| 299 |
+
pipe.vae.to(memory_format=torch.channels_last)
|
| 300 |
+
|
| 301 |
+
if not compile:
|
| 302 |
+
return pipe
|
| 303 |
+
|
| 304 |
+
# set torch compile flags
|
| 305 |
+
config = torch._inductor.config
|
| 306 |
+
config.disable_progress = False
|
| 307 |
+
config.conv_1x1_as_mm = True
|
| 308 |
+
config.coordinate_descent_tuning = True
|
| 309 |
+
config.coordinate_descent_check_all_directions = True
|
| 310 |
+
config.epilogue_fusion = False
|
| 311 |
+
|
| 312 |
+
# compile the compute-intensive modules
|
| 313 |
+
pipe.transformer = torch.compile(
|
| 314 |
+
pipe.transformer, mode="max-autotune", fullgraph=True
|
| 315 |
+
)
|
| 316 |
+
pipe.vae.decode = torch.compile(
|
| 317 |
+
pipe.vae.decode, mode="max-autotune", fullgraph=True
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
# trigger torch compilation
|
| 321 |
+
print("π¦ Running torch compilation (may take up to 20 minutes)...")
|
| 322 |
+
pipe(
|
| 323 |
+
"dummy prompt to trigger torch compilation",
|
| 324 |
+
output_type="pil",
|
| 325 |
+
num_inference_steps=NUM_INFERENCE_STEPS,
|
| 326 |
+
).images[0]
|
| 327 |
+
print("π¦ Finished torch compilation")
|
| 328 |
+
|
| 329 |
+
return pipe
|
| 330 |
+
|
| 331 |
+
if __name__ == "__main__":
|
| 332 |
+
print("Starting Modal Flux API server...")
|
| 333 |
+
# This will be handled by Modal's deployment
|