Spaces:
Configuration error
Configuration error
Upload 3 files
Browse files- README.md +3 -28
- app.py +18 -71
- requirements.txt +1 -3
README.md
CHANGED
|
@@ -10,36 +10,11 @@ app_file: "app.py"
|
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 14 |
# WorldGen — Text -> 3D Scene (Hugging Face Spaces)
|
| 15 |
|
| 16 |
-
這個 Space 只做 **文字 -> 3D 場景**
|
| 17 |
-
> 注意:Free CPU 會非常慢;建議到 Settings → Hardware 升級 GPU(T4/A10G)再生成,穩定很多。
|
| 18 |
-
|
| 19 |
-
- 原始專案(作者 GitHub):https://github.com/ZiYang-xie/WorldGen
|
| 20 |
-
- 專案頁(示例與說明):https://worldgen.github.io/
|
| 21 |
-
- WorldGen 在 Hugging Face 的模型卡:https://huggingface.co/LeoXie/WorldGen
|
| 22 |
|
| 23 |
## 使用方式
|
| 24 |
-
1. 在左側輸入英文場景描述
|
| 25 |
-
- 範例:`a neon-lit cyberpunk street with rain reflections, highly detailed, moody lighting`
|
| 26 |
2. 按「生成 3D 場景」。
|
| 27 |
-
3.
|
| 28 |
-
- 不同版本 WorldGen 的輸出結構可能不同;若你只拿到 3DGS 或其他格式,請依作者 repo 的 README / demo.py 指南進行轉檔或可視化。
|
| 29 |
-
|
| 30 |
-
## 執行環境
|
| 31 |
-
- Hugging Face Spaces(Gradio):本專案即為一個最小可運行的 Space。
|
| 32 |
-
- 硬體建議:
|
| 33 |
-
- Free CPU:僅驗證流程,很容易超時。
|
| 34 |
-
- GPU(T4/A10G/A100):推理較穩定快速。作者表示 A100 約 ~40 秒一個場景,low_vram 模式可再快一些。
|
| 35 |
-
- 安裝:本 Space 會自動依 requirements.txt 安裝:
|
| 36 |
-
- gradio(UI)、torch/torchvision(推理)、worldgen(從作者 GitHub 安裝最新版本)。
|
| 37 |
-
|
| 38 |
-
## 已知限制
|
| 39 |
-
- 保存/匯出:WorldGen 不同版本的輸出物件可能有所差異(3DGS/mesh/中介資產)。app.py 已嘗試常見的 save()/export()/to_glb() 等方法;若仍無法輸出,請查看作者的 demo.py 與 README 並在 app.py 的 _save_scene(...) 中替換對應保存程式碼。
|
| 40 |
-
- 線上預覽:為提高相容性,預設只提供 ZIP 下載。若你要直接在頁面預覽 .glb,可在 app.py 加入 <model-viewer> 或 three.js(用 gr.HTML)。
|
| 41 |
-
|
| 42 |
-
## 參考
|
| 43 |
-
- WorldGen GitHub(安裝與 WorldGen(mode="t2s") 用法):https://github.com/ZiYang-xie/WorldGen
|
| 44 |
-
- 作者回覆的推理耗時(A100 ~ 40s):https://github.com/ZiYang-xie/WorldGen/issues/5
|
| 45 |
-
- Hugging Face Spaces 硬體與計價(CPU 免費、GPU 按時計費):https://huggingface.co/docs/hub/en/spaces-overview
|
|
|
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
|
|
|
| 13 |
# WorldGen — Text -> 3D Scene (Hugging Face Spaces)
|
| 14 |
|
| 15 |
+
這個 Space 只做 **文字 -> 3D 場景**。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
## 使用方式
|
| 18 |
+
1. 在左側輸入英文場景描述。
|
|
|
|
| 19 |
2. 按「生成 3D 場景」。
|
| 20 |
+
3. 下載 ZIP 檔(內含 3D 資產)。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
-
import
|
| 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:
|
|
@@ -18,47 +12,17 @@ def _zip_directory(dir_path: Path) -> str:
|
|
| 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 |
-
|
| 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 |
-
|
| 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
|
| 62 |
|
| 63 |
def _get_worldgen():
|
| 64 |
global _worldgen
|
|
@@ -67,48 +31,31 @@ def _get_worldgen():
|
|
| 67 |
_worldgen = WorldGen(mode='t2s', device=device, low_vram=True)
|
| 68 |
return _worldgen
|
| 69 |
|
| 70 |
-
def generate_from_text(prompt:
|
| 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 |
-
|
| 91 |
-
|
| 92 |
-
'
|
| 93 |
-
'- 僅啟用 Text -> 3D。
|
| 94 |
-
|
| 95 |
-
'
|
| 96 |
-
'- Free CPU 會很慢,建議在 Settings → Hardware 升級 GPU(T4/A10G)。'
|
| 97 |
-
)
|
| 98 |
with gr.Row():
|
| 99 |
-
with gr.Column(
|
| 100 |
-
prompt = gr.Textbox(
|
| 101 |
-
|
| 102 |
-
|
| 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()
|
|
|
|
| 1 |
+
import shutil, tempfile
|
|
|
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
+
import gradio as gr, torch
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from worldgen import WorldGen
|
| 5 |
|
| 6 |
def _zip_directory(dir_path: Path) -> str:
|
|
|
|
| 12 |
|
| 13 |
def _save_scene(generated, out_dir: Path) -> Path:
|
| 14 |
out_dir.mkdir(parents=True, exist_ok=True)
|
| 15 |
+
for meth in ('save','export','write','to_disk'):
|
|
|
|
|
|
|
| 16 |
if hasattr(generated, meth):
|
| 17 |
+
getattr(generated, meth)(str(out_dir)); return out_dir
|
| 18 |
+
for meth in ('save_as_glb','to_glb','save_as_ply','to_ply'):
|
|
|
|
|
|
|
|
|
|
| 19 |
if hasattr(generated, meth):
|
| 20 |
fp = out_dir / ('scene.glb' if 'glb' in meth else 'scene.ply')
|
| 21 |
+
getattr(generated, meth)(str(fp)); return out_dir
|
| 22 |
+
(out_dir / 'README.txt').write_text('未自動偵測到保存方法。請依 WorldGen repo 更新保存流程。')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
return out_dir
|
| 24 |
|
| 25 |
+
_worldgen=None
|
| 26 |
|
| 27 |
def _get_worldgen():
|
| 28 |
global _worldgen
|
|
|
|
| 31 |
_worldgen = WorldGen(mode='t2s', device=device, low_vram=True)
|
| 32 |
return _worldgen
|
| 33 |
|
| 34 |
+
def generate_from_text(prompt:str):
|
| 35 |
if not prompt or not prompt.strip():
|
| 36 |
return None, '請輸入場景描述(英文更穩定)。'
|
| 37 |
+
wg=_get_worldgen()
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
generated = wg.generate_world(prompt.strip())
|
| 40 |
except Exception as e:
|
| 41 |
return None, f'生成失敗:{e}'
|
|
|
|
| 42 |
tmpdir = Path(tempfile.mkdtemp())
|
| 43 |
out_dir = tmpdir / 'worldgen_output'
|
| 44 |
out_dir = _save_scene(generated, out_dir)
|
| 45 |
zip_path = _zip_directory(out_dir)
|
|
|
|
| 46 |
return zip_path, '生成完成!請下載 ZIP(含 3D 資產)。'
|
| 47 |
|
| 48 |
with gr.Blocks(title='WorldGen • Text -> 3D Scene') as demo:
|
| 49 |
+
gr.Markdown('# WorldGen — 文字 -> 3D 場景
|
| 50 |
+
- 僅啟用 Text -> 3D。
|
| 51 |
+
- Free CPU 會很慢,建議升級 GPU(T4/A10G)。')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
with gr.Row():
|
| 53 |
+
with gr.Column():
|
| 54 |
+
prompt = gr.Textbox(label='輸入英文場景描述', placeholder='a neon-lit cyberpunk street...')
|
| 55 |
+
btn = gr.Button('生成 3D 場景')
|
| 56 |
+
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
out_zip = gr.File(label='下載輸出(ZIP)')
|
| 58 |
status = gr.Markdown()
|
| 59 |
+
btn.click(generate_from_text, inputs=[prompt], outputs=[out_zip, status])
|
|
|
|
| 60 |
|
| 61 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
gradio==4.44.0
|
| 2 |
torch==2.4.*
|
| 3 |
-
|
| 4 |
-
# 視需要再打開:在 CPU 免費層常常不是必需
|
| 5 |
# torchvision==0.19.*
|
| 6 |
|
| 7 |
git+https://github.com/ZiYang-xie/WorldGen.git
|
| 8 |
-
``
|
|
|
|
| 1 |
gradio==4.44.0
|
| 2 |
torch==2.4.*
|
| 3 |
+
# 如需再加:
|
|
|
|
| 4 |
# torchvision==0.19.*
|
| 5 |
|
| 6 |
git+https://github.com/ZiYang-xie/WorldGen.git
|
|
|