JingyeChen commited on
Commit
4373d44
1 Parent(s): 401dece
Files changed (1) hide show
  1. app.py +27 -39
app.py CHANGED
@@ -18,30 +18,13 @@ alphabet = string.digits + string.ascii_lowercase + string.ascii_uppercase + str
18
  0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
19
  '''
20
 
21
- # if not os.path.exists('Arial.ttf'):
22
- # os.system('wget https://huggingface.co/datasets/JingyeChen22/TextDiffuser/resolve/main/Arial.ttf')
23
-
24
-
25
  if not os.path.exists('images2'):
26
  os.system('wget https://huggingface.co/datasets/JingyeChen22/TextDiffuser/resolve/main/images2.zip')
27
  with zipfile.ZipFile('images2.zip', 'r') as zip_ref:
28
  zip_ref.extractall('.')
29
 
30
-
31
- # if not os.path.exists('architecture.jpg'):
32
- # os.system('wget https://huggingface.co/JingyeChen22/textdiffuser2-full-ft/tree/main/layout_planner_m1')
33
-
34
- # if not os.path.exists('gray256.jpg'):
35
- # os.system('wget https://huggingface.co/JingyeChen22/textdiffuser2-full-ft/blob/main/gray256.jpg')
36
-
37
- # print(os.system('apt install mlocate'))
38
  os.system('nvidia-smi')
39
  os.system('ls')
40
- # print(os.system('pwd'))
41
- # print(os.system('locate gray256.jpg'))
42
- # # img = Image.open('locate gray256.jpg')
43
- # # print(img.size)
44
- # exit(0)
45
 
46
  #### import m1
47
  from fastchat.model import load_model, get_conversation_template
@@ -372,9 +355,6 @@ def text_to_image(prompt,keywords,radio,slider_step,slider_guidance,slider_batch
372
  row = index // 2
373
  col = index % 2
374
  new_image.paste(image, (col*width, row*height))
375
- # new_image.save(f'{args.output_dir}/pred_img_{sample_index}_{args.local_rank}.jpg')
376
- # results.insert(0, new_image)
377
- # return new_image
378
  os.system('nvidia-smi')
379
  torch.cuda.empty_cache()
380
  os.system('nvidia-smi')
@@ -400,7 +380,7 @@ with gr.Blocks() as demo:
400
  gr.HTML(
401
  """
402
  <div style="text-align: center; max-width: 1600px; margin: 20px auto;">
403
- <h2 style="font-weight: 900; font-size: 2.4rem; margin: 0rem">
404
  TextDiffuser-2: Unleashing the Power of Language Models for Text Rendering
405
  </h2>
406
  <h2 style="font-weight: 460; font-size: 1.1rem; margin: 0rem">
@@ -420,7 +400,7 @@ with gr.Blocks() as demo:
420
  👀 <b>Tips for using this demo</b>: <b>(1)</b> Please carefully read the disclaimer in the below. <b>(2)</b> The specification of keywords is optional. If provided, the language model will do its best to plan layouts using the given keywords. <b>(3)</b> If a template is given, the layout planner (M1) is not used. <b>(4)</b> Three operations, including redo, undo, and skip are provided. When using skip, only the left-top point of a keyword will be recorded, resulting in more diversity but sometimes decreasing the accuracy. <b>(5)</b> The layout planner can produce different layouts. You can increase the temperature to enhance the diversity.
421
  </h2>
422
  <h2 style="text-align: left; font-weight: 450; font-size: 1rem; margin-top: 0.5rem; margin-bottom: 0.5rem">
423
- <b>[L] [C] [M]</b>: We also provide the model combining <b>TextDiffuser-2</b> and <b>LCM</b>. The inference speed is fast using less sampling steps.
424
  </h2>
425
 
426
  <style>
@@ -439,17 +419,15 @@ with gr.Blocks() as demo:
439
  prompt = gr.Textbox(label="Input your prompt here.", placeholder="A beautiful city skyline stamp of Shanghai")
440
  keywords = gr.Textbox(label="(Optional) Input your keywords here. Keywords should be seperated by / (e.g., keyword1/keyword2/...)", placeholder="keyword1/keyword2")
441
 
442
- # 这里加一个会话框
443
-
444
  with gr.Accordion("(Optional) Template - Click to paint", open=False):
445
  with gr.Row():
446
  with gr.Column(scale=1):
447
  i = gr.Image(label="Canvas", type='filepath', value=f'./gray256.jpg', height=256, width=256)
448
  with gr.Column(scale=1):
449
  t = gr.Textbox(label="Keyword", value='input_keyword')
450
- redo = gr.Button(value='Redo - Cancel the last keyword') # 如何给b绑定事件
451
- undo = gr.Button(value='Undo - Clear the canvas') # 如何给b绑定事件
452
- skip_button = gr.Button(value='Skip - Operate next keyword') # 如何给b绑定事件
453
 
454
  i.select(get_pixels,[i,t],[i])
455
  redo.click(exe_redo, [i,t],[i])
@@ -458,7 +436,7 @@ with gr.Blocks() as demo:
458
 
459
  radio = gr.Radio(["TextDiffuser-2", "TextDiffuser-2-LCM"], label="Choice of models", value="TextDiffuser-2")
460
  slider_step = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Sampling step", info="The sampling step for TextDiffuser-2. You may decease the step to 4 when using LCM.")
461
- slider_guidance = gr.Slider(minimum=1, maximum=9, value=7.5, step=0.5, label="Scale of classifier-free guidance", info="The scale of cfg and is set to 7.5 in default. When using LCM, cfg is set to 1.")
462
  slider_batch = gr.Slider(minimum=1, maximum=4, value=4, step=1, label="Batch size", info="The number of images to be sampled.")
463
  slider_temperature = gr.Slider(minimum=0.1, maximum=2, value=0.7, step=0.1, label="Temperature", info="Control the diversity of layout planner. Higher value indicates more diversity.")
464
  slider_natural = gr.Checkbox(label="Natural image generation", value=False, info="The text position and content info will not be incorporated.")
@@ -471,29 +449,39 @@ with gr.Blocks() as demo:
471
  with gr.Accordion("Intermediate results", open=False):
472
  gr.Markdown("Composed prompt")
473
  composed_prompt = gr.Textbox(label='')
474
-
475
- # with gr.Accordion("Intermediate results", open=False):
476
- # gr.Markdown("Layout, segmentation mask, and details of segmentation mask from left to right.")
477
- # intermediate_results = gr.Image(label='')
478
-
479
- # gr.Markdown("## Prompt Examples")
480
 
481
  button.click(text_to_image, inputs=[prompt,keywords,radio,slider_step,slider_guidance,slider_batch,slider_temperature,slider_natural], outputs=[output, composed_prompt])
482
 
483
  gr.Markdown("## Prompt Examples")
484
  gr.Examples(
485
  [
486
- ["A beautiful city skyline stamp of Shanghai", ""],
487
- ["A stamp of U.S.A.", ""],
488
- ["A book cover named summer vibe", ""],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
  ],
490
  prompt,
491
  keywords,
 
492
  examples_per_page=10
493
  )
494
 
495
-
496
-
497
  gr.HTML(
498
  """
499
  <div style="text-align: justify; max-width: 1100px; margin: 20px auto;">
 
18
  0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
19
  '''
