JingyeChen commited on
Commit
9dc9163
1 Parent(s): 160996c
Files changed (1) hide show
  1. app.py +68 -51
app.py CHANGED
@@ -82,37 +82,43 @@ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
82
  pipe.load_lora_weights(lcm_lora_id)
83
  pipe.to(device="cuda")
84
 
85
-
86
  #### for interactive
87
- stack = []
88
- state = 0
89
  font = ImageFont.truetype("./Arial.ttf", 32)
90
 
91
- def skip_fun(i, t):
92
- global state
93
- state = 0
 
 
94
 
 
95
 
96
- def exe_undo(i, t):
97
- global stack
98
- global state
99
- state = 0
100
- stack = []
 
 
101
  image = Image.open(f'./gray256.jpg')
102
- print('stack', stack)
103
  return image
104
 
105
 
106
- def exe_redo(i, t):
107
- global state
108
- state = 0
 
109
 
110
- if len(stack) > 0:
111
- stack.pop()
112
  image = Image.open(f'./gray256.jpg')
113
  draw = ImageDraw.Draw(image)
114
 
115
- for items in stack:
116
  # print('now', items)
117
  text_position, t = items
118
  if len(text_position) == 2:
@@ -133,34 +139,34 @@ def exe_redo(i, t):
133
  draw.ellipse((leftUpPoint,rightDownPoint), fill='red')
134
  draw.rectangle((x0,y0,x1,y1), outline=(255, 0, 0) )
135
 
136
- print('stack', stack)
137
  return image
138
 
139
- def get_pixels(i, t, evt: gr.SelectData):
140
- global state
141
 
142
  text_position = evt.index
143
 
144
- if state == 0:
145
- stack.append(
146
  (text_position, t)
147
  )
148
- print(text_position, stack)
149
- state = 1
150
  else:
151
 
152
- (_, t) = stack.pop()
153
  x, y = _
154
- stack.append(
155
  ((x,y,text_position[0],text_position[1]), t)
156
  )
157
- state = 0
158
 
159
 
160
  image = Image.open(f'./gray256.jpg')
161
  draw = ImageDraw.Draw(image)
162
 
163
- for items in stack:
164
  # print('now', items)
165
  text_position, t = items
166
  if len(text_position) == 2:
@@ -181,7 +187,7 @@ def get_pixels(i, t, evt: gr.SelectData):
181
  draw.ellipse((leftUpPoint,rightDownPoint), fill='red')
182
  draw.rectangle((x0,y0,x1,y1), outline=(255, 0, 0) )
183
 
184
- print('stack', stack)
185
 
186
  return image
187
 
@@ -209,12 +215,12 @@ def get_layout_image(ocrs):
209
 
210
 
211
 
212
- def text_to_image(prompt,keywords,positive_prompt,radio,slider_step,slider_guidance,slider_batch,slider_temperature,slider_natural):
213
 
214
  print(f'[info] Prompt: {prompt} | Keywords: {keywords} | Radio: {radio} | Steps: {slider_step} | Guidance: {slider_guidance} | Natural: {slider_natural}')
215
 
216
- global stack
217
- global state
218
 
219
  if len(positive_prompt.strip()) != 0:
220
  prompt += positive_prompt
