Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,7 +74,6 @@ class GenerateRequest(BaseModel):
|
|
| 74 |
simplify: bool = True
|
| 75 |
target_face_num: int = DEFAULT_FACE_NUMBER
|
| 76 |
|
| 77 |
-
# Existing Gradio header (unchanged)
|
| 78 |
HEADER = """
|
| 79 |
# 🌌 PolyGenixAI: Craft 3D Worlds with Cosmic Precision
|
| 80 |
## Unleash Infinite Creativity with AI-Powered 3D Generation by AnvilInteractive Solutions
|
|
@@ -158,7 +157,7 @@ HEADER = """
|
|
| 158 |
</style>
|
| 159 |
"""
|
| 160 |
|
| 161 |
-
#
|
| 162 |
from image_process import prepare_image
|
| 163 |
from briarmbg import BriaRMBG
|
| 164 |
snapshot_download("briaai/RMBG-1.4", local_dir=RMBG_PRETRAINED_MODEL)
|
|
@@ -168,6 +167,7 @@ from triposg.pipelines.pipeline_triposg import TripoSGPipeline
|
|
| 168 |
snapshot_download("VAST-AI/TripoSG", local_dir=TRIPOSG_PRETRAINED_MODEL)
|
| 169 |
triposg_pipe = TripoSGPipeline.from_pretrained(TRIPOSG_PRETRAINED_MODEL).to(DEVICE, DTYPE)
|
| 170 |
|
|
|
|
| 171 |
NUM_VIEWS = 6
|
| 172 |
from inference_ig2mv_sdxl import prepare_pipeline, preprocess_image, remove_bg
|
| 173 |
from mvadapter.utils import get_orthogonal_camera, tensor_to_image, make_image_grid
|
|
@@ -197,7 +197,7 @@ transform_image = transforms.Compose(
|
|
| 197 |
remove_bg_fn = lambda x: remove_bg(x, birefnet, transform_image, DEVICE)
|
| 198 |
|
| 199 |
if not os.path.exists("checkpoints/RealESRGAN_x2plus.pth"):
|
| 200 |
-
hf_hub_download("dtarnow/UPscaler", filename="
|
| 201 |
if not os.path.exists("checkpoints/big-lama.pt"):
|
| 202 |
subprocess.run("wget -P checkpoints/ https://github.com/Sanster/models/releases/download/add_big_lama/big-lama.pt", shell=True, check=True)
|
| 203 |
|
|
@@ -253,6 +253,7 @@ def run_full(image: str, req: gr.Request):
|
|
| 253 |
torch.cuda.empty_cache()
|
| 254 |
|
| 255 |
height, width = 768, 768
|
|
|
|
| 256 |
cameras = get_orthogonal_camera(
|
| 257 |
elevation_deg=[0, 0, 0, 0, 89.99, -89.99],
|
| 258 |
distance=[1.8] * NUM_VIEWS,
|
|
@@ -336,6 +337,31 @@ def run_full(image: str, req: gr.Request):
|
|
| 336 |
|
| 337 |
return image_seg, mesh_path, textured_glb_path
|
| 338 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
@spaces.GPU()
|
| 340 |
@torch.no_grad()
|
| 341 |
def run_segmentation(image: str):
|
|
@@ -380,6 +406,7 @@ def image_to_3d(
|
|
| 380 |
@torch.no_grad()
|
| 381 |
def run_texture(image: Image, mesh_path: str, seed: int, req: gr.Request):
|
| 382 |
height, width = 768, 768
|
|
|
|
| 383 |
cameras = get_orthogonal_camera(
|
| 384 |
elevation_deg=[0, 0, 0, 0, 89.99, -89.99],
|
| 385 |
distance=[1.8] * NUM_VIEWS,
|
|
@@ -464,31 +491,6 @@ def run_texture(image: Image, mesh_path: str, seed: int, req: gr.Request):
|
|
| 464 |
|
| 465 |
return textured_glb_path
|
| 466 |
|
| 467 |
-
# FastAPI endpoint for generating 3D models
|
| 468 |
-
@app.post("/api/generate")
|
| 469 |
-
async def generate_3d_model(request: GenerateRequest, image: UploadFile = File(...), api_key: str = Depends(verify_api_key)):
|
| 470 |
-
try:
|
| 471 |
-
# Save uploaded image to temporary directory
|
| 472 |
-
session_hash = get_random_hex()
|
| 473 |
-
save_dir = os.path.join(TMP_DIR, session_hash)
|
| 474 |
-
os.makedirs(save_dir, exist_ok=True)
|
| 475 |
-
image_path = os.path.join(save_dir, f"input_{get_random_hex()}.png")
|
| 476 |
-
with open(image_path, "wb") as f:
|
| 477 |
-
f.write(await image.read())
|
| 478 |
-
|
| 479 |
-
# Run the full pipeline
|
| 480 |
-
image_seg, mesh_path, textured_glb_path = run_full(image_path, req=None)
|
| 481 |
-
|
| 482 |
-
# Return the file URL for the textured GLB
|
| 483 |
-
file_url = f"/files/{session_hash}/{os.path.basename(textured_glb_path)}"
|
| 484 |
-
return {"file_url": file_url}
|
| 485 |
-
except Exception as e:
|
| 486 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 487 |
-
finally:
|
| 488 |
-
# Clean up temporary directory
|
| 489 |
-
if os.path.exists(save_dir):
|
| 490 |
-
shutil.rmtree(save_dir)
|
| 491 |
-
|
| 492 |
# Gradio interface (unchanged)
|
| 493 |
with gr.Blocks(title="PolyGenixAI", css="body { background-color: #1A1A1A; } .gr-panel { background-color: #2D2D2D; }") as demo:
|
| 494 |
gr.Markdown(HEADER)
|
|
|
|
| 74 |
simplify: bool = True
|
| 75 |
target_face_num: int = DEFAULT_FACE_NUMBER
|
| 76 |
|
|
|
|
| 77 |
HEADER = """
|
| 78 |
# 🌌 PolyGenixAI: Craft 3D Worlds with Cosmic Precision
|
| 79 |
## Unleash Infinite Creativity with AI-Powered 3D Generation by AnvilInteractive Solutions
|
|
|
|
| 157 |
</style>
|
| 158 |
"""
|
| 159 |
|
| 160 |
+
# triposg
|
| 161 |
from image_process import prepare_image
|
| 162 |
from briarmbg import BriaRMBG
|
| 163 |
snapshot_download("briaai/RMBG-1.4", local_dir=RMBG_PRETRAINED_MODEL)
|
|
|
|
| 167 |
snapshot_download("VAST-AI/TripoSG", local_dir=TRIPOSG_PRETRAINED_MODEL)
|
| 168 |
triposg_pipe = TripoSGPipeline.from_pretrained(TRIPOSG_PRETRAINED_MODEL).to(DEVICE, DTYPE)
|
| 169 |
|
| 170 |
+
# mv adapter
|
| 171 |
NUM_VIEWS = 6
|
| 172 |
from inference_ig2mv_sdxl import prepare_pipeline, preprocess_image, remove_bg
|
| 173 |
from mvadapter.utils import get_orthogonal_camera, tensor_to_image, make_image_grid
|
|
|
|
| 197 |
remove_bg_fn = lambda x: remove_bg(x, birefnet, transform_image, DEVICE)
|
| 198 |
|
| 199 |
if not os.path.exists("checkpoints/RealESRGAN_x2plus.pth"):
|
| 200 |
+
hf_hub_download("dtarnow/UPscaler", filename="RealESRGAN_x2plus.pth", local_dir="checkpoints")
|
| 201 |
if not os.path.exists("checkpoints/big-lama.pt"):
|
| 202 |
subprocess.run("wget -P checkpoints/ https://github.com/Sanster/models/releases/download/add_big_lama/big-lama.pt", shell=True, check=True)
|
| 203 |
|
|
|
|
| 253 |
torch.cuda.empty_cache()
|
| 254 |
|
| 255 |
height, width = 768, 768
|
| 256 |
+
# Prepare cameras
|
| 257 |
cameras = get_orthogonal_camera(
|
| 258 |
elevation_deg=[0, 0, 0, 0, 89.99, -89.99],
|
| 259 |
distance=[1.8] * NUM_VIEWS,
|
|
|
|
| 337 |
|
| 338 |
return image_seg, mesh_path, textured_glb_path
|
| 339 |
|
| 340 |
+
# FastAPI endpoint for generating 3D models
|
| 341 |
+
@app.post("/api/generate")
|
| 342 |
+
async def generate_3d_model(request: GenerateRequest, image: UploadFile = File(...), api_key: str = Depends(verify_api_key)):
|
| 343 |
+
try:
|
| 344 |
+
# Save uploaded image to temporary directory
|
| 345 |
+
session_hash = get_random_hex()
|
| 346 |
+
save_dir = os.path.join(TMP_DIR, session_hash)
|
| 347 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 348 |
+
image_path = os.path.join(save_dir, f"input_{get_random_hex()}.png")
|
| 349 |
+
with open(image_path, "wb") as f:
|
| 350 |
+
f.write(await image.read())
|
| 351 |
+
|
| 352 |
+
# Run the full pipeline
|
| 353 |
+
image_seg, mesh_path, textured_glb_path = run_full(image_path, req=None)
|
| 354 |
+
|
| 355 |
+
# Return the file URL for the textured GLB
|
| 356 |
+
file_url = f"/files/{session_hash}/{os.path.basename(textured_glb_path)}"
|
| 357 |
+
return {"file_url": file_url}
|
| 358 |
+
except Exception as e:
|
| 359 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 360 |
+
finally:
|
| 361 |
+
# Clean up temporary directory
|
| 362 |
+
if os.path.exists(save_dir):
|
| 363 |
+
shutil.rmtree(save_dir)
|
| 364 |
+
|
| 365 |
@spaces.GPU()
|
| 366 |
@torch.no_grad()
|
| 367 |
def run_segmentation(image: str):
|
|
|
|
| 406 |
@torch.no_grad()
|
| 407 |
def run_texture(image: Image, mesh_path: str, seed: int, req: gr.Request):
|
| 408 |
height, width = 768, 768
|
| 409 |
+
# Prepare cameras
|
| 410 |
cameras = get_orthogonal_camera(
|
| 411 |
elevation_deg=[0, 0, 0, 0, 89.99, -89.99],
|
| 412 |
distance=[1.8] * NUM_VIEWS,
|
|
|
|
| 491 |
|
| 492 |
return textured_glb_path
|
| 493 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 494 |
# Gradio interface (unchanged)
|
| 495 |
with gr.Blocks(title="PolyGenixAI", css="body { background-color: #1A1A1A; } .gr-panel { background-color: #2D2D2D; }") as demo:
|
| 496 |
gr.Markdown(HEADER)
|