Percy3822 commited on
Commit
cc5532b
·
verified ·
1 Parent(s): 8fa71f3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr, tempfile, os, subprocess, json
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:
9
+ spec = json.load(f)
10
+ except Exception as e:
11
+ return None, f"Invalid JSON: {e}"
12
+
13
+ # Minimal validation
14
+ if "type" not in spec:
15
+ return None, "Spec must include a 'type' field (e.g., 'book')."
16
+ if "name" not in spec:
17
+ spec["name"] = f"{spec['type']}_auto"
18
+
19
+ # Choose generator by type (extend later)
20
+ generator = "generate_book.py" if spec["type"] == "book" else "generate_book.py"
21
+
22
+ out_dir = tempfile.mkdtemp()
23
+ cmd = [BLENDER, "-b", "-P", generator, "--", "--spec", spec_path, "--out", out_dir]
24
+
25
+ try:
26
+ run = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
27
+ logs = run.stdout
28
+ except subprocess.CalledProcessError as e:
29
+ return None, f"Blender failed:\n{e.stdout}"
30
+
31
+ # Return the first .glb in out_dir
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}"
35
+ return glbs[0], "Success."
36
+
37
+ 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-Generated Props (Blender Headless)",
42
+ description="Upload a JSON PropSpec to generate a .glb (book prop for now)."
43
+ )
44
+
45
+ if _name_ == "_main_":
46
+ demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))