Commit ·
7f377fc
1
Parent(s): 6594660
still initial commit
Browse files
app.py
CHANGED
|
@@ -29,8 +29,12 @@ def get_modal_backend():
|
|
| 29 |
# ── Core Backend Execution ──────────────────────────────────────────────────
|
| 30 |
def run_modal_backend(frame: np.ndarray) -> np.ndarray:
|
| 31 |
"""Compresses the frame, sends it to Modal, and decodes the returned bytes."""
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return frame
|
| 35 |
|
| 36 |
# Compress to JPEG to save network bandwidth
|
|
@@ -39,7 +43,7 @@ def run_modal_backend(frame: np.ndarray) -> np.ndarray:
|
|
| 39 |
return frame
|
| 40 |
|
| 41 |
try:
|
| 42 |
-
# Fire bytes to Modal serverless container
|
| 43 |
processed_bytes = backend.remote(encoded.tobytes())
|
| 44 |
# Decode the returning bytes back into an OpenCV image
|
| 45 |
result = cv2.imdecode(np.frombuffer(processed_bytes, dtype=np.uint8), cv2.IMREAD_COLOR)
|
|
@@ -52,28 +56,26 @@ def run_modal_backend(frame: np.ndarray) -> np.ndarray:
|
|
| 52 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
| 53 |
return err_frame
|
| 54 |
|
| 55 |
-
|
| 56 |
# ── Activation & Pre-Warming Logic ──────────────────────────────────────────
|
| 57 |
def start_and_warmup_container():
|
| 58 |
"""Forces the Modal container to start up before enabling the webcam stream."""
|
| 59 |
print("🚀 [START CLICKED] Waking up Modal container to prevent cold-start lag...")
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
return True
|
| 75 |
|
| 76 |
-
|
| 77 |
# ── Streaming Logic ─────────────────────────────────────────────────────────
|
| 78 |
def process_video_stream(frame: np.ndarray, mode: str, is_running: bool) -> np.ndarray:
|
| 79 |
"""Handles the webcam feed and respects the Start/Stop toggle."""
|
|
|
|
| 29 |
# ── Core Backend Execution ──────────────────────────────────────────────────
|
| 30 |
def run_modal_backend(frame: np.ndarray) -> np.ndarray:
|
| 31 |
"""Compresses the frame, sends it to Modal, and decodes the returned bytes."""
|
| 32 |
+
# Look up dynamically to prevent cross-thread event loop pollution
|
| 33 |
+
try:
|
| 34 |
+
VoxelModelCls = modal.Cls.from_name("flux-klein-voxel-backend", "VoxelModel")
|
| 35 |
+
backend = VoxelModelCls().process_frame
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"❌ Failed to resolve Modal class: {e}")
|
| 38 |
return frame
|
| 39 |
|
| 40 |
# Compress to JPEG to save network bandwidth
|
|
|
|
| 43 |
return frame
|
| 44 |
|
| 45 |
try:
|
| 46 |
+
# Fire bytes to Modal serverless container synchronously
|
| 47 |
processed_bytes = backend.remote(encoded.tobytes())
|
| 48 |
# Decode the returning bytes back into an OpenCV image
|
| 49 |
result = cv2.imdecode(np.frombuffer(processed_bytes, dtype=np.uint8), cv2.IMREAD_COLOR)
|
|
|
|
| 56 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
| 57 |
return err_frame
|
| 58 |
|
|
|
|
| 59 |
# ── Activation & Pre-Warming Logic ──────────────────────────────────────────
|
| 60 |
def start_and_warmup_container():
|
| 61 |
"""Forces the Modal container to start up before enabling the webcam stream."""
|
| 62 |
print("🚀 [START CLICKED] Waking up Modal container to prevent cold-start lag...")
|
| 63 |
+
try:
|
| 64 |
+
VoxelModelCls = modal.Cls.from_name("flux-klein-voxel-backend", "VoxelModel")
|
| 65 |
+
backend = VoxelModelCls().process_frame
|
| 66 |
+
|
| 67 |
+
# Create a tiny 1x1 blank image payload
|
| 68 |
+
dummy_frame = np.zeros((1, 1, 3), dtype=np.uint8)
|
| 69 |
+
success, encoded = cv2.imencode(".jpg", dummy_frame)
|
| 70 |
+
if success:
|
| 71 |
+
print("⏳ Sending ignition payload to remote container...")
|
| 72 |
+
backend.remote(encoded.tobytes())
|
| 73 |
+
print("✅ [CONTAINER READY] Modal container is hot and ready for frames.")
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"ℹ️ [CONTAINER NOTIFICATION] Warmup pipeline updated: {e}")
|
| 76 |
+
|
| 77 |
return True
|
| 78 |
|
|
|
|
| 79 |
# ── Streaming Logic ─────────────────────────────────────────────────────────
|
| 80 |
def process_video_stream(frame: np.ndarray, mode: str, is_running: bool) -> np.ndarray:
|
| 81 |
"""Handles the webcam feed and respects the Start/Stop toggle."""
|