Spaces:
Runtime error
Runtime error
CJ Hauser
commited on
Upload 2 files
Browse files- app.py +49 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from shap_e.diffusion.sample import sample_latents
|
| 4 |
+
from shap_e.models.download import load_model
|
| 5 |
+
from shap_e.util.notebooks import decode_latent_mesh
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# pick device
|
| 9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
+
|
| 11 |
+
# load models once
|
| 12 |
+
print("Loading Shap-E models (this may take a bit)...")
|
| 13 |
+
xm = load_model('transmitter', device=device)
|
| 14 |
+
im_model = load_model('image300M', device=device)
|
| 15 |
+
|
| 16 |
+
def generate_3d(image):
|
| 17 |
+
"""Takes an uploaded image and returns path to generated 3D model"""
|
| 18 |
+
img = image.convert("RGB")
|
| 19 |
+
|
| 20 |
+
# generate latents
|
| 21 |
+
latents = sample_latents(
|
| 22 |
+
batch_size=1,
|
| 23 |
+
model=im_model,
|
| 24 |
+
guidance_scale=3.0,
|
| 25 |
+
model_kwargs=dict(images=[img]),
|
| 26 |
+
device=device
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# decode into mesh
|
| 30 |
+
mesh = decode_latent_mesh(xm, latents[0])
|
| 31 |
+
|
| 32 |
+
# save model
|
| 33 |
+
output_path = "output.obj"
|
| 34 |
+
with open(output_path, "w") as f:
|
| 35 |
+
mesh.write_obj(f)
|
| 36 |
+
|
| 37 |
+
return output_path
|
| 38 |
+
|
| 39 |
+
# Gradio UI
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=generate_3d,
|
| 42 |
+
inputs=gr.Image(type="pil"),
|
| 43 |
+
outputs=gr.File(),
|
| 44 |
+
title="Shap-E: 2D β 3D Model",
|
| 45 |
+
description="Upload a 2D image and download a generated 3D model (.obj)"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
torchaudio
|
| 4 |
+
git+https://github.com/openai/shap-e.git
|
| 5 |
+
Pillow
|
| 6 |
+
gradio
|