Update app.py
Browse files
app.py
CHANGED
|
@@ -6,17 +6,27 @@ import cadquery as cq
|
|
| 6 |
from cadquery import exporters
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
-
from huggingface_hub import hf_hub_download
|
| 10 |
import numpy as np
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
# Load 2D models
|
| 13 |
model_id = "Osama03/Finetuned_diffusion_interiordesign"
|
| 14 |
-
|
| 15 |
-
txt2img_pipe =
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Try to load TripoSR
|
| 20 |
try:
|
| 21 |
from tsr.system import TSR
|
| 22 |
triposr_model = TSR.from_pretrained("stabilityai/TripoSR", config_name="config.yaml", weight_name="model.ckpt")
|
|
@@ -36,6 +46,74 @@ def generate_2d_image(prompt, style, features, input_image):
|
|
| 36 |
|
| 37 |
return image
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def generate_3d_model(input_image, prompt):
|
| 40 |
if triposr_model is not None and input_image is not None:
|
| 41 |
# Use TripoSR for 3D
|
|
|
|
| 6 |
from cadquery import exporters
|
| 7 |
import tempfile
|
| 8 |
import os
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
|
| 11 |
+
# Set Hugging Face token (set in Space Secrets as HF_TOKEN)
|
| 12 |
+
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 13 |
+
|
| 14 |
# Load 2D models
|
| 15 |
model_id = "Osama03/Finetuned_diffusion_interiordesign"
|
| 16 |
+
try:
|
| 17 |
+
txt2img_pipe = StableDiffusionPipeline.from_pretrained(
|
| 18 |
+
model_id, torch_dtype=torch.float16, use_auth_token=HF_TOKEN
|
| 19 |
+
)
|
| 20 |
+
txt2img_pipe = txt2img_pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
+
img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 22 |
+
model_id, torch_dtype=torch.float16, use_auth_token=HF_TOKEN
|
| 23 |
+
)
|
| 24 |
+
img2img_pipe = img2img_pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Failed to load {model_id}: {e}. Check token or model availability.")
|
| 27 |
+
raise
|
| 28 |
|
| 29 |
+
# Try to load TripoSR
|
| 30 |
try:
|
| 31 |
from tsr.system import TSR
|
| 32 |
triposr_model = TSR.from_pretrained("stabilityai/TripoSR", config_name="config.yaml", weight_name="model.ckpt")
|
|
|
|
| 46 |
|
| 47 |
return image
|
| 48 |
|
| 49 |
+
def generate_3d_model(input_image, prompt):
|
| 50 |
+
if triposr_model is not None and input_image is not None:
|
| 51 |
+
try:
|
| 52 |
+
temp_image = Image.fromarray(input_image)
|
| 53 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 54 |
+
triposr_model(temp_image, device="cuda" if torch.cuda.is_available() else "cpu", export_format="glb", output_dir=tmpdir)
|
| 55 |
+
glb_path = os.path.join(tmpdir, "mesh.glb")
|
| 56 |
+
return glb_path
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"TripoSR failed: {e}. Using CadQuery fallback.")
|
| 59 |
+
|
| 60 |
+
# Fallback: Simple CadQuery room
|
| 61 |
+
room = cq.Workplane("XY").box(5, 4, 3).faces(">Z").shell(-0.1)
|
| 62 |
+
with tempfile.NamedTemporaryFile(suffix=".glb", delete=False) as tmpfile:
|
| 63 |
+
exporters.export(room, tmpfile.name, exportType='GLB')
|
| 64 |
+
return tmpfile.name
|
| 65 |
+
|
| 66 |
+
def generate_cad_model(room_length, room_width, room_height, features):
|
| 67 |
+
room = cq.Workplane("XY").box(room_length, room_width, room_height).faces(">Z").shell(-0.1)
|
| 68 |
+
feature_positions = {"sofa": (room_length/4, room_width/2, 0.5), "coffee table": (room_length/2, room_width/2, 0.3)}
|
| 69 |
+
feature_sizes = {"sofa": (2, 1, 0.8), "coffee table": (1, 1, 0.4)}
|
| 70 |
+
|
| 71 |
+
for feat in features:
|
| 72 |
+
if feat in feature_positions:
|
| 73 |
+
pos = feature_positions[feat]
|
| 74 |
+
size = feature_sizes[feat]
|
| 75 |
+
room = room.union(cq.Workplane("XY").transformed(offset=(pos[0], pos[1], pos[2])).box(*size))
|
| 76 |
+
|
| 77 |
+
with tempfile.NamedTemporaryFile(suffix=".glb", delete=False) as tmpfile:
|
| 78 |
+
exporters.export(room, tmpfile.name, exportType='GLB')
|
| 79 |
+
return tmpfile.name
|
| 80 |
+
|
| 81 |
+
# UI with Tabs
|
| 82 |
+
with gr.Blocks(title="Interior Design Generator MVP") as demo:
|
| 83 |
+
gr.Markdown("# Interior Design Image Generator MVP with 3D & CAD")
|
| 84 |
+
|
| 85 |
+
with gr.Tabs():
|
| 86 |
+
with gr.Tab("2D Generation"):
|
| 87 |
+
with gr.Row():
|
| 88 |
+
prompt = gr.Textbox(label="Text Prompt")
|
| 89 |
+
style = gr.Dropdown(choices=["modern", "vintage", "minimalist", "industrial", "bohemian", "scandinavian", "rustic"], value="modern")
|
| 90 |
+
features = gr.Checkboxgroup(choices=["sofa", "coffee table", "lamp", "bookshelf", "fireplace", "plants", "artwork", "rug"])
|
| 91 |
+
input_image = gr.Image(label="Upload Input Image (optional)", type="numpy")
|
| 92 |
+
generate_2d_btn = gr.Button("Generate 2D Image")
|
| 93 |
+
output_2d = gr.Image(label="Generated 2D Design")
|
| 94 |
+
generate_2d_btn.click(generate_2d_image, inputs=[prompt, style, features, input_image], outputs=output_2d)
|
| 95 |
+
|
| 96 |
+
with gr.Tab("3D Visualization"):
|
| 97 |
+
gr.Markdown("Generate and view 3D from the 2D image (or prompt if no image).")
|
| 98 |
+
input_3d_image = gr.Image(label="Use Generated 2D Image (paste from above or upload)", type="numpy")
|
| 99 |
+
input_3d_prompt = gr.Textbox(label="Fallback Prompt (if no image)")
|
| 100 |
+
generate_3d_btn = gr.Button("Generate 3D Model")
|
| 101 |
+
output_3d = gr.Model3D(label="3D Model (rotate/zoom in browser)")
|
| 102 |
+
generate_3d_btn.click(generate_3d_model, inputs=[input_3d_image, input_3d_prompt], outputs=output_3d)
|
| 103 |
+
|
| 104 |
+
with gr.Tab("CAD-like Editing"):
|
| 105 |
+
gr.Markdown("Parametric editing: Adjust room dimensions and features, regenerate CAD model.")
|
| 106 |
+
room_length = gr.Slider(3, 10, value=5, label="Room Length (m)")
|
| 107 |
+
room_width = gr.Slider(3, 10, value=4, label="Room Width (m)")
|
| 108 |
+
room_height = gr.Slider(2, 4, value=3, label="Room Height (m)")
|
| 109 |
+
cad_features = gr.Checkboxgroup(choices=["sofa", "coffee table"], label="Features (positioned simply)")
|
| 110 |
+
generate_cad_btn = gr.Button("Generate CAD Model")
|
| 111 |
+
output_cad = gr.Model3D(label="CAD 3D Model (edit params to update)")
|
| 112 |
+
generate_cad_btn.click(generate_cad_model, inputs=[room_length, room_width, room_height, cad_features], outputs=output_cad)
|
| 113 |
+
|
| 114 |
+
if __name__ == "__main__":
|
| 115 |
+
demo.launch() return image
|
| 116 |
+
|
| 117 |
def generate_3d_model(input_image, prompt):
|
| 118 |
if triposr_model is not None and input_image is not None:
|
| 119 |
# Use TripoSR for 3D
|