gstaff commited on
Commit
ebf6ce9
1 Parent(s): 411cb25

Add output examples, use EulerA sampler and reduce inference steps to 20 to reduce CPU runtime from 20 minutes.

Browse files
Files changed (4) hide show
  1. app.py +10 -6
  2. examples/card.png +0 -0
  3. examples/image.png +0 -0
  4. examples/text.txt +10 -0
app.py CHANGED
@@ -21,7 +21,7 @@ import random
21
  from transformers import GPT2TokenizerFast
22
 
23
  import torch
24
- from diffusers import DiffusionPipeline
25
 
26
  gpu = False
27
 
@@ -30,10 +30,14 @@ AUTH_TOKEN = os.environ.get('AUTH_TOKEN')
30
 
31
  if gpu:
32
  pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", custom_pipeline="stable_diffusion_mega", torch_dtype=torch.float16, revision="fp16", use_auth_token=AUTH_TOKEN)
 
 
33
  pipeline.to("cuda")
34
  else:
35
  pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5",
36
  custom_pipeline="stable_diffusion_mega", use_auth_token=AUTH_TOKEN)
 
 
37
 
38
  # Huggingface Spaces have 16GB RAM and 8 CPU cores
39
  # See https://huggingface.co/docs/hub/spaces-overview#hardware-resources
@@ -105,7 +109,7 @@ def run(name):
105
  prompt_template = f"fantasy illustration of a {card_type} {name}, by Greg Rutkowski"
106
  print(f"GENERATING IMAGE FOR {prompt_template}")
107
  # Regarding sizing see https://huggingface.co/blog/stable_diffusion#:~:text=When%20choosing%20image%20sizes%2C%20we%20advise%20the%20following%3A
108
- images = pipeline.text2img(prompt_template, width=512, height=368).images
109
  card_image = None
110
  for image in images:
111
  save_name = get_savename('card_images', name, 'png')
@@ -258,12 +262,12 @@ def html_to_png(card_name, html):
258
  app_description = (
259
  """
260
  # Create your own Magic: The Gathering cards!
261
- Enter a name, click Submit, and wait for about 20 seconds to see the result.
262
  """).strip()
263
  input_box = gr.Textbox(label="Enter a card name", placeholder="Firebolt")
264
- rendered_card = gr.Image(label="Card", type='pil')
265
- output_text_box = gr.Textbox(label="Card Text")
266
- output_card_image = gr.Image(label="Card Image", type='pil')
267
  output_card_html = gr.HTML(label="Card HTML", visible=False, show_label=False)
268
  x = gr.components.Textbox()
269
  iface = gr.Interface(title="MagicGen", theme="default", description=app_description, fn=run, inputs=[input_box],
 
21
  from transformers import GPT2TokenizerFast
22
 
23
  import torch
24
+ from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
25
 
26
  gpu = False
27
 
 
30
 
31
  if gpu:
32
  pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", custom_pipeline="stable_diffusion_mega", torch_dtype=torch.float16, revision="fp16", use_auth_token=AUTH_TOKEN)
33
+ scheduler = EulerAncestralDiscreteScheduler.from_config(pipeline.scheduler.config)
34
+ pipeline.scheduler = scheduler
35
  pipeline.to("cuda")
36
  else:
37
  pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5",
38
  custom_pipeline="stable_diffusion_mega", use_auth_token=AUTH_TOKEN)
39
+ scheduler = EulerAncestralDiscreteScheduler.from_config(pipeline.scheduler.config)
40
+ pipeline.scheduler = scheduler
41
 
42
  # Huggingface Spaces have 16GB RAM and 8 CPU cores
43
  # See https://huggingface.co/docs/hub/spaces-overview#hardware-resources
 
109
  prompt_template = f"fantasy illustration of a {card_type} {name}, by Greg Rutkowski"
110
  print(f"GENERATING IMAGE FOR {prompt_template}")
111
  # Regarding sizing see https://huggingface.co/blog/stable_diffusion#:~:text=When%20choosing%20image%20sizes%2C%20we%20advise%20the%20following%3A
112
+ images = pipeline.text2img(prompt_template, width=512, height=368, num_inference_steps=20).images
113
  card_image = None
114
  for image in images:
115
  save_name = get_savename('card_images', name, 'png')
 
262
  app_description = (
263
  """
264
  # Create your own Magic: The Gathering cards!
265
+ Enter a name, click Submit, it may take up to 10 minutes to run on the free CPU only instance.
266
  """).strip()
267
  input_box = gr.Textbox(label="Enter a card name", placeholder="Firebolt")
268
+ rendered_card = gr.Image(label="Card", type='pil', value="examples/card.png")
269
+ output_text_box = gr.Textbox(label="Card Text", value=pathlib.Path("examples/text.txt").read_text('utf-8'))
270
+ output_card_image = gr.Image(label="Card Image", type='pil', value="examples/image.png")
271
  output_card_html = gr.HTML(label="Card HTML", visible=False, show_label=False)
272
  x = gr.components.Textbox()
273
  iface = gr.Interface(title="MagicGen", theme="default", description=app_description, fn=run, inputs=[input_box],
examples/card.png ADDED
examples/image.png ADDED
examples/text.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ Name: Firebolt
2
+ ManaCost: {1}{R}
3
+ Type: Instant
4
+ Rarity: common
5
+ Text: Firebolt deals 2 damage to target creature and you gain 2 life.
6
+ Draw a card at the beginning of the next end step.
7
+ FlavorText:
8
+ Power:
9
+ Toughness:
10
+ Color: ['R']