File size: 1,453 Bytes
0d36818
 
 
 
768bc7d
0d36818
487d3dd
0d36818
 
a40e9f6
0d36818
b380e5c
a40e9f6
 
 
 
 
0d36818
487d3dd
0d36818
 
 
 
 
 
 
 
 
 
 
 
 
 
 
768bc7d
0d36818
 
 
 
 
 
 
 
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
41
42
43
44
import torch
from diffusers import DiffusionPipeline
import trimesh
import numpy as np
from PIL import Image
from io import BytesIO

def load_pipeline():
    """
    Load the LGM-full model pipeline from Hugging Face with remote code execution enabled.
    """
    ckpt_id = "dylanebert/LGM-full"
    pipe = DiffusionPipeline.from_pretrained(
        ckpt_id, 
        torch_dtype=torch.float32, 
        trust_remote_code=True  # Enable remote code execution for custom model code
    ).to("cpu")
    return pipe

def generate_3d_model(pipe, prompt, output_path="output.obj", guidance_scale=7.5, num_inference_steps=32):
    """
    Generate a 3D model from the prompt and save it in a Blender-compatible format (.obj).
    """
    # Generate the model output
    outputs = pipe(prompt=prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps)
    
    # Extract mesh data if the output structure allows
    vertices = outputs["vertices"][0].detach().cpu().numpy()
    faces = outputs["faces"][0].detach().cpu().numpy()
    
    # Create and save the mesh using trimesh
    mesh = trimesh.Trimesh(vertices=vertices, faces=faces, process=True)
    mesh.export(output_path)
    return output_path

def convert_to_gif(images, gif_path="output.gif"):
    """
    Convert a list of images into a GIF.
    """
    images[0].save(
        gif_path, save_all=True, append_images=images[1:], loop=0, duration=100
    )
    return gif_path