amgad59 commited on
Commit
a6382c0
·
1 Parent(s): 8ad908a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tensorflow import keras
2
+
3
+ keras.mixed_precision.set_global_policy("mixed_float16")
4
+
5
+ import time
6
+
7
+ import gradio as gr
8
+ import keras_cv
9
+
10
+ from constants import css, examples, img_height, img_width, num_images_to_gen
11
+ from share_btn import community_icon_html, loading_icon_html, share_js
12
+
13
+ # Load model.
14
+ weights_path = keras.utils.get_file(
15
+ origin="https://huggingface.co/mayve/GP/blob/main/%D9%86%D8%B3%D8%AE%D8%A9%20%D9%85%D9%86%20ckpt_epoch_96.h5",
16
+ file_hash="4b4348297aa9853ff9dc4da7f52dcb240210564400f164e5155e5f4dc1866626"
17
+ )
18
+ pokemon_model = keras_cv.models.StableDiffusion(
19
+ img_width=img_width, img_height=img_height
20
+ )
21
+ pokemon_model.diffusion_model.load_weights(weights_path)
22
+
23
+ pokemon_model.diffusion_model.compile(jit_compile=True)
24
+ pokemon_model.decoder.compile(jit_compile=True)
25
+ pokemon_model.text_encoder.compile(jit_compile=True)
26
+
27
+ # Warm-up the model.
28
+ #_ = pokemon_model.text_to_image("Teddy bear", batch_size=num_images_to_gen)
29
+
30
+
31
+ def generate_image_fn(prompt: str, unconditional_guidance_scale: int) -> list:
32
+ start_time = time.time()
33
+ # `images is an `np.ndarray`. So we convert it to a list of ndarrays.
34
+ # Each ndarray represents a generated image.
35
+ # Reference: https://gradio.app/docs/#gallery
36
+ images = pokemon_model.text_to_image(
37
+ prompt,
38
+ batch_size=num_images_to_gen,
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 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/)."
47
+ 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."
48
+ gr.Interface(
49
+ generate_image_fn,
50
+ inputs=[
51
+ gr.Textbox(
52
+ label="Enter your prompt",
53
+ max_lines=1,
54
+ placeholder="cute Sundar Pichai creature",
55
+ ),
56
+ gr.Slider(value=40, minimum=8, maximum=50, step=1),
57
+ ],
58
+ outputs=[gr.outputs.Image(type="pil"),gr.outputs.Image(type="pil")],
59
+ title="Generate custom pokemons",
60
+ description=description,
61
+ article=article,
62
+ examples=[["cute Sundar Pichai creature", 40], ["Hello kitty", 40]],
63
+ allow_flagging=False,
64
+ ).launch()