viser icon indicating copy to clipboard operation
viser copied to clipboard

Integrating Viser with Hugging Face Spaces: Handling Port Restrictions

Open jytime opened this issue 10 months ago • 3 comments

Hi Brent,

Thanks for the great work on Viser! I’m trying to integrate Viser into a Gradio demo on Hugging Face Spaces for 3D visualization. Locally, I got it working with something like:

import viser
server = viser.ViserServer(port=port)
viser_url = f"http://localhost:{port}"

iframe_code = f'<iframe src="{viser_url}" width="100%" height="520px"></iframe>'
viser_output = gradio.HTML(label="Viser Visualization",)
# pseudo code, pass iframe_code to viser_output

However, when I deploy this on Hugging Face Spaces, I run into an issue that the container seems not to allow opening multiple ports. Although Viser runs in the backend, the gradio.HTML iframe remains empty and logs the error suuch as “localhost refused to connect.” I also posted a question on the Hugging Face discussion forum about this.

From the perspective of Viser, is there a way to make it a pure HTML solution that doesn’t require a separate python -m http.server process (similar to issue https://github.com/nerfstudio-project/viser/issues/326)? Or otherwise, is it possible to let Viser share the same port with Gradio? If we can find a solution, it might help avoid these porting restrictions, and may help users explore more applications in the hugging face spaces.

Best, Jianyuan

jytime avatar Feb 25 '25 04:02 jytime

Hi Jianyuan!

To answer your specific questions:

  • Pure HTML solution: this is plausible but my guess is it would involve nontrivial engineering effort. I would offer to help look into it / gauge difficulty but am unfortunately juggling deadlines for a while :(
  • Sharing port: it seems unlikely

Since HF Spaces allows exposing one port: if the Gradio UI you have is not too complicated, the "easiest" solution for me would be to try and build it within Viser, then host only Viser on HF Spaces. There's a lot of stuff Gradio can do that won't be as nice in Viser though, of course, so this may not be possible.

For getting public URLs / avoiding port restrictions:

  • You could try something like localtunnel or pyngrok from within the HF space
  • Viser also has request_share_url(). But this is really meant for quick experiments, if you're hosting a public demo it's probably not reliable enough

brentyi avatar Feb 26 '25 12:02 brentyi

@jytime @wbhu I had some free time and experimented with this today, actually sharing the same port between Gradio + multiple Viser instances was not too hard: https://github.com/brentyi/viser-gradio-embed/

brentyi avatar Mar 27 '25 00:03 brentyi

Thanks a lot! I am going to check it in details :)

jytime avatar Mar 27 '25 02:03 jytime