@@ -229,7 +235,7 @@ def text_to_image(prompt,keywords,positive_prompt,radio,slider_step,slider_guida
229
  prompt = tokenizer.encode(user_prompt)
230
  layout_image = None
231
  else:
232
- if len(stack) == 0:
233
 
234
  if len(keywords.strip()) == 0:
235
  template = f'Given a prompt that will be used to generate an image, plan the layout of visual text for the image. The size of the image is 128x128. Therefore, all properties of the positions should not exceed 128, including the coordinates of top, left, right, and bottom. All keywords are included in the caption. You dont need to specify the details of font styles. At each line, the format should be keyword left, top, right, bottom. So let us begin. Prompt: {user_prompt}'
@@ -313,7 +319,7 @@ def text_to_image(prompt,keywords,positive_prompt,radio,slider_step,slider_guida
313
  user_prompt += ' <|endoftext|>'
314
  layout_image = None
315
 
316
- for items in stack:
317
  position, text = items
318
 
319
 
@@ -407,6 +413,14 @@ def text_to_image(prompt,keywords,positive_prompt,radio,slider_step,slider_guida
407
 
408
  with gr.Blocks() as demo:
409
 
 
 
 
 
 
 
 
 
410
  gr.HTML(
411
  """
412
  <div style="text-align: center; max-width: 1600px; margin: 20px auto;">
@@ -447,21 +461,17 @@ with gr.Blocks() as demo:
447
  keywords = gr.Textbox(label="(Optional) Keywords. Should be seperated by / (e.g., keyword1/keyword2/...)", placeholder="keyword1/keyword2")
448
  positive_prompt = gr.Textbox(label="(Optional) Positive prompt", value=", digital art, very detailed, fantasy, high definition, cinematic light, dnd, trending on artstation")
449
 
450
- # # many encounter concurrent problem
451
- # with gr.Accordion("(Optional) Template - Click to paint", open=False):
452
- # with gr.Row():
453
- # with gr.Column(scale=1):
454
- # i = gr.Image(label="Canvas", type='filepath', value=f'./gray256.jpg', height=256, width=256)
455
- # with gr.Column(scale=1):
456
- # t = gr.Textbox(label="Keyword", value='input_keyword')
457
- # redo = gr.Button(value='Redo - Cancel the last keyword')
458
- # undo = gr.Button(value='Undo - Clear the canvas')
459
- # skip_button = gr.Button(value='Skip - Operate the next keyword')
460
-
461
- # i.select(get_pixels,[i,t],[i])
462
- # redo.click(exe_redo, [i,t],[i])
463
- # undo.click(exe_undo, [i,t],[i])
464
- # skip_button.click(skip_fun, [i,t])
465
 
466
  radio = gr.Radio(["TextDiffuser-2", "TextDiffuser-2-LCM"], label="Choice of models", value="TextDiffuser-2")
467
  slider_natural = gr.Checkbox(label="Natural image generation", value=False, info="The text position and content info will not be incorporated.")
@@ -471,6 +481,13 @@ with gr.Blocks() as demo:
471
  slider_temperature = gr.Slider(minimum=0.1, maximum=2, value=1.4, step=0.1, label="Temperature", info="Control the diversity of layout planner. Higher value indicates more diversity.")
472
  # slider_seed = gr.Slider(minimum=1, maximum=10000, label="Seed", randomize=True)
473
  button = gr.Button("Generate")
 
 
 
 
 
 
 
474
 
475
  with gr.Column(scale=1):
476
  output = gr.Gallery(label='Generated image')
@@ -482,7 +499,7 @@ with gr.Blocks() as demo:
482
  layout = gr.Image(height=256, width=256)
483
 
484
 
485
- button.click(text_to_image, inputs=[prompt,keywords,positive_prompt, radio,slider_step,slider_guidance,slider_batch,slider_temperature,slider_natural], outputs=[output, composed_prompt, layout])
486
 
487
  gr.Markdown("## Prompt Examples")
488
  gr.Examples(
 
82
  pipe.load_lora_weights(lcm_lora_id)
83
  pipe.to(device="cuda")
84
 
85
+ global_dict = {}
86
  #### for interactive
87
+ # stack = []
88
+ # state = 0
89
  font = ImageFont.truetype("./Arial.ttf", 32)
90
 
91
+ def skip_fun(i, t, guest_id):
92
+ global_dict[guest_id]['state'] = 0
93
+ # global state
94
+ # state = 0
95
+
96
 
97
+ def exe_undo(i, t, guest_id):
98
 
99
+ global_dict[guest_id]['stack'] = []
100
+ global_dict[guest_id]['state'] = 0
101
+
102
+ # global stack
103
+ # global state
104
+ # state = 0
105
+ # stack = []
106
  image = Image.open(f'./gray256.jpg')
107
+ # print('stack', stack)
108
  return image
109
 
110
 
111
+ def exe_redo(i, t, guest_id):
112
+ # global state
113
+ # state = 0
114
+ global_dict[guest_id]['state'] = 0
115
 
116
+ if len(global_dict[guest_id]['stack']) > 0:
117
+ global_dict[guest_id]['stack'].pop()
118
  image = Image.open(f'./gray256.jpg')
119
  draw = ImageDraw.Draw(image)
120
 
121
+ for items in global_dict[guest_id]['stack']:
122
  # print('now', items)
123
  text_position, t = items
124
  if len(text_position) == 2:
 
139
  draw.ellipse((leftUpPoint,rightDownPoint), fill='red')
140
  draw.rectangle((x0,y0,x1,y1), outline=(255, 0, 0) )
141
 
142
+ print('stack', global_dict[guest_id]['stack'])
143
  return image
144
 
145
+ def get_pixels(i, t, guest_id, evt: gr.SelectData):
146
+ # global state
147
 
148
  text_position = evt.index
149
 
150
+ if global_dict[guest_id]['state'] == 0:
151
+ global_dict[guest_id]['stack'].append(
152
  (text_position, t)
153
  )
154
+ print(text_position, global_dict[guest_id]['stack'])
155
+ global_dict[guest_id]['state'] = 1
156
  else:
157
 
158
+ (_, t) = global_dict[guest_id]['stack'].pop()
159
  x, y = _
160
+ global_dict[guest_id]['stack'].append(
161
  ((x,y,text_position[0],text_position[1]), t)
162
  )
163
+ global_dict[guest_id]['state'] = 0
164
 
165
 
166
  image = Image.open(f'./gray256.jpg')
167
  draw = ImageDraw.Draw(image)
168
 
169
+ for items in global_dict[guest_id]['stack']:
170
  # print('now', items)
171
  text_position, t = items
172
  if len(text_position) == 2:
 
187
  draw.ellipse((leftUpPoint,rightDownPoint), fill='red')
188
  draw.rectangle((x0,y0,x1,y1), outline=(255, 0, 0) )
189
 
190
+ print('stack', global_dict[guest_id]['stack'])
191
 
192
  return image
193
 
 
215
 
216
 
217
 
218
+ def text_to_image(guest_id, prompt,keywords,positive_prompt,radio,slider_step,slider_guidance,slider_batch,slider_temperature,slider_natural):
219
 
220
  print(f'[info] Prompt: {prompt} | Keywords: {keywords} | Radio: {radio} | Steps: {slider_step} | Guidance: {slider_guidance} | Natural: {slider_natural}')
221
 
222
+ # global stack
223
+ # global state
224
 
225
  if len(positive_prompt.strip()) != 0:
226
  prompt += positive_prompt
 
235
  prompt = tokenizer.encode(user_prompt)
236
  layout_image = None
237
  else:
238
+ if len(global_dict[guest_id]['stack']) == 0:
239
 
240
  if len(keywords.strip()) == 0:
241
  template = f'Given a prompt that will be used to generate an image, plan the layout of visual text for the image. The size of the image is 128x128. Therefore, all properties of the positions should not exceed 128, including the coordinates of top, left, right, and bottom. All keywords are included in the caption. You dont need to specify the details of font styles. At each line, the format should be keyword left, top, right, bottom. So let us begin. Prompt: {user_prompt}'
 
319
  user_prompt += ' <|endoftext|>'
320
  layout_image = None
321
 
322
+ for items in global_dict[guest_id]['stack']:
323
  position, text = items
324
 
325
 
 
413
 
414
  with gr.Blocks() as demo:
415
 
416
+
417
+ guest_id = random.randint(0,100000000)
418
+ # register
419
+ global_dict[guest_id] = {
420
+ 'state': 0,
421
+ 'stack': []
422
+ }
423
+
424
  gr.HTML(
425
  """
426
  <div style="text-align: center; max-width: 1600px; margin: 20px auto;">
 
461
  keywords = gr.Textbox(label="(Optional) Keywords. Should be seperated by / (e.g., keyword1/keyword2/...)", placeholder="keyword1/keyword2")
462
  positive_prompt = gr.Textbox(label="(Optional) Positive prompt", value=", digital art, very detailed, fantasy, high definition, cinematic light, dnd, trending on artstation")
463
 
464
+ # many encounter concurrent problem
465
+ with gr.Accordion("(Optional) Template - Click to paint", open=False):
466
+ with gr.Row():
467
+ with gr.Column(scale=1):
468
+ i = gr.Image(label="Canvas", type='filepath', value=f'./gray256.jpg', height=256, width=256)
469
+ with gr.Column(scale=1):
470
+ t = gr.Textbox(label="Keyword", value='input_keyword')
471
+ redo = gr.Button(value='Redo - Cancel the last keyword')
472
+ undo = gr.Button(value='Undo - Clear the canvas')
473
+ skip_button = gr.Button(value='Skip - Operate the next keyword')
474
+
 
 
 
 
475
 
476
  radio = gr.Radio(["TextDiffuser-2", "TextDiffuser-2-LCM"], label="Choice of models", value="TextDiffuser-2")
477
  slider_natural = gr.Checkbox(label="Natural image generation", value=False, info="The text position and content info will not be incorporated.")
 
481
  slider_temperature = gr.Slider(minimum=0.1, maximum=2, value=1.4, step=0.1, label="Temperature", info="Control the diversity of layout planner. Higher value indicates more diversity.")
482
  # slider_seed = gr.Slider(minimum=1, maximum=10000, label="Seed", randomize=True)
483
  button = gr.Button("Generate")
484
+
485
+ guest_id_box = gr.Textbox(label="guest_id", value=f"{guest_id}")
486
+ i.select(get_pixels,[i,t,guest_id_box],[i])
487
+ redo.click(exe_redo, [i,t,guest_id_box],[i])
488
+ undo.click(exe_undo, [i,t,guest_id_box],[i])
489
+ skip_button.click(skip_fun, [i,t,guest_id_box])
490
+
491
 
492
  with gr.Column(scale=1):
493
  output = gr.Gallery(label='Generated image')
 
499
  layout = gr.Image(height=256, width=256)
500
 
501
 
502
+ button.click(text_to_image, inputs=[guest_id, prompt,keywords,positive_prompt, radio,slider_step,slider_guidance,slider_batch,slider_temperature,slider_natural], outputs=[output, composed_prompt, layout])
503
 
504
  gr.Markdown("## Prompt Examples")
505
  gr.Examples(