Spaces:
Configuration error
Configuration error
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -5,36 +5,39 @@ import threading
|
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
# === Runtime environment tweaks ===
|
| 8 |
-
os.environ.setdefault("CUDA_VISIBLE_DEVICES", "")
|
| 9 |
-
os.environ.setdefault("WORLDGEN_DISABLE_EVAL", "1")
|
| 10 |
-
os.environ.setdefault("GRADIO_TEMP_DIR", "/tmp/gradio")
|
| 11 |
|
| 12 |
# Monkey patch to avoid CUDA errors
|
| 13 |
-
import nunchaku.utils
|
| 14 |
-
def safe_get_precision(*args, **kwargs):
|
| 15 |
-
return "fp32"
|
| 16 |
-
nunchaku.utils.get_precision = safe_get_precision
|
| 17 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
import nunchaku.models.transformers.transformer_flux as tf
|
| 19 |
tf.get_precision = safe_get_precision
|
| 20 |
except Exception:
|
| 21 |
pass
|
| 22 |
|
| 23 |
import gradio as gr
|
| 24 |
-
from huggingface_hub import hf_hub_download
|
| 25 |
from worldgen import WorldGen
|
| 26 |
|
|
|
|
| 27 |
_wg = None
|
| 28 |
def get_worldgen():
|
| 29 |
global _wg
|
| 30 |
if _wg is None:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
)
|
| 37 |
-
_wg = WorldGen(mode="t2s", device="cpu", low_vram=True, lora_path=None)
|
| 38 |
return _wg
|
| 39 |
|
| 40 |
def _zip_directory(dir_path: Path) -> str:
|
|
@@ -46,15 +49,18 @@ def _zip_directory(dir_path: Path) -> str:
|
|
| 46 |
|
| 47 |
def _save_scene(generated, out_dir: Path) -> Path:
|
| 48 |
out_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 49 |
for meth in ("save", "export", "write", "to_disk"):
|
| 50 |
if hasattr(generated, meth):
|
| 51 |
getattr(generated, meth)(str(out_dir))
|
| 52 |
return out_dir
|
|
|
|
| 53 |
for meth in ("save_as_glb", "to_glb", "save_as_ply", "to_ply"):
|
| 54 |
if hasattr(generated, meth):
|
| 55 |
fp = out_dir / ("scene.glb" if "glb" in meth else "scene.ply")
|
| 56 |
getattr(generated, meth)(str(fp))
|
| 57 |
return out_dir
|
|
|
|
| 58 |
(out_dir / "README.txt").write_text(
|
| 59 |
"No known save/export method detected. Please adjust _save_scene according to your WorldGen version."
|
| 60 |
)
|
|
@@ -63,20 +69,36 @@ def _save_scene(generated, out_dir: Path) -> Path:
|
|
| 63 |
def generate_from_text(prompt: str):
|
| 64 |
if not prompt or not prompt.strip():
|
| 65 |
return None, "Please enter a scene description (English works best)."
|
|
|
|
| 66 |
wg = get_worldgen()
|
|
|
|
| 67 |
try:
|
| 68 |
result = wg.generate_world(prompt.strip())
|
| 69 |
except Exception as e:
|
| 70 |
return None, f"Generation failed: {e}"
|
|
|
|
| 71 |
tmpdir = Path(tempfile.mkdtemp())
|
| 72 |
out_dir = tmpdir / "worldgen_output"
|
| 73 |
out_dir = _save_scene(result, out_dir)
|
| 74 |
zip_path = _zip_directory(out_dir)
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
return zip_path, "Done. Download the ZIP (contains 3D assets)."
|
| 77 |
|
| 78 |
-
with gr.Blocks(title="WorldGen • Text
|
| 79 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
with gr.Row():
|
| 81 |
with gr.Column():
|
| 82 |
prompt = gr.Textbox(
|
|
@@ -88,5 +110,7 @@ with gr.Blocks(title="WorldGen • Text -> 3D Scene") as demo:
|
|
| 88 |
with gr.Column():
|
| 89 |
out_zip = gr.File(label="Download output (ZIP)")
|
| 90 |
status = gr.Markdown()
|
|
|
|
| 91 |
btn.click(generate_from_text, inputs=[prompt], outputs=[out_zip, status])
|
|
|
|
| 92 |
demo.launch()
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
# === Runtime environment tweaks ===
|
| 8 |
+
os.environ.setdefault("CUDA_VISIBLE_DEVICES", "") # force CPU
|
| 9 |
+
os.environ.setdefault("WORLDGEN_DISABLE_EVAL", "1") # avoid KNN/eval paths if supported
|
| 10 |
+
os.environ.setdefault("GRADIO_TEMP_DIR", "/tmp/gradio") # centralize gradio cache
|
| 11 |
|
| 12 |
# Monkey patch to avoid CUDA errors
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
+
import nunchaku.utils
|
| 15 |
+
|
| 16 |
+
def safe_get_precision(*args, **kwargs):
|
| 17 |
+
return "fp32" # force CPU precision
|
| 18 |
+
|
| 19 |
+
nunchaku.utils.get_precision = safe_get_precision
|
| 20 |
+
|
| 21 |
import nunchaku.models.transformers.transformer_flux as tf
|
| 22 |
tf.get_precision = safe_get_precision
|
| 23 |
except Exception:
|
| 24 |
pass
|
| 25 |
|
| 26 |
import gradio as gr
|
|
|
|
| 27 |
from worldgen import WorldGen
|
| 28 |
|
| 29 |
+
# Lazy init
|
| 30 |
_wg = None
|
| 31 |
def get_worldgen():
|
| 32 |
global _wg
|
| 33 |
if _wg is None:
|
| 34 |
+
_wg = WorldGen(
|
| 35 |
+
mode="t2s",
|
| 36 |
+
device="cpu",
|
| 37 |
+
low_vram=True,
|
| 38 |
+
lora_path=None,
|
| 39 |
+
repo_id="mit-han-lab/svdq-int4-flux.1-dev"
|
| 40 |
)
|
|
|
|
| 41 |
return _wg
|
| 42 |
|
| 43 |
def _zip_directory(dir_path: Path) -> str:
|
|
|
|
| 49 |
|
| 50 |
def _save_scene(generated, out_dir: Path) -> Path:
|
| 51 |
out_dir.mkdir(parents=True, exist_ok=True)
|
| 52 |
+
|
| 53 |
for meth in ("save", "export", "write", "to_disk"):
|
| 54 |
if hasattr(generated, meth):
|
| 55 |
getattr(generated, meth)(str(out_dir))
|
| 56 |
return out_dir
|
| 57 |
+
|
| 58 |
for meth in ("save_as_glb", "to_glb", "save_as_ply", "to_ply"):
|
| 59 |
if hasattr(generated, meth):
|
| 60 |
fp = out_dir / ("scene.glb" if "glb" in meth else "scene.ply")
|
| 61 |
getattr(generated, meth)(str(fp))
|
| 62 |
return out_dir
|
| 63 |
+
|
| 64 |
(out_dir / "README.txt").write_text(
|
| 65 |
"No known save/export method detected. Please adjust _save_scene according to your WorldGen version."
|
| 66 |
)
|
|
|
|
| 69 |
def generate_from_text(prompt: str):
|
| 70 |
if not prompt or not prompt.strip():
|
| 71 |
return None, "Please enter a scene description (English works best)."
|
| 72 |
+
|
| 73 |
wg = get_worldgen()
|
| 74 |
+
|
| 75 |
try:
|
| 76 |
result = wg.generate_world(prompt.strip())
|
| 77 |
except Exception as e:
|
| 78 |
return None, f"Generation failed: {e}"
|
| 79 |
+
|
| 80 |
tmpdir = Path(tempfile.mkdtemp())
|
| 81 |
out_dir = tmpdir / "worldgen_output"
|
| 82 |
out_dir = _save_scene(result, out_dir)
|
| 83 |
zip_path = _zip_directory(out_dir)
|
| 84 |
+
|
| 85 |
+
def _cleanup(p: Path):
|
| 86 |
+
try:
|
| 87 |
+
shutil.rmtree(p, ignore_errors=True)
|
| 88 |
+
except Exception:
|
| 89 |
+
pass
|
| 90 |
+
|
| 91 |
+
threading.Timer(300, _cleanup, args=(tmpdir,)).start()
|
| 92 |
+
|
| 93 |
return zip_path, "Done. Download the ZIP (contains 3D assets)."
|
| 94 |
|
| 95 |
+
with gr.Blocks(title="WorldGen • Text → 3D Scene") as demo:
|
| 96 |
+
gr.Markdown(
|
| 97 |
+
"""# WorldGen - Text → 3D
|
| 98 |
+
- CPU-only fallback is enabled. For real runs, switch the Space to a GPU (T4/A10G).
|
| 99 |
+
- Outputs are zipped for download; temp files are cleaned shortly after each run.
|
| 100 |
+
"""
|
| 101 |
+
)
|
| 102 |
with gr.Row():
|
| 103 |
with gr.Column():
|
| 104 |
prompt = gr.Textbox(
|
|
|
|
| 110 |
with gr.Column():
|
| 111 |
out_zip = gr.File(label="Download output (ZIP)")
|
| 112 |
status = gr.Markdown()
|
| 113 |
+
|
| 114 |
btn.click(generate_from_text, inputs=[prompt], outputs=[out_zip, status])
|
| 115 |
+
|
| 116 |
demo.launch()
|