prithivMLmods commited on
Commit
573a8ee
·
verified ·
1 Parent(s): 63c6557

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -7,6 +7,7 @@ import asyncio
7
  import tempfile
8
  from threading import Thread
9
  import base64
 
10
 
11
  import gradio as gr
12
  import spaces
@@ -43,7 +44,7 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
43
  def glb_to_data_url(glb_path: str) -> str:
44
  """
45
  Reads a GLB file from disk and returns a data URL with a base64 encoded representation.
46
- This data URL can be used as the `src` for an HTML <model-viewer> tag.
47
  """
48
  with open(glb_path, "rb") as f:
49
  data = f.read()
@@ -332,22 +333,18 @@ def generate(
332
  num_steps=64,
333
  randomize_seed=True,
334
  )
335
- # Convert the GLB file to a base64 data URL.
336
- data_url = glb_to_data_url(glb_path)
337
- # Create an HTML snippet that loads model-viewer and converts the data URL to a Blob URL.
 
 
 
 
 
338
  html_output = f'''
339
  <script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script>
340
- <model-viewer id="model-viewer" alt="3D Model" auto-rotate camera-controls
341
  style="width: 100%; height: 400px;"></model-viewer>
342
- <script>
343
- (async () => {{
344
- const response = await fetch("{data_url}");
345
- const arrayBuffer = await response.arrayBuffer();
346
- const blob = new Blob([arrayBuffer], {{type: 'model/gltf-binary'}});
347
- const blobUrl = URL.createObjectURL(blob);
348
- document.getElementById("model-viewer").src = blobUrl;
349
- }})();
350
- </script>
351
  '''
352
  yield gr.HTML(html_output)
353
  return
@@ -480,5 +477,5 @@ demo = gr.ChatInterface(
480
  )
481
 
482
  if __name__ == "__main__":
483
- # To create a public link, set share=True in launch().
484
- demo.queue(max_size=20).launch(share=True)
 
7
  import tempfile
8
  from threading import Thread
9
  import base64
10
+ import shutil
11
 
12
  import gradio as gr
13
  import spaces
 
44
  def glb_to_data_url(glb_path: str) -> str:
45
  """
46
  Reads a GLB file from disk and returns a data URL with a base64 encoded representation.
47
+ (Not used in this method.)
48
  """
49
  with open(glb_path, "rb") as f:
50
  data = f.read()
 
333
  num_steps=64,
334
  randomize_seed=True,
335
  )
336
+ # Instead of embedding via data URL, copy the GLB file to a static folder.
337
+ static_folder = os.path.join(os.getcwd(), "static")
338
+ if not os.path.exists(static_folder):
339
+ os.makedirs(static_folder)
340
+ new_filename = f"mesh_{uuid.uuid4()}.glb"
341
+ new_filepath = os.path.join(static_folder, new_filename)
342
+ shutil.copy(glb_path, new_filepath)
343
+ # Reference the file via a relative URL.
344
  html_output = f'''
345
  <script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script>
346
+ <model-viewer src="/static/{new_filename}" alt="3D Model" auto-rotate camera-controls
347
  style="width: 100%; height: 400px;"></model-viewer>
 
 
 
 
 
 
 
 
 
348
  '''
349
  yield gr.HTML(html_output)
350
  return
 
477
  )
478
 
479
  if __name__ == "__main__":
480
+ # To serve the generated GLB files, add the static directory.
481
+ demo.queue(max_size=20).launch(share=True, static_dirs={"static": "static"})