Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
-
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
BLENDER = "blender" # symlinked in Dockerfile
|
| 4 |
|
| 5 |
def build_prop(spec_file):
|
|
|
|
| 6 |
try:
|
| 7 |
spec_path = spec_file.name
|
| 8 |
with open(spec_path, "r") as f:
|
|
@@ -10,14 +12,15 @@ def build_prop(spec_file):
|
|
| 10 |
except Exception as e:
|
| 11 |
return None, f"Invalid JSON: {e}"
|
| 12 |
|
| 13 |
-
# Minimal
|
| 14 |
-
|
|
|
|
| 15 |
return None, "Spec must include a 'type' field (e.g., 'book')."
|
| 16 |
if "name" not in spec:
|
| 17 |
-
spec["name"] = f"{
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
generator = "generate_book.py" if
|
| 21 |
|
| 22 |
out_dir = tempfile.mkdtemp()
|
| 23 |
cmd = [BLENDER, "-b", "-P", generator, "--", "--spec", spec_path, "--out", out_dir]
|
|
@@ -28,7 +31,7 @@ def build_prop(spec_file):
|
|
| 28 |
except subprocess.CalledProcessError as e:
|
| 29 |
return None, f"Blender failed:\n{e.stdout}"
|
| 30 |
|
| 31 |
-
# Return
|
| 32 |
glbs = [os.path.join(out_dir, f) for f in os.listdir(out_dir) if f.lower().endswith(".glb")]
|
| 33 |
if not glbs:
|
| 34 |
return None, f"No .glb exported.\nLogs:\n{logs}"
|
|
@@ -38,8 +41,9 @@ demo = gr.Interface(
|
|
| 38 |
fn=build_prop,
|
| 39 |
inputs=gr.File(label="Upload PropSpec JSON"),
|
| 40 |
outputs=[gr.File(label="Generated .glb"), gr.Textbox(label="Log")],
|
| 41 |
-
title="Code
|
| 42 |
-
description="Upload a JSON PropSpec
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
if _name_ == "_main_":
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tempfile, os, subprocess, json
|
| 3 |
|
| 4 |
BLENDER = "blender" # symlinked in Dockerfile
|
| 5 |
|
| 6 |
def build_prop(spec_file):
|
| 7 |
+
# Load and sanity-check JSON
|
| 8 |
try:
|
| 9 |
spec_path = spec_file.name
|
| 10 |
with open(spec_path, "r") as f:
|
|
|
|
| 12 |
except Exception as e:
|
| 13 |
return None, f"Invalid JSON: {e}"
|
| 14 |
|
| 15 |
+
# Minimal schema checks
|
| 16 |
+
ptype = spec.get("type")
|
| 17 |
+
if not ptype:
|
| 18 |
return None, "Spec must include a 'type' field (e.g., 'book')."
|
| 19 |
if "name" not in spec:
|
| 20 |
+
spec["name"] = f"{ptype}_auto"
|
| 21 |
|
| 22 |
+
# Route to generator (extend here with more types later)
|
| 23 |
+
generator = "generate_book.py" if ptype == "book" else "generate_book.py"
|
| 24 |
|
| 25 |
out_dir = tempfile.mkdtemp()
|
| 26 |
cmd = [BLENDER, "-b", "-P", generator, "--", "--spec", spec_path, "--out", out_dir]
|
|
|
|
| 31 |
except subprocess.CalledProcessError as e:
|
| 32 |
return None, f"Blender failed:\n{e.stdout}"
|
| 33 |
|
| 34 |
+
# Return first .glb produced
|
| 35 |
glbs = [os.path.join(out_dir, f) for f in os.listdir(out_dir) if f.lower().endswith(".glb")]
|
| 36 |
if not glbs:
|
| 37 |
return None, f"No .glb exported.\nLogs:\n{logs}"
|
|
|
|
| 41 |
fn=build_prop,
|
| 42 |
inputs=gr.File(label="Upload PropSpec JSON"),
|
| 43 |
outputs=[gr.File(label="Generated .glb"), gr.Textbox(label="Log")],
|
| 44 |
+
title="Code‑Generated Props (Blender Headless)",
|
| 45 |
+
description="Upload a JSON PropSpec (supports 'book' for now) and download a .glb.",
|
| 46 |
+
allow_flagging="never" # prevent write-permission errors on HF Spaces
|
| 47 |
)
|
| 48 |
|
| 49 |
if _name_ == "_main_":
|