chahui commited on
Commit
aa9eb7b
·
verified ·
1 Parent(s): 55fc9e2

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +14 -32
app.py CHANGED
@@ -4,16 +4,16 @@ import tempfile
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
 
@@ -24,13 +24,19 @@ except Exception:
24
  pass
25
 
26
  import gradio as gr
27
- import torch
28
  from worldgen import WorldGen
29
 
30
  _wg = None
31
  def get_worldgen():
32
  global _wg
33
  if _wg is None:
 
 
 
 
 
 
34
  _wg = WorldGen(mode="t2s", device="cpu", low_vram=True, lora_path=None)
35
  return _wg
36
 
@@ -43,18 +49,15 @@ def _zip_directory(dir_path: Path) -> str:
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,48 +66,27 @@ 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
-
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()
 
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
  # Monkey patch to avoid CUDA errors
13
  import nunchaku.utils
14
 
15
  def safe_get_precision(*args, **kwargs):
16
+ return "fp32"
17
 
18
  nunchaku.utils.get_precision = safe_get_precision
19
 
 
24
  pass
25
 
26
  import gradio as gr
27
+ from huggingface_hub import hf_hub_download
28
  from worldgen import WorldGen
29
 
30
  _wg = None
31
  def get_worldgen():
32
  global _wg
33
  if _wg is None:
34
+ # Download model file from correct repo
35
+ model_path = hf_hub_download(
36
+ repo_id="mit-han-lab/svdq-int4-flux.1-dev",
37
+ filename="unquantized_layers.safetensors",
38
+ token=None
39
+ )
40
  _wg = WorldGen(mode="t2s", device="cpu", low_vram=True, lora_path=None)
41
  return _wg
42
 
 
49
 
50
  def _save_scene(generated, out_dir: Path) -> Path:
51
  out_dir.mkdir(parents=True, exist_ok=True)
 
52
  for meth in ("save", "export", "write", "to_disk"):
53
  if hasattr(generated, meth):
54
  getattr(generated, meth)(str(out_dir))
55
  return out_dir
 
56
  for meth in ("save_as_glb", "to_glb", "save_as_ply", "to_ply"):
57
  if hasattr(generated, meth):
58
  fp = out_dir / ("scene.glb" if "glb" in meth else "scene.ply")
59
  getattr(generated, meth)(str(fp))
60
  return out_dir
 
61
  (out_dir / "README.txt").write_text(
62
  "No known save/export method detected. Please adjust _save_scene according to your WorldGen version."
63
  )
 
66
  def generate_from_text(prompt: str):
67
  if not prompt or not prompt.strip():
68
  return None, "Please enter a scene description (English works best)."
 
69
  wg = get_worldgen()
 
70
  try:
71
  result = wg.generate_world(prompt.strip())
72
  except Exception as e:
73
  return None, f"Generation failed: {e}"
 
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
+ threading.Timer(300, shutil.rmtree, args=(tmpdir,)).start()
 
 
 
 
 
 
 
 
79
  return zip_path, "Done. Download the ZIP (contains 3D assets)."
80
 
81
  with gr.Blocks(title="WorldGen • Text -> 3D Scene") as demo:
82
+ gr.Markdown("# WorldGen - Text -> 3D\n- CPU-only fallback is enabled.\n- Outputs are zipped for download.")
 
 
 
 
 
83
  with gr.Row():
84
  with gr.Column():
85
+ prompt = gr.Textbox(label="Scene prompt (English)", placeholder="a neon-lit cyberpunk street", lines=3)
 
 
 
 
86
  btn = gr.Button("Generate 3D Scene")
87
  with gr.Column():
88
  out_zip = gr.File(label="Download output (ZIP)")
89
  status = gr.Markdown()
 
90
  btn.click(generate_from_text, inputs=[prompt], outputs=[out_zip, status])
91
 
92
  demo.launch()