Commit ·
6ba9d20
1
Parent(s): 61b3a19
still initial commit
Browse files
app.py
CHANGED
|
@@ -3,23 +3,38 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
import modal
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# ── Modal Backend ───────────────────────────────────────────────────────────
|
| 8 |
try:
|
| 9 |
VoxelModelCls = modal.Cls.from_name("flux-klein-voxel-backend", "VoxelModel")
|
| 10 |
voxel_backend = VoxelModelCls().process_frame
|
| 11 |
-
|
| 12 |
except Exception as e:
|
|
|
|
| 13 |
try:
|
| 14 |
voxel_backend = modal.Function.from_name("flux-klein-voxel-backend", "demo_stream_frame")
|
| 15 |
-
|
| 16 |
except Exception as ex:
|
| 17 |
-
|
| 18 |
voxel_backend = None
|
| 19 |
|
| 20 |
status_color = "🟢" if voxel_backend is not None else "🔴"
|
| 21 |
status_text = "Connected" if voxel_backend is not None else "Offline"
|
| 22 |
|
|
|
|
| 23 |
# ── Helpers ─────────────────────────────────────────────────────────────────
|
| 24 |
def _offline_frame(frame: np.ndarray, message: str) -> np.ndarray:
|
| 25 |
"""Generates a styled placeholder frame when backend is offline."""
|
|
@@ -29,6 +44,7 @@ def _offline_frame(frame: np.ndarray, message: str) -> np.ndarray:
|
|
| 29 |
cv2.FONT_HERSHEY_DUPLEX, 0.8, (0, 0, 220), 2)
|
| 30 |
return out
|
| 31 |
|
|
|
|
| 32 |
def _run_voxel_backend(frame: np.ndarray) -> np.ndarray:
|
| 33 |
"""Encodes and ships raw image bytes to the Modal worker."""
|
| 34 |
success, encoded = cv2.imencode(".jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 85])
|
|
@@ -44,20 +60,33 @@ def _run_voxel_backend(frame: np.ndarray) -> np.ndarray:
|
|
| 44 |
cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 1)
|
| 45 |
return out
|
| 46 |
|
|
|
|
| 47 |
# ── Core Stream Handler ─────────────────────────────────────────────────────
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
"""
|
| 50 |
-
Accepts incoming frame from the webcam
|
| 51 |
"""
|
|
|
|
| 52 |
if frame is None:
|
| 53 |
return None
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
if voxel_backend is None:
|
| 56 |
err_frame = _offline_frame(frame, "Modal Backend Offline")
|
| 57 |
if mode == "Streaming Demo":
|
| 58 |
return np.hstack([frame, err_frame])
|
| 59 |
return err_frame
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# Process via the lightweight single-image pipeline
|
| 62 |
processed = _run_voxel_backend(frame)
|
| 63 |
|
|
@@ -77,17 +106,29 @@ def process_video_stream(frame: np.ndarray, mode: str) -> np.ndarray:
|
|
| 77 |
|
| 78 |
return processed
|
| 79 |
|
|
|
|
| 80 |
# ── Custom Gradio Interface Layout ──────────────────────────────────────────
|
| 81 |
with gr.Blocks(title="⛏️ Minecraft Spatial Voxel Filter") as demo:
|
|
|
|
|
|
|
|
|
|
| 82 |
gr.Markdown("# ⛏️ Minecraft Spatial Voxel Filter")
|
| 83 |
|
| 84 |
with gr.Row():
|
| 85 |
-
# Configuration Settings &
|
| 86 |
with gr.Column(scale=1):
|
| 87 |
gr.Markdown(
|
| 88 |
f"### ⚡ Backend Connection Status\n"
|
| 89 |
-
f"Status: {status_color} **{status_text}**
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
mode_dropdown = gr.Dropdown(
|
|
@@ -97,15 +138,23 @@ with gr.Blocks(title="⛏️ Minecraft Spatial Voxel Filter") as demo:
|
|
| 97 |
interactive=True,
|
| 98 |
)
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
with gr.Column(scale=2):
|
| 102 |
input_stream = gr.Image(sources=["webcam"], streaming=True, label="Live Webcam Input")
|
| 103 |
output_stream = gr.Image(interactive=False, label="Voxel Output Viewport")
|
| 104 |
|
| 105 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
input_stream.stream(
|
| 107 |
fn=process_video_stream,
|
| 108 |
-
inputs=[input_stream, mode_dropdown],
|
| 109 |
outputs=[output_stream]
|
| 110 |
)
|
| 111 |
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
import modal
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# ── Logging Setup ───────────────────────────────────────────────────────────
|
| 9 |
+
init_logs = []
|
| 10 |
+
|
| 11 |
+
def log_system_event(message: str):
|
| 12 |
+
"""Formats logs with a timestamp and syncs to stdout and UI."""
|
| 13 |
+
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 14 |
+
formatted_log = f"[{timestamp}] {message}"
|
| 15 |
+
print(formatted_log)
|
| 16 |
+
init_logs.append(formatted_log)
|
| 17 |
+
|
| 18 |
+
# ── Modal Backend Connection ────────────────────────────────────────────────
|
| 19 |
+
log_system_event("Initializing connection to Modal remote infrastructure...")
|
| 20 |
|
|
|
|
| 21 |
try:
|
| 22 |
VoxelModelCls = modal.Cls.from_name("flux-klein-voxel-backend", "VoxelModel")
|
| 23 |
voxel_backend = VoxelModelCls().process_frame
|
| 24 |
+
log_system_event("✅ Success: Bound directly to Modal Class deployment ('VoxelModel').")
|
| 25 |
except Exception as e:
|
| 26 |
+
log_system_event(f"⚠️ Modal Class lookup failed ({e}). Attempting fallback function router...")
|
| 27 |
try:
|
| 28 |
voxel_backend = modal.Function.from_name("flux-klein-voxel-backend", "demo_stream_frame")
|
| 29 |
+
log_system_event("✅ Success: Hooked into fallback standalone Modal Function.")
|
| 30 |
except Exception as ex:
|
| 31 |
+
log_system_event(f"❌ Critical: All remote Modal endpoints are unreachable. Error: {ex}")
|
| 32 |
voxel_backend = None
|
| 33 |
|
| 34 |
status_color = "🟢" if voxel_backend is not None else "🔴"
|
| 35 |
status_text = "Connected" if voxel_backend is not None else "Offline"
|
| 36 |
|
| 37 |
+
|
| 38 |
# ── Helpers ─────────────────────────────────────────────────────────────────
|
| 39 |
def _offline_frame(frame: np.ndarray, message: str) -> np.ndarray:
|
| 40 |
"""Generates a styled placeholder frame when backend is offline."""
|
|
|
|
| 44 |
cv2.FONT_HERSHEY_DUPLEX, 0.8, (0, 0, 220), 2)
|
| 45 |
return out
|
| 46 |
|
| 47 |
+
|
| 48 |
def _run_voxel_backend(frame: np.ndarray) -> np.ndarray:
|
| 49 |
"""Encodes and ships raw image bytes to the Modal worker."""
|
| 50 |
success, encoded = cv2.imencode(".jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 85])
|
|
|
|
| 60 |
cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 1)
|
| 61 |
return out
|
| 62 |
|
| 63 |
+
|
| 64 |
# ── Core Stream Handler ─────────────────────────────────────────────────────
|
| 65 |
+
frame_counter = 0
|
| 66 |
+
|
| 67 |
+
def process_video_stream(frame: np.ndarray, mode: str, is_running: bool) -> np.ndarray:
|
| 68 |
"""
|
| 69 |
+
Accepts incoming frame from the webcam, pipeline settings, and execution state flag.
|
| 70 |
"""
|
| 71 |
+
global frame_counter
|
| 72 |
if frame is None:
|
| 73 |
return None
|
| 74 |
|
| 75 |
+
# If user hasn't toggled "Start Processing", safely pass raw video back as preview
|
| 76 |
+
if not is_running:
|
| 77 |
+
return frame
|
| 78 |
+
|
| 79 |
if voxel_backend is None:
|
| 80 |
err_frame = _offline_frame(frame, "Modal Backend Offline")
|
| 81 |
if mode == "Streaming Demo":
|
| 82 |
return np.hstack([frame, err_frame])
|
| 83 |
return err_frame
|
| 84 |
|
| 85 |
+
# Runtime console indicator: logs transmission health to terminal every 15 frames
|
| 86 |
+
frame_counter += 1
|
| 87 |
+
if frame_counter % 15 == 0:
|
| 88 |
+
print(f"🚀 [LIVE PIPELINE] Actively transmitting frames. Dispatched {frame_counter} payloads to Modal worker.")
|
| 89 |
+
|
| 90 |
# Process via the lightweight single-image pipeline
|
| 91 |
processed = _run_voxel_backend(frame)
|
| 92 |
|
|
|
|
| 106 |
|
| 107 |
return processed
|
| 108 |
|
| 109 |
+
|
| 110 |
# ── Custom Gradio Interface Layout ──────────────────────────────────────────
|
| 111 |
with gr.Blocks(title="⛏️ Minecraft Spatial Voxel Filter") as demo:
|
| 112 |
+
# State tracking engine variable
|
| 113 |
+
is_running = gr.State(value=False)
|
| 114 |
+
|
| 115 |
gr.Markdown("# ⛏️ Minecraft Spatial Voxel Filter")
|
| 116 |
|
| 117 |
with gr.Row():
|
| 118 |
+
# Configuration Settings & Log Panel (Left Column)
|
| 119 |
with gr.Column(scale=1):
|
| 120 |
gr.Markdown(
|
| 121 |
f"### ⚡ Backend Connection Status\n"
|
| 122 |
+
f"Status: {status_color} **{status_text}**"
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Live Diagnostic Log Display View
|
| 126 |
+
ui_logs = gr.Textbox(
|
| 127 |
+
value="\n".join(init_logs),
|
| 128 |
+
label="💻 System Initialization Logs",
|
| 129 |
+
lines=6,
|
| 130 |
+
max_lines=10,
|
| 131 |
+
interactive=False,
|
| 132 |
)
|
| 133 |
|
| 134 |
mode_dropdown = gr.Dropdown(
|
|
|
|
| 138 |
interactive=True,
|
| 139 |
)
|
| 140 |
|
| 141 |
+
with gr.Row():
|
| 142 |
+
start_btn = gr.Button("🚀 Start Processing", variant="primary")
|
| 143 |
+
stop_btn = gr.Button("🛑 Stop", variant="secondary")
|
| 144 |
+
|
| 145 |
+
# Video Capture Viewports (Right Column)
|
| 146 |
with gr.Column(scale=2):
|
| 147 |
input_stream = gr.Image(sources=["webcam"], streaming=True, label="Live Webcam Input")
|
| 148 |
output_stream = gr.Image(interactive=False, label="Voxel Output Viewport")
|
| 149 |
|
| 150 |
+
# Wire up button interface trigger mappings to flip our State flag
|
| 151 |
+
start_btn.click(fn=lambda: True, inputs=None, outputs=is_running)
|
| 152 |
+
stop_btn.click(fn=lambda: False, inputs=None, outputs=is_running)
|
| 153 |
+
|
| 154 |
+
# Core engine transmission loop
|
| 155 |
input_stream.stream(
|
| 156 |
fn=process_video_stream,
|
| 157 |
+
inputs=[input_stream, mode_dropdown, is_running],
|
| 158 |
outputs=[output_stream]
|
| 159 |
)
|
| 160 |
|