20
 
 
 
 
 
21
  if not os.path.exists('images2'):
22
  os.system('wget https://huggingface.co/datasets/JingyeChen22/TextDiffuser/resolve/main/images2.zip')
23
  with zipfile.ZipFile('images2.zip', 'r') as zip_ref:
24
  zip_ref.extractall('.')
25
 
 
 
 
 
 
 
 
 
26
  os.system('nvidia-smi')
27
  os.system('ls')
 
 
 
 
 
28
 
29
  #### import m1
30
  from fastchat.model import load_model, get_conversation_template
 
355
  row = index // 2
356
  col = index % 2
357
  new_image.paste(image, (col*width, row*height))
 
 
 
358
  os.system('nvidia-smi')
359
  torch.cuda.empty_cache()
360
  os.system('nvidia-smi')
 
380
  gr.HTML(
381
  """
382
  <div style="text-align: center; max-width: 1600px; margin: 20px auto;">
383
+ <h2 style="font-weight: 900; font-size: 2.3rem; margin: 0rem">
384
  TextDiffuser-2: Unleashing the Power of Language Models for Text Rendering
385
  </h2>
386
  <h2 style="font-weight: 460; font-size: 1.1rem; margin: 0rem">
 
400
  👀 <b>Tips for using this demo</b>: <b>(1)</b> Please carefully read the disclaimer in the below. <b>(2)</b> The specification of keywords is optional. If provided, the language model will do its best to plan layouts using the given keywords. <b>(3)</b> If a template is given, the layout planner (M1) is not used. <b>(4)</b> Three operations, including redo, undo, and skip are provided. When using skip, only the left-top point of a keyword will be recorded, resulting in more diversity but sometimes decreasing the accuracy. <b>(5)</b> The layout planner can produce different layouts. You can increase the temperature to enhance the diversity.
401
  </h2>
402
  <h2 style="text-align: left; font-weight: 450; font-size: 1rem; margin-top: 0.5rem; margin-bottom: 0.5rem">
403
+ ✨ We also provide the experimental demo combining <b>TextDiffuser-2</b> and <b>LCM</b>. The inference is fast using less sampling steps, although the precision in text rendering might decrease.
404
  </h2>
405
 
406
  <style>
 
419
  prompt = gr.Textbox(label="Input your prompt here.", placeholder="A beautiful city skyline stamp of Shanghai")
420
  keywords = gr.Textbox(label="(Optional) Input your keywords here. Keywords should be seperated by / (e.g., keyword1/keyword2/...)", placeholder="keyword1/keyword2")
421
 
 
 
422
  with gr.Accordion("(Optional) Template - Click to paint", open=False):
423
  with gr.Row():
424
  with gr.Column(scale=1):
425
  i = gr.Image(label="Canvas", type='filepath', value=f'./gray256.jpg', height=256, width=256)
426
  with gr.Column(scale=1):
427
  t = gr.Textbox(label="Keyword", value='input_keyword')
428
+ redo = gr.Button(value='Redo - Cancel the last keyword')
429
+ undo = gr.Button(value='Undo - Clear the canvas')
430
+ skip_button = gr.Button(value='Skip - Operate next keyword')
431
 
432
  i.select(get_pixels,[i,t],[i])
433
  redo.click(exe_redo, [i,t],[i])
 
436
 
437
  radio = gr.Radio(["TextDiffuser-2", "TextDiffuser-2-LCM"], label="Choice of models", value="TextDiffuser-2")
438
  slider_step = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Sampling step", info="The sampling step for TextDiffuser-2. You may decease the step to 4 when using LCM.")
439
+ slider_guidance = gr.Slider(minimum=1, maximum=13, value=7.5, step=0.5, label="Scale of classifier-free guidance", info="The scale of cfg and is set to 7.5 in default. When using LCM, cfg is set to 1.")
440
  slider_batch = gr.Slider(minimum=1, maximum=4, value=4, step=1, label="Batch size", info="The number of images to be sampled.")
441
  slider_temperature = gr.Slider(minimum=0.1, maximum=2, value=0.7, step=0.1, label="Temperature", info="Control the diversity of layout planner. Higher value indicates more diversity.")
442
  slider_natural = gr.Checkbox(label="Natural image generation", value=False, info="The text position and content info will not be incorporated.")
 
449
  with gr.Accordion("Intermediate results", open=False):
450
  gr.Markdown("Composed prompt")
451
  composed_prompt = gr.Textbox(label='')
 
 
 
 
 
 
452
 
453
  button.click(text_to_image, inputs=[prompt,keywords,radio,slider_step,slider_guidance,slider_batch,slider_temperature,slider_natural], outputs=[output, composed_prompt])
454
 
455
  gr.Markdown("## Prompt Examples")
456
  gr.Examples(
457
  [
458
+ ["A beautiful city skyline stamp of Shanghai", "", False],
459
+ ["A logo of superman", "", False],
460
+ ["A pencil sketch of a tree with the title nothing to tree here", "", False],
461
+ ["handwritten signature of peter", "", False],
462
+ ["Delicate greeting card of happy birthday to xyz", "", False],
463
+ ["Book cover of good morning baby ", "", False],
464
+ ["The handwritten words Hello World displayed on a wall in a neon light effect", "", False],
465
+ ["Logo of winter in artistic font, made by snowflake", "", False],
466
+ ["A book cover named summer vibe", "", False],
467
+ ["Newspaper with the title Love Story", "", False],
468
+ ["A logo for the company EcoGrow, where the letters look like plants", "EcoGrow", False],
469
+ ["A poster titled 'Quails of North America', showing different kinds of quails.", "Quails/of/North/America", False],
470
+ ["A detailed portrait of a fox guardian with a shield with Kung Fu written on it, by victo ngai and justin gerard, digital art, realistic painting, very detailed, fantasy, high definition, cinematic light, dnd, trending on artstation", "kung/fu", False],
471
+ ["A stamp of breath of the wild", "breath/of/the/wild", False],
472
+ ["Poster of the incoming movie Transformers", "Transformers", False],
473
+ ["Some apples are on a table", "", True],
474
+ ["a hotdog with mustard and other toppings on it", "", True],
475
+ ["a bathroom that has a slanted ceiling and a large bath tub", "", True],
476
+ ["a man holding a tennis racquet on a tennis court", "", True],
477
+ ["hamburger with bacon, lettuce, tomato and cheese| promotional image| hyperquality| products shot| full - color| extreme render| mouthwatering", "", True],
478
  ],
479
  prompt,
480
  keywords,
481
+ slider_natural,
482
  examples_per_page=10
483
  )
484
 
 
 
485
  gr.HTML(
486
  """
487
  <div style="text-align: justify; max-width: 1100px; margin: 20px auto;">