| import gradio as gr |
| import torch |
| import gc |
| import os |
| import numpy as np |
| from PIL import Image |
| from diffusers import StableDiffusionPipeline |
|
|
| |
| from tsr.system import TSR |
| from tsr.utils import remove_background, resize_foreground, save_video |
|
|
| |
| device = "cpu" |
|
|
| def generate_pipeline(prompt): |
| output_obj_path = "output_model.obj" |
| |
| |
| |
| |
| print(f">>> [Step 1/3] Loading 2D Model (Miro) for: {prompt}") |
| |
| |
| pipe = StableDiffusionPipeline.from_pretrained( |
| "IntimeAI/Miro", |
| torch_dtype=torch.float32, |
| use_safetensors=True |
| ).to(device) |
| |
| |
| image = pipe(prompt, num_inference_steps=20).images[0] |
| |
| |
| image_path = "generated_2d.png" |
| image.save(image_path) |
| print(">>> 2D Image Generated!") |
|
|
| |
| del pipe |
| gc.collect() |
| print(">>> 2D Model unloaded to free up RAM.") |
|
|
| |
| |
| |
| print(f">>> [Step 2/3] Removing Background...") |
| |
| |
| rembg_session = None |
| image_no_bg = remove_background(image, rembg_session) |
| image_no_bg = resize_foreground(image_no_bg, 0.85) |
| |
| |
| |
| |
| print(f">>> [Step 3/3] Loading 3D Model (TripoSR)...") |
| |
| |
| model_3d = TSR.from_pretrained( |
| "stabilityai/TripoSR", |
| config_name="config.yaml", |
| weight_name="model.ckpt", |
| ) |
| model_3d.to(device) |
| |
| |
| scene_codes = model_3d(image_no_bg, device=device) |
| meshes = model_3d.extract_mesh(scene_codes)[0] |
| |
| |
| meshes.export(output_obj_path) |
| print(">>> 3D OBJ Saved!") |
|
|
| |
| del model_3d |
| gc.collect() |
|
|
| return output_obj_path |
|
|
| |
| iface = gr.Interface( |
| fn=generate_pipeline, |
| inputs=gr.Textbox(label="Enter Prompt (e.g., A isometric sword in fantasy style)"), |
| outputs=gr.Model3D(label="Generated 3D Object", clear_color=[0, 0, 0, 0]), |
| title="Miro to 3D Generator (Text -> 2D -> 3D)", |
| description="IntimeAI/Miro 모델을 사용하여 이미지를 만들고 3D로 변환합니다. (CPU 모드: 약 5~10분 소요)" |
| ) |
|
|
| iface.launch() |