Spaces:
Paused
Paused
Update gradio_app.py
Browse files- gradio_app.py +15 -19
gradio_app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
import base64
|
| 3 |
import tempfile
|
| 4 |
from typing import Any
|
| 5 |
import torch
|
|
@@ -60,13 +59,11 @@ def create_batch(input_image: Image) -> dict[str, Any]:
|
|
| 60 |
}
|
| 61 |
return batch
|
| 62 |
|
| 63 |
-
def process_image(
|
| 64 |
-
"""Process image and return GLB
|
| 65 |
try:
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
input_image = Image.open(tempfile.SpooledTemporaryFile(suffix='.png'))
|
| 69 |
-
input_image.frombytes(image_data)
|
| 70 |
|
| 71 |
# Remove background if needed
|
| 72 |
if input_image.mode != 'RGBA':
|
|
@@ -100,14 +97,7 @@ def process_image(image_base64: str) -> str:
|
|
| 100 |
temp_file = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
|
| 101 |
trimesh_mesh.export(temp_file.name, file_type="glb", include_normals=True)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
with open(temp_file.name, 'rb') as f:
|
| 105 |
-
glb_base64 = base64.b64encode(f.read()).decode('utf-8')
|
| 106 |
-
|
| 107 |
-
# Cleanup
|
| 108 |
-
os.unlink(temp_file.name)
|
| 109 |
-
|
| 110 |
-
return glb_base64
|
| 111 |
|
| 112 |
except Exception as e:
|
| 113 |
return str(e)
|
|
@@ -115,11 +105,17 @@ def process_image(image_base64: str) -> str:
|
|
| 115 |
# Create Gradio interface
|
| 116 |
demo = gr.Interface(
|
| 117 |
fn=process_image,
|
| 118 |
-
inputs=gr.
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
title="SPAR3D Image to GLB Converter",
|
| 121 |
-
description="Upload
|
| 122 |
)
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
-
demo.launch(
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import tempfile
|
| 3 |
from typing import Any
|
| 4 |
import torch
|
|
|
|
| 59 |
}
|
| 60 |
return batch
|
| 61 |
|
| 62 |
+
def process_image(image_path: str) -> str:
|
| 63 |
+
"""Process image and return path to GLB file."""
|
| 64 |
try:
|
| 65 |
+
# Load image
|
| 66 |
+
input_image = Image.open(image_path)
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Remove background if needed
|
| 69 |
if input_image.mode != 'RGBA':
|
|
|
|
| 97 |
temp_file = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
|
| 98 |
trimesh_mesh.export(temp_file.name, file_type="glb", include_normals=True)
|
| 99 |
|
| 100 |
+
return temp_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
except Exception as e:
|
| 103 |
return str(e)
|
|
|
|
| 105 |
# Create Gradio interface
|
| 106 |
demo = gr.Interface(
|
| 107 |
fn=process_image,
|
| 108 |
+
inputs=gr.File(
|
| 109 |
+
label="Upload Image",
|
| 110 |
+
file_types=["image"],
|
| 111 |
+
),
|
| 112 |
+
outputs=gr.File(
|
| 113 |
+
label="Download GLB",
|
| 114 |
+
file_types=[".glb"],
|
| 115 |
+
),
|
| 116 |
title="SPAR3D Image to GLB Converter",
|
| 117 |
+
description="Upload an image (JPG, PNG, or WebP) and get back a 3D model in GLB format",
|
| 118 |
)
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
+
demo.launch()
|