-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
197 lines (145 loc) · 5.87 KB
/
app.py
File metadata and controls
197 lines (145 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3
"""
TRELLIS.2 Unity Studio - Web Interface
Gradio-based web interface for 3D generation.
This is a complementary interface to the Unity Editor integration.
"""
import gradio as gr
import os
import sys
from pathlib import Path
import argparse
import requests
from datetime import datetime
import time
# Configuration
API_URL = os.environ.get("TRELLIS2_API_URL", "http://localhost:8000")
OUTPUT_DIR = Path(__file__).parent / "outputs"
OUTPUT_DIR.mkdir(exist_ok=True)
def check_server():
"""Check if the API server is running."""
try:
resp = requests.get(f"{API_URL}/health", timeout=5)
return resp.status_code == 200
except:
return False
def submit_text_job(prompt: str, quality: str, seed: int):
"""Submit a text-to-3D job."""
resp = requests.post(
f"{API_URL}/submit/text",
json={"prompt": prompt, "quality": quality.lower(), "seed": seed}
)
resp.raise_for_status()
return resp.json()["job_id"]
def submit_image_job(image_path: str, quality: str, seed: int):
"""Submit an image-to-3D job."""
with open(image_path, "rb") as f:
resp = requests.post(
f"{API_URL}/submit/image",
files={"file": f},
params={"quality": quality.lower(), "seed": seed}
)
resp.raise_for_status()
return resp.json()["job_id"]
def wait_for_job(job_id: str, progress=gr.Progress()):
"""Wait for job completion with progress updates."""
start = time.time()
while True:
resp = requests.get(f"{API_URL}/status/{job_id}")
data = resp.json()
status = data.get("status", "unknown")
elapsed = time.time() - start
stage = data.get("stage_description", status)
progress(0.5, desc=f"{stage} ({elapsed:.0f}s)")
if status == "done":
return data
elif status == "failed":
raise Exception(data.get("error", "Job failed"))
time.sleep(2)
def download_result(job_data: dict):
"""Download the GLB result."""
result = job_data.get("result", {})
glb_url = result.get("glb")
if not glb_url:
raise Exception("No GLB in result")
job_id = job_data["job_id"]
local_path = OUTPUT_DIR / f"{job_id}.glb"
resp = requests.get(f"{API_URL}/{glb_url}")
resp.raise_for_status()
with open(local_path, "wb") as f:
f.write(resp.content)
return str(local_path)
def generate_from_text(prompt: str, quality: str, seed: int, progress=gr.Progress()):
"""Generate 3D model from text prompt."""
if not check_server():
raise gr.Error("Server not running. Start with: python src/trellis2_server.py")
if not prompt.strip():
raise gr.Error("Please enter a prompt")
progress(0.1, desc="Submitting job...")
job_id = submit_text_job(prompt, quality, seed)
progress(0.2, desc="Processing...")
job_data = wait_for_job(job_id, progress)
progress(0.9, desc="Downloading...")
glb_path = download_result(job_data)
progress(1.0, desc="Done!")
return glb_path, f"✅ Generated: {Path(glb_path).name}"
def generate_from_image(image, quality: str, seed: int, progress=gr.Progress()):
"""Generate 3D model from image."""
if not check_server():
raise gr.Error("Server not running. Start with: python src/trellis2_server.py")
if image is None:
raise gr.Error("Please upload an image")
# Save temp image
temp_path = OUTPUT_DIR / "temp_input.png"
image.save(temp_path)
progress(0.1, desc="Submitting job...")
job_id = submit_image_job(str(temp_path), quality, seed)
progress(0.2, desc="Processing...")
job_data = wait_for_job(job_id, progress)
progress(0.9, desc="Downloading...")
glb_path = download_result(job_data)
progress(1.0, desc="Done!")
return glb_path, f"✅ Generated: {Path(glb_path).name}"
# Build interface
with gr.Blocks(title="TRELLIS.2 Unity Studio", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# 🎮 TRELLIS.2 Unity Studio
**Web Interface for 3D Generation**
For Unity integration, use the Editor window: `Tools > TRELLIS.2 > Generation Window`
""")
with gr.Row():
with gr.Column():
with gr.Tabs():
with gr.Tab("Text to 3D"):
prompt = gr.Textbox(label="Prompt", placeholder="A cute robot toy")
text_btn = gr.Button("Generate", variant="primary")
with gr.Tab("Image to 3D"):
image = gr.Image(type="pil", label="Input Image")
image_btn = gr.Button("Generate", variant="primary")
with gr.Accordion("Settings", open=False):
quality = gr.Radio(["Fast", "Balanced", "High"], value="Balanced", label="Quality")
seed = gr.Number(value=42, label="Seed", precision=0)
with gr.Column():
output = gr.File(label="Generated GLB")
status = gr.Textbox(label="Status", interactive=False)
gr.Markdown("""
---
**Server:** Make sure the API server is running: `uvicorn src.trellis2_server:app --port 8000`
""")
# Events
text_btn.click(generate_from_text, [prompt, quality, seed], [output, status])
image_btn.click(generate_from_image, [image, quality, seed], [output, status])
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, default=7860)
parser.add_argument("--share", action="store_true")
args = parser.parse_args()
print(f"\n{'='*50}")
print("TRELLIS.2 Unity Studio - Web Interface")
print(f"{'='*50}")
print(f"Web UI: http://localhost:{args.port}")
print(f"API Server: {API_URL}")
print(f"{'='*50}\n")
demo.launch(server_port=args.port, share=args.share)
if __name__ == "__main__":
main()