Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,9 +11,15 @@ from torchvision import transforms
|
|
| 11 |
from huggingface_hub import hf_hub_download, snapshot_download
|
| 12 |
import subprocess
|
| 13 |
import shutil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
subprocess.run("pip install spandrel==0.4.1 --no-deps", shell=True, check=True)
|
|
|
|
| 17 |
|
| 18 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
DTYPE = torch.float16
|
|
@@ -45,6 +51,30 @@ sys.path.append(os.path.join(TRIPOSG_CODE_DIR, "scripts"))
|
|
| 45 |
sys.path.append(MV_ADAPTER_CODE_DIR)
|
| 46 |
sys.path.append(os.path.join(MV_ADAPTER_CODE_DIR, "scripts"))
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
HEADER = """
|
| 49 |
# 🌌 PolyGenixAI: Craft 3D Worlds with Cosmic Precision
|
| 50 |
## Unleash Infinite Creativity with AI-Powered 3D Generation by AnvilInteractive Solutions
|
|
@@ -128,7 +158,7 @@ HEADER = """
|
|
| 128 |
</style>
|
| 129 |
"""
|
| 130 |
|
| 131 |
-
#
|
| 132 |
from image_process import prepare_image
|
| 133 |
from briarmbg import BriaRMBG
|
| 134 |
snapshot_download("briaai/RMBG-1.4", local_dir=RMBG_PRETRAINED_MODEL)
|
|
@@ -138,7 +168,6 @@ from triposg.pipelines.pipeline_triposg import TripoSGPipeline
|
|
| 138 |
snapshot_download("VAST-AI/TripoSG", local_dir=TRIPOSG_PRETRAINED_MODEL)
|
| 139 |
triposg_pipe = TripoSGPipeline.from_pretrained(TRIPOSG_PRETRAINED_MODEL).to(DEVICE, DTYPE)
|
| 140 |
|
| 141 |
-
# mv adapter
|
| 142 |
NUM_VIEWS = 6
|
| 143 |
from inference_ig2mv_sdxl import prepare_pipeline, preprocess_image, remove_bg
|
| 144 |
from mvadapter.utils import get_orthogonal_camera, tensor_to_image, make_image_grid
|
|
@@ -224,7 +253,6 @@ def run_full(image: str, req: gr.Request):
|
|
| 224 |
torch.cuda.empty_cache()
|
| 225 |
|
| 226 |
height, width = 768, 768
|
| 227 |
-
# Prepare cameras
|
| 228 |
cameras = get_orthogonal_camera(
|
| 229 |
elevation_deg=[0, 0, 0, 0, 89.99, -89.99],
|
| 230 |
distance=[1.8] * NUM_VIEWS,
|
|
@@ -307,7 +335,6 @@ def run_full(image: str, req: gr.Request):
|
|
| 307 |
)
|
| 308 |
|
| 309 |
return image_seg, mesh_path, textured_glb_path
|
| 310 |
-
|
| 311 |
|
| 312 |
@spaces.GPU()
|
| 313 |
@torch.no_grad()
|
|
@@ -353,7 +380,6 @@ def image_to_3d(
|
|
| 353 |
@torch.no_grad()
|
| 354 |
def run_texture(image: Image, mesh_path: str, seed: int, req: gr.Request):
|
| 355 |
height, width = 768, 768
|
| 356 |
-
# Prepare cameras
|
| 357 |
cameras = get_orthogonal_camera(
|
| 358 |
elevation_deg=[0, 0, 0, 0, 89.99, -89.99],
|
| 359 |
distance=[1.8] * NUM_VIEWS,
|
|
@@ -421,7 +447,7 @@ def run_texture(image: Image, mesh_path: str, seed: int, req: gr.Request):
|
|
| 421 |
from texture import TexturePipeline, ModProcessConfig
|
| 422 |
texture_pipe = TexturePipeline(
|
| 423 |
upscaler_ckpt_path="checkpoints/RealESRGAN_x2plus.pth",
|
| 424 |
-
|
| 425 |
device=DEVICE,
|
| 426 |
)
|
| 427 |
|
|
@@ -438,7 +464,33 @@ def run_texture(image: Image, mesh_path: str, seed: int, req: gr.Request):
|
|
| 438 |
|
| 439 |
return textured_glb_path
|
| 440 |
|
| 441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
with gr.Blocks(title="PolyGenixAI", css="body { background-color: #1A1A1A; } .gr-panel { background-color: #2D2D2D; }") as demo:
|
| 443 |
gr.Markdown(HEADER)
|
| 444 |
|
|
@@ -544,4 +596,6 @@ with gr.Blocks(title="PolyGenixAI", css="body { background-color: #1A1A1A; } .gr
|
|
| 544 |
demo.load(start_session)
|
| 545 |
demo.unload(end_session)
|
| 546 |
|
| 547 |
-
|
|
|
|
|
|
|
|
|
| 11 |
from huggingface_hub import hf_hub_download, snapshot_download
|
| 12 |
import subprocess
|
| 13 |
import shutil
|
| 14 |
+
from fastapi import FastAPI, HTTPException, Depends, File, UploadFile
|
| 15 |
+
from fastapi.security import APIKeyHeader
|
| 16 |
+
from fastapi.staticfiles import StaticFiles
|
| 17 |
+
from pydantic import BaseModel
|
| 18 |
+
import uvicorn
|
| 19 |
|
| 20 |
+
# Install additional dependencies
|
| 21 |
subprocess.run("pip install spandrel==0.4.1 --no-deps", shell=True, check=True)
|
| 22 |
+
subprocess.run("pip install fastapi uvicorn", shell=True, check=True)
|
| 23 |
|
| 24 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
DTYPE = torch.float16
|
|
|
|
| 51 |
sys.path.append(MV_ADAPTER_CODE_DIR)
|
| 52 |
sys.path.append(os.path.join(MV_ADAPTER_CODE_DIR, "scripts"))
|
| 53 |
|
| 54 |
+
# Initialize FastAPI app
|
| 55 |
+
app = FastAPI()
|
| 56 |
+
|
| 57 |
+
# Mount static files for serving generated models
|
| 58 |
+
app.mount("/files", StaticFiles(directory=TMP_DIR), name="files")
|
| 59 |
+
|
| 60 |
+
# API key authentication
|
| 61 |
+
api_key_header = APIKeyHeader(name="X-API-Key")
|
| 62 |
+
VALID_API_KEY = os.getenv("POLYGENIX_API_KEY", "your-secret-api-key") # Set in Hugging Face Space secrets
|
| 63 |
+
|
| 64 |
+
async def verify_api_key(api_key: str = Depends(api_key_header)):
|
| 65 |
+
if api_key != VALID_API_KEY:
|
| 66 |
+
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 67 |
+
return api_key
|
| 68 |
+
|
| 69 |
+
# API request model
|
| 70 |
+
class GenerateRequest(BaseModel):
|
| 71 |
+
seed: int = 0
|
| 72 |
+
num_inference_steps: int = 50
|
| 73 |
+
guidance_scale: float = 7.5
|
| 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 |
</style>
|
| 159 |
"""
|
| 160 |
|
| 161 |
+
# TripoSG and MV-Adapter setup (unchanged)
|
| 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 |
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
|
|
|
|
| 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,
|
|
|
|
| 335 |
)
|
| 336 |
|
| 337 |
return image_seg, mesh_path, textured_glb_path
|
|
|
|
| 338 |
|
| 339 |
@spaces.GPU()
|
| 340 |
@torch.no_grad()
|
|
|
|
| 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,
|
|
|
|
| 447 |
from texture import TexturePipeline, ModProcessConfig
|
| 448 |
texture_pipe = TexturePipeline(
|
| 449 |
upscaler_ckpt_path="checkpoints/RealESRGAN_x2plus.pth",
|
| 450 |
+
inpaint_cckpt_path="checkpoints/big-lama.pt",
|
| 451 |
device=DEVICE,
|
| 452 |
)
|
| 453 |
|
|
|
|
| 464 |
|
| 465 |
return textured_glb_path
|
| 466 |
|
| 467 |
+
# FastAPI endpoint for generating 3D models
|
| 468 |
+
@spaces.GPU(duration=180)
|
| 469 |
+
@app.post("/api/generate")
|
| 470 |
+
async def generate_3d_model(request: GenerateRequest, image: UploadFile = File(...), api_key: str = Depends(verify_api_key)):
|
| 471 |
+
try:
|
| 472 |
+
# Save uploaded image to temporary directory
|
| 473 |
+
session_hash = get_random_hex()
|
| 474 |
+
save_dir = os.path.join(TMP_DIR, session_hash)
|
| 475 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 476 |
+
image_path = os.path.join(save_dir, f"input_{get_random_hex()}.png")
|
| 477 |
+
with open(image_path, "wb") as f:
|
| 478 |
+
f.write(await image.read())
|
| 479 |
+
|
| 480 |
+
# Run the full pipeline
|
| 481 |
+
image_seg, mesh_path, textured_glb_path = run_full(image_path, req=None)
|
| 482 |
+
|
| 483 |
+
# Return the file URL for the textured GLB
|
| 484 |
+
file_url = f"/files/{session_hash}/{os.path.basename(textured_glb_path)}"
|
| 485 |
+
return {"file_url": file_url}
|
| 486 |
+
except Exception as e:
|
| 487 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 488 |
+
finally:
|
| 489 |
+
# Clean up temporary directory
|
| 490 |
+
if os.path.exists(save_dir):
|
| 491 |
+
shutil.rmtree(save_dir)
|
| 492 |
+
|
| 493 |
+
# Gradio interface (unchanged)
|
| 494 |
with gr.Blocks(title="PolyGenixAI", css="body { background-color: #1A1A1A; } .gr-panel { background-color: #2D2D2D; }") as demo:
|
| 495 |
gr.Markdown(HEADER)
|
| 496 |
|
|
|
|
| 596 |
demo.load(start_session)
|
| 597 |
demo.unload(end_session)
|
| 598 |
|
| 599 |
+
# Run both Gradio and FastAPI
|
| 600 |
+
if __name__ == "__main__":
|
| 601 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|