svjack commited on
Commit
cd03317
1 Parent(s): 9a6c033

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -108
app.py CHANGED
@@ -1,7 +1,3 @@
1
- '''
2
- pip install extcolors
3
- '''
4
-
5
  import os
6
  import tensorflow as tf
7
  os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
@@ -10,7 +6,6 @@ import PIL.Image
10
  import gradio as gr
11
  import tensorflow_hub as hub
12
  import matplotlib.pyplot as plt
13
- #from real_esrgan_app import *
14
 
15
  import gradio as gr
16
  import requests
@@ -34,14 +29,6 @@ import pathlib
34
 
35
  API_TOKEN = os.environ.get("HF_READ_TOKEN")
36
 
37
- '''
38
- dataset = load_dataset("Gustavosta/Stable-Diffusion-Prompts")
39
- prompt_df = dataset["train"].to_pandas()
40
- prompt_df = pd.read_csv("Stable-Diffusion-Prompts.csv")
41
- '''
42
-
43
- #DEFAULT_MODEL = "stabilityai/stable-diffusion-2-1"
44
- #DEFAULT_PROMPT = "1girl, aqua eyes, baseball cap, blonde hair, closed mouth, earrings, green background, hat, hoop earrings, jewelry, looking at viewer, shirt, short hair, simple background, solo, upper body, yellow shirt"
45
  DEFAULT_PROMPT = "X go to Istanbul"
46
  DEFAULT_ROLE = "Superman"
47
  DEFAULT_BOOK_COVER = "book_cover_dir/Blank.jpg"
@@ -60,67 +47,27 @@ def tensor_to_image(tensor):
60
  def perform_neural_transfer(content_image_input, style_image_input, hub_module = hub_module):
61
  content_image = content_image_input.astype(np.float32)[np.newaxis, ...] / 255.
62
  content_image = tf.image.resize(content_image, (400, 600))
63
-
64
- #style_image_input = style_urls[style_image_input]
65
- #style_image_input = plt.imread(style_image_input)
66
  style_image = style_image_input.astype(np.float32)[np.newaxis, ...] / 255.
67
-
68
  style_image = tf.image.resize(style_image, (256, 256))
69
-
70
  outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
71
  stylized_image = outputs[0]
72
-
73
  stylized_image = tensor_to_image(stylized_image)
74
  content_image_input = tensor_to_image(content_image_input)
75
  stylized_image = stylized_image.resize(content_image_input.size)
76
  return stylized_image
77
- '''
78
- print("super_resolution_type :")
79
- print(super_resolution_type)
80
- #print(super_resolution_type.value)
81
-
82
- if super_resolution_type not in ["base", "anime"]:
83
- return stylized_image
84
- else:
85
- print("call else :")
86
- stylized_image = inference(stylized_image, super_resolution_type)
87
- return stylized_image
88
- '''
89
 
90
  list_models = [
91
- #"SDXL-1.0",
92
  "Pixel-Art-XL",
93
  "SD-1.5",
94
  "OpenJourney-V4",
95
  "Anything-V4",
96
  "Disney-Pixar-Cartoon",
97
  "Dalle-3-XL",
98
- #"Midjourney-V4-XL",
99
  ]
100
 
101
- #list_prompts = get_samples()
102
 
103
  def generate_txt2img(current_model, prompt, is_negative=False, image_style="None style", steps=50, cfg_scale=7,
104
  seed=None, API_TOKEN = API_TOKEN):
105
-
106
- '''
107
- if current_model == "SD-1.5":
108
- API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
109
- elif current_model == "SDXL-1.0":
110
- API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
111
- elif current_model == "OpenJourney-V4":
112
- API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"
113
- elif current_model == "Anything-V4":
114
- API_URL = "https://api-inference.huggingface.co/models/xyn-ai/anything-v4.0"
115
- elif current_model == "Disney-Pixar-Cartoon":
116
- API_URL = "https://api-inference.huggingface.co/models/stablediffusionapi/disney-pixar-cartoon"
117
- elif current_model == "Pixel-Art-XL":
118
- API_URL = "https://api-inference.huggingface.co/models/nerijs/pixel-art-xl"
119
- elif current_model == "Dalle-3-XL":
120
- API_URL = "https://api-inference.huggingface.co/models/openskyml/dalle-3-xl"
121
- elif current_model == "Midjourney-V4-XL":
122
- API_URL = "https://api-inference.huggingface.co/models/openskyml/midjourney-v4-xl"
123
- '''
124
  if current_model == "SD-1.5":
125
  API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
126
  elif current_model == "OpenJourney-V4":
