clint-greene commited on
Commit
f52a776
·
1 Parent(s): 08fa0c4

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. .ipynb_checkpoints/app-checkpoint.py +64 -0
  2. app.py +1 -1
.ipynb_checkpoints/app-checkpoint.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Adapted from https://huggingface.co/spaces/stabilityai/stable-diffusion
3
+ """
4
+
5
+ import time
6
+ import keras_cv
7
+ import gradio as gr
8
+ from tensorflow import keras
9
+ from share_btn import community_icon_html, loading_icon_html, share_js
10
+ from constants import css, img_height, img_width, num_images_to_gen
11
+ keras.mixed_precision.set_global_policy("mixed_float16")
12
+
13
+ # Load model
14
+ weights_path = keras.utils.get_file(
15
+ origin="https://huggingface.co/clint-greene/magic-the-gathering-sd/blob/main/magic-the-gathering-sd.h5",
16
+ )
17
+
18
+ magic_model = keras_cv.models.StableDiffusion(
19
+ img_width=img_width, img_height=img_height
20
+ )
21
+
22
+ magic_model.diffusion_model.load_weights(weights_path)
23
+ magic_model.diffusion_model.compile(jit_compile=True)
24
+ magic_model.decoder.compile(jit_compile=True)
25
+ magic_model.text_encoder.compile(jit_compile=True)
26
+
27
+ # Warm-up the model
28
+ _ = magic_model.text_to_image("flying dragons", batch_size=num_images_to_gen)
29
+
30
+ def generate_image_fn(prompt: str, steps: int) -> list:
31
+ start_time = time.time()
32
+ # `images is an `np.ndarray`. So we convert it to a list of ndarrays.
33
+ # Each ndarray represents a generated image.
34
+ # Reference: https://gradio.app/docs/#gallery
35
+ images = magic_model.text_to_image(
36
+ prompt,
37
+ batch_size=num_images_to_gen,
38
+ num_steps=steps,
39
+ unconditional_guidance_scale=unconditional_guidance_scale,
40
+ )
41
+ end_time = time.time()
42
+ print(f"Time taken: {end_time - start_time} seconds.")
43
+ return [image for image in images]
44
+
45
+
46
+ description = "This Space demonstrates a fine-tuned Stable Diffusion model. You can use it for generating custom Magic the Gathering cards. To get started, either enter a prompt or pick one from the examples below. For details on the fine-tuning procedure, refer to [this repository](https://gpuopen.com/)."
47
+ article = "We use mixed-precision and XLA to speed up the inference latency."
48
+ gr.Interface(
49
+ generate_image_fn,
50
+ inputs=[
51
+ gr.Textbox(
52
+ label="Enter your prompt",
53
+ max_lines=1,
54
+ placeholder="Jedi",
55
+ ),
56
+ gr.Slider(value=70, minimum=10, maximum=100, step=1),
57
+ ],
58
+ outputs=gr.Gallery().style(grid=[2], height="auto"),
59
+ title="Generate custom magic the gathering cards",
60
+ description=description,
61
+ article=article,
62
+ examples=[["Yoda", 70], ["Lisa Su", 70]],
63
+ allow_flagging=False,
64
+ ).launch(enable_queue=True)
app.py CHANGED
@@ -7,7 +7,7 @@ import keras_cv
7
  import gradio as gr
8
  from tensorflow import keras
9
  from share_btn import community_icon_html, loading_icon_html, share_js
10
- from constants import css, examples, img_height, img_width, num_images_to_gen
11
  keras.mixed_precision.set_global_policy("mixed_float16")
12
 
13
  # Load model
 
7
  import gradio as gr
8
  from tensorflow import keras
9
  from share_btn import community_icon_html, loading_icon_html, share_js
10
+ from constants import css, img_height, img_width, num_images_to_gen
11
  keras.mixed_precision.set_global_policy("mixed_float16")
12
 
13
  # Load model