Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from daggr import FnNode, GradioNode, InferenceNode, Graph
|
| 2 |
+
from daggr.state import get_daggr_files_dir
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from typing import Any
|
| 8 |
+
import uuid
|
| 9 |
+
|
| 10 |
+
def downscale_image_to_file(image: Any, scale: float = 0.25) -> str | None:
|
| 11 |
+
|
| 12 |
+
pil_img = Image.open(image)
|
| 13 |
+
scale_f = max(0.05, min(1.0, float(scale)))
|
| 14 |
+
w, h = pil_img.size
|
| 15 |
+
new_w = max(1, int(w * scale_f))
|
| 16 |
+
new_h = max(1, int(h * scale_f))
|
| 17 |
+
resized = pil_img.resize((new_w, new_h), resample=Image.LANCZOS)
|
| 18 |
+
out_path = get_daggr_files_dir() / f"{uuid.uuid4()}.png"
|
| 19 |
+
resized.save(out_path)
|
| 20 |
+
return str(out_path)
|
| 21 |
+
|
| 22 |
+
background_remover = GradioNode(
|
| 23 |
+
"merve/background-removal",
|
| 24 |
+
api_name="/image",
|
| 25 |
+
run_locally=True,
|
| 26 |
+
inputs={
|
| 27 |
+
"image": gr.Image(),
|
| 28 |
+
},
|
| 29 |
+
outputs={
|
| 30 |
+
"original_image": None,
|
| 31 |
+
"final_image": gr.Image(
|
| 32 |
+
label="Final Image"
|
| 33 |
+
),
|
| 34 |
+
},
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
downscaler = FnNode(
|
| 38 |
+
downscale_image_to_file,
|
| 39 |
+
name="Downscale image for Inference",
|
| 40 |
+
inputs={
|
| 41 |
+
"image": background_remover.final_image,
|
| 42 |
+
"scale": gr.Slider(
|
| 43 |
+
label="Downscale factor",
|
| 44 |
+
minimum=0.25,
|
| 45 |
+
maximum=0.75,
|
| 46 |
+
step=0.05,
|
| 47 |
+
value=0.25,
|
| 48 |
+
),
|
| 49 |
+
},
|
| 50 |
+
outputs={
|
| 51 |
+
"image": gr.Image(label="Downscaled Image", type="filepath"),
|
| 52 |
+
},
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
flux_enhancer = InferenceNode(
|
| 56 |
+
model="black-forest-labs/FLUX.2-klein-4B:fal-ai",
|
| 57 |
+
inputs={
|
| 58 |
+
"image": downscaler.image,
|
| 59 |
+
"prompt": gr.Textbox(
|
| 60 |
+
label="prompt",
|
| 61 |
+
value=("Transform this into a clean 3D asset render"),
|
| 62 |
+
lines=3,
|
| 63 |
+
),
|
| 64 |
+
},
|
| 65 |
+
outputs={
|
| 66 |
+
"image": gr.Image(label="3D-Ready Enhanced Image"),
|
| 67 |
+
},
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
trellis_3d = GradioNode(
|
| 72 |
+
"microsoft/TRELLIS.2",
|
| 73 |
+
api_name="/image_to_3d",
|
| 74 |
+
inputs={
|
| 75 |
+
"image": flux_enhancer.image,
|
| 76 |
+
"ss_guidance_strength": 7.5,
|
| 77 |
+
"ss_sampling_steps": 12,
|
| 78 |
+
},
|
| 79 |
+
outputs={
|
| 80 |
+
"glb": gr.HTML(label="3D Asset (GLB preview)"),
|
| 81 |
+
},
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
graph = Graph(
|
| 85 |
+
name="Image to 3D Asset Pipeline",
|
| 86 |
+
nodes=[background_remover, downscaler, flux_enhancer, trellis_3d],
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
graph.launch()
|