Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,6 +27,18 @@ def containsNumber(value):
|
|
| 27 |
if character.isdigit():
|
| 28 |
return True
|
| 29 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
housegan_labels = {"living_room": 1, "kitchen": 2, "bedroom": 3, "bathroom": 4, "missing": 5, "closet": 6,
|
| 32 |
"balcony": 7, "corridor": 8, "dining_room": 9, "laundry_room": 10}
|
|
@@ -38,7 +50,7 @@ regex = re.compile(".*?\((.*?)\)")
|
|
| 38 |
|
| 39 |
def draw_polygons(polygons, colors, im_size=(256, 256), b_color="white", fpath=None):
|
| 40 |
|
| 41 |
-
image = Image.new("
|
| 42 |
draw = aggdraw.Draw(image)
|
| 43 |
|
| 44 |
for poly, color, in zip(polygons, colors):
|
|
@@ -66,7 +78,7 @@ def draw_polygons(polygons, colors, im_size=(256, 256), b_color="white", fpath=N
|
|
| 66 |
brush2 = aggdraw.Brush((color[0], color[1], color[2]), opacity=255)
|
| 67 |
draw.polygon(coords2, brush2)
|
| 68 |
|
| 69 |
-
image = Image.frombytes("
|
| 70 |
|
| 71 |
if(fpath):
|
| 72 |
image.save(fpath, quality=100, subsampling=0)
|
|
@@ -80,14 +92,14 @@ def prompt_to_layout(user_prompt, top_p, top_k, fpath=None):
|
|
| 80 |
new_prompt = ' '.join([word if word.isdigit() == False else num2words(int(word)).lower() for word in spaced_prompt])
|
| 81 |
model_prompt = '[User prompt] {} [Layout]'.format(new_prompt)
|
| 82 |
|
|
|
|
| 83 |
model_prompt = '[User prompt] {} [Layout]'.format(user_prompt)
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
output = tokenizer.batch_decode(output, skip_special_tokens=True)
|
| 88 |
-
#print(output)
|
| 89 |
|
| 90 |
-
layout = output[0].
|
| 91 |
spaces = [txt.split(':')[0] for txt in layout]
|
| 92 |
|
| 93 |
coordinates = [txt.split(':')[1] for txt in layout]
|
|
@@ -99,64 +111,16 @@ def prompt_to_layout(user_prompt, top_p, top_k, fpath=None):
|
|
| 99 |
|
| 100 |
geom = []
|
| 101 |
for poly in polygons:
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
colors = [
|
| 105 |
|
| 106 |
_, im = draw_polygons(geom, colors, fpath=fpath)
|
| 107 |
|
| 108 |
-
legend = Image.open("legend.png")
|
| 109 |
-
|
| 110 |
-
im = np.array(im)
|
| 111 |
-
im[:40, :] = np.array(legend)
|
| 112 |
-
im = Image.fromarray(im)
|
| 113 |
-
|
| 114 |
-
return im, layout, output
|
| 115 |
-
|
| 116 |
-
def mut_txt2layout(mut_output):
|
| 117 |
-
output = mut_output[0].rstrip().split('[User prompt]')[1].split('[Layout]')[1].split(', ')
|
| 118 |
-
spaces = [txt.split(':')[0].strip(' ') for txt in output]
|
| 119 |
-
coordinates = [txt.split(':')[1] for txt in output]
|
| 120 |
-
coordinates = [re.findall(regex, coord) for coord in coordinates]
|
| 121 |
-
|
| 122 |
-
polygons = []
|
| 123 |
-
for coord in coordinates:
|
| 124 |
-
polygons.append([point.split(',') for point in coord])
|
| 125 |
-
|
| 126 |
-
geom = []
|
| 127 |
-
for poly in polygons:
|
| 128 |
-
geom.append(Polygon(np.array(poly, dtype=int)))
|
| 129 |
-
|
| 130 |
-
colors = [architext_colors[housegan_labels[space]] for space in spaces]
|
| 131 |
-
_, im = draw_polygons(geom, colors, fpath=None)
|
| 132 |
-
|
| 133 |
-
legend = Image.open("legend.png")
|
| 134 |
-
|
| 135 |
-
im = np.array(im)
|
| 136 |
-
im[:40, :] = np.array(legend)
|
| 137 |
-
im = Image.fromarray(im)
|
| 138 |
-
|
| 139 |
return im
|
| 140 |
|
| 141 |
-
def prompt_with_mutation(user_prompt, top_p, top_k, mut_rate, fpath=None):
|
| 142 |
-
|
| 143 |
-
#Create initial layout based on prompt
|
| 144 |
-
im, layout, output = prompt_to_layout(user_prompt, top_p=top_p, top_k=top_k)
|
| 145 |
-
|
| 146 |
-
#Create mutated layout based on initial
|
| 147 |
-
mut_len = int((1-mut_rate)*len(layout))
|
| 148 |
-
index1 = random.randrange(0,len(layout)-mut_len)
|
| 149 |
-
rooms = layout[index1:index1+mut_len]
|
| 150 |
-
rooms[-1] = rooms[-1].split(':')[0] + ':'
|
| 151 |
-
rooms = ', '.join(rooms)# + ', '
|
| 152 |
-
new_prompt = '[User prompt] {} [Layout] {}'.format(user_prompt, rooms)
|
| 153 |
-
input_ids = tokenizer(new_prompt, return_tensors='pt').to(device)
|
| 154 |
-
mut_output = finetuned.generate(**input_ids, do_sample=True, top_p=top_p, top_k=top_k, eos_token_id=50256, max_length=400)
|
| 155 |
-
mut_output = tokenizer.batch_decode(mut_output, skip_special_tokens=True)
|
| 156 |
-
mut_im = mut_txt2layout(mut_output)
|
| 157 |
-
|
| 158 |
-
return im, mut_im
|
| 159 |
-
|
| 160 |
# Gradio App
|
| 161 |
|
| 162 |
custom_css="""
|
|
@@ -230,33 +194,17 @@ custom_css="""
|
|
| 230 |
.gradio_interface[theme=default] .component_set {
|
| 231 |
background: transparent;
|
| 232 |
opacity: 1 !important;
|
| 233 |
-
}"""
|
| 234 |
-
|
| 235 |
-
def gen_and_mutate(user_prompt, mutate=False, top_p=0.94, top_k=100, mut_rate=0.2):
|
| 236 |
-
if(mutate):
|
| 237 |
-
im, mut_im = None, None
|
| 238 |
-
while (mut_im is None):
|
| 239 |
-
try:
|
| 240 |
-
im, mut_im = prompt_with_mutation(user_prompt, top_p, top_k, mut_rate)
|
| 241 |
-
except:
|
| 242 |
-
pass
|
| 243 |
-
else:
|
| 244 |
-
mut_im=Image.open("empty.png")
|
| 245 |
-
im, _, _ = prompt_to_layout(user_prompt, top_p, top_k)
|
| 246 |
-
|
| 247 |
-
return im, mut_im
|
| 248 |
-
|
| 249 |
-
checkbox = gr.inputs.Checkbox(label='Mutate')
|
| 250 |
-
topp_slider = gr.inputs.Slider(0.1, 1.0, 0.01, default=0.94, label='top_p')
|
| 251 |
-
topk_slider = gr.inputs.Slider(0, 100, 25, default=0, label='top_k')
|
| 252 |
-
mut_slider = gr.inputs.Slider(0.2, 0.8, 0.1, default=0.3, label='Mutation rate')
|
| 253 |
textbox = gr.inputs.Textbox(placeholder='a house with two bedrooms and one bathroom', lines="2",
|
| 254 |
label="DESCRIBE YOUR DESIGN")
|
| 255 |
generated = gr.outputs.Image(label='Generated Layout')
|
| 256 |
-
mutated = gr.outputs.Image(label='Mutated Layout')
|
| 257 |
|
| 258 |
-
|
|
|
|
| 259 |
css=custom_css,
|
|
|
|
|
|
|
| 260 |
thumbnail="thumbnail_gradio.PNG",
|
| 261 |
description='Demo of Semantic Generation of Residential Layouts \n',
|
| 262 |
article='''<div>
|
|
@@ -271,38 +219,4 @@ iface = gr.Interface(fn=gen_and_mutate, inputs=[textbox, checkbox, topp_slider,
|
|
| 271 |
<p> Made by: <a href='https://www.linkedin.com/in/theodorosgalanos/'>Theodoros </a> <a href='https://twitter.com/TheodoreGalanos'> Galanos</a> and <a href='https://twitter.com/tylerlastovich'>Tyler Lastovich</a>, using a finetuned <a href='https://huggingface.co/EleutherAI/gpt-neo-125M'> GPT-Neo</a> model. </p>
|
| 272 |
</div>''')
|
| 273 |
|
| 274 |
-
iface.launch()
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
|
|
|
| 27 |
if character.isdigit():
|
| 28 |
return True
|
| 29 |
return False
|
| 30 |
+
|
| 31 |
+
def creativity(intensity):
|
| 32 |
+
if(intensity == 'Low'):
|
| 33 |
+
top_p = 0.95
|
| 34 |
+
top_k = 10
|
| 35 |
+
elif(intensity == 'Medium'):
|
| 36 |
+
top_p = 0.9
|
| 37 |
+
top_k = 50
|
| 38 |
+
if(intensity == 'High'):
|
| 39 |
+
top_p = 0.85
|
| 40 |
+
top_k = 100
|
| 41 |
+
return top_p, top_k
|
| 42 |
|
| 43 |
housegan_labels = {"living_room": 1, "kitchen": 2, "bedroom": 3, "bathroom": 4, "missing": 5, "closet": 6,
|
| 44 |
"balcony": 7, "corridor": 8, "dining_room": 9, "laundry_room": 10}
|
|
|
|
| 50 |
|
| 51 |
def draw_polygons(polygons, colors, im_size=(256, 256), b_color="white", fpath=None):
|
| 52 |
|
| 53 |
+
image = Image.new("RGBA", im_size, color="white")
|
| 54 |
draw = aggdraw.Draw(image)
|
| 55 |
|
| 56 |
for poly, color, in zip(polygons, colors):
|
|
|
|
| 78 |
brush2 = aggdraw.Brush((color[0], color[1], color[2]), opacity=255)
|
| 79 |
draw.polygon(coords2, brush2)
|
| 80 |
|
| 81 |
+
image = Image.frombytes("RGBA", (256,256), draw.tobytes()).transpose(Image.FLIP_TOP_BOTTOM)
|
| 82 |
|
| 83 |
if(fpath):
|
| 84 |
image.save(fpath, quality=100, subsampling=0)
|
|
|
|
| 92 |
new_prompt = ' '.join([word if word.isdigit() == False else num2words(int(word)).lower() for word in spaced_prompt])
|
| 93 |
model_prompt = '[User prompt] {} [Layout]'.format(new_prompt)
|
| 94 |
|
| 95 |
+
top_p, top_k = creativity(intensity)
|
| 96 |
model_prompt = '[User prompt] {} [Layout]'.format(user_prompt)
|
| 97 |
+
input_ids = tokenizer(model_prompt, return_tensors='pt')
|
| 98 |
+
output = finetuned.generate(**input_ids, do_sample=True, top_p=top_p, top_k=top_k,
|
| 99 |
+
eos_token_id=50256, max_length=400)
|
| 100 |
output = tokenizer.batch_decode(output, skip_special_tokens=True)
|
|
|
|
| 101 |
|
| 102 |
+
layout = output[0].split('[User prompt]')[1].split('[Layout] ')[1].split(', ')
|
| 103 |
spaces = [txt.split(':')[0] for txt in layout]
|
| 104 |
|
| 105 |
coordinates = [txt.split(':')[1] for txt in layout]
|
|
|
|
| 111 |
|
| 112 |
geom = []
|
| 113 |
for poly in polygons:
|
| 114 |
+
scaled_poly = scale(Polygon(np.array(poly, dtype=int)), xfact=2, yfact=2, origin=(0,0))
|
| 115 |
+
geom.append(scaled_poly)
|
| 116 |
+
#geom.append(Polygon(np.array(poly, dtype=int)))
|
| 117 |
|
| 118 |
+
colors = [architext_colors2[housegan_labels[space]] for space in spaces]
|
| 119 |
|
| 120 |
_, im = draw_polygons(geom, colors, fpath=fpath)
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
return im
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
# Gradio App
|
| 125 |
|
| 126 |
custom_css="""
|
|
|
|
| 194 |
.gradio_interface[theme=default] .component_set {
|
| 195 |
background: transparent;
|
| 196 |
opacity: 1 !important;
|
| 197 |
+
}"""
|
| 198 |
+
creative_slider = gr.inputs.Radio(["Low", "Medium", "High"], default="Medium", label='Creativity')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
textbox = gr.inputs.Textbox(placeholder='a house with two bedrooms and one bathroom', lines="2",
|
| 200 |
label="DESCRIBE YOUR DESIGN")
|
| 201 |
generated = gr.outputs.Image(label='Generated Layout')
|
|
|
|
| 202 |
|
| 203 |
+
iface2 = gr.Interface(fn=prompt_to_layout, inputs=[textbox, creative_slider],
|
| 204 |
+
outputs=[generated],
|
| 205 |
css=custom_css,
|
| 206 |
+
allow_flagging=False,
|
| 207 |
+
allow_screenshot=False,
|
| 208 |
thumbnail="thumbnail_gradio.PNG",
|
| 209 |
description='Demo of Semantic Generation of Residential Layouts \n',
|
| 210 |
article='''<div>
|
|
|
|
| 219 |
<p> Made by: <a href='https://www.linkedin.com/in/theodorosgalanos/'>Theodoros </a> <a href='https://twitter.com/TheodoreGalanos'> Galanos</a> and <a href='https://twitter.com/tylerlastovich'>Tyler Lastovich</a>, using a finetuned <a href='https://huggingface.co/EleutherAI/gpt-neo-125M'> GPT-Neo</a> model. </p>
|
| 220 |
</div>''')
|
| 221 |
|
| 222 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|