bolajiev commited on
Commit ·
c1ec90c
1
Parent(s): 1707749
Add Style/Motion dropdowns, remove mock checkbox, multi-object FEWSHOT
Browse files- app.py +26 -19
- compiler.py +13 -5
- llm.py +33 -0
app.py
CHANGED
|
@@ -49,28 +49,25 @@ EXAMPLES = [
|
|
| 49 |
]
|
| 50 |
|
| 51 |
|
| 52 |
-
def generate(prompt: str,
|
| 53 |
prompt = (prompt or "").strip()
|
| 54 |
if not prompt:
|
| 55 |
return gr.update(), gr.update(), gr.update(), "Type a prompt first."
|
| 56 |
if len(prompt) > MAX_PROMPT_CHARS:
|
| 57 |
return gr.update(), gr.update(), gr.update(), f"Prompt too long — keep it under {MAX_PROMPT_CHARS} chars."
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
raw = mock_scene_json(prompt)
|
| 62 |
-
note = "
|
| 63 |
-
else:
|
| 64 |
-
try:
|
| 65 |
-
raw = _generate_json(prompt)
|
| 66 |
-
note = "Rendered."
|
| 67 |
-
except Exception as e: # graceful fallback keeps the demo alive
|
| 68 |
-
log.error("LLM error, falling back to mock: %s", e, exc_info=True)
|
| 69 |
-
raw = mock_scene_json(prompt)
|
| 70 |
-
note = f"Model error, used mock fallback. ({type(e).__name__})"
|
| 71 |
|
| 72 |
scene = build_scene(extract_json(raw))
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
pretty = json.dumps(scene.model_dump(), indent=2)
|
| 75 |
return iframe(html), html, pretty, note
|
| 76 |
|
|
@@ -82,8 +79,13 @@ def _initial():
|
|
| 82 |
return iframe(html), html, json.dumps(scene.model_dump(), indent=2), "Demo scene."
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
| 85 |
with gr.Blocks(title="ThreeGen", theme=gr.themes.Soft()) as demo:
|
| 86 |
-
gr.
|
|
|
|
|
|
|
|
|
|
| 87 |
gr.Markdown(
|
| 88 |
"A tiny model writes a JSON scene graph; we compile it to clean Three.js. "
|
| 89 |
"Drag the preview to orbit. Copy the standalone HTML and open it anywhere."
|
|
@@ -95,12 +97,17 @@ with gr.Blocks(title="ThreeGen", theme=gr.themes.Soft()) as demo:
|
|
| 95 |
label="Describe a 3D scene", lines=3,
|
| 96 |
placeholder="neon low-poly tree, slowly rotating",
|
| 97 |
)
|
| 98 |
-
|
| 99 |
-
btn = gr.Button("Generate", variant="primary")
|
| 100 |
-
mock = gr.Checkbox(label="Mock (no GPU)", value=False)
|
| 101 |
with gr.Row():
|
| 102 |
glow = gr.Checkbox(label="Glow", value=True)
|
| 103 |
glow_strength = gr.Slider(0.0, 2.0, value=0.9, step=0.1, label="Glow strength")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
gr.Examples(examples=EXAMPLES, inputs=prompt)
|
| 105 |
status = gr.Markdown("")
|
| 106 |
|
|
@@ -118,8 +125,8 @@ with gr.Blocks(title="ThreeGen", theme=gr.themes.Soft()) as demo:
|
|
| 118 |
scene_json = gr.Code(language="json", label="scene graph")
|
| 119 |
|
| 120 |
outputs = [preview, code, scene_json, status]
|
| 121 |
-
btn.click(generate, [prompt,
|
| 122 |
-
prompt.submit(generate, [prompt,
|
| 123 |
demo.load(_initial, None, outputs)
|
| 124 |
|
| 125 |
|
|
|
|
| 49 |
]
|
| 50 |
|
| 51 |
|
| 52 |
+
def generate(prompt: str, glow: bool, glow_strength: float, style: str, motion: str):
|
| 53 |
prompt = (prompt or "").strip()
|
| 54 |
if not prompt:
|
| 55 |
return gr.update(), gr.update(), gr.update(), "Type a prompt first."
|
| 56 |
if len(prompt) > MAX_PROMPT_CHARS:
|
| 57 |
return gr.update(), gr.update(), gr.update(), f"Prompt too long — keep it under {MAX_PROMPT_CHARS} chars."
|
| 58 |
|
| 59 |
+
try:
|
| 60 |
+
raw = mock_scene_json(prompt) if os.environ.get("MOCK") else _generate_json(prompt)
|
| 61 |
+
note = "Rendered."
|
| 62 |
+
except Exception as e:
|
| 63 |
+
log.error("LLM error, falling back to mock: %s", e, exc_info=True)
|
| 64 |
raw = mock_scene_json(prompt)
|
| 65 |
+
note = f"Fallback ({type(e).__name__})."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
scene = build_scene(extract_json(raw))
|
| 68 |
+
if motion != "auto":
|
| 69 |
+
scene.animation.type = motion
|
| 70 |
+
html = compile_html(scene, glow=glow, glow_strength=glow_strength, style=style)
|
| 71 |
pretty = json.dumps(scene.model_dump(), indent=2)
|
| 72 |
return iframe(html), html, pretty, note
|
| 73 |
|
|
|
|
| 79 |
return iframe(html), html, json.dumps(scene.model_dump(), indent=2), "Demo scene."
|
| 80 |
|
| 81 |
|
| 82 |
+
_LOGO = os.path.exists("logo.png")
|
| 83 |
+
|
| 84 |
with gr.Blocks(title="ThreeGen", theme=gr.themes.Soft()) as demo:
|
| 85 |
+
with gr.Row():
|
| 86 |
+
if _LOGO:
|
| 87 |
+
gr.Image("logo.png", height=64, show_label=False, container=False)
|
| 88 |
+
gr.Markdown("# 🧊 ThreeGen — text → 3D, live preview, copy code")
|
| 89 |
gr.Markdown(
|
| 90 |
"A tiny model writes a JSON scene graph; we compile it to clean Three.js. "
|
| 91 |
"Drag the preview to orbit. Copy the standalone HTML and open it anywhere."
|
|
|
|
| 97 |
label="Describe a 3D scene", lines=3,
|
| 98 |
placeholder="neon low-poly tree, slowly rotating",
|
| 99 |
)
|
| 100 |
+
btn = gr.Button("Generate", variant="primary")
|
|
|
|
|
|
|
| 101 |
with gr.Row():
|
| 102 |
glow = gr.Checkbox(label="Glow", value=True)
|
| 103 |
glow_strength = gr.Slider(0.0, 2.0, value=0.9, step=0.1, label="Glow strength")
|
| 104 |
+
with gr.Row():
|
| 105 |
+
style = gr.Dropdown(
|
| 106 |
+
["realistic", "flat", "wireframe", "toon"],
|
| 107 |
+
value="realistic", label="Style")
|
| 108 |
+
motion = gr.Dropdown(
|
| 109 |
+
["auto", "none", "rotate", "float", "orbit"],
|
| 110 |
+
value="auto", label="Motion")
|
| 111 |
gr.Examples(examples=EXAMPLES, inputs=prompt)
|
| 112 |
status = gr.Markdown("")
|
| 113 |
|
|
|
|
| 125 |
scene_json = gr.Code(language="json", label="scene graph")
|
| 126 |
|
| 127 |
outputs = [preview, code, scene_json, status]
|
| 128 |
+
btn.click(generate, [prompt, glow, glow_strength, style, motion], outputs)
|
| 129 |
+
prompt.submit(generate, [prompt, glow, glow_strength, style, motion], outputs)
|
| 130 |
demo.load(_initial, None, outputs)
|
| 131 |
|
| 132 |
|
compiler.py
CHANGED
|
@@ -47,9 +47,17 @@ def geometry_js(o: Obj) -> str:
|
|
| 47 |
return "new THREE.BoxGeometry(1, 1, 1)"
|
| 48 |
|
| 49 |
|
| 50 |
-
def material_js(o: Obj) -> str:
|
| 51 |
col = _hex0x(o.color)
|
| 52 |
emi = _hex0x(o.emissive)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
if o.material == "basic":
|
| 54 |
return f"new THREE.MeshBasicMaterial({{ color: {col} }})"
|
| 55 |
if o.material == "phong":
|
|
@@ -60,14 +68,14 @@ def material_js(o: Obj) -> str:
|
|
| 60 |
f"metalness: {o.metalness}, roughness: {o.roughness} }})")
|
| 61 |
|
| 62 |
|
| 63 |
-
def objects_js(scene: Scene) -> str:
|
| 64 |
lines = []
|
| 65 |
for i, o in enumerate(scene.objects):
|
| 66 |
px, py, pz = o.position
|
| 67 |
rx, ry, rz = o.rotation
|
| 68 |
sx, sy, sz = o.scale
|
| 69 |
lines += [
|
| 70 |
-
f"const mesh{i} = new THREE.Mesh({geometry_js(o)}, {material_js(o)});",
|
| 71 |
f"mesh{i}.position.set({px}, {py}, {pz});",
|
| 72 |
f"mesh{i}.rotation.set({rx}, {ry}, {rz});",
|
| 73 |
f"mesh{i}.scale.set({sx}, {sy}, {sz});",
|
|
@@ -196,11 +204,11 @@ TEMPLATE = """<!DOCTYPE html>
|
|
| 196 |
"""
|
| 197 |
|
| 198 |
|
| 199 |
-
def compile_html(scene: Scene, glow: bool = True, glow_strength: float = 0.9) -> str:
|
| 200 |
html = TEMPLATE
|
| 201 |
html = html.replace("__BG__", scene.background)
|
| 202 |
html = html.replace("__LIGHTS__", lights_js(scene))
|
| 203 |
-
html = html.replace("__OBJECTS__", objects_js(scene))
|
| 204 |
html = html.replace("__ANIM__", animation_js(scene.animation))
|
| 205 |
html = html.replace("__USE_BLOOM__", "true" if glow else "false")
|
| 206 |
html = html.replace("__BLOOM_STRENGTH__", str(float(glow_strength)))
|
|
|
|
| 47 |
return "new THREE.BoxGeometry(1, 1, 1)"
|
| 48 |
|
| 49 |
|
| 50 |
+
def material_js(o: Obj, style: str = "realistic") -> str:
|
| 51 |
col = _hex0x(o.color)
|
| 52 |
emi = _hex0x(o.emissive)
|
| 53 |
+
if style == "wireframe":
|
| 54 |
+
return f"new THREE.MeshStandardMaterial({{ color: {col}, wireframe: true }})"
|
| 55 |
+
if style == "toon":
|
| 56 |
+
return f"new THREE.MeshToonMaterial({{ color: {col}, emissive: {emi} }})"
|
| 57 |
+
if style == "flat":
|
| 58 |
+
return (f"new THREE.MeshStandardMaterial({{ color: {col}, emissive: {emi}, "
|
| 59 |
+
f"metalness: {o.metalness}, roughness: {o.roughness}, flatShading: true }})")
|
| 60 |
+
# realistic — honour per-object material field
|
| 61 |
if o.material == "basic":
|
| 62 |
return f"new THREE.MeshBasicMaterial({{ color: {col} }})"
|
| 63 |
if o.material == "phong":
|
|
|
|
| 68 |
f"metalness: {o.metalness}, roughness: {o.roughness} }})")
|
| 69 |
|
| 70 |
|
| 71 |
+
def objects_js(scene: Scene, style: str = "realistic") -> str:
|
| 72 |
lines = []
|
| 73 |
for i, o in enumerate(scene.objects):
|
| 74 |
px, py, pz = o.position
|
| 75 |
rx, ry, rz = o.rotation
|
| 76 |
sx, sy, sz = o.scale
|
| 77 |
lines += [
|
| 78 |
+
f"const mesh{i} = new THREE.Mesh({geometry_js(o)}, {material_js(o, style)});",
|
| 79 |
f"mesh{i}.position.set({px}, {py}, {pz});",
|
| 80 |
f"mesh{i}.rotation.set({rx}, {ry}, {rz});",
|
| 81 |
f"mesh{i}.scale.set({sx}, {sy}, {sz});",
|
|
|
|
| 204 |
"""
|
| 205 |
|
| 206 |
|
| 207 |
+
def compile_html(scene: Scene, glow: bool = True, glow_strength: float = 0.9, style: str = "realistic") -> str:
|
| 208 |
html = TEMPLATE
|
| 209 |
html = html.replace("__BG__", scene.background)
|
| 210 |
html = html.replace("__LIGHTS__", lights_js(scene))
|
| 211 |
+
html = html.replace("__OBJECTS__", objects_js(scene, style))
|
| 212 |
html = html.replace("__ANIM__", animation_js(scene.animation))
|
| 213 |
html = html.replace("__USE_BLOOM__", "true" if glow else "false")
|
| 214 |
html = html.replace("__BLOOM_STRENGTH__", str(float(glow_strength)))
|
llm.py
CHANGED
|
@@ -64,6 +64,39 @@ FEWSHOT = [
|
|
| 64 |
],
|
| 65 |
"animation": {"type": "float", "speed": 1.2, "axis": "y"}
|
| 66 |
})},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
]
|
| 68 |
|
| 69 |
_tok = None
|
|
|
|
| 64 |
],
|
| 65 |
"animation": {"type": "float", "speed": 1.2, "axis": "y"}
|
| 66 |
})},
|
| 67 |
+
{"role": "user", "content": "three glowing cubes stacked into a tower"},
|
| 68 |
+
{"role": "assistant", "content": json.dumps({
|
| 69 |
+
"background": "#0b0e14",
|
| 70 |
+
"objects": [
|
| 71 |
+
{"shape": "box", "color": "#39ff14", "emissive": "#0a3a0a", "material": "standard",
|
| 72 |
+
"metalness": 0.2, "roughness": 0.3, "position": [0, 0.8, 0]},
|
| 73 |
+
{"shape": "box", "color": "#39ff14", "emissive": "#0a3a0a", "material": "standard",
|
| 74 |
+
"metalness": 0.2, "roughness": 0.3, "position": [0, 0.0, 0]},
|
| 75 |
+
{"shape": "box", "color": "#39ff14", "emissive": "#0a3a0a", "material": "standard",
|
| 76 |
+
"metalness": 0.2, "roughness": 0.3, "position": [0, -0.8, 0]},
|
| 77 |
+
],
|
| 78 |
+
"lights": [
|
| 79 |
+
{"type": "ambient", "intensity": 0.5},
|
| 80 |
+
{"type": "directional", "intensity": 1.1, "position": [5, 8, 6]},
|
| 81 |
+
],
|
| 82 |
+
"animation": {"type": "rotate", "speed": 1.0, "axis": "y"},
|
| 83 |
+
})},
|
| 84 |
+
{"role": "user", "content": "a red sphere floating above a flat grey plane"},
|
| 85 |
+
{"role": "assistant", "content": json.dumps({
|
| 86 |
+
"background": "#1a1a2e",
|
| 87 |
+
"objects": [
|
| 88 |
+
{"shape": "sphere", "color": "#ff4444", "emissive": "#330000", "material": "standard",
|
| 89 |
+
"metalness": 0.1, "roughness": 0.5, "position": [0, 0.8, 0], "params": {"radius": 0.5}},
|
| 90 |
+
{"shape": "plane", "color": "#555566", "material": "standard",
|
| 91 |
+
"metalness": 0.0, "roughness": 0.9, "position": [0, -0.2, 0],
|
| 92 |
+
"rotation": [-1.5708, 0, 0], "params": {"width": 6, "height": 6}},
|
| 93 |
+
],
|
| 94 |
+
"lights": [
|
| 95 |
+
{"type": "ambient", "intensity": 0.4},
|
| 96 |
+
{"type": "directional", "color": "#ffffff", "intensity": 1.2, "position": [5, 8, 6]},
|
| 97 |
+
],
|
| 98 |
+
"animation": {"type": "float", "speed": 0.8, "axis": "y"},
|
| 99 |
+
})},
|
| 100 |
]
|
| 101 |
|
| 102 |
_tok = None
|