Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,9 @@ import numpy as np
|
|
4 |
import os
|
5 |
import random
|
6 |
import re
|
7 |
-
import textwrap
|
8 |
import torch
|
9 |
from shapely.geometry.polygon import Polygon
|
10 |
from shapely.affinity import scale
|
11 |
-
import aggdraw
|
12 |
from PIL import Image, ImageDraw, ImageOps, ImageFilter, ImageFont, ImageColor
|
13 |
|
14 |
import gradio as gr
|
@@ -51,87 +49,62 @@ architext_colors = [[0, 0, 0], [249, 222, 182], [195, 209, 217], [250, 120, 128]
|
|
51 |
regex = re.compile(".*?\((.*?)\)")
|
52 |
|
53 |
def draw_polygons(polygons, colors, im_size=(512, 512), b_color="white", fpath=None):
|
54 |
-
|
55 |
image = Image.new("RGBA", im_size, color="white")
|
56 |
-
#draw = aggdraw.Draw(image)
|
57 |
draw = ImageDraw.Draw(image)
|
58 |
-
|
59 |
for poly, color, in zip(polygons, colors):
|
60 |
#get initial polygon coordinates
|
61 |
xy = poly.exterior.xy
|
62 |
coords = np.dstack((xy[1], xy[0])).flatten()
|
63 |
# draw it on canvas, with the appropriate colors
|
64 |
-
#brush = aggdraw.Brush((0, 0, 0), opacity=255)
|
65 |
-
#draw.polygon(coords, brush)
|
66 |
draw.polygon(list(coords), fill=(0, 0, 0))
|
67 |
-
|
68 |
#get inner polygon coordinates
|
69 |
small_poly = poly.buffer(-1, resolution=32, cap_style=2, join_style=2, mitre_limit=5.0)
|
70 |
if small_poly.geom_type == 'MultiPolygon':
|
71 |
mycoordslist = [list(x.exterior.coords) for x in small_poly]
|
72 |
for coord in mycoordslist:
|
73 |
coords = np.dstack((np.array(coord)[:,1], np.array(coord)[:, 0])).flatten()
|
74 |
-
#brush2 = aggdraw.Brush((0, 0, 0), opacity=255)
|
75 |
-
#draw.polygon(coords, brush2)
|
76 |
draw.polygon(list(coords), fill=tuple(color))
|
77 |
elif poly.geom_type == 'Polygon':
|
78 |
#get inner polygon coordinates
|
79 |
xy2 = small_poly.exterior.xy
|
80 |
coords2 = np.dstack((xy2[1], xy2[0])).flatten()
|
81 |
# draw it on canvas, with the appropriate colors
|
82 |
-
#brush2 = aggdraw.Brush((color[0], color[1], color[2]), opacity=255)
|
83 |
-
#draw.polygon(coords2, brush2)
|
84 |
draw.polygon(list(coords2), fill=tuple(color))
|
85 |
-
|
86 |
-
#image = Image.frombytes("RGBA", im_size, draw.tobytes()).transpose(Image.FLIP_TOP_BOTTOM)
|
87 |
image = image.transpose(Image.FLIP_TOP_BOTTOM)
|
88 |
-
|
89 |
if(fpath):
|
90 |
image.save(fpath, quality=100, subsampling=0)
|
91 |
-
|
92 |
return draw, image
|
93 |
|
94 |
def prompt_to_layout(user_prompt, intensity, fpath=None):
|
95 |
-
|
96 |
if(containsNumber(user_prompt) == True):
|
97 |
spaced_prompt = user_prompt.split(' ')
|
98 |
new_prompt = ' '.join([word if word.isdigit() == False else num2words(int(word)).lower() for word in spaced_prompt])
|
99 |
model_prompt = '[User prompt] {} [Layout]'.format(new_prompt)
|
100 |
-
|
101 |
top_p, top_k = creativity(intensity)
|
102 |
model_prompt = '[User prompt] {} [Layout]'.format(user_prompt)
|
103 |
input_ids = tokenizer(model_prompt, return_tensors='pt').to(device)
|
104 |
output = finetuned.generate(**input_ids, do_sample=True, top_p=top_p, top_k=top_k,
|
105 |
eos_token_id=50256, max_length=400)
|
106 |
output = tokenizer.batch_decode(output, skip_special_tokens=True)
|
107 |
-
|
108 |
layout = output[0].split('[User prompt]')[1].split('[Layout] ')[1].split(', ')
|
109 |
spaces = [txt.split(':')[0] for txt in layout]
|
110 |
-
|
111 |
-
coordinates = [
|
112 |
-
|
113 |
-
|
114 |
polygons = []
|
115 |
for coord in coordinates:
|
116 |
-
polygons.append([point.split(',') for point in coord])
|
117 |
-
|
118 |
geom = []
|
119 |
for poly in polygons:
|
120 |
scaled_poly = scale(Polygon(np.array(poly, dtype=int)), xfact=2, yfact=2, origin=(0,0))
|
121 |
-
geom.append(scaled_poly)
|
122 |
-
#geom.append(Polygon(np.array(poly, dtype=int)))
|
123 |
-
|
124 |
colors = [architext_colors[housegan_labels[space]] for space in spaces]
|
125 |
-
|
126 |
_, im = draw_polygons(geom, colors, fpath=fpath)
|
127 |
-
|
128 |
html = '<img class="labels" src="images/labels.png" />'
|
129 |
-
|
130 |
legend = Image.open("labels.png")
|
131 |
imgs_comb = np.vstack([im, legend])
|
132 |
imgs_comb = Image.fromarray(imgs_comb)
|
133 |
-
|
134 |
-
return imgs_comb, layout
|
135 |
|
136 |
|
137 |
# Gradio App
|
@@ -257,13 +230,14 @@ creative_slider = gr.inputs.Radio(["Low", "Medium", "High"], default="Low", labe
|
|
257 |
textbox = gr.inputs.Textbox(placeholder='An apartment with two bedrooms and one bathroom', lines="3",
|
258 |
label="DESCRIBE YOUR IDEAL APARTMENT")
|
259 |
generated = gr.outputs.Image(label='Generated Layout')
|
|
|
260 |
|
261 |
iface = gr.Interface(fn=prompt_to_layout, inputs=[textbox, creative_slider],
|
262 |
outputs=[generated, layout],
|
263 |
-
css=custom_css,
|
264 |
theme="default",
|
265 |
allow_flagging='never',
|
266 |
allow_screenshot=False,
|
267 |
-
thumbnail="thumbnail_gradio.PNG")
|
268 |
|
269 |
iface.launch(enable_queue=True)
|
|
|
4 |
import os
|
5 |
import random
|
6 |
import re
|
|
|
7 |
import torch
|
8 |
from shapely.geometry.polygon import Polygon
|
9 |
from shapely.affinity import scale
|
|
|
10 |
from PIL import Image, ImageDraw, ImageOps, ImageFilter, ImageFont, ImageColor
|
11 |
|
12 |
import gradio as gr
|
|
|
49 |
regex = re.compile(".*?\((.*?)\)")
|
50 |
|
51 |
def draw_polygons(polygons, colors, im_size=(512, 512), b_color="white", fpath=None):
|
|
|
52 |
image = Image.new("RGBA", im_size, color="white")
|
|
|
53 |
draw = ImageDraw.Draw(image)
|
|
|
54 |
for poly, color, in zip(polygons, colors):
|
55 |
#get initial polygon coordinates
|
56 |
xy = poly.exterior.xy
|
57 |
coords = np.dstack((xy[1], xy[0])).flatten()
|
58 |
# draw it on canvas, with the appropriate colors
|
|
|
|
|
59 |
draw.polygon(list(coords), fill=(0, 0, 0))
|
|
|
60 |
#get inner polygon coordinates
|
61 |
small_poly = poly.buffer(-1, resolution=32, cap_style=2, join_style=2, mitre_limit=5.0)
|
62 |
if small_poly.geom_type == 'MultiPolygon':
|
63 |
mycoordslist = [list(x.exterior.coords) for x in small_poly]
|
64 |
for coord in mycoordslist:
|
65 |
coords = np.dstack((np.array(coord)[:,1], np.array(coord)[:, 0])).flatten()
|
|
|
|
|
66 |
draw.polygon(list(coords), fill=tuple(color))
|
67 |
elif poly.geom_type == 'Polygon':
|
68 |
#get inner polygon coordinates
|
69 |
xy2 = small_poly.exterior.xy
|
70 |
coords2 = np.dstack((xy2[1], xy2[0])).flatten()
|
71 |
# draw it on canvas, with the appropriate colors
|
|
|
|
|
72 |
draw.polygon(list(coords2), fill=tuple(color))
|
|
|
|
|
73 |
image = image.transpose(Image.FLIP_TOP_BOTTOM)
|
|
|
74 |
if(fpath):
|
75 |
image.save(fpath, quality=100, subsampling=0)
|
|
|
76 |
return draw, image
|
77 |
|
78 |
def prompt_to_layout(user_prompt, intensity, fpath=None):
|
|
|
79 |
if(containsNumber(user_prompt) == True):
|
80 |
spaced_prompt = user_prompt.split(' ')
|
81 |
new_prompt = ' '.join([word if word.isdigit() == False else num2words(int(word)).lower() for word in spaced_prompt])
|
82 |
model_prompt = '[User prompt] {} [Layout]'.format(new_prompt)
|
|
|
83 |
top_p, top_k = creativity(intensity)
|
84 |
model_prompt = '[User prompt] {} [Layout]'.format(user_prompt)
|
85 |
input_ids = tokenizer(model_prompt, return_tensors='pt').to(device)
|
86 |
output = finetuned.generate(**input_ids, do_sample=True, top_p=top_p, top_k=top_k,
|
87 |
eos_token_id=50256, max_length=400)
|
88 |
output = tokenizer.batch_decode(output, skip_special_tokens=True)
|
|
|
89 |
layout = output[0].split('[User prompt]')[1].split('[Layout] ')[1].split(', ')
|
90 |
spaces = [txt.split(':')[0] for txt in layout]
|
91 |
+
coords = [txt.split(':')[1] for txt in layout]
|
92 |
+
coordinates = [re.findall(regex, coord) for coord in coords]
|
93 |
+
layout_dict = dict(zip(spaces, coords))
|
|
|
94 |
polygons = []
|
95 |
for coord in coordinates:
|
96 |
+
polygons.append([point.split(',') for point in coord])
|
|
|
97 |
geom = []
|
98 |
for poly in polygons:
|
99 |
scaled_poly = scale(Polygon(np.array(poly, dtype=int)), xfact=2, yfact=2, origin=(0,0))
|
100 |
+
geom.append(scaled_poly)
|
|
|
|
|
101 |
colors = [architext_colors[housegan_labels[space]] for space in spaces]
|
|
|
102 |
_, im = draw_polygons(geom, colors, fpath=fpath)
|
|
|
103 |
html = '<img class="labels" src="images/labels.png" />'
|
|
|
104 |
legend = Image.open("labels.png")
|
105 |
imgs_comb = np.vstack([im, legend])
|
106 |
imgs_comb = Image.fromarray(imgs_comb)
|
107 |
+
return imgs_comb, layout_dict
|
|
|
108 |
|
109 |
|
110 |
# Gradio App
|
|
|
230 |
textbox = gr.inputs.Textbox(placeholder='An apartment with two bedrooms and one bathroom', lines="3",
|
231 |
label="DESCRIBE YOUR IDEAL APARTMENT")
|
232 |
generated = gr.outputs.Image(label='Generated Layout')
|
233 |
+
layout = gr.outputs.Text(label='Layout Coordinates')
|
234 |
|
235 |
iface = gr.Interface(fn=prompt_to_layout, inputs=[textbox, creative_slider],
|
236 |
outputs=[generated, layout],
|
237 |
+
#css=custom_css,
|
238 |
theme="default",
|
239 |
allow_flagging='never',
|
240 |
allow_screenshot=False,
|
241 |
+
#thumbnail="thumbnail_gradio.PNG")
|
242 |
|
243 |
iface.launch(enable_queue=True)
|