Spaces:
Runtime error
Runtime error
Duplicate from architext/Architext_deployed
Browse filesCo-authored-by: Julien Chaumond <julien-c@users.noreply.huggingface.co>
- .gitattributes +16 -0
- README.md +34 -0
- app.py +267 -0
- empty.png +0 -0
- labels.png +0 -0
- legend.png +0 -0
- model/config.json +36 -0
- model/pytorch_model.bin +3 -0
- requirements.txt +8 -0
- thumbnail_gradio.PNG +0 -0
.gitattributes
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.tar.gz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: ArchiText
|
3 |
+
emoji: 💻
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: green
|
6 |
+
sdk: gradio
|
7 |
+
app_file: app.py
|
8 |
+
pinned: false
|
9 |
+
duplicated_from: architext/Architext_deployed
|
10 |
+
---
|
11 |
+
|
12 |
+
# Configuration
|
13 |
+
|
14 |
+
`title`: _string_
|
15 |
+
Display title for the Space
|
16 |
+
|
17 |
+
`emoji`: _string_
|
18 |
+
Space emoji (emoji-only character allowed)
|
19 |
+
|
20 |
+
`colorFrom`: _string_
|
21 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
22 |
+
|
23 |
+
`colorTo`: _string_
|
24 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
25 |
+
|
26 |
+
`sdk`: _string_
|
27 |
+
Can be either `gradio` or `streamlit`
|
28 |
+
|
29 |
+
`app_file`: _string_
|
30 |
+
Path to your main application file (which contains either `gradio` or `streamlit` Python code).
|
31 |
+
Path is relative to the root of the repository.
|
32 |
+
|
33 |
+
`pinned`: _boolean_
|
34 |
+
Whether the Space stays on top of your list.
|
app.py
ADDED
@@ -0,0 +1,267 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from num2words import num2words
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
import random
|
6 |
+
import re
|
7 |
+
import torch
|
8 |
+
import json
|
9 |
+
from shapely.geometry.polygon import Polygon
|
10 |
+
from shapely.affinity import scale
|
11 |
+
from PIL import Image, ImageDraw, ImageOps, ImageFilter, ImageFont, ImageColor
|
12 |
+
|
13 |
+
os.system('pip3 install gradio==2.7.5')
|
14 |
+
import gradio as gr
|
15 |
+
|
16 |
+
from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM
|
17 |
+
|
18 |
+
finetuned = AutoModelForCausalLM.from_pretrained('model')
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained('gpt2')
|
20 |
+
|
21 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
22 |
+
print(device)
|
23 |
+
finetuned = finetuned.to(device)
|
24 |
+
|
25 |
+
# Utility functions
|
26 |
+
|
27 |
+
def containsNumber(value):
|
28 |
+
for character in value:
|
29 |
+
if character.isdigit():
|
30 |
+
return True
|
31 |
+
return False
|
32 |
+
|
33 |
+
def creativity(intensity):
|
34 |
+
if(intensity == 'Low'):
|
35 |
+
top_p = 0.95
|
36 |
+
top_k = 10
|
37 |
+
elif(intensity == 'Medium'):
|
38 |
+
top_p = 0.9
|
39 |
+
top_k = 50
|
40 |
+
if(intensity == 'High'):
|
41 |
+
top_p = 0.85
|
42 |
+
top_k = 100
|
43 |
+
return top_p, top_k
|
44 |
+
|
45 |
+
housegan_labels = {"living_room": 1, "kitchen": 2, "bedroom": 3, "bathroom": 4, "missing": 5, "closet": 6,
|
46 |
+
"balcony": 7, "corridor": 8, "dining_room": 9, "laundry_room": 10}
|
47 |
+
|
48 |
+
architext_colors = [[0, 0, 0], [249, 222, 182], [195, 209, 217], [250, 120, 128], [126, 202, 234], [190, 0, 198], [255, 255, 255],
|
49 |
+
[6, 53, 17], [17, 33, 58], [132, 151, 246], [197, 203, 159], [6, 53, 17],]
|
50 |
+
|
51 |
+
regex = re.compile(".*?\((.*?)\)")
|
52 |
+
|
53 |
+
def draw_polygons(polygons, colors, im_size=(512, 512), b_color="white", fpath=None):
|
54 |
+
image = Image.new("RGBA", im_size, color="white")
|
55 |
+
draw = ImageDraw.Draw(image)
|
56 |
+
for poly, color, in zip(polygons, colors):
|
57 |
+
#get initial polygon coordinates
|
58 |
+
xy = poly.exterior.xy
|
59 |
+
coords = np.dstack((xy[1], xy[0])).flatten()
|
60 |
+
# draw it on canvas, with the appropriate colors
|
61 |
+
draw.polygon(list(coords), fill=(0, 0, 0))
|
62 |
+
#get inner polygon coordinates
|
63 |
+
small_poly = poly.buffer(-1, resolution=32, cap_style=2, join_style=2, mitre_limit=5.0)
|
64 |
+
if small_poly.geom_type == 'MultiPolygon':
|
65 |
+
mycoordslist = [list(x.exterior.coords) for x in small_poly]
|
66 |
+
for coord in mycoordslist:
|
67 |
+
coords = np.dstack((np.array(coord)[:,1], np.array(coord)[:, 0])).flatten()
|
68 |
+
draw.polygon(list(coords), fill=tuple(color))
|
69 |
+
elif poly.geom_type == 'Polygon':
|
70 |
+
#get inner polygon coordinates
|
71 |
+
xy2 = small_poly.exterior.xy
|
72 |
+
coords2 = np.dstack((xy2[1], xy2[0])).flatten()
|
73 |
+
# draw it on canvas, with the appropriate colors
|
74 |
+
draw.polygon(list(coords2), fill=tuple(color))
|
75 |
+
image = image.transpose(Image.FLIP_TOP_BOTTOM)
|
76 |
+
if(fpath):
|
77 |
+
image.save(fpath, quality=100, subsampling=0)
|
78 |
+
return draw, image
|
79 |
+
|
80 |
+
def prompt_to_layout(user_prompt, intensity, fpath=None):
|
81 |
+
if(containsNumber(user_prompt) == True):
|
82 |
+
spaced_prompt = user_prompt.split(' ')
|
83 |
+
new_prompt = ' '.join([word if word.isdigit() == False else num2words(int(word)).lower() for word in spaced_prompt])
|
84 |
+
model_prompt = '[User prompt] {} [Layout]'.format(new_prompt)
|
85 |
+
top_p, top_k = creativity(intensity)
|
86 |
+
model_prompt = '[User prompt] {} [Layout]'.format(user_prompt)
|
87 |
+
input_ids = tokenizer(model_prompt, return_tensors='pt').to(device)
|
88 |
+
output = finetuned.generate(**input_ids, do_sample=True, top_p=top_p, top_k=top_k,
|
89 |
+
eos_token_id=50256, max_length=400)
|
90 |
+
output = tokenizer.batch_decode(output, skip_special_tokens=True)
|
91 |
+
layout = output[0].split('[User prompt]')[1].split('[Layout] ')[1].split(', ')
|
92 |
+
spaces = [txt.split(':')[0] for txt in layout]
|
93 |
+
coords = [txt.split(':')[1].rstrip() for txt in layout]
|
94 |
+
coordinates = [re.findall(regex, coord) for coord in coords]
|
95 |
+
|
96 |
+
num_coords = []
|
97 |
+
for coord in coordinates:
|
98 |
+
temp = []
|
99 |
+
for xy in coord:
|
100 |
+
numbers = xy.split(',')
|
101 |
+
temp.append(tuple([int(num)/14.2 for num in numbers]))
|
102 |
+
num_coords.append(temp)
|
103 |
+
|
104 |
+
new_spaces = []
|
105 |
+
for i, v in enumerate(spaces):
|
106 |
+
totalcount = spaces.count(v)
|
107 |
+
count = spaces[:i].count(v)
|
108 |
+
new_spaces.append(v + str(count + 1) if totalcount > 1 else v)
|
109 |
+
|
110 |
+
out_dict = dict(zip(new_spaces, num_coords))
|
111 |
+
out_dict = json.dumps(out_dict)
|
112 |
+
|
113 |
+
polygons = []
|
114 |
+
for coord in coordinates:
|
115 |
+
polygons.append([point.split(',') for point in coord])
|
116 |
+
geom = []
|
117 |
+
for poly in polygons:
|
118 |
+
scaled_poly = scale(Polygon(np.array(poly, dtype=int)), xfact=2, yfact=2, origin=(0,0))
|
119 |
+
geom.append(scaled_poly)
|
120 |
+
colors = [architext_colors[housegan_labels[space]] for space in spaces]
|
121 |
+
_, im = draw_polygons(geom, colors, fpath=fpath)
|
122 |
+
html = '<img class="labels" src="images/labels.png" />'
|
123 |
+
legend = Image.open("labels.png")
|
124 |
+
imgs_comb = np.vstack([im, legend])
|
125 |
+
imgs_comb = Image.fromarray(imgs_comb)
|
126 |
+
return imgs_comb, out_dict
|
127 |
+
|
128 |
+
|
129 |
+
# Gradio App
|
130 |
+
|
131 |
+
custom_css="""
|
132 |
+
@import url("https://use.typekit.net/nid3pfr.css");
|
133 |
+
.gradio_wrapper .gradio_bg[is_embedded=false] {
|
134 |
+
min-height: 80%;
|
135 |
+
}
|
136 |
+
|
137 |
+
.gradio_wrapper .gradio_bg[is_embedded=false] .gradio_page {
|
138 |
+
display: flex;
|
139 |
+
width: 100vw;
|
140 |
+
min-height: 50vh;
|
141 |
+
flex-direction: column;
|
142 |
+
justify-content: center;
|
143 |
+
align-items: center;
|
144 |
+
margin: 0px;
|
145 |
+
max-width: 100vw;
|
146 |
+
background: #FFFFFF;
|
147 |
+
}
|
148 |
+
|
149 |
+
.gradio_wrapper .gradio_bg[is_embedded=false] .gradio_page .content {
|
150 |
+
padding: 0px;
|
151 |
+
margin: 0px;
|
152 |
+
}
|
153 |
+
|
154 |
+
.gradio_interface {
|
155 |
+
width: 100vw;
|
156 |
+
max-width: 1500px;
|
157 |
+
}
|
158 |
+
|
159 |
+
.gradio_interface .panel:nth-child(2) .component:nth-child(3) {
|
160 |
+
display:none
|
161 |
+
}
|
162 |
+
|
163 |
+
.gradio_wrapper .gradio_bg[theme=default] .panel_buttons {
|
164 |
+
justify-content: flex-end;
|
165 |
+
}
|
166 |
+
|
167 |
+
.gradio_wrapper .gradio_bg[theme=default] .panel_button {
|
168 |
+
flex: 0 0 0;
|
169 |
+
min-width: 150px;
|
170 |
+
}
|
171 |
+
|
172 |
+
.gradio_wrapper .gradio_bg[theme=default] .gradio_interface .panel_button.submit {
|
173 |
+
background: #11213A;
|
174 |
+
border-radius: 5px;
|
175 |
+
color: #FFFFFF;
|
176 |
+
text-transform: uppercase;
|
177 |
+
min-width: 150px;
|
178 |
+
height: 4em;
|
179 |
+
letter-spacing: 0.15em;
|
180 |
+
flex: 0 0 0;
|
181 |
+
}
|
182 |
+
.gradio_wrapper .gradio_bg[theme=default] .gradio_interface .panel_button.submit:hover {
|
183 |
+
background: #000000;
|
184 |
+
}
|
185 |
+
|
186 |
+
.input_text:focus {
|
187 |
+
border-color: #FA7880;
|
188 |
+
}
|
189 |
+
.gradio_wrapper .gradio_bg[theme=default] .gradio_interface .input_text input,
|
190 |
+
.gradio_wrapper .gradio_bg[theme=default] .gradio_interface .input_text textarea {
|
191 |
+
font: 200 45px garamond-premier-pro-display, serif;
|
192 |
+
line-height: 110%;
|
193 |
+
color: #11213A;
|
194 |
+
border-radius: 5px;
|
195 |
+
padding: 15px;
|
196 |
+
border: none;
|
197 |
+
background: #F2F4F4;
|
198 |
+
}
|
199 |
+
.input_text textarea:focus-visible {
|
200 |
+
outline: none;
|
201 |
+
}
|
202 |
+
.gradio_wrapper .gradio_bg[theme=default] .gradio_interface .input_radio .radio_item.selected {
|
203 |
+
background-color: #11213A;
|
204 |
+
}
|
205 |
+
.gradio_wrapper .gradio_bg[theme=default] .gradio_interface .input_radio .selected .radio_circle {
|
206 |
+
border-color: #4365c4;
|
207 |
+
}
|
208 |
+
.gradio_wrapper .gradio_bg[theme=default] .gradio_interface .output_image {
|
209 |
+
width: 100%;
|
210 |
+
height: 40vw;
|
211 |
+
max-height: 630px;
|
212 |
+
}
|
213 |
+
.gradio_wrapper .gradio_bg[theme=default] .gradio_interface .output_image .image_preview_holder {
|
214 |
+
background: transparent;
|
215 |
+
}
|
216 |
+
.panel:nth-child(1) {
|
217 |
+
margin-left: 50px;
|
218 |
+
margin-right: 50px;
|
219 |
+
margin-bottom: 80px;
|
220 |
+
max-width: 750px;
|
221 |
+
}
|
222 |
+
.panel {
|
223 |
+
background: transparent;
|
224 |
+
}
|
225 |
+
.gradio_wrapper .gradio_bg[theme=default] .gradio_interface .component_set {
|
226 |
+
background: transparent;
|
227 |
+
box-shadow: none;
|
228 |
+
}
|
229 |
+
.panel:nth-child(2) .gradio_wrapper .gradio_bg[theme=default] .gradio_interface .panel_header {
|
230 |
+
display: none;
|
231 |
+
}
|
232 |
+
|
233 |
+
.gradio_wrapper .gradio_bg[is_embedded=false] .gradio_page .footer {
|
234 |
+
transform: scale(0.75);
|
235 |
+
filter: grayscale(1);
|
236 |
+
}
|
237 |
+
|
238 |
+
.labels {
|
239 |
+
height: 20px;
|
240 |
+
width: auto;
|
241 |
+
}
|
242 |
+
|
243 |
+
@media (max-width: 1000px){
|
244 |
+
.panel:nth-child(1) {
|
245 |
+
margin-left: 0px;
|
246 |
+
margin-right: 0px;
|
247 |
+
}
|
248 |
+
.gradio_wrapper .gradio_bg[theme=default] .gradio_interface .output_image {
|
249 |
+
height: auto;
|
250 |
+
}
|
251 |
+
}
|
252 |
+
"""
|
253 |
+
creative_slider = gr.inputs.Radio(["Low", "Medium", "High"], default="Low", label='Creativity')
|
254 |
+
textbox = gr.inputs.Textbox(placeholder='An apartment with two bedrooms and one bathroom', lines="3",
|
255 |
+
label="DESCRIBE YOUR IDEAL APARTMENT")
|
256 |
+
generated = gr.outputs.Image(label='Generated Layout')
|
257 |
+
layout = gr.outputs.Textbox(label='Layout Coordinates')
|
258 |
+
|
259 |
+
iface = gr.Interface(fn=prompt_to_layout, inputs=[textbox, creative_slider],
|
260 |
+
outputs=[generated, layout],
|
261 |
+
css=custom_css,
|
262 |
+
theme="default",
|
263 |
+
allow_flagging='never',
|
264 |
+
allow_screenshot=False,
|
265 |
+
thumbnail="thumbnail_gradio.PNG")
|
266 |
+
|
267 |
+
iface.launch(enable_queue=True, share=True)
|
empty.png
ADDED
![]() |
labels.png
ADDED
![]() |
legend.png
ADDED
![]() |
model/config.json
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"activation_function": "gelu_new",
|
3 |
+
"architectures": [
|
4 |
+
"GPTJForCausalLM"
|
5 |
+
],
|
6 |
+
"attn_pdrop": 0.0,
|
7 |
+
"bos_token_id": 50256,
|
8 |
+
"embd_pdrop": 0.0,
|
9 |
+
"eos_token_id": 50256,
|
10 |
+
"gradient_checkpointing": false,
|
11 |
+
"initializer_range": 0.02,
|
12 |
+
"layer_norm_epsilon": 1e-05,
|
13 |
+
"model_type": "gptj",
|
14 |
+
"n_embd": 768,
|
15 |
+
"n_head": 16,
|
16 |
+
"n_layer": 12,
|
17 |
+
"n_positions": 2048,
|
18 |
+
"rotary_dim": 64,
|
19 |
+
"summary_activation": null,
|
20 |
+
"summary_first_dropout": 0.1,
|
21 |
+
"summary_proj_to_labels": true,
|
22 |
+
"summary_type": "cls_index",
|
23 |
+
"summary_use_proj": true,
|
24 |
+
"transformers_version": "4.10.0.dev0",
|
25 |
+
"tokenizer_class": "GPT2Tokenizer",
|
26 |
+
"task_specific_params": {
|
27 |
+
"text-generation": {
|
28 |
+
"do_sample": true,
|
29 |
+
"temperature": 1.0,
|
30 |
+
"max_length": 50
|
31 |
+
}
|
32 |
+
},
|
33 |
+
"torch_dtype": "float16",
|
34 |
+
"use_cache": true,
|
35 |
+
"vocab_size": 50400
|
36 |
+
}
|
model/pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4cd37ed39ddd8a7b95e24f558f632c53feb589aa7fa0904922ebda4aad54e89a
|
3 |
+
size 329164611
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
num2words
|
2 |
+
numpy
|
3 |
+
torch
|
4 |
+
shapely
|
5 |
+
pillow
|
6 |
+
gradio==2.7.5
|
7 |
+
jinja2
|
8 |
+
transformers
|
thumbnail_gradio.PNG
ADDED
|