Upload ZeroGPU/app.py with huggingface_hub
Browse files- ZeroGPU/app.py +95 -0
ZeroGPU/app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import sys
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
# Add repo root to path
|
| 8 |
+
ROOT = Path(__file__).resolve().parent.parent
|
| 9 |
+
sys.path.insert(0, str(ROOT))
|
| 10 |
+
sys.path.insert(0, str(ROOT / "external" / "TripoSG"))
|
| 11 |
+
sys.path.insert(0, str(ROOT / "external" / "MV-Adapter"))
|
| 12 |
+
|
| 13 |
+
@spaces.GPU(duration=120)
|
| 14 |
+
def generate_shape(image, seed=42, steps=30, guidance=7.5):
|
| 15 |
+
from app import load_triposg, generate_shape as _gen
|
| 16 |
+
import numpy as np
|
| 17 |
+
from PIL import Image as PILImage
|
| 18 |
+
|
| 19 |
+
if image is None:
|
| 20 |
+
return None, "No image provided"
|
| 21 |
+
|
| 22 |
+
img_array = np.array(PILImage.fromarray(image).convert("RGB"))
|
| 23 |
+
glb_path, status = _gen(
|
| 24 |
+
input_image=img_array,
|
| 25 |
+
remove_background=True,
|
| 26 |
+
num_steps=steps,
|
| 27 |
+
guidance_scale=guidance,
|
| 28 |
+
seed=int(seed),
|
| 29 |
+
face_count=120000,
|
| 30 |
+
)
|
| 31 |
+
return glb_path, status
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@spaces.GPU(duration=300)
|
| 35 |
+
def apply_texture(glb_path, image, seed=42):
|
| 36 |
+
from app import apply_texture as _tex
|
| 37 |
+
import numpy as np
|
| 38 |
+
from PIL import Image as PILImage
|
| 39 |
+
|
| 40 |
+
if glb_path is None or image is None:
|
| 41 |
+
return None, None, "Run shape generation first"
|
| 42 |
+
|
| 43 |
+
img_array = np.array(PILImage.fromarray(image).convert("RGB"))
|
| 44 |
+
textured_glb, mv_img, status = _tex(
|
| 45 |
+
glb_path=glb_path,
|
| 46 |
+
input_image=img_array,
|
| 47 |
+
remove_background=True,
|
| 48 |
+
variant="sdxl",
|
| 49 |
+
tex_seed=int(seed),
|
| 50 |
+
enhance_face=True,
|
| 51 |
+
rembg_threshold=0.5,
|
| 52 |
+
rembg_erode=2,
|
| 53 |
+
)
|
| 54 |
+
return textured_glb, mv_img, status
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
with gr.Blocks(title="MeshForge — Portrait to 3D") as demo:
|
| 58 |
+
gr.Markdown("# MeshForge\n### Portrait → 3D Mesh with Texture")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
input_image = gr.Image(label="Portrait", type="numpy")
|
| 63 |
+
seed = gr.Slider(0, 2**31 - 1, value=42, step=1, label="Seed")
|
| 64 |
+
steps = gr.Slider(10, 50, value=30, step=1, label="Shape Steps")
|
| 65 |
+
guidance = gr.Slider(1.0, 15.0, value=7.5, step=0.5, label="Guidance Scale")
|
| 66 |
+
shape_btn = gr.Button("1. Generate Shape", variant="primary")
|
| 67 |
+
|
| 68 |
+
with gr.Column():
|
| 69 |
+
shape_output = gr.Model3D(label="Shape")
|
| 70 |
+
shape_status = gr.Textbox(label="Status")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
with gr.Column():
|
| 74 |
+
tex_seed = gr.Slider(0, 2**31 - 1, value=42, step=1, label="Texture Seed")
|
| 75 |
+
tex_btn = gr.Button("2. Apply Texture", variant="primary")
|
| 76 |
+
with gr.Column():
|
| 77 |
+
textured_output = gr.Model3D(label="Textured")
|
| 78 |
+
mv_preview = gr.Image(label="Multiview Preview")
|
| 79 |
+
tex_status = gr.Textbox(label="Status")
|
| 80 |
+
|
| 81 |
+
glb_state = gr.State(None)
|
| 82 |
+
|
| 83 |
+
shape_btn.click(
|
| 84 |
+
generate_shape,
|
| 85 |
+
inputs=[input_image, seed, steps, guidance],
|
| 86 |
+
outputs=[glb_state, shape_status],
|
| 87 |
+
).then(lambda x: x, inputs=glb_state, outputs=shape_output)
|
| 88 |
+
|
| 89 |
+
tex_btn.click(
|
| 90 |
+
apply_texture,
|
| 91 |
+
inputs=[glb_state, input_image, tex_seed],
|
| 92 |
+
outputs=[textured_output, mv_preview, tex_status],
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
demo.launch()
|