Spaces:
Running
on
A100
Running
on
A100
Ava Pun
commited on
Commit
·
5218da5
1
Parent(s):
f75356d
Add data collection
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import json
|
2 |
import os
|
|
|
3 |
import subprocess
|
4 |
import time
|
5 |
import uuid
|
@@ -45,6 +46,10 @@ def main():
|
|
45 |
|
46 |
# Define inputs and outputs
|
47 |
in_prompt = gr.Textbox(label='Prompt', placeholder='Enter a prompt to generate a LEGO model.', max_length=500)
|
|
|
|
|
|
|
|
|
48 |
in_temperature = gr.Slider(0.01, 2.0, value=model_cfg.temperature, step=0.01,
|
49 |
label='Temperature', info=get_help_string('temperature'))
|
50 |
in_seed = gr.Number(value=42, label='Seed', info='Random seed for generation.',
|
@@ -68,7 +73,7 @@ def main():
|
|
68 |
'The model is restricted to creating structures made of 1-unit-tall cuboid bricks on a 20x20x20 grid. It was trained on a dataset of 21 object categories: '
|
69 |
'*basket, bed, bench, birdhouse, bookshelf, bottle, bowl, bus, camera, car, chair, guitar, jar, mug, piano, pot, sofa, table, tower, train, vessel.* '
|
70 |
'Performance on prompts from outside these categories may be limited. This demo does not include texturing or coloring.',
|
71 |
-
inputs=[in_prompt],
|
72 |
additional_inputs=[in_temperature, in_seed, in_bricks, in_rejections, in_regenerations],
|
73 |
outputs=[out_img, out_txt],
|
74 |
flagging_mode='never',
|
@@ -100,12 +105,13 @@ class LegoGenerator:
|
|
100 |
def generate_lego(
|
101 |
self,
|
102 |
prompt: str,
|
|
|
103 |
temperature: float | None,
|
104 |
seed: int | None,
|
105 |
max_bricks: int | None,
|
106 |
max_brick_rejections: int | None,
|
107 |
max_regenerations: int | None,
|
108 |
-
):
|
109 |
# Set model parameters
|
110 |
if temperature is not None: self.model.temperature = temperature
|
111 |
if max_bricks is not None: self.model.max_bricks = max_bricks
|
@@ -125,19 +131,46 @@ class LegoGenerator:
|
|
125 |
ldr_filename = os.path.join(output_dir, f'{output_uuid}.ldr')
|
126 |
with open(ldr_filename, 'w') as f:
|
127 |
f.write(output['lego'].to_ldr())
|
128 |
-
|
|
|
129 |
|
130 |
# Render LEGO model to image
|
131 |
print('Rendering image...')
|
132 |
-
start_time = time.time()
|
133 |
img_filename = os.path.join(output_dir, f'{output_uuid}.png')
|
134 |
subprocess.run(['python', 'render_lego.py', '--in_file', ldr_filename, '--out_file', img_filename],
|
135 |
check=True) # Run render as a subprocess to prevent issues with Blender
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
"""
|
142 |
Run generation as a subprocess so that multiple requests can be handled concurrently.
|
143 |
"""
|
|
|
1 |
import json
|
2 |
import os
|
3 |
+
import shutil
|
4 |
import subprocess
|
5 |
import time
|
6 |
import uuid
|
|
|
46 |
|
47 |
# Define inputs and outputs
|
48 |
in_prompt = gr.Textbox(label='Prompt', placeholder='Enter a prompt to generate a LEGO model.', max_length=500)
|
49 |
+
in_optout = gr.Checkbox(label='Do not save my data',
|
50 |
+
info='We may collect model inputs and outputs to help us improve the model. '
|
51 |
+
'Your data will never be shared or used for any other purpose. '
|
52 |
+
'If you wish to opt out of data collection, please check the box below.')
|
53 |
in_temperature = gr.Slider(0.01, 2.0, value=model_cfg.temperature, step=0.01,
|
54 |
label='Temperature', info=get_help_string('temperature'))
|
55 |
in_seed = gr.Number(value=42, label='Seed', info='Random seed for generation.',
|
|
|
73 |
'The model is restricted to creating structures made of 1-unit-tall cuboid bricks on a 20x20x20 grid. It was trained on a dataset of 21 object categories: '
|
74 |
'*basket, bed, bench, birdhouse, bookshelf, bottle, bowl, bus, camera, car, chair, guitar, jar, mug, piano, pot, sofa, table, tower, train, vessel.* '
|
75 |
'Performance on prompts from outside these categories may be limited. This demo does not include texturing or coloring.',
|
76 |
+
inputs=[in_prompt, in_optout],
|
77 |
additional_inputs=[in_temperature, in_seed, in_bricks, in_rejections, in_regenerations],
|
78 |
outputs=[out_img, out_txt],
|
79 |
flagging_mode='never',
|
|
|
105 |
def generate_lego(
|
106 |
self,
|
107 |
prompt: str,
|
108 |
+
do_not_save_data: bool,
|
109 |
temperature: float | None,
|
110 |
seed: int | None,
|
111 |
max_bricks: int | None,
|
112 |
max_brick_rejections: int | None,
|
113 |
max_regenerations: int | None,
|
114 |
+
) -> (str, str):
|
115 |
# Set model parameters
|
116 |
if temperature is not None: self.model.temperature = temperature
|
117 |
if max_bricks is not None: self.model.max_bricks = max_bricks
|
|
|
131 |
ldr_filename = os.path.join(output_dir, f'{output_uuid}.ldr')
|
132 |
with open(ldr_filename, 'w') as f:
|
133 |
f.write(output['lego'].to_ldr())
|
134 |
+
generation_time = time.time() - start_time
|
135 |
+
print(f'Finished generation in {generation_time:.1f}s!')
|
136 |
|
137 |
# Render LEGO model to image
|
138 |
print('Rendering image...')
|
|
|
139 |
img_filename = os.path.join(output_dir, f'{output_uuid}.png')
|
140 |
subprocess.run(['python', 'render_lego.py', '--in_file', ldr_filename, '--out_file', img_filename],
|
141 |
check=True) # Run render as a subprocess to prevent issues with Blender
|
142 |
+
rendering_time = time.time() - start_time - generation_time
|
143 |
+
print(f'Finished rendering in {rendering_time:.1f}s!')
|
144 |
+
|
145 |
+
# Save data persistently
|
146 |
+
if not do_not_save_data:
|
147 |
+
data_dir = '/data/apun/legogpt_demo_out'
|
148 |
+
os.makedirs(data_dir, exist_ok=True)
|
149 |
+
|
150 |
+
# Copy output image to persistent storage
|
151 |
+
img_copy_filename = os.path.join(data_dir, f'{output_uuid}.png')
|
152 |
+
shutil.copy(img_filename, img_copy_filename)
|
153 |
+
|
154 |
+
# Save metadata
|
155 |
+
metadata_filename = os.path.join(data_dir, f'{output_uuid}.json')
|
156 |
+
with open(metadata_filename, 'w') as f:
|
157 |
+
json.dump({
|
158 |
+
'prompt': prompt,
|
159 |
+
'temperature': self.model.temperature,
|
160 |
+
'seed': seed,
|
161 |
+
'max_bricks': self.model.max_bricks,
|
162 |
+
'max_brick_rejections': self.model.max_brick_rejections,
|
163 |
+
'max_regenerations': self.model.max_regenerations,
|
164 |
+
'start_timestamp': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time)),
|
165 |
+
'generation_time': generation_time,
|
166 |
+
'rendering_time': rendering_time,
|
167 |
+
'output_txt': output['lego'].to_txt(),
|
168 |
+
}, f)
|
169 |
+
print(f'Saved data to {metadata_filename}.')
|
170 |
+
|
171 |
+
return img_filename, output['lego'].to_txt()
|
172 |
+
|
173 |
+
def generate_lego_subprocess(self, *args) -> (str, str):
|
174 |
"""
|
175 |
Run generation as a subprocess so that multiple requests can be handled concurrently.
|
176 |
"""
|