Commit ·
be3210f
1
Parent(s): 94f0003
still initial commit
Browse files
app.py
CHANGED
|
@@ -22,6 +22,25 @@ status_color = "🟢" if voxel_backend is not None else "🔴"
|
|
| 22 |
status_text = "Connected" if voxel_backend is not None else "Offline"
|
| 23 |
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# ── Helpers ─────────────────────────────────────────────────────────────────
|
| 26 |
def _offline_frame(frame: np.ndarray, message: str) -> np.ndarray:
|
| 27 |
"""Generates a styled placeholder frame when backend is offline."""
|
|
@@ -38,7 +57,6 @@ def _run_voxel_backend(frame: np.ndarray) -> np.ndarray:
|
|
| 38 |
if not success:
|
| 39 |
return frame
|
| 40 |
try:
|
| 41 |
-
# Prompt and strength arguments have been fully stripped here
|
| 42 |
processed_bytes = voxel_backend.remote(encoded.tobytes())
|
| 43 |
result = cv2.imdecode(np.frombuffer(processed_bytes, dtype=np.uint8), cv2.IMREAD_COLOR)
|
| 44 |
return result if result is not None else frame
|
|
@@ -50,10 +68,10 @@ def _run_voxel_backend(frame: np.ndarray) -> np.ndarray:
|
|
| 50 |
|
| 51 |
|
| 52 |
# ── Unified Core Stream Handler ─────────────────────────────────────────────
|
| 53 |
-
def process_video_stream(frame: np.ndarray, mode: str) -> np.ndarray:
|
| 54 |
"""
|
| 55 |
Unified real-time handler mapping directly to FastRTC's stream pipeline.
|
| 56 |
-
Accepts incoming frame
|
| 57 |
"""
|
| 58 |
if frame is None:
|
| 59 |
return None
|
|
@@ -89,7 +107,7 @@ stream = Stream(
|
|
| 89 |
handler=process_video_stream,
|
| 90 |
modality="video",
|
| 91 |
mode="send-receive",
|
| 92 |
-
rtc_configuration=
|
| 93 |
additional_inputs=[
|
| 94 |
gr.Dropdown(
|
| 95 |
choices=["Minecraft Filter", "Streaming Demo"],
|
|
|
|
| 22 |
status_text = "Connected" if voxel_backend is not None else "Offline"
|
| 23 |
|
| 24 |
|
| 25 |
+
# ── WebRTC Configuration Fallback ───────────────────────────────────────────
|
| 26 |
+
def get_safe_rtc_configuration():
|
| 27 |
+
"""
|
| 28 |
+
Prevents startup crashes if Cloudflare or HF tokens are missing from the environment.
|
| 29 |
+
Falls back gracefully to standard public Google STUN routing if name resolution drops out.
|
| 30 |
+
"""
|
| 31 |
+
has_hf = bool(os.environ.get("HF_TOKEN"))
|
| 32 |
+
has_cf = bool(os.environ.get("CLOUDFLARE_TURN_KEY_ID") and os.environ.get("CLOUDFLARE_TURN_KEY_API_TOKEN"))
|
| 33 |
+
|
| 34 |
+
if has_hf or has_cf:
|
| 35 |
+
try:
|
| 36 |
+
return get_cloudflare_turn_credentials()
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"⚠️ Failed to fetch Cloudflare TURN credentials: {e}")
|
| 39 |
+
|
| 40 |
+
print("ℹ️ Missing credentials for Cloudflare TURN. Falling back to public Google STUN server.")
|
| 41 |
+
return {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
# ── Helpers ─────────────────────────────────────────────────────────────────
|
| 45 |
def _offline_frame(frame: np.ndarray, message: str) -> np.ndarray:
|
| 46 |
"""Generates a styled placeholder frame when backend is offline."""
|
|
|
|
| 57 |
if not success:
|
| 58 |
return frame
|
| 59 |
try:
|
|
|
|
| 60 |
processed_bytes = voxel_backend.remote(encoded.tobytes())
|
| 61 |
result = cv2.imdecode(np.frombuffer(processed_bytes, dtype=np.uint8), cv2.IMREAD_COLOR)
|
| 62 |
return result if result is not None else frame
|
|
|
|
| 68 |
|
| 69 |
|
| 70 |
# ── Unified Core Stream Handler ─────────────────────────────────────────────
|
| 71 |
+
def process_video_stream(frame: np.ndarray, mode: str, _status=None) -> np.ndarray:
|
| 72 |
"""
|
| 73 |
Unified real-time handler mapping directly to FastRTC's stream pipeline.
|
| 74 |
+
Accepts incoming frame, mode dropdown, and a placeholder for the Markdown element.
|
| 75 |
"""
|
| 76 |
if frame is None:
|
| 77 |
return None
|
|
|
|
| 107 |
handler=process_video_stream,
|
| 108 |
modality="video",
|
| 109 |
mode="send-receive",
|
| 110 |
+
rtc_configuration=get_safe_rtc_configuration(),
|
| 111 |
additional_inputs=[
|
| 112 |
gr.Dropdown(
|
| 113 |
choices=["Minecraft Filter", "Streaming Demo"],
|