Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from torchvision import transforms
|
|
| 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)
|
|
@@ -204,16 +205,27 @@ def run_full(image: str, seed: int = 0, num_inference_steps: int = 50, guidance_
|
|
| 204 |
return image_seg, mesh_path, textured_glb_path
|
| 205 |
|
| 206 |
def gradio_generate(image: str, seed: int = 0, num_inference_steps: int = 50, guidance_scale: float = 7.5, simplify: bool = True, target_face_num: int = DEFAULT_FACE_NUMBER):
|
| 207 |
-
if not image or not os.path.exists(image):
|
| 208 |
-
raise ValueError("Invalid or missing image file")
|
| 209 |
-
|
| 210 |
# Verify API key
|
| 211 |
api_key = os.getenv("POLYGENIX_API_KEY", "your-secret-api-key")
|
| 212 |
request = gr.Request()
|
| 213 |
if not request.headers.get("x-api-key") == api_key:
|
| 214 |
raise ValueError("Invalid API key")
|
| 215 |
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
session_hash = os.path.basename(os.path.dirname(textured_glb_path))
|
| 218 |
return {"file_url": f"/files/{session_hash}/{os.path.basename(textured_glb_path)}"}
|
| 219 |
|
|
@@ -362,7 +374,7 @@ def run_texture(image: Image, mesh_path: str, seed: int, req: gr.Request):
|
|
| 362 |
api_interface = gr.Interface(
|
| 363 |
fn=gradio_generate,
|
| 364 |
inputs=[
|
| 365 |
-
gr.Image(type="filepath", label="Image"),
|
| 366 |
gr.Number(label="Seed", value=0, precision=0),
|
| 367 |
gr.Number(label="Inference Steps", value=50, precision=0),
|
| 368 |
gr.Number(label="Guidance Scale", value=7.5),
|
|
|
|
| 11 |
from huggingface_hub import hf_hub_download, snapshot_download
|
| 12 |
import subprocess
|
| 13 |
import shutil
|
| 14 |
+
import base64
|
| 15 |
|
| 16 |
# Install additional dependencies
|
| 17 |
subprocess.run("pip install spandrel==0.4.1 --no-deps", shell=True, check=True)
|
|
|
|
| 205 |
return image_seg, mesh_path, textured_glb_path
|
| 206 |
|
| 207 |
def gradio_generate(image: str, seed: int = 0, num_inference_steps: int = 50, guidance_scale: float = 7.5, simplify: bool = True, target_face_num: int = DEFAULT_FACE_NUMBER):
|
|
|
|
|
|
|
|
|
|
| 208 |
# Verify API key
|
| 209 |
api_key = os.getenv("POLYGENIX_API_KEY", "your-secret-api-key")
|
| 210 |
request = gr.Request()
|
| 211 |
if not request.headers.get("x-api-key") == api_key:
|
| 212 |
raise ValueError("Invalid API key")
|
| 213 |
|
| 214 |
+
# Handle base64 image
|
| 215 |
+
if image.startswith("data:image"):
|
| 216 |
+
# Extract base64 data (e.g., "data:image/jpeg;base64,...")
|
| 217 |
+
base64_string = image.split(",")[1]
|
| 218 |
+
image_data = base64.b64decode(base64_string)
|
| 219 |
+
# Save to temporary file
|
| 220 |
+
temp_image_path = os.path.join(TMP_DIR, f"input_{get_random_hex()}.png")
|
| 221 |
+
with open(temp_image_path, "wb") as f:
|
| 222 |
+
f.write(image_data)
|
| 223 |
+
else:
|
| 224 |
+
temp_image_path = image
|
| 225 |
+
if not os.path.exists(temp_image_path):
|
| 226 |
+
raise ValueError("Invalid or missing image file")
|
| 227 |
+
|
| 228 |
+
image_seg, mesh_path, textured_glb_path = run_full(temp_image_path, seed, num_inference_steps, guidance_scale, simplify, target_face_num, req=None)
|
| 229 |
session_hash = os.path.basename(os.path.dirname(textured_glb_path))
|
| 230 |
return {"file_url": f"/files/{session_hash}/{os.path.basename(textured_glb_path)}"}
|
| 231 |
|
|
|
|
| 374 |
api_interface = gr.Interface(
|
| 375 |
fn=gradio_generate,
|
| 376 |
inputs=[
|
| 377 |
+
gr.Image(type="filepath", label="Image"), # Accepts base64 strings or file paths
|
| 378 |
gr.Number(label="Seed", value=0, precision=0),
|
| 379 |
gr.Number(label="Inference Steps", value=50, precision=0),
|
| 380 |
gr.Number(label="Guidance Scale", value=7.5),
|