Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -11,16 +11,9 @@ from torchvision import transforms
|
|
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 |
-
import asyncio
|
20 |
|
21 |
# Install additional dependencies
|
22 |
subprocess.run("pip install spandrel==0.4.1 --no-deps", shell=True, check=True)
|
23 |
-
subprocess.run("pip install fastapi==0.116.1 uvicorn==0.31.0", shell=True, check=True)
|
24 |
|
25 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
26 |
DTYPE = torch.float16
|
@@ -52,143 +45,6 @@ sys.path.append(os.path.join(TRIPOSG_CODE_DIR, "scripts"))
|
|
52 |
sys.path.append(MV_ADAPTER_CODE_DIR)
|
53 |
sys.path.append(os.path.join(MV_ADAPTER_CODE_DIR, "scripts"))
|
54 |
|
55 |
-
# Initialize FastAPI app
|
56 |
-
app = FastAPI()
|
57 |
-
|
58 |
-
# Mount static files for serving generated models
|
59 |
-
app.mount("/files", StaticFiles(directory=TMP_DIR), name="files")
|
60 |
-
|
61 |
-
# API key authentication
|
62 |
-
api_key_header = APIKeyHeader(name="X-API-Key")
|
63 |
-
VALID_API_KEY = os.getenv("POLYGENIX_API_KEY", "your-secret-api-key")
|
64 |
-
|
65 |
-
async def verify_api_key(api_key: str = Depends(api_key_header)):
|
66 |
-
if api_key != VALID_API_KEY:
|
67 |
-
raise HTTPException(status_code=401, detail="Invalid API key")
|
68 |
-
return api_key
|
69 |
-
|
70 |
-
# API request model
|
71 |
-
class GenerateRequest(BaseModel):
|
72 |
-
seed: int = 0
|
73 |
-
num_inference_steps: int = 50
|
74 |
-
guidance_scale: float = 7.5
|
75 |
-
simplify: bool = True
|
76 |
-
target_face_num: int = DEFAULT_FACE_NUMBER
|
77 |
-
|
78 |
-
# Test endpoint
|
79 |
-
@app.get("/api/test")
|
80 |
-
async def test_endpoint():
|
81 |
-
return {"message": "FastAPI is running"}
|
82 |
-
|
83 |
-
# FastAPI endpoint for generating 3D models
|
84 |
-
@app.post("/api/generate")
|
85 |
-
async def generate_3d_model(request: GenerateRequest, image: UploadFile = File(...), api_key: str = Depends(verify_api_key)):
|
86 |
-
try:
|
87 |
-
# Save uploaded image to temporary directory
|
88 |
-
session_hash = get_random_hex()
|
89 |
-
save_dir = os.path.join(TMP_DIR, session_hash)
|
90 |
-
os.makedirs(save_dir, exist_ok=True)
|
91 |
-
image_path = os.path.join(save_dir, f"input_{get_random_hex()}.png")
|
92 |
-
with open(image_path, "wb") as f:
|
93 |
-
f.write(await image.read())
|
94 |
-
|
95 |
-
# Run the full pipeline
|
96 |
-
image_seg, mesh_path, textured_glb_path = run_full(image_path, req=None)
|
97 |
-
|
98 |
-
# Return the file URL for the textured GLB
|
99 |
-
file_url = f"/files/{session_hash}/{os.path.basename(textured_glb_path)}"
|
100 |
-
return {"file_url": file_url}
|
101 |
-
except Exception as e:
|
102 |
-
raise HTTPException(status_code=500, detail=str(e))
|
103 |
-
finally:
|
104 |
-
# Clean up temporary directory
|
105 |
-
if os.path.exists(save_dir):
|
106 |
-
shutil.rmtree(save_dir)
|
107 |
-
|
108 |
-
# Gradio-related functions
|
109 |
-
HEADER = """
|
110 |
-
# 🌌 PolyGenixAI: Craft 3D Worlds with Cosmic Precision
|
111 |
-
## Unleash Infinite Creativity with AI-Powered 3D Generation by AnvilInteractive Solutions
|
112 |
-
<p style="font-size: 1.1em; color: #A78BFA;">By <a href="https://www.anvilinteractive.com/" style="color: #A78BFA; text-decoration: none; font-weight: bold;">AnvilInteractive Solutions</a></p>
|
113 |
-
## 🚀 Launch Your Creation:
|
114 |
-
1. **Upload an Image** (clear, single-object images shine brightest)
|
115 |
-
2. **Choose a Style Filter** to infuse your unique vision
|
116 |
-
3. Click **Generate 3D Model** to sculpt your mesh
|
117 |
-
4. Click **Apply Texture** to bring your model to life
|
118 |
-
5. **Download GLB** to share your masterpiece
|
119 |
-
<p style="font-size: 0.9em; margin-top: 10px; color: #D1D5DB;">Powered by cutting-edge AI and multi-view technology from AnvilInteractive Solutions. Join our <a href="https://www.anvilinteractive.com/community" style="color: #A78BFA; text-decoration: none;">PolyGenixAI Community</a> to connect with creators and spark inspiration.</p>
|
120 |
-
<style>
|
121 |
-
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap');
|
122 |
-
body {
|
123 |
-
background-color: #1A1A1A !important;
|
124 |
-
font-family: 'Inter', sans-serif !important;
|
125 |
-
color: #D1D5DB !important;
|
126 |
-
}
|
127 |
-
.gr-panel {
|
128 |
-
background-color: #2D2D2D !important;
|
129 |
-
border: 1px solid #7C3AED !important;
|
130 |
-
border-radius: 12px !important;
|
131 |
-
padding: 20px !important;
|
132 |
-
box-shadow: 0 4px 10px rgba(124, 58, 237, 0.2) !important;
|
133 |
-
}
|
134 |
-
.gr-button-primary {
|
135 |
-
background: linear-gradient(45deg, #7C3AED, #A78BFA) !important;
|
136 |
-
color: white !important;
|
137 |
-
border: none !important;
|
138 |
-
border-radius: 8px !important;
|
139 |
-
padding: 12px 24px !important;
|
140 |
-
font-weight: 600 !important;
|
141 |
-
transition: transform 0.2s, box-shadow 0.2s !important;
|
142 |
-
}
|
143 |
-
.gr-button-primary:hover {
|
144 |
-
transform: translateY(-2px) !important;
|
145 |
-
box-shadow: 0 4px 12px rgba(124, 58, 237, 0.5) !important;
|
146 |
-
}
|
147 |
-
.gr-button-secondary {
|
148 |
-
background-color: #4B4B4B !important;
|
149 |
-
color: #D1D5DB !important;
|
150 |
-
border: 1px solid #A78BFA !important;
|
151 |
-
border-radius: 8px !important;
|
152 |
-
padding: 10px 20px !important;
|
153 |
-
transition: transform 0.2s !important;
|
154 |
-
}
|
155 |
-
.gr-button-secondary:hover {
|
156 |
-
transform: translateY(-1px) !important;
|
157 |
-
background-color: #6B6B6B !important;
|
158 |
-
}
|
159 |
-
.gr-accordion {
|
160 |
-
background-color: #2D2D2D !important;
|
161 |
-
border-radius: 8px !important;
|
162 |
-
border: 1px solid #7C3AED !important;
|
163 |
-
}
|
164 |
-
.gr-tab {
|
165 |
-
background-color: #2D2D2D !important;
|
166 |
-
color: #A78BFA !important;
|
167 |
-
border: 1px solid #7C3AED !important;
|
168 |
-
border-radius: 8px !important;
|
169 |
-
margin: 5px !important;
|
170 |
-
}
|
171 |
-
.gr-tab:hover, .gr-tab-selected {
|
172 |
-
background: linear-gradient(45deg, #7C3AED, #A78BFA) !important;
|
173 |
-
color: white !important;
|
174 |
-
}
|
175 |
-
.gr-slider input[type=range]::-webkit-slider-thumb {
|
176 |
-
background-color: #7C3AED !important;
|
177 |
-
border: 2px solid #A78BFA !important;
|
178 |
-
}
|
179 |
-
.gr-dropdown {
|
180 |
-
background-color: #2D2D2D !important;
|
181 |
-
color: #D1D5DB !important;
|
182 |
-
border: 1px solid #A78BFA !important;
|
183 |
-
border-radius: 8px !important;
|
184 |
-
}
|
185 |
-
h1, h3 {
|
186 |
-
color: #A78BFA !important;
|
187 |
-
text-shadow: 0 0 10px rgba(124, 58, 237, 0.5) !important;
|
188 |
-
}
|
189 |
-
</style>
|
190 |
-
"""
|
191 |
-
|
192 |
# triposg
|
193 |
from image_process import prepare_image
|
194 |
from briarmbg import BriaRMBG
|
@@ -230,7 +86,7 @@ remove_bg_fn = lambda x: remove_bg(x, birefnet, transform_image, DEVICE)
|
|
230 |
if not os.path.exists("checkpoints/RealESRGAN_x2plus.pth"):
|
231 |
hf_hub_download("dtarnow/UPscaler", filename="RealESRGAN_x2plus.pth", local_dir="checkpoints")
|
232 |
if not os.path.exists("checkpoints/big-lama.pt"):
|
233 |
-
|
234 |
|
235 |
def start_session(req: gr.Request):
|
236 |
save_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
@@ -495,6 +351,89 @@ def run_texture(image: Image, mesh_path: str, seed: int, req: gr.Request):
|
|
495 |
|
496 |
return textured_glb_path
|
497 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
498 |
# Gradio interface
|
499 |
with gr.Blocks(title="PolyGenixAI", css="body { background-color: #1A1A1A; } .gr-panel { background-color: #2D2D2D; }") as demo:
|
500 |
gr.Markdown(HEADER)
|
@@ -595,13 +534,5 @@ with gr.Blocks(title="PolyGenixAI", css="body { background-color: #1A1A1A; } .gr
|
|
595 |
demo.load(start_session)
|
596 |
demo.unload(end_session)
|
597 |
|
598 |
-
# Run FastAPI and Gradio concurrently
|
599 |
-
async def run_servers():
|
600 |
-
config = uvicorn.Config(app=app, host="0.0.0.0", port=8000)
|
601 |
-
server = uvicorn.Server(config)
|
602 |
-
fastapi_task = asyncio.create_task(server.serve())
|
603 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
604 |
-
await fastapi_task
|
605 |
-
|
606 |
if __name__ == "__main__":
|
607 |
-
|
|
|
11 |
from huggingface_hub import hf_hub_download, snapshot_download
|
12 |
import subprocess
|
13 |
import shutil
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Install additional dependencies
|
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 |
sys.path.append(MV_ADAPTER_CODE_DIR)
|
46 |
sys.path.append(os.path.join(MV_ADAPTER_CODE_DIR, "scripts"))
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
# triposg
|
49 |
from image_process import prepare_image
|
50 |
from briarmbg import BriaRMBG
|
|
|
86 |
if not os.path.exists("checkpoints/RealESRGAN_x2plus.pth"):
|
87 |
hf_hub_download("dtarnow/UPscaler", filename="RealESRGAN_x2plus.pth", local_dir="checkpoints")
|
88 |
if not os.path.exists("checkpoints/big-lama.pt"):
|
89 |
+
subprocess.run("wget -P checkpoints/ https://github.com/Sanster/models/releases/download/add_big_lama/big-lama.pt", shell=True, check=True)
|
90 |
|
91 |
def start_session(req: gr.Request):
|
92 |
save_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
|
|
351 |
|
352 |
return textured_glb_path
|
353 |
|
354 |
+
HEADER = """
|
355 |
+
# 🌌 PolyGenixAI: Craft 3D Worlds with Cosmic Precision
|
356 |
+
## Unleash Infinite Creativity with AI-Powered 3D Generation by AnvilInteractive Solutions
|
357 |
+
<p style="font-size: 1.1em; color: #A78BFA;">By <a href="https://www.anvilinteractive.com/" style="color: #A78BFA; text-decoration: none; font-weight: bold;">AnvilInteractive Solutions</a></p>
|
358 |
+
## 🚀 Launch Your Creation:
|
359 |
+
1. **Upload an Image** (clear, single-object images shine brightest)
|
360 |
+
2. **Choose a Style Filter** to infuse your unique vision
|
361 |
+
3. Click **Generate 3D Model** to sculpt your mesh
|
362 |
+
4. Click **Apply Texture** to bring your model to life
|
363 |
+
5. **Download GLB** to share your masterpiece
|
364 |
+
<p style="font-size: 0.9em; margin-top: 10px; color: #D1D5DB;">Powered by cutting-edge AI and multi-view technology from AnvilInteractive Solutions. Join our <a href="https://www.anvilinteractive.com/community" style="color: #A78BFA; text-decoration: none;">PolyGenixAI Community</a> to connect with creators and spark inspiration.</p>
|
365 |
+
<style>
|
366 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap');
|
367 |
+
body {
|
368 |
+
background-color: #1A1A1A !important;
|
369 |
+
font-family: 'Inter', sans-serif !important;
|
370 |
+
color: #D1D5DB !important;
|
371 |
+
}
|
372 |
+
.gr-panel {
|
373 |
+
background-color: #2D2D2D !important;
|
374 |
+
border: 1px solid #7C3AED !important;
|
375 |
+
border-radius: 12px !important;
|
376 |
+
padding: 20px !important;
|
377 |
+
box-shadow: 0 4px 10px rgba(124, 58, 237, 0.2) !important;
|
378 |
+
}
|
379 |
+
.gr-button-primary {
|
380 |
+
background: linear-gradient(45deg, #7C3AED, #A78BFA) !important;
|
381 |
+
color: white !important;
|
382 |
+
border: none !important;
|
383 |
+
border-radius: 8px !important;
|
384 |
+
padding: 12px 24px !important;
|
385 |
+
font-weight: 600 !important;
|
386 |
+
transition: transform 0.2s, box-shadow 0.2s !important;
|
387 |
+
}
|
388 |
+
.gr-button-primary:hover {
|
389 |
+
transform: translateY(-2px) !important;
|
390 |
+
box-shadow: 0 4px 12px rgba(124, 58, 237, 0.5) !important;
|
391 |
+
}
|
392 |
+
.gr-button-secondary {
|
393 |
+
background-color: #4B4B4B !important;
|
394 |
+
color: #D1D5DB !important;
|
395 |
+
border: 1px solid #A78BFA !important;
|
396 |
+
border-radius: 8px !important;
|
397 |
+
padding: 10px 20px !important;
|
398 |
+
transition: transform 0.2s !important;
|
399 |
+
}
|
400 |
+
.gr-button-secondary:hover {
|
401 |
+
transform: translateY(-1px) !important;
|
402 |
+
background-color: #6B6B6B !important;
|
403 |
+
}
|
404 |
+
.gr-accordion {
|
405 |
+
background-color: #2D2D2D !important;
|
406 |
+
border-radius: 8px !important;
|
407 |
+
border: 1px solid #7C3AED !important;
|
408 |
+
}
|
409 |
+
.gr-tab {
|
410 |
+
background-color: #2D2D2D !important;
|
411 |
+
color: #A78BFA !important;
|
412 |
+
border: 1px solid #7C3AED !important;
|
413 |
+
border-radius: 8px !important;
|
414 |
+
margin: 5px !important;
|
415 |
+
}
|
416 |
+
.gr-tab:hover, .gr-tab-selected {
|
417 |
+
background: linear-gradient(45deg, #7C3AED, #A78BFA) !important;
|
418 |
+
color: white !important;
|
419 |
+
}
|
420 |
+
.gr-slider input[type=range]::-webkit-slider-thumb {
|
421 |
+
background-color: #7C3AED !important;
|
422 |
+
border: 2px solid #A78BFA !important;
|
423 |
+
}
|
424 |
+
.gr-dropdown {
|
425 |
+
background-color: #2D2D2D !important;
|
426 |
+
color: #D1D5DB !important;
|
427 |
+
border: 1px solid #A78BFA !important;
|
428 |
+
border-radius: 8px !important;
|
429 |
+
}
|
430 |
+
h1, h3 {
|
431 |
+
color: #A78BFA !important;
|
432 |
+
text-shadow: 0 0 10px rgba(124, 58, 237, 0.5) !important;
|
433 |
+
}
|
434 |
+
</style>
|
435 |
+
"""
|
436 |
+
|
437 |
# Gradio interface
|
438 |
with gr.Blocks(title="PolyGenixAI", css="body { background-color: #1A1A1A; } .gr-panel { background-color: #2D2D2D; }") as demo:
|
439 |
gr.Markdown(HEADER)
|
|
|
534 |
demo.load(start_session)
|
535 |
demo.unload(end_session)
|
536 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
537 |
if __name__ == "__main__":
|
538 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|