chahui commited on
Commit
8323af0
·
verified ·
1 Parent(s): 5de4ad5

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +40 -5
app.py CHANGED
@@ -4,12 +4,27 @@ import tempfile
4
  import threading
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")
10
- os.environ.setdefault("GRADIO_TEMP_DIR", "/tmp/gradio")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  import gradio as gr
 
13
  from worldgen import WorldGen
14
 
15
  _wg = None
@@ -28,15 +43,18 @@ def _zip_directory(dir_path: Path) -> str:
28
 
29
  def _save_scene(generated, out_dir: Path) -> Path:
30
  out_dir.mkdir(parents=True, exist_ok=True)
 
31
  for meth in ("save", "export", "write", "to_disk"):
32
  if hasattr(generated, meth):
33
  getattr(generated, meth)(str(out_dir))
34
  return out_dir
 
35
  for meth in ("save_as_glb", "to_glb", "save_as_ply", "to_ply"):
36
  if hasattr(generated, meth):
37
  fp = out_dir / ("scene.glb" if "glb" in meth else "scene.ply")
38
  getattr(generated, meth)(str(fp))
39
  return out_dir
 
40
  (out_dir / "README.txt").write_text(
41
  "No known save/export method detected. Please adjust _save_scene according to your WorldGen version."
42
  )
@@ -45,31 +63,48 @@ def _save_scene(generated, out_dir: Path) -> Path:
45
  def generate_from_text(prompt: str):
46
  if not prompt or not prompt.strip():
47
  return None, "Please enter a scene description (English works best)."
 
48
  wg = get_worldgen()
 
49
  try:
50
  result = wg.generate_world(prompt.strip())
51
  except Exception as e:
52
  return None, f"Generation failed: {e}"
 
53
  tmpdir = Path(tempfile.mkdtemp())
54
  out_dir = tmpdir / "worldgen_output"
55
  out_dir = _save_scene(result, out_dir)
56
  zip_path = _zip_directory(out_dir)
 
57
  def _cleanup(p: Path):
58
  try:
59
  shutil.rmtree(p, ignore_errors=True)
60
  except Exception:
61
  pass
 
62
  threading.Timer(300, _cleanup, args=(tmpdir,)).start()
 
63
  return zip_path, "Done. Download the ZIP (contains 3D assets)."
64
 
65
  with gr.Blocks(title="WorldGen • Text -> 3D Scene") as demo:
66
- gr.Markdown("# WorldGen - Text -> 3D\n- CPU-only fallback is enabled.\n- Outputs are zipped for download.")
 
 
 
 
 
67
  with gr.Row():
68
  with gr.Column():
69
- prompt = gr.Textbox(label="Scene prompt (English)", placeholder="a neon-lit cyberpunk street", lines=3)
 
 
 
 
70
  btn = gr.Button("Generate 3D Scene")
71
  with gr.Column():
72
  out_zip = gr.File(label="Download output (ZIP)")
73
  status = gr.Markdown()
 
74
  btn.click(generate_from_text, inputs=[prompt], outputs=[out_zip, status])
 
75
  demo.launch()
 
4
  import threading
5
  from pathlib import Path
6
 
7
+ # === Runtime environment tweaks (must be set before importing heavy libs) ===
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
+ import nunchaku.utils
14
+
15
+ def safe_get_precision(*args, **kwargs):
16
+ return "fp32" # force CPU precision
17
+
18
+ nunchaku.utils.get_precision = safe_get_precision
19
+
20
+ try:
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
+ import torch
28
  from worldgen import WorldGen
29
 
30
  _wg = None
 
43
 
44
  def _save_scene(generated, out_dir: Path) -> Path:
45
  out_dir.mkdir(parents=True, exist_ok=True)
46
+
47
  for meth in ("save", "export", "write", "to_disk"):
48
  if hasattr(generated, meth):
49
  getattr(generated, meth)(str(out_dir))
50
  return out_dir
51
+
52
  for meth in ("save_as_glb", "to_glb", "save_as_ply", "to_ply"):
53
  if hasattr(generated, meth):
54
  fp = out_dir / ("scene.glb" if "glb" in meth else "scene.ply")
55
  getattr(generated, meth)(str(fp))
56
  return out_dir
57
+
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
  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
+
67
  wg = get_worldgen()
68
+
69
  try:
70
  result = wg.generate_world(prompt.strip())
71
  except Exception as e:
72
  return None, f"Generation failed: {e}"
73
+
74
  tmpdir = Path(tempfile.mkdtemp())
75
  out_dir = tmpdir / "worldgen_output"
76
  out_dir = _save_scene(result, out_dir)
77
  zip_path = _zip_directory(out_dir)
78
+
79
  def _cleanup(p: Path):
80
  try:
81
  shutil.rmtree(p, ignore_errors=True)
82
  except Exception:
83
  pass
84
+
85
  threading.Timer(300, _cleanup, args=(tmpdir,)).start()
86
+
87
  return zip_path, "Done. Download the ZIP (contains 3D assets)."
88
 
89
  with gr.Blocks(title="WorldGen • Text -> 3D Scene") as demo:
90
+ gr.Markdown(
91
+ """# WorldGen - Text -> 3D
92
+ - CPU-only fallback is enabled. For real runs, switch the Space to a GPU (T4/A10G).
93
+ - Outputs are zipped for download; temp files are cleaned shortly after each run.
94
+ """
95
+ )
96
  with gr.Row():
97
  with gr.Column():
98
+ prompt = gr.Textbox(
99
+ label="Scene prompt (English)",
100
+ placeholder="a neon-lit cyberpunk street with rain reflections, highly detailed",
101
+ lines=3,
102
+ )
103
  btn = gr.Button("Generate 3D Scene")
104
  with gr.Column():
105
  out_zip = gr.File(label="Download output (ZIP)")
106
  status = gr.Markdown()
107
+
108
  btn.click(generate_from_text, inputs=[prompt], outputs=[out_zip, status])
109
+
110
  demo.launch()