AnimeOverlord commited on
Commit
0977f6f
·
1 Parent(s): 178c9ad

still initial commit

Browse files
Files changed (1) hide show
  1. app.py +65 -98
app.py CHANGED
@@ -42,7 +42,6 @@ def process_video_stream(frame: np.ndarray, prompt: str, strength: float) -> np.
42
  frame_bytes = encoded_image.tobytes()
43
 
44
  try:
45
- # Dynamically handle single-parameter or multi-parameter signatures
46
  try:
47
  processed_bytes = voxel_backend.remote(frame_bytes, prompt, strength)
48
  except TypeError:
@@ -52,10 +51,8 @@ def process_video_stream(frame: np.ndarray, prompt: str, strength: float) -> np.
52
  return cv2.imdecode(numpy_buffer, cv2.IMREAD_COLOR)
53
  except Exception as err:
54
  fallback_frame = frame.copy()
55
- cv2.putText(fallback_frame, "Serverless Warm-up / Processing Error...", (20, 40),
56
  cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)
57
- cv2.putText(fallback_frame, str(err)[:40], (20, 70),
58
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
59
  return fallback_frame
60
 
61
 
@@ -67,14 +64,11 @@ def process_demo_stream(frame: np.ndarray) -> np.ndarray:
67
  if frame is None:
68
  return None
69
 
70
- # High-visibility on-screen error rendering for Demo Mode
71
  if voxel_backend is None:
72
  error_frame = np.zeros((480, 640, 3), dtype=np.uint8)
73
- error_frame[:] = 30 # Dark slate gray canvas
74
- cv2.putText(error_frame, "⚠️ BACKEND APP OFFLINE", (40, 200),
75
  cv2.FONT_HERSHEY_DUPLEX, 0.9, (0, 0, 255), 2)
76
- cv2.putText(error_frame, "Status: Inactive or unauthorized Modal keys.", (40, 250),
77
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (180, 180, 180), 1)
78
  return error_frame
79
 
80
  success, encoded_image = cv2.imencode(".jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 85])
@@ -93,108 +87,82 @@ def process_demo_stream(frame: np.ndarray) -> np.ndarray:
93
  return cv2.imdecode(numpy_buffer, cv2.IMREAD_COLOR)
94
 
95
  except Exception as err:
96
- # Format and write runtime cluster exceptions directly onto the video container
97
  error_frame = np.zeros((480, 640, 3), dtype=np.uint8)
98
  error_frame[:] = 20
99
- cv2.putText(error_frame, "MODAL RUNTIME EXCEPTION", (40, 180),
100
  cv2.FONT_HERSHEY_DUPLEX, 0.8, (0, 140, 255), 2)
101
- cv2.putText(error_frame, f"Error trace: {str(err)[:50]}...", (40, 240),
102
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
103
  return error_frame
104
 
105
 
106
- # --- UI LAYOUT & STYLE RULES ---
107
- custom_css = """
108
- #container { max-width: 1200px; margin: 0 auto; padding-top: 20px; }
109
- .header-text { text-align: center; margin-bottom: 25px; }
110
- .header-text h1 { color: #5c8e32; font-family: 'Courier New', Courier, monospace; font-weight: bold; margin-bottom: 5px; }
111
- .header-text p { color: #666; font-size: 1.1em; }
112
-
113
- /* Constrain video bounding tracks to prevent screen-flooding layout shifts */
114
- video, .webrtc-video, div[class*="webrtc"] {
115
- max-height: 440px !important;
116
- width: 100% !important;
117
- max-width: 580px !important;
118
- margin: 0 auto !important;
119
- object-fit: contain !important;
120
- border-radius: 10px;
121
- }
122
- """
123
-
124
- with gr.Blocks(css=custom_css, title="Minecraft Spatial Voxel Filter") as demo:
125
 
126
- with gr.Group(elem_id="container"):
127
- with gr.Group(elem_classes="header-text"):
128
- gr.Markdown("# ⛏️ MINECRAFT SPATIAL VOXEL FILTER ⛏️")
129
- gr.Markdown("Transform your physical environment into an interactive, real-time 3D blocky landscape running on FLUX.2 Klein.")
130
-
131
- gr.HTML("<hr style='border: 1px solid #ddd; margin-bottom: 20px;'>")
 
 
 
 
 
 
 
 
132
 
133
- # Pipeline view selector
134
- mode_dropdown = gr.Dropdown(
135
- choices=["Minecraft Filter", "Streaming Demo"],
136
- value="Minecraft Filter",
137
- label="🎯 Pipeline View Configuration",
138
- interactive=True
139
- )
 
 
 
 
 
 
 
 
 
 
140
 
141
- with gr.Row():
142
- # Left Hand Side Settings Column
143
- with gr.Column(scale=1):
144
- gr.Markdown("### 🎛️ Environmental Filters")
145
-
146
- prompt_input = gr.Textbox(
147
- value="vanilla minecraft voxel landscape, 3d blocky style, retro game cube aesthetic, highly detailed texture pack",
148
- label="Biome Environment Blueprint (Prompt)",
149
- lines=3,
150
- placeholder="Describe your voxel theme..."
151
- )
152
-
153
- with gr.Accordion("Advanced Tuning", open=True):
154
- denoise_strength = gr.Slider(
155
- minimum=0.1,
156
- maximum=1.0,
157
- step=0.05,
158
- value=0.55,
159
- label="Voxelization Denoising Strength"
160
- )
161
-
162
- status_msg = "Connected" if voxel_backend is not None else "Offline / Error"
163
- gr.Markdown(
164
- f"""
165
- > **💡 Pipeline Status:** Deployed via FastRTC. Modal Backend Status: `{status_msg}`.
166
- """
167
  )
168
 
169
- # Right Hand Side Viewport Area
170
- with gr.Column(scale=2):
171
-
172
- # --- VARIANT 1: MINECRAFT FILTER MODE (Bidirectional Single Box) ---
173
- with gr.Column(visible=True) as minecraft_layout:
174
- gr.Markdown("### 📺 Spatial Render Pipeline")
175
- webrtc_single = WebRTC(
176
- label="Live Voxel Viewport",
 
 
 
 
177
  modality="video",
178
- mode="send-receive",
179
  rtc_configuration=get_cloudflare_turn_credentials
180
  )
181
-
182
- # --- VARIANT 2: STREAMING DEMO MODE (Decoupled dual boxes) ---
183
- with gr.Column(visible=False) as demo_layout:
184
- gr.Markdown("### 📺 Dual-Feed Stream Monitor")
185
- with gr.Row():
186
- webrtc_raw = WebRTC(
187
- label="1. Your Camera Feed (Raw Local)",
188
- modality="video",
189
- mode="send",
190
- rtc_configuration=get_cloudflare_turn_credentials
191
- )
192
- webrtc_processed = WebRTC(
193
- label="2. Streamed from Modal (Processed Backend)",
194
- modality="video",
195
- mode="receive",
196
- rtc_configuration=get_cloudflare_turn_credentials
197
- )
198
 
199
  # --- ROUTING LOGIC & EVENT WIRES ---
200
 
@@ -210,7 +178,7 @@ with gr.Blocks(css=custom_css, title="Minecraft Spatial Voxel Filter") as demo:
210
  outputs=[minecraft_layout, demo_layout]
211
  )
212
 
213
- # Wire up single component bidirectional streaming pipeline
214
  webrtc_single.stream(
215
  fn=process_video_stream,
216
  inputs=[webrtc_single, prompt_input, denoise_strength],
@@ -218,7 +186,6 @@ with gr.Blocks(css=custom_css, title="Minecraft Spatial Voxel Filter") as demo:
218
  time_limit=150
219
  )
220
 
221
- # Wire up cross-component dual tracking streaming pipeline
222
  webrtc_raw.stream(
223
  fn=process_demo_stream,
224
  inputs=[webrtc_raw],
 
42
  frame_bytes = encoded_image.tobytes()
43
 
44
  try:
 
45
  try:
46
  processed_bytes = voxel_backend.remote(frame_bytes, prompt, strength)
47
  except TypeError:
 
51
  return cv2.imdecode(numpy_buffer, cv2.IMREAD_COLOR)
52
  except Exception as err:
53
  fallback_frame = frame.copy()
54
+ cv2.putText(fallback_frame, "Serverless Processing...", (20, 40),
55
  cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)
 
 
56
  return fallback_frame
57
 
58
 
 
64
  if frame is None:
65
  return None
66
 
 
67
  if voxel_backend is None:
68
  error_frame = np.zeros((480, 640, 3), dtype=np.uint8)
69
+ error_frame[:] = 30
70
+ cv2.putText(error_frame, "BACKEND APP OFFLINE", (40, 200),
71
  cv2.FONT_HERSHEY_DUPLEX, 0.9, (0, 0, 255), 2)
 
 
72
  return error_frame
73
 
74
  success, encoded_image = cv2.imencode(".jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 85])
 
87
  return cv2.imdecode(numpy_buffer, cv2.IMREAD_COLOR)
88
 
89
  except Exception as err:
 
90
  error_frame = np.zeros((480, 640, 3), dtype=np.uint8)
91
  error_frame[:] = 20
92
+ cv2.putText(error_frame, "MODAL RUNTIME EXCEPTION", (40, 180),
93
  cv2.FONT_HERSHEY_DUPLEX, 0.8, (0, 140, 255), 2)
94
+ cv2.putText(error_frame, f"Error: {str(err)[:40]}...", (40, 240),
95
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
96
  return error_frame
97
 
98
 
99
+ # --- PURE NATIVE GRADIO UI LAYOUT ---
100
+
101
+ with gr.Blocks(title="Minecraft Spatial Voxel Filter") as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
+ # Header Section
104
+ gr.Markdown("<h1 style='text-align: center;'>⛏️ MINECRAFT SPATIAL VOXEL FILTER ⛏️</h1>")
105
+ gr.Markdown("<p style='text-align: center;'>Transform your physical environment into an interactive, real-time 3D blocky landscape.</p>")
106
+
107
+ # Global Controls
108
+ mode_dropdown = gr.Dropdown(
109
+ choices=["Minecraft Filter", "Streaming Demo"],
110
+ value="Minecraft Filter",
111
+ label="🎯 Pipeline View Configuration",
112
+ interactive=True
113
+ )
114
+
115
+ # Main Application Area (Row keeps things side-by-side cleanly)
116
+ with gr.Row():
117
 
118
+ # Left Column: Settings
119
+ with gr.Column(scale=1):
120
+ gr.Markdown("### 🎛️ Environmental Filters")
121
+
122
+ prompt_input = gr.Textbox(
123
+ value="vanilla minecraft voxel landscape, 3d blocky style, retro game cube aesthetic",
124
+ label="Biome Environment Blueprint (Prompt)",
125
+ lines=3
126
+ )
127
+
128
+ denoise_strength = gr.Slider(
129
+ minimum=0.1, maximum=1.0, step=0.05, value=0.55,
130
+ label="Voxelization Denoising Strength"
131
+ )
132
+
133
+ status_msg = "Connected" if voxel_backend is not None else "Offline / Error"
134
+ gr.Markdown(f"**Pipeline Status:** Deployed via FastRTC. Modal Backend: `{status_msg}`.")
135
 
136
+ # Right Column: Video Viewports
137
+ with gr.Column(scale=2):
138
+
139
+ # Variant 1: Minecraft Filter
140
+ with gr.Group(visible=True) as minecraft_layout:
141
+ gr.Markdown("### 📺 Spatial Render Pipeline")
142
+ # The WebRTC component natively provides Start/Stop buttons in its UI
143
+ webrtc_single = WebRTC(
144
+ label="Live Voxel Viewport",
145
+ modality="video",
146
+ mode="send-receive",
147
+ rtc_configuration=get_cloudflare_turn_credentials
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  )
149
 
150
+ # Variant 2: Streaming Demo
151
+ with gr.Group(visible=False) as demo_layout:
152
+ gr.Markdown("### 📺 Dual-Feed Stream Monitor")
153
+ with gr.Row():
154
+ webrtc_raw = WebRTC(
155
+ label="Raw Local Camera",
156
+ modality="video",
157
+ mode="send",
158
+ rtc_configuration=get_cloudflare_turn_credentials
159
+ )
160
+ webrtc_processed = WebRTC(
161
+ label="Processed Backend Output",
162
  modality="video",
163
+ mode="receive",
164
  rtc_configuration=get_cloudflare_turn_credentials
165
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  # --- ROUTING LOGIC & EVENT WIRES ---
168
 
 
178
  outputs=[minecraft_layout, demo_layout]
179
  )
180
 
181
+ # Stream bindings
182
  webrtc_single.stream(
183
  fn=process_video_stream,
184
  inputs=[webrtc_single, prompt_input, denoise_strength],
 
186
  time_limit=150
187
  )
188
 
 
189
  webrtc_raw.stream(
190
  fn=process_demo_stream,
191
  inputs=[webrtc_raw],