Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import trimesh
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import base64
|
6 |
+
|
7 |
+
def text_image_to_glb(text, image):
|
8 |
+
"""
|
9 |
+
Converts text and image into a 3D GLB object.
|
10 |
+
Args:
|
11 |
+
text (str): Input text.
|
12 |
+
image (PIL.Image): Input image.
|
13 |
+
Returns:
|
14 |
+
bytes: GLB file as bytes.
|
15 |
+
str: HTML for 3D preview.
|
16 |
+
"""
|
17 |
+
# Create a simple 3D mesh (e.g., a cube)
|
18 |
+
mesh = trimesh.creation.box(extents=[1, 1, 1]) # Create a 1x1x1 cube
|
19 |
+
|
20 |
+
# Add text to the 3D object (as metadata)
|
21 |
+
mesh.metadata['text'] = text
|
22 |
+
|
23 |
+
# Convert the image to a texture and apply it to the mesh
|
24 |
+
if image:
|
25 |
+
image = np.array(image) # Convert PIL image to numpy array
|
26 |
+
texture = trimesh.visual.TextureVisuals(image=image)
|
27 |
+
mesh.visual = texture
|
28 |
+
|
29 |
+
# Export the mesh to GLB format
|
30 |
+
glb_data = trimesh.exchange.gltf.export_glb(mesh)
|
31 |
+
|
32 |
+
# Encode the GLB data as base64 for the HTML preview
|
33 |
+
glb_base64 = base64.b64encode(glb_data).decode("utf-8")
|
34 |
+
|
35 |
+
# Create an HTML preview for the 3D object
|
36 |
+
html_preview = f"""
|
37 |
+
<div style="text-align:center;">
|
38 |
+
<h3>3D Object Preview</h3>
|
39 |
+
<p><strong>Text:</strong> {text}</p>
|
40 |
+
<p><strong>Image:</strong> Applied as texture</p>
|
41 |
+
<model-viewer style="width:100%; height:400px;" src="data:model/gltf-binary;base64,{glb_base64}" auto-rotate camera-controls></model-viewer>
|
42 |
+
<script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script>
|
43 |
+
</div>
|
44 |
+
"""
|
45 |
+
|
46 |
+
return glb_data, html_preview
|
47 |
+
|
48 |
+
# Gradio interface
|
49 |
+
def create_interface():
|
50 |
+
iface = gr.Interface(
|
51 |
+
fn=text_image_to_glb,
|
52 |
+
inputs=[
|
53 |
+
gr.Textbox(label="Enter Text"),
|
54 |
+
gr.Image(label="Upload Image", type="pil")
|
55 |
+
],
|
56 |
+
outputs=[
|
57 |
+
gr.File(label="Download GLB File"),
|
58 |
+
gr.HTML(label="3D Object Preview")
|
59 |
+
],
|
60 |
+
title="Text & Image to 3D GLB Converter",
|
61 |
+
description="Convert text and an image into a 3D GLB object with a preview."
|
62 |
+
)
|
63 |
+
return iface
|
64 |
+
|
65 |
+
# Launch the app
|
66 |
+
if __name__ == "__main__":
|
67 |
+
iface = create_interface()
|
68 |
+
iface.launch()
|