AnimeOverlord commited on
Commit
61b3a19
·
1 Parent(s): 99ea5fd

still initial commit

Browse files
Files changed (1) hide show
  1. app.py +10 -41
app.py CHANGED
@@ -3,7 +3,6 @@ import cv2
3
  import numpy as np
4
  import gradio as gr
5
  import modal
6
- from fastrtc import WebRTC, get_cloudflare_turn_credentials
7
 
8
  # ── Modal Backend ───────────────────────────────────────────────────────────
9
  try:
@@ -21,26 +20,6 @@ except Exception as e:
21
  status_color = "🟢" if voxel_backend is not None else "🔴"
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."""
@@ -50,9 +29,8 @@ def _offline_frame(frame: np.ndarray, message: str) -> np.ndarray:
50
  cv2.FONT_HERSHEY_DUPLEX, 0.8, (0, 0, 220), 2)
51
  return out
52
 
53
-
54
  def _run_voxel_backend(frame: np.ndarray) -> np.ndarray:
55
- """Encodes and ships raw image bytes to the Modal worker without extra parameters."""
56
  success, encoded = cv2.imencode(".jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 85])
57
  if not success:
58
  return frame
@@ -66,12 +44,10 @@ def _run_voxel_backend(frame: np.ndarray) -> np.ndarray:
66
  cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 1)
67
  return out
68
 
69
-
70
  # ── Core Stream Handler ─────────────────────────────────────────────────────
71
  def process_video_stream(frame: np.ndarray, mode: str) -> np.ndarray:
72
  """
73
- Unified real-time handler mapping directly to FastRTC's stream pipeline.
74
- Accepts incoming frame from the WebRTC component and the mode dropdown selection.
75
  """
76
  if frame is None:
77
  return None
@@ -101,7 +77,6 @@ def process_video_stream(frame: np.ndarray, mode: str) -> np.ndarray:
101
 
102
  return processed
103
 
104
-
105
  # ── Custom Gradio Interface Layout ──────────────────────────────────────────
106
  with gr.Blocks(title="⛏️ Minecraft Spatial Voxel Filter") as demo:
107
  gr.Markdown("# ⛏️ Minecraft Spatial Voxel Filter")
@@ -109,7 +84,7 @@ with gr.Blocks(title="⛏️ Minecraft Spatial Voxel Filter") as demo:
109
  with gr.Row():
110
  # Configuration Settings & Status (Left Side Box)
111
  with gr.Column(scale=1):
112
- status_md = gr.Markdown(
113
  f"### ⚡ Backend Connection Status\n"
114
  f"Status: {status_color} **{status_text}**\n\n"
115
  f"Switching between pipeline modes instantly updates your active viewport feed."
@@ -122,22 +97,16 @@ with gr.Blocks(title="⛏️ Minecraft Spatial Voxel Filter") as demo:
122
  interactive=True,
123
  )
124
 
125
- # Contained Video Box Viewport (Right Side Box)
126
  with gr.Column(scale=2):
127
- webrtc_stream = WebRTC(
128
- label="Live Filter Stream",
129
- modality="video",
130
- mode="send-receive",
131
- rtc_configuration=get_safe_rtc_configuration(),
132
- )
133
 
134
- # Explicitly attach the handler to the custom WebRTC component
135
- webrtc_stream.stream(
136
  fn=process_video_stream,
137
- inputs=[webrtc_stream, mode_dropdown],
138
- outputs=[webrtc_stream],
139
- time_limit=150,
140
- concurrency_limit=4,
141
  )
142
 
143
  if __name__ == "__main__":
 
3
  import numpy as np
4
  import gradio as gr
5
  import modal
 
6
 
7
  # ── Modal Backend ───────────────────────────────────────────────────────────
8
  try:
 
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
  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])
35
  if not success:
36
  return frame
 
44
  cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 1)
45
  return out
46
 
 
47
  # ── Core Stream Handler ─────────────────────────────────────────────────────
48
  def process_video_stream(frame: np.ndarray, mode: str) -> np.ndarray:
49
  """
50
+ Accepts incoming frame from the webcam and the mode dropdown selection.
 
51
  """
52
  if frame is None:
53
  return None
 
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")
 
84
  with gr.Row():
85
  # Configuration Settings & Status (Left Side Box)
86
  with gr.Column(scale=1):
87
+ gr.Markdown(
88
  f"### ⚡ Backend Connection Status\n"
89
  f"Status: {status_color} **{status_text}**\n\n"
90
  f"Switching between pipeline modes instantly updates your active viewport feed."
 
97
  interactive=True,
98
  )
99
 
100
+ # Standard Gradio Streaming Setup (Right Side Box)
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
+ # Native Gradio stream event triggers whenever a new frame comes from the webcam
106
+ input_stream.stream(
107
  fn=process_video_stream,
108
+ inputs=[input_stream, mode_dropdown],
109
+ outputs=[output_stream]
 
 
110
  )
111
 
112
  if __name__ == "__main__":