Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from shap_e.diffusion.sample import sample_latents
|
| 4 |
+
from shap_e.diffusion.gaussian_diffusion import diffusion_from_config
|
| 5 |
+
from shap_e.models.download import load_model, load_config
|
| 6 |
+
from shap_e.util.notebooks import decode_latent_mesh
|
| 7 |
+
import zipfile
|
| 8 |
+
import io
|
| 9 |
+
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
# Load models
|
| 13 |
+
transmitter = load_model("transmitter", device=device)
|
| 14 |
+
xm = load_model("text300M", device=device)
|
| 15 |
+
diffusion = diffusion_from_config(load_config("diffusion"))
|
| 16 |
+
|
| 17 |
+
def generate_3d(prompt):
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
latents = sample_latents(
|
| 20 |
+
batch_size=1,
|
| 21 |
+
model=xm,
|
| 22 |
+
diffusion=diffusion,
|
| 23 |
+
guidance_scale=3.0,
|
| 24 |
+
model_kwargs=dict(texts=[prompt]),
|
| 25 |
+
progress=True,
|
| 26 |
+
clip_denoised=True,
|
| 27 |
+
use_fp16=True,
|
| 28 |
+
device=device,
|
| 29 |
+
)
|
| 30 |
+
mesh = decode_latent_mesh(xm, latents[0]).tri_mesh()
|
| 31 |
+
|
| 32 |
+
# Save zip
|
| 33 |
+
buf = io.BytesIO()
|
| 34 |
+
with zipfile.ZipFile(buf, "w") as z:
|
| 35 |
+
z.writestr("mesh.ply", mesh.to_ply())
|
| 36 |
+
buf.seek(0)
|
| 37 |
+
return buf
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(
|
| 40 |
+
fn=generate_3d,
|
| 41 |
+
inputs=gr.Textbox(label="Prompt"),
|
| 42 |
+
outputs=gr.File(label="Generated 3D Model (.zip)")
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
demo.launch()
|