Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import trimesh
|
| 4 |
import os
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
# --- Constants ---
|
| 8 |
# We use the InstantMesh space as the backend engine because it is currently
|
|
@@ -40,10 +39,10 @@ def generate_3d_geometry(input_image, do_remove_background):
|
|
| 40 |
raise gr.Error("Please upload an image first!")
|
| 41 |
|
| 42 |
print("Initializing Client connection to backend...")
|
|
|
|
| 43 |
client = Client(BACKEND_SPACE)
|
| 44 |
|
| 45 |
# 1. Preprocess: Remove background (crucial for accurate geometry)
|
| 46 |
-
# The backend space expects this step first.
|
| 47 |
print("Step 1: Removing background...")
|
| 48 |
try:
|
| 49 |
processed_image_path = client.predict(
|
|
@@ -57,14 +56,18 @@ def generate_3d_geometry(input_image, do_remove_background):
|
|
| 57 |
# 2. Generate: Create the 3D mesh from the processed image
|
| 58 |
print("Step 2: Generating 3D mesh (this may take 30-60 seconds)...")
|
| 59 |
try:
|
| 60 |
-
#
|
| 61 |
result_paths = client.predict(
|
| 62 |
processed_image_path,
|
| 63 |
api_name="/generate_mesh"
|
| 64 |
)
|
| 65 |
-
|
| 66 |
-
#
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
raise gr.Error(f"3D Generation failed: {e}")
|
|
@@ -74,7 +77,7 @@ def generate_3d_geometry(input_image, do_remove_background):
|
|
| 74 |
stl_output_path = convert_obj_to_stl(obj_output_path)
|
| 75 |
|
| 76 |
if stl_output_path is None:
|
| 77 |
-
raise gr.Error("Failed to convert model to STL.")
|
| 78 |
|
| 79 |
# Return the interactive 3D model path and the downloadable STL path
|
| 80 |
return obj_output_path, stl_output_path
|
|
@@ -82,27 +85,26 @@ def generate_3d_geometry(input_image, do_remove_background):
|
|
| 82 |
|
| 83 |
# --- Gradio Interface UI Setup ---
|
| 84 |
css = """
|
| 85 |
-
#col-container {max-width:
|
| 86 |
h1 {text-align: center;}
|
| 87 |
"""
|
| 88 |
|
| 89 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 90 |
with gr.Column(elem_id="col-container"):
|
| 91 |
gr.Markdown("""
|
| 92 |
-
# AI
|
| 93 |
-
|
| 94 |
-
Upload your best, evenly lit photo of the geometric shape below.
|
| 95 |
""")
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column():
|
| 99 |
input_img = gr.Image(label="Upload Best Image", type="filepath", height=300)
|
| 100 |
-
rm_bg_checkbox = gr.Checkbox(label="Remove Background (Recommended)", value=True, info="Helps isolate the shape
|
| 101 |
gen_btn = gr.Button("Generate 3D Model & STL", variant="primary")
|
| 102 |
|
| 103 |
with gr.Column():
|
| 104 |
# The interactive 3D viewer
|
| 105 |
-
model_output = gr.Model3D(label="Interactive 3D Preview", clear_color=[1.0, 1.0, 1.0, 0.0],
|
| 106 |
# The download button for the STL
|
| 107 |
stl_download = gr.File(label="Download STL File", file_count="single")
|
| 108 |
|
|
@@ -114,6 +116,6 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
| 114 |
api_name="run_generation"
|
| 115 |
)
|
| 116 |
|
| 117 |
-
# Launch the app
|
| 118 |
if __name__ == "__main__":
|
| 119 |
demo.queue(max_size=10).launch()
|
|
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import trimesh
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
# --- Constants ---
|
| 7 |
# We use the InstantMesh space as the backend engine because it is currently
|
|
|
|
| 39 |
raise gr.Error("Please upload an image first!")
|
| 40 |
|
| 41 |
print("Initializing Client connection to backend...")
|
| 42 |
+
# Initialize the client inside the function to ensure a fresh connection
|
| 43 |
client = Client(BACKEND_SPACE)
|
| 44 |
|
| 45 |
# 1. Preprocess: Remove background (crucial for accurate geometry)
|
|
|
|
| 46 |
print("Step 1: Removing background...")
|
| 47 |
try:
|
| 48 |
processed_image_path = client.predict(
|
|
|
|
| 56 |
# 2. Generate: Create the 3D mesh from the processed image
|
| 57 |
print("Step 2: Generating 3D mesh (this may take 30-60 seconds)...")
|
| 58 |
try:
|
| 59 |
+
# The AI generates the mesh.
|
| 60 |
result_paths = client.predict(
|
| 61 |
processed_image_path,
|
| 62 |
api_name="/generate_mesh"
|
| 63 |
)
|
| 64 |
+
|
| 65 |
+
# InstantMesh API returns a tuple: (glb_path, obj_path)
|
| 66 |
+
# We extract the OBJ path (index 1) for reliable STL conversion.
|
| 67 |
+
if isinstance(result_paths, (list, tuple)) and len(result_paths) > 1:
|
| 68 |
+
obj_output_path = result_paths[1]
|
| 69 |
+
else:
|
| 70 |
+
obj_output_path = result_paths
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
raise gr.Error(f"3D Generation failed: {e}")
|
|
|
|
| 77 |
stl_output_path = convert_obj_to_stl(obj_output_path)
|
| 78 |
|
| 79 |
if stl_output_path is None:
|
| 80 |
+
raise gr.Error("Failed to convert model to STL. The AI may have generated a corrupted mesh.")
|
| 81 |
|
| 82 |
# Return the interactive 3D model path and the downloadable STL path
|
| 83 |
return obj_output_path, stl_output_path
|
|
|
|
| 85 |
|
| 86 |
# --- Gradio Interface UI Setup ---
|
| 87 |
css = """
|
| 88 |
+
#col-container {max-width: 800px; margin-left: auto; margin-right: auto;}
|
| 89 |
h1 {text-align: center;}
|
| 90 |
"""
|
| 91 |
|
| 92 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 93 |
with gr.Column(elem_id="col-container"):
|
| 94 |
gr.Markdown("""
|
| 95 |
+
# 🧊 AI Image to 3D STL Generator
|
| 96 |
+
Upload your **best, clearest, and most evenly lit single photo** of the geometric shape below. The AI will extrapolate the geometry and generate a downloadable STL file for 3D printing or CAD use.
|
|
|
|
| 97 |
""")
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
with gr.Column():
|
| 101 |
input_img = gr.Image(label="Upload Best Image", type="filepath", height=300)
|
| 102 |
+
rm_bg_checkbox = gr.Checkbox(label="Remove Background (Recommended)", value=True, info="Helps the AI isolate the exact shape of your object.")
|
| 103 |
gen_btn = gr.Button("Generate 3D Model & STL", variant="primary")
|
| 104 |
|
| 105 |
with gr.Column():
|
| 106 |
# The interactive 3D viewer
|
| 107 |
+
model_output = gr.Model3D(label="Interactive 3D Preview", clear_color=[1.0, 1.0, 1.0, 0.0], height=300)
|
| 108 |
# The download button for the STL
|
| 109 |
stl_download = gr.File(label="Download STL File", file_count="single")
|
| 110 |
|
|
|
|
| 116 |
api_name="run_generation"
|
| 117 |
)
|
| 118 |
|
| 119 |
+
# Launch the app with a queue to prevent timeouts
|
| 120 |
if __name__ == "__main__":
|
| 121 |
demo.queue(max_size=10).launch()
|