THEODOROS commited on
Commit
e1ff935
·
1 Parent(s): 401dc2f

update app

Browse files

updating for gpt-j model

Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -62,14 +62,14 @@ def draw_polygons(polygons, colors, im_size=(256, 256), b_color="white", fpath=N
62
 
63
  return draw, image
64
 
65
- def prompt_to_layout(user_prompt, fpath=None):
66
 
67
  model_prompt = '[User prompt] {} [Layout]'.format(user_prompt)
68
  input_ids = tokenizer(model_prompt, return_tensors='pt')
69
- output = finetuned.generate(**input_ids, do_sample=True, top_p=0.94, top_k=100, max_length=300)
70
  output = tokenizer.batch_decode(output, skip_special_tokens=True)
71
 
72
- layout = output[0].split('[User prompt]')[1].split('[Layout]')[1].split(', ')
73
  spaces = [txt.split(':')[0] for txt in layout]
74
 
75
  coordinates = [txt.split(':')[1] for txt in layout]
@@ -83,11 +83,11 @@ def prompt_to_layout(user_prompt, fpath=None):
83
  for poly in polygons:
84
  geom.append(Polygon(np.array(poly, dtype=int)))
85
 
86
- colors = [architext_colors[housegan_labels[space]] for space in spaces]
87
 
88
  _, im = draw_polygons(geom, colors, fpath=fpath)
89
 
90
- legend = Image.open("legend.png")
91
 
92
  im = np.array(im)
93
  im[:40, :] = np.array(legend)
@@ -96,7 +96,7 @@ def prompt_to_layout(user_prompt, fpath=None):
96
  return im, layout, output
97
 
98
  def mut_txt2layout(mut_output):
99
- output = mut_output[0].split('[User prompt]')[1].split('[Layout]')[1].split(', ')
100
  spaces = [txt.split(':')[0].strip(' ') for txt in output]
101
  coordinates = [txt.split(':')[1] for txt in output]
102
  coordinates = [re.findall(regex, coord) for coord in coordinates]
@@ -109,10 +109,10 @@ def mut_txt2layout(mut_output):
109
  for poly in polygons:
110
  geom.append(Polygon(np.array(poly, dtype=int)))
111
 
112
- colors = [architext_colors[housegan_labels[space]] for space in spaces]
113
  _, im = draw_polygons(geom, colors, fpath=None)
114
 
115
- legend = Image.open("legend.png")
116
 
117
  im = np.array(im)
118
  im[:40, :] = np.array(legend)
@@ -120,19 +120,20 @@ def mut_txt2layout(mut_output):
120
 
121
  return im
122
 
123
- def prompt_with_mutation(user_prompt, mut_rate, fpath=None):
124
 
125
  #Create initial layout based on prompt
126
- im, layout, output = prompt_to_layout(user_prompt)
127
 
128
  #Create mutated layout based on initial
129
  mut_len = int((1-mut_rate)*len(layout))
130
  index1 = random.randrange(0,len(layout)-mut_len)
131
  rooms = layout[index1:index1+mut_len]
132
- rooms = ', '.join(rooms) + ', '
 
133
  new_prompt = '[User prompt] {} [Layout] {}'.format(user_prompt, rooms)
134
  input_ids = tokenizer(new_prompt, return_tensors='pt')
135
- mut_output = finetuned.generate(**input_ids, do_sample=True, top_p=0.94, top_k=100, max_length=300)
136
  mut_output = tokenizer.batch_decode(mut_output, skip_special_tokens=True)
137
  mut_im = mut_txt2layout(mut_output)
138
 
@@ -213,28 +214,30 @@ custom_css="""
213
  opacity: 1 !important;
214
  }"""
215
 
216
- def gen_and_mutate(user_prompt, mutate=False, mut_rate=0.2):
217
  if(mutate):
218
  im, mut_im = None, None
219
  while (mut_im is None):
220
  try:
221
- im, mut_im = prompt_with_mutation(user_prompt, mut_rate)
222
  except:
223
  pass
224
  else:
225
  mut_im=Image.open("empty.png")
226
- im, _, _ = prompt_to_layout(user_prompt)
227
 
228
  return im, mut_im
229
 
230
  checkbox = gr.inputs.Checkbox(label='Mutate')
231
- slider = gr.inputs.Slider(0.2, 0.8, 0.1, label='Mutation rate')
232
- textbox = gr.inputs.Textbox(placeholder='house with two bedrooms and one bathroom', lines="3", label="DESCRIBE YOUR DESIGN")
233
-
 
 
234
  generated = gr.outputs.Image(label='Generated Layout')
235
  mutated = gr.outputs.Image(label='Mutated Layout')
236
 
237
- iface = gr.Interface(fn=gen_and_mutate, inputs=[textbox, checkbox, slider], outputs=[generated, mutated],
238
  css=custom_css,
239
  thumbnail="thumbnail_gradio.PNG",
240
  description='Demo of Semantic Generation of Residential Layouts \n',
 
62
 
63
  return draw, image
64
 
65
+ def prompt_to_layout(user_prompt, top_p, top_k, fpath=None):
66
 
67
  model_prompt = '[User prompt] {} [Layout]'.format(user_prompt)
68
  input_ids = tokenizer(model_prompt, return_tensors='pt')
69
+ output = finetuned.generate(**input_ids, do_sample=True, top_p=top_p, top_k=top_k, max_length=300)
70
  output = tokenizer.batch_decode(output, skip_special_tokens=True)
71
 
72
+ layout = output[0].split('[User prompt]')[1].split('[Layout] ')[1].split(', ')
73
  spaces = [txt.split(':')[0] for txt in layout]
74
 
75
  coordinates = [txt.split(':')[1] for txt in layout]
 
83
  for poly in polygons:
84
  geom.append(Polygon(np.array(poly, dtype=int)))
85
 
86
+ colors = [architext_colors2[housegan_labels[space]] for space in spaces]
87
 
88
  _, im = draw_polygons(geom, colors, fpath=fpath)
89
 
90
+ legend = Image.open(r"F:\Personal\Startup\ArchiText\Media\legend.png")
91
 
92
  im = np.array(im)
93
  im[:40, :] = np.array(legend)
 
96
  return im, layout, output
97
 
98
  def mut_txt2layout(mut_output):
99
+ output = mut_output[0].rstrip().split('[User prompt]')[1].split('[Layout]')[1].split(', ')
100
  spaces = [txt.split(':')[0].strip(' ') for txt in output]
101
  coordinates = [txt.split(':')[1] for txt in output]
102
  coordinates = [re.findall(regex, coord) for coord in coordinates]
 
109
  for poly in polygons:
110
  geom.append(Polygon(np.array(poly, dtype=int)))
111
 
112
+ colors = [architext_colors2[housegan_labels[space]] for space in spaces]
113
  _, im = draw_polygons(geom, colors, fpath=None)
114
 
115
+ legend = Image.open(r"F:\Personal\Startup\ArchiText\Media\legend.png")
116
 
117
  im = np.array(im)
118
  im[:40, :] = np.array(legend)
 
120
 
121
  return im
122
 
123
+ def prompt_with_mutation(user_prompt, top_p, top_k, mut_rate, fpath=None):
124
 
125
  #Create initial layout based on prompt
126
+ im, layout, output = prompt_to_layout(user_prompt, top_p=top_p, top_k=top_k)
127
 
128
  #Create mutated layout based on initial
129
  mut_len = int((1-mut_rate)*len(layout))
130
  index1 = random.randrange(0,len(layout)-mut_len)
131
  rooms = layout[index1:index1+mut_len]
132
+ rooms[-1] = rooms[-1].split(':')[0] + ':'
133
+ rooms = ', '.join(rooms)# + ', '
134
  new_prompt = '[User prompt] {} [Layout] {}'.format(user_prompt, rooms)
135
  input_ids = tokenizer(new_prompt, return_tensors='pt')
136
+ mut_output = finetuned.generate(**input_ids, do_sample=True, top_p=top_p, top_k=top_k, max_length=400)
137
  mut_output = tokenizer.batch_decode(mut_output, skip_special_tokens=True)
138
  mut_im = mut_txt2layout(mut_output)
139
 
 
214
  opacity: 1 !important;
215
  }"""
216
 
217
+ def gen_and_mutate(user_prompt, mutate=False, top_p=0.94, top_k=100, mut_rate=0.2):
218
  if(mutate):
219
  im, mut_im = None, None
220
  while (mut_im is None):
221
  try:
222
+ im, mut_im = prompt_with_mutation(user_prompt, top_p, top_k, mut_rate)
223
  except:
224
  pass
225
  else:
226
  mut_im=Image.open("empty.png")
227
+ im, _, _ = prompt_to_layout(user_prompt, top_p, top_k)
228
 
229
  return im, mut_im
230
 
231
  checkbox = gr.inputs.Checkbox(label='Mutate')
232
+ topp_slider = gr.inputs.Slider(0.1, 1.0, 0.01, default=0.94, label='top_p')
233
+ topk_slider = gr.inputs.Slider(0, 100, 25, default=0, label='top_k')
234
+ mut_slider = gr.inputs.Slider(0.2, 0.8, 0.1, default=0.3, label='Mutation rate')
235
+ textbox = gr.inputs.Textbox(placeholder='house with two bedrooms and one bathroom', lines="2",
236
+ label="DESCRIBE YOUR DESIGN")
237
  generated = gr.outputs.Image(label='Generated Layout')
238
  mutated = gr.outputs.Image(label='Mutated Layout')
239
 
240
+ iface = gr.Interface(fn=gen_and_mutate, inputs=[textbox, checkbox, topp_slider, topk_slider, mut_slider], outputs=[generated, mutated],
241
  css=custom_css,
242
  thumbnail="thumbnail_gradio.PNG",
243
  description='Demo of Semantic Generation of Residential Layouts \n',