brentyi commited on
Commit
108d4e0
·
1 Parent(s): e5bd0cd

Add arg for max websocket message size

Browse files
Files changed (2) hide show
  1. README.md +24 -1
  2. viser_proxy_manager.py +6 -1
README.md CHANGED
@@ -8,4 +8,27 @@ app_port: 7860
8
  pinned: false
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  pinned: false
9
  ---
10
 
11
+ # Viser + Gradio
12
+
13
+ Demo for integrating [viser](https://github.com/nerfstudio-project/viser) 3D
14
+ visualizations into a [Gradio](https://www.gradio.app/) application.
15
+
16
+ - Uses Gradio's session management to create isolated 3D visualization contexts.
17
+ - Exposes both Gradio and Viser over the same port.
18
+
19
+ ## Deploying on HuggingFace Spaces
20
+
21
+ **[ [Live example](https://brentyi-viser-gradio-embed.hf.space/) ]**
22
+
23
+ This repository should work out-of-the-box with HF Spaces via Docker.
24
+
25
+ - Unlike a vanilla Gradio Space, this is unfortunately not supported by [ZeroGPU](https://huggingface.co/docs/hub/en/spaces-zerogpu).
26
+
27
+ ## Local Demo
28
+
29
+ ```bash
30
+ pip install -r requirements.txt
31
+ python app.py
32
+ ```
33
+
34
+ https://github.com/user-attachments/assets/b94a117a-b9e5-4854-805a-8666941c7816
viser_proxy_manager.py CHANGED
@@ -19,6 +19,7 @@ class ViserProxyManager:
19
  These ports are used only for internal communication and don't need to be publicly exposed.
20
  max_local_port: Maximum local port number to use for Viser servers. Defaults to 9000.
21
  These ports are used only for internal communication and don't need to be publicly exposed.
 
22
  """
23
 
24
  def __init__(
@@ -26,11 +27,13 @@ class ViserProxyManager:
26
  app: FastAPI,
27
  min_local_port: int = 8000,
28
  max_local_port: int = 9000,
 
29
  ) -> None:
30
  self._min_port = min_local_port
31
  self._max_port = max_local_port
32
  self._server_from_session_hash: dict[str, viser.ViserServer] = {}
33
  self._last_port = self._min_port - 1 # Track last port tried
 
34
 
35
  @app.get("/viser/{server_id}/{proxy_path:path}")
36
  async def proxy(request: Request, server_id: str, proxy_path: str):
@@ -96,7 +99,9 @@ class ViserProxyManager:
96
 
97
  try:
98
  # Connect to the target WebSocket
99
- async with websockets.connect(target_ws_url) as ws_target:
 
 
100
  # Create tasks for bidirectional communication
101
  async def forward_to_target():
102
  """Forward messages from the client to the target WebSocket."""
 
19
  These ports are used only for internal communication and don't need to be publicly exposed.
20
  max_local_port: Maximum local port number to use for Viser servers. Defaults to 9000.
21
  These ports are used only for internal communication and don't need to be publicly exposed.
22
+ max_websocket_message_size_bytes: Maximum size of WebSocket messages in bytes. Defaults to 32 MB.
23
  """
24
 
25
  def __init__(
 
27
  app: FastAPI,
28
  min_local_port: int = 8000,
29
  max_local_port: int = 9000,
30
+ max_websocket_message_size_bytes: int = 32_000_000,
31
  ) -> None:
32
  self._min_port = min_local_port
33
  self._max_port = max_local_port
34
  self._server_from_session_hash: dict[str, viser.ViserServer] = {}
35
  self._last_port = self._min_port - 1 # Track last port tried
36
+ self._max_websocket_message_size_bytes = max_websocket_message_size_bytes
37
 
38
  @app.get("/viser/{server_id}/{proxy_path:path}")
39
  async def proxy(request: Request, server_id: str, proxy_path: str):
 
99
 
100
  try:
101
  # Connect to the target WebSocket
102
+ async with websockets.connect(
103
+ target_ws_url, max_size=self._max_websocket_message_size_bytes
104
+ ) as ws_target:
105
  # Create tasks for bidirectional communication
106
  async def forward_to_target():
107
  """Forward messages from the client to the target WebSocket."""