@@ -324,20 +271,6 @@ def add_caption_on_image(input_image, caption, marg_ratio = 0.15, row_token_num
324
  marg_size = int(marg_ratio * max_image_size)
325
  colors, pixel_count = extcolors.extract_from_image(input_image)
326
  input_image = add_margin(input_image, marg_size, 0, 0, marg_size, colors[0][0])
327
- '''
328
- tmp_name = "{}.png".format(uuid1())
329
- input_image.save(tmp_name)
330
- ImageCaptioner.add_captions(tmp_name,
331
- caption,
332
- overwrite = 1,
333
- size = int(marg_size / 4),
334
- align = "TOP_LEFT",
335
- output = tmp_name,
336
- color = "black",
337
- )
338
- output_image = Image.open(tmp_name)
339
- os.remove(tmp_name)
340
- '''
341
  font = ImageFont.truetype("DejaVuSerif-Italic.ttf" ,int(marg_size / 4))
342
  caption_token_list = list(map(lambda x: x.strip() ,caption.split(" ")))
343
  caption_list = list(map(" ".join ,batch_as_list(caption_token_list, row_token_num)))
@@ -349,7 +282,6 @@ def add_caption_on_image(input_image, caption, marg_ratio = 0.15, row_token_num
349
  (line_num + 1) * 1.1
350
  )))
351
  draw.text(position, line_caption, fill="black", font = font)
352
-
353
  return input_image
354
 
355
 
@@ -404,26 +336,6 @@ def make_video_from_image_list(image_list, video_name = "ppt.avi"):
404
  generate_video(image_list, video_name = video_name)
405
  return video_name
406
 
407
- '''
408
- style_transfer_client = Client("https://svjack-super-resolution-neural-style-transfer.hf.space")
409
- def style_transfer_func(content_img, style_img, style_transfer_client = style_transfer_client):
410
- from uuid import uuid1
411
- assert hasattr(content_img, "save")
412
- assert hasattr(style_img, "save")
413
- content_im_name = "{}.png".format(uuid1())
414
- style_im_name = "{}.png".format(uuid1())
415
- content_img.save(content_im_name)
416
- style_img.save(style_im_name)
417
- out = style_transfer_client.predict(
418
- content_im_name,
419
- style_im_name,
420
- "none",
421
- fn_index=1
422
- )
423
- os.remove(content_im_name)
424
- os.remove(style_im_name)
425
- return Image.open(out)
426
- '''
427
  def style_transfer_func(content_img, style_img):
428
  assert hasattr(content_img, "save")
429
  assert hasattr(style_img, "save")
@@ -469,10 +381,7 @@ def gen_images_from_event_fact(current_model, event_fact = DEFAULT_PROMPT, role_
469
 
470
  def image_click(images, evt: gr.SelectData,
471
  ):
472
- #print(images)
473
- #print(evt.index)
474
  img_selected = images[evt.index][0]["name"]
475
- #print(img_selected)
476
  return img_selected
477
 
478
  def get_book_covers():
@@ -496,23 +405,16 @@ with gr.Blocks(css=".caption-label {display:none}") as demo:
496
  with gr.Column(elem_id="prompt-container"):
497
  current_model = gr.Dropdown(label="Current Model", choices=list_models, value="Pixel-Art-XL")
498
  style_reference_input_gallery = gr.Gallery(get_book_covers(),
499
- #width = 512,
500
- height = 512,
501
- label = "StoryBook Cover (click to use)")
 
502
  with gr.Column(elem_id="prompt-container"):
503
- #with gr.Row(elem_id="prompt-container"):
504
  style_reference_input_image = gr.Image(
505
  label = "StoryBook Cover (you can upload yourself or click from left gallery)",
506
- #width = 512,
507
  value = DEFAULT_BOOK_COVER,
508
  interactive = True,
509
  )
510
- '''
511
- super_resolution_type = gr.Radio(choices = ["SD(Standard Definition)" ,"HD(High Definition)"],
512
- value="SD(Standard Definition)", label="Story Video Quality",
513
- interactive = True)
514
- '''
515
-
516
  with gr.Row():
517
  text_prompt = gr.Textbox(label="Event Prompt", placeholder=DEFAULT_PROMPT,
518
  lines=1, elem_id="prompt-text-input", value = DEFAULT_PROMPT,
@@ -522,14 +424,12 @@ with gr.Blocks(css=".caption-label {display:none}") as demo:
522
  elem_id="prompt-text-input", value = DEFAULT_ROLE,
523
  info = "You should set the Role (X) with some famous man (like: Confucius Superman)"
524
  )
 
525
  text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")
526
 
527
  with gr.Row():
528
- #image_output = gr.Image(type="pil", label="Output Image", elem_id="gallery")
529
- #image_output = gr.Gallery(label="Output Images", elem_id="gallery")
530
- video_output = gr.Video(label = "Story Video", elem_id="gallery")
531
 
532
- #text_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)
533
  style_reference_input_gallery.select(
534
  image_click, style_reference_input_gallery, style_reference_input_image
535
  )
@@ -537,7 +437,5 @@ with gr.Blocks(css=".caption-label {display:none}") as demo:
537
  text_button.click(gen_images_from_event_fact, inputs=[current_model, text_prompt, role_name, style_reference_input_image],
538
  outputs=video_output)
539
 
540
- #select_button.click(generate_txt2img, inputs=[current_model, select_prompt, negative_prompt, image_style], outputs=image_output)
541
- #demo.load(get_params, None, select_prompt)
542
 
543
  demo.launch(show_api=False)
 
 
 
 
 
1
  import os
2
  import tensorflow as tf
3
  os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
 
6
  import gradio as gr
7
  import tensorflow_hub as hub
8
  import matplotlib.pyplot as plt
 
9
 
10
  import gradio as gr
11
  import requests
 
29
 
30
  API_TOKEN = os.environ.get("HF_READ_TOKEN")
31
 
 
 
 
 
 
 
 
 
32
  DEFAULT_PROMPT = "X go to Istanbul"
33
  DEFAULT_ROLE = "Superman"
34
  DEFAULT_BOOK_COVER = "book_cover_dir/Blank.jpg"
 
47
  def perform_neural_transfer(content_image_input, style_image_input, hub_module = hub_module):
48
  content_image = content_image_input.astype(np.float32)[np.newaxis, ...] / 255.
49
  content_image = tf.image.resize(content_image, (400, 600))
 
 
 
50
  style_image = style_image_input.astype(np.float32)[np.newaxis, ...] / 255.
 
51
  style_image = tf.image.resize(style_image, (256, 256))
 
52
  outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
53
  stylized_image = outputs[0]
 
54
  stylized_image = tensor_to_image(stylized_image)
55
  content_image_input = tensor_to_image(content_image_input)
56
  stylized_image = stylized_image.resize(content_image_input.size)
57
  return stylized_image
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  list_models = [
 
60
  "Pixel-Art-XL",
61
  "SD-1.5",
62
  "OpenJourney-V4",
63
  "Anything-V4",
64
  "Disney-Pixar-Cartoon",
65
  "Dalle-3-XL",
 
66
  ]
67
 
 
68
 
69
  def generate_txt2img(current_model, prompt, is_negative=False, image_style="None style", steps=50, cfg_scale=7,
70
  seed=None, API_TOKEN = API_TOKEN):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  if current_model == "SD-1.5":
72
  API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
73
  elif current_model == "OpenJourney-V4":
 
271
  marg_size = int(marg_ratio * max_image_size)
272
  colors, pixel_count = extcolors.extract_from_image(input_image)
273
  input_image = add_margin(input_image, marg_size, 0, 0, marg_size, colors[0][0])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  font = ImageFont.truetype("DejaVuSerif-Italic.ttf" ,int(marg_size / 4))
275
  caption_token_list = list(map(lambda x: x.strip() ,caption.split(" ")))
276
  caption_list = list(map(" ".join ,batch_as_list(caption_token_list, row_token_num)))
 
282
  (line_num + 1) * 1.1
283
  )))
284
  draw.text(position, line_caption, fill="black", font = font)
 
285
  return input_image
286
 
287
 
 
336
  generate_video(image_list, video_name = video_name)
337
  return video_name
338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339
  def style_transfer_func(content_img, style_img):
340
  assert hasattr(content_img, "save")
341
  assert hasattr(style_img, "save")
 
381
 
382
  def image_click(images, evt: gr.SelectData,
383
  ):
 
 
384
  img_selected = images[evt.index][0]["name"]
 
385
  return img_selected
386
 
387
  def get_book_covers():
 
405
  with gr.Column(elem_id="prompt-container"):
406
  current_model = gr.Dropdown(label="Current Model", choices=list_models, value="Pixel-Art-XL")
407
  style_reference_input_gallery = gr.Gallery(get_book_covers(),
408
+ height = 768 + 64 + 32,
409
+ label = "StoryBook Cover (click to use)",
410
+ object_fit = "contain"
411
+ )
412
  with gr.Column(elem_id="prompt-container"):
 
413
  style_reference_input_image = gr.Image(
414
  label = "StoryBook Cover (you can upload yourself or click from left gallery)",
 
415
  value = DEFAULT_BOOK_COVER,
416
  interactive = True,
417
  )
 
 
 
 
 
 
418
  with gr.Row():
419
  text_prompt = gr.Textbox(label="Event Prompt", placeholder=DEFAULT_PROMPT,
420
  lines=1, elem_id="prompt-text-input", value = DEFAULT_PROMPT,
 
424
  elem_id="prompt-text-input", value = DEFAULT_ROLE,
425
  info = "You should set the Role (X) with some famous man (like: Confucius Superman)"
426
  )
427
+ with gr.Row():
428
  text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")
429
 
430
  with gr.Row():
431
+ video_output = gr.Video(label = "Story Video", elem_id="gallery", height = 512,)
 
 
432
 
 
433
  style_reference_input_gallery.select(
434
  image_click, style_reference_input_gallery, style_reference_input_image
435
  )
 
437
  text_button.click(gen_images_from_event_fact, inputs=[current_model, text_prompt, role_name, style_reference_input_image],
438
  outputs=video_output)
439
 
 
 
440
 
441
  demo.launch(show_api=False)