Spaces:
Configuration error
Configuration error
Upload 3 files
Browse files- README.md +32 -12
- app.py +114 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,12 +1,32 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# WorldGen — Text -> 3D Scene (Hugging Face Spaces)
|
| 2 |
+
|
| 3 |
+
這個 Space 只做 **文字 -> 3D 場景**,使用開源的 **WorldGen** 專案(作者:Ziyang Xie / Manifolds AI)。
|
| 4 |
+
> 注意:Free CPU 會非常慢;建議到 Settings → Hardware 升級 GPU(T4/A10G)再生成,穩定很多。
|
| 5 |
+
|
| 6 |
+
- 原始專案(作者 GitHub):https://github.com/ZiYang-xie/WorldGen
|
| 7 |
+
- 專案頁(示例與說明):https://worldgen.github.io/
|
| 8 |
+
- WorldGen 在 Hugging Face 的模型卡:https://huggingface.co/LeoXie/WorldGen
|
| 9 |
+
|
| 10 |
+
## 使用方式
|
| 11 |
+
1. 在左側輸入英文場景描述(建議具體到風格、材質、光影)。
|
| 12 |
+
- 範例:`a neon-lit cyberpunk street with rain reflections, highly detailed, moody lighting`
|
| 13 |
+
2. 按「生成 3D 場景」。
|
| 14 |
+
3. 右側會提供 ZIP 檔下載,內含 3D 資產(例如 GLB/PLY/3DGS 或中介檔),可用 Blender / Unity / Three.js 開啟。
|
| 15 |
+
- 不同版本 WorldGen 的輸出結構可能不同;若你只拿到 3DGS 或其他格式,請依作者 repo 的 README / demo.py 指南進行轉檔或可視化。
|
| 16 |
+
|
| 17 |
+
## 執行環境
|
| 18 |
+
- Hugging Face Spaces(Gradio):本專案即為一個最小可運行的 Space。
|
| 19 |
+
- 硬體建議:
|
| 20 |
+
- Free CPU:僅驗證流程,很容易超時。
|
| 21 |
+
- GPU(T4/A10G/A100):推理較穩定快速。作者表示 A100 約 ~40 秒一個場景,low_vram 模式可再快一些。
|
| 22 |
+
- 安裝:本 Space 會自動依 requirements.txt 安裝:
|
| 23 |
+
- gradio(UI)、torch/torchvision(推理)、worldgen(從作者 GitHub 安裝最新版本)。
|
| 24 |
+
|
| 25 |
+
## 已知限制
|
| 26 |
+
- 保存/匯出:WorldGen 不同版本的輸出物件可能有所差異(3DGS/mesh/中介資產)。app.py 已嘗試常見的 save()/export()/to_glb() 等方法;若仍無法輸出,請查看作者的 demo.py 與 README 並在 app.py 的 _save_scene(...) 中替換對應保存程式碼。
|
| 27 |
+
- 線上預覽:為提高相容性,預設只提供 ZIP 下載。若你要直接在頁面預覽 .glb,可在 app.py 加入 <model-viewer> 或 three.js(用 gr.HTML)。
|
| 28 |
+
|
| 29 |
+
## 參考
|
| 30 |
+
- WorldGen GitHub(安裝與 WorldGen(mode="t2s") 用法):https://github.com/ZiYang-xie/WorldGen
|
| 31 |
+
- 作者回覆的推理耗時(A100 ~ 40s):https://github.com/ZiYang-xie/WorldGen/issues/5
|
| 32 |
+
- Hugging Face Spaces 硬體與計價(CPU 免費、GPU 按時計費):https://huggingface.co/docs/hub/en/spaces-overview
|
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import tempfile
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# 來自 WorldGen 開源庫,支援 from worldgen import WorldGen 與 mode="t2s"
|
| 10 |
+
from worldgen import WorldGen
|
| 11 |
+
|
| 12 |
+
def _zip_directory(dir_path: Path) -> str:
|
| 13 |
+
zip_path = dir_path.with_suffix('.zip')
|
| 14 |
+
if zip_path.exists():
|
| 15 |
+
zip_path.unlink()
|
| 16 |
+
shutil.make_archive(str(dir_path), 'zip', root_dir=str(dir_path))
|
| 17 |
+
return str(zip_path)
|
| 18 |
+
|
| 19 |
+
def _save_scene(generated, out_dir: Path) -> Path:
|
| 20 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 21 |
+
|
| 22 |
+
# 1) 物件方法保存
|
| 23 |
+
for meth in ('save', 'export', 'write', 'to_disk'):
|
| 24 |
+
if hasattr(generated, meth):
|
| 25 |
+
getattr(generated, meth)(str(out_dir))
|
| 26 |
+
return out_dir
|
| 27 |
+
|
| 28 |
+
# 2) 導出單一檔案(GLB/PLY)
|
| 29 |
+
for meth in ('save_as_glb', 'to_glb', 'save_as_ply', 'to_ply'):
|
| 30 |
+
if hasattr(generated, meth):
|
| 31 |
+
fp = out_dir / ('scene.glb' if 'glb' in meth else 'scene.ply')
|
| 32 |
+
getattr(generated, meth)(str(fp))
|
| 33 |
+
return out_dir
|
| 34 |
+
|
| 35 |
+
# 3) 直接給了路徑
|
| 36 |
+
if isinstance(generated, (str, Path)):
|
| 37 |
+
src = Path(generated)
|
| 38 |
+
if src.exists():
|
| 39 |
+
if src.is_dir():
|
| 40 |
+
shutil.copytree(src, out_dir, dirs_exist_ok=True)
|
| 41 |
+
else:
|
| 42 |
+
shutil.copy(src, out_dir / src.name)
|
| 43 |
+
return out_dir
|
| 44 |
+
|
| 45 |
+
if isinstance(generated, dict):
|
| 46 |
+
for v in generated.values():
|
| 47 |
+
p = Path(v)
|
| 48 |
+
if p.exists():
|
| 49 |
+
if p.is_dir():
|
| 50 |
+
shutil.copytree(p, out_dir, dirs_exist_ok=True)
|
| 51 |
+
else:
|
| 52 |
+
shutil.copy(p, out_dir / p.name)
|
| 53 |
+
return out_dir
|
| 54 |
+
|
| 55 |
+
# 4) 最後手段:放一個說明檔
|
| 56 |
+
(out_dir / 'README.txt').write_text(
|
| 57 |
+
'未自動偵測到保存方法。請依 WorldGen repo 的 demo.py/README 更新此函式以正確保存輸出。'
|
| 58 |
+
)
|
| 59 |
+
return out_dir
|
| 60 |
+
|
| 61 |
+
_worldgen = None
|
| 62 |
+
|
| 63 |
+
def _get_worldgen():
|
| 64 |
+
global _worldgen
|
| 65 |
+
if _worldgen is None:
|
| 66 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 67 |
+
_worldgen = WorldGen(mode='t2s', device=device, low_vram=True)
|
| 68 |
+
return _worldgen
|
| 69 |
+
|
| 70 |
+
def generate_from_text(prompt: str):
|
| 71 |
+
if not prompt or not prompt.strip():
|
| 72 |
+
return None, '請輸入場景描述(英文更穩定)。'
|
| 73 |
+
|
| 74 |
+
wg = _get_worldgen()
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
generated = wg.generate_world(prompt.strip())
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return None, f'生成失敗:{e}'
|
| 80 |
+
|
| 81 |
+
tmpdir = Path(tempfile.mkdtemp())
|
| 82 |
+
out_dir = tmpdir / 'worldgen_output'
|
| 83 |
+
out_dir = _save_scene(generated, out_dir)
|
| 84 |
+
zip_path = _zip_directory(out_dir)
|
| 85 |
+
|
| 86 |
+
return zip_path, '生成完成!請下載 ZIP(含 3D 資產)。'
|
| 87 |
+
|
| 88 |
+
with gr.Blocks(title='WorldGen • Text -> 3D Scene') as demo:
|
| 89 |
+
gr.Markdown(
|
| 90 |
+
'# WorldGen — 文字 -> 3D 場景
|
| 91 |
+
|
| 92 |
+
'
|
| 93 |
+
'- 僅啟用 Text -> 3D。
|
| 94 |
+
|
| 95 |
+
'
|
| 96 |
+
'- Free CPU 會很慢,建議在 Settings → Hardware 升級 GPU(T4/A10G)。'
|
| 97 |
+
)
|
| 98 |
+
with gr.Row():
|
| 99 |
+
with gr.Column(scale=1):
|
| 100 |
+
prompt = gr.Textbox(
|
| 101 |
+
label='輸入英文場景描述',
|
| 102 |
+
placeholder=(
|
| 103 |
+
'e.g. a neon-lit cyberpunk street with rain reflections, highly detailed'
|
| 104 |
+
),
|
| 105 |
+
lines=3,
|
| 106 |
+
)
|
| 107 |
+
run_btn = gr.Button('生成 3D 場景')
|
| 108 |
+
with gr.Column(scale=1):
|
| 109 |
+
out_zip = gr.File(label='下載輸出(ZIP)')
|
| 110 |
+
status = gr.Markdown()
|
| 111 |
+
|
| 112 |
+
run_btn.click(generate_from_text, inputs=[prompt], outputs=[out_zip, status])
|
| 113 |
+
|
| 114 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.44.0
|
| 2 |
+
|
| 3 |
+
torch
|
| 4 |
+
torchvision
|
| 5 |
+
|
| 6 |
+
git+https://github.com/ZiYang-xie/WorldGen.git
|