amgad59 commited on
Commit
2189fb9
1 Parent(s): 957106d

Create app.py

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