Commit ·
3a269bf
1
Parent(s): 1c0a58f
still initial commit
Browse files- app.py +3 -1
- backend/backend.py +13 -12
app.py
CHANGED
|
@@ -189,7 +189,9 @@ with gr.Blocks(title="⛏️ Minecraft Spatial Voxel Filter") as demo:
|
|
| 189 |
input_stream.stream(
|
| 190 |
fn=process_video_stream,
|
| 191 |
inputs=[input_stream, mode_dropdown, is_running, prompt_input, strength_slider],
|
| 192 |
-
outputs=[output_stream]
|
|
|
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
if __name__ == "__main__":
|
|
|
|
| 189 |
input_stream.stream(
|
| 190 |
fn=process_video_stream,
|
| 191 |
inputs=[input_stream, mode_dropdown, is_running, prompt_input, strength_slider],
|
| 192 |
+
outputs=[output_stream],
|
| 193 |
+
trigger_mode="always_last", # Drops intermediate backlog frames when backend is busy
|
| 194 |
+
concurrency_limit=1 # Ensures only one frame flies over the network at a time
|
| 195 |
)
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
backend/backend.py
CHANGED
|
@@ -27,10 +27,11 @@ image = (
|
|
| 27 |
app = modal.App("flux-klein-voxel-backend", image=image)
|
| 28 |
|
| 29 |
# ==============================================================================
|
| 30 |
-
# 🏎️ 1. THE DEMO PIPELINE
|
| 31 |
# ==============================================================================
|
| 32 |
@app.function()
|
| 33 |
-
def demo_stream_frame(img_bytes: bytes) -> bytes:
|
|
|
|
| 34 |
from PIL import Image, ImageDraw
|
| 35 |
|
| 36 |
input_image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
@@ -65,29 +66,29 @@ class VoxelModel:
|
|
| 65 |
self.pipe = AutoPipelineForImage2Image.from_pretrained(
|
| 66 |
model_id,
|
| 67 |
torch_dtype=torch.bfloat16,
|
| 68 |
-
|
| 69 |
)
|
| 70 |
self.pipe.to("cuda")
|
| 71 |
self.pipe.enable_attention_slicing()
|
| 72 |
|
| 73 |
# ---------------------------------------------------------
|
| 74 |
-
# ✨ COMPILE AND OPTIMIZE THE
|
| 75 |
# ---------------------------------------------------------
|
| 76 |
-
print("Initializing torch.compile
|
| 77 |
|
| 78 |
-
# Channels-last
|
| 79 |
-
self.pipe.
|
| 80 |
|
| 81 |
-
# Compile the heaviest part of the FLUX architecture
|
| 82 |
self.pipe.transformer = torch.compile(
|
| 83 |
self.pipe.transformer,
|
| 84 |
-
mode="reduce-overhead",
|
| 85 |
fullgraph=False
|
| 86 |
)
|
| 87 |
|
| 88 |
-
# WARMUP RUN:
|
| 89 |
-
# If the cache is empty (first run
|
| 90 |
-
# If the cache exists
|
| 91 |
print("Running warmup to build/load inductor cache...")
|
| 92 |
dummy_image = Image.new("RGB", (512, 512), (0, 0, 0))
|
| 93 |
with torch.inference_mode():
|
|
|
|
| 27 |
app = modal.App("flux-klein-voxel-backend", image=image)
|
| 28 |
|
| 29 |
# ==============================================================================
|
| 30 |
+
# 🏎️ 1. THE DEMO PIPELINE (FALLBACK ROUTE)
|
| 31 |
# ==============================================================================
|
| 32 |
@app.function()
|
| 33 |
+
def demo_stream_frame(img_bytes: bytes, prompt: str, strength: float) -> bytes:
|
| 34 |
+
"""Fallback route structurally aligned to match the frontend signature."""
|
| 35 |
from PIL import Image, ImageDraw
|
| 36 |
|
| 37 |
input_image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
|
|
| 66 |
self.pipe = AutoPipelineForImage2Image.from_pretrained(
|
| 67 |
model_id,
|
| 68 |
torch_dtype=torch.bfloat16,
|
| 69 |
+
token=os.environ["HF_TOKEN"] # Modernized from use_auth_token
|
| 70 |
)
|
| 71 |
self.pipe.to("cuda")
|
| 72 |
self.pipe.enable_attention_slicing()
|
| 73 |
|
| 74 |
# ---------------------------------------------------------
|
| 75 |
+
# ✨ COMPILE AND OPTIMIZE THE INFRASTRUCTURE
|
| 76 |
# ---------------------------------------------------------
|
| 77 |
+
print("Initializing torch.compile optimization loops...")
|
| 78 |
|
| 79 |
+
# Channels-last optimization applied exclusively to the VAE (CNN-based)
|
| 80 |
+
self.pipe.vae.to(memory_format=torch.channels_last)
|
| 81 |
|
| 82 |
+
# Compile the heaviest part of the FLUX architecture safely without memory layout issues
|
| 83 |
self.pipe.transformer = torch.compile(
|
| 84 |
self.pipe.transformer,
|
| 85 |
+
mode="reduce-overhead", # Trades a bit of compile time for faster inference
|
| 86 |
fullgraph=False
|
| 87 |
)
|
| 88 |
|
| 89 |
+
# WARMUP RUN: Force a dummy inference immediately execution starts.
|
| 90 |
+
# If the cache is empty (first run), this traces the graph and saves to the Modal Volume.
|
| 91 |
+
# If the cache exists, it instantly hotloads from the Volume.
|
| 92 |
print("Running warmup to build/load inductor cache...")
|
| 93 |
dummy_image = Image.new("RGB", (512, 512), (0, 0, 0))
|
| 94 |
with torch.inference_mode():
|