Spaces:
mashroo
/
Runtime error

File size: 1,201 Bytes
9d0b3b4
d63ad23
9d0b3b4
 
d63ad23
9d0b3b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6a8a22
f4e8cf6
9d0b3b4
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
import torch
import numpy as np
from PIL import Image
from mesh import Mesh
from pipelines.pipeline_text_to_3d import TextTo3D


# === Load Model (assumes this is done once at startup, not per request) ===
model = TextTo3D.from_pretrained("./checkpoints/zeroscope_v1_5")
model.to(torch.device("cpu"))
model.eval()

def generate3d(prompt: str, guidance_scale: float = 15.0, steps: int = 50) -> str:
    # === Set up paths ===
    output_dir = "outputs"
    os.makedirs(output_dir, exist_ok=True)
    base_name = prompt.replace(" ", "_").lower()
    mesh_path_base = os.path.join(output_dir, base_name)

    # === Generate 3D Mesh ===
    mesh = model(prompt, guidance_scale=guidance_scale, steps=steps)
    obj_path = mesh_path_base + ".obj"
    mesh.export_mesh_wt_uv(obj_path)

    # === Convert to GLB with textures ===
    mesh_loaded = Mesh.load(obj_path, device=torch.device("cpu"))
    glb_path = mesh_path_base + ".glb"
    mesh_loaded.write(glb_path)

    # === Return GLB path for Gradio display ===
    return glb_path


if __name__ == "__main__":
    # Example run
    prompt = "a modern wooden chair"
    output_glb = generate3d(prompt)
    print(f"Generated GLB: {output_glb}")