"""
Adapted from https://huggingface.co/spaces/stabilityai/stable-diffusion
"""
from tensorflow import keras
keras.mixed_precision.set_global_policy("mixed_float16")
import time
import gradio as gr
import keras_cv
from constants import css, examples, img_height, img_width, num_images_to_gen
from share_btn import community_icon_html, loading_icon_html, share_js
# Load model.
weights_path = keras.utils.get_file(
origin="https://storage.googleapis.com/sd-weights-kerascv/woga/ckpt_epochs_72_res_512_mp_True.h5"
)
pokemon_model = keras_cv.models.StableDiffusion(
img_width=img_width, img_height=img_height
)
pokemon_model.diffusion_model.load_weights(weights_path)
pokemon_model.diffusion_model.compile(jit_compile=True)
pokemon_model.decoder.compile(jit_compile=True)
pokemon_model.text_encoder.compile(jit_compile=True)
# Warm-up the model.
_ = pokemon_model.text_to_image(
"Teddy bear",
batch_size=num_images_to_gen,
)
def generate_image_fn(prompt: str, unconditional_guidance_scale: int) -> list:
start_time = time.time()
images = pokemon_model.text_to_image(
prompt,
batch_size=num_images_to_gen,
unconditional_guidance_scale=unconditional_guidance_scale,
)
end_time = time.time()
print(f"Time taken: {end_time - start_time} seconds.")
images = [image.tolist() for image in images]
return images
block = gr.Blocks(css=css)
with block:
gr.HTML(
"""
"""
)
with gr.Group():
with gr.Box():
with gr.Row(elem_id="prompt-container").style(
mobile_collapse=False, equal_height=True
):
with gr.Column():
text = gr.Textbox(
label="Enter your prompt",
show_label=False,
max_lines=1,
placeholder="Enter your prompt",
elem_id="prompt-text-input",
).style(
border=(True, False, True, True),
rounded=(True, False, False, True),
container=False,
)
guidance_scale = gr.Slider(
label="Guidance Scale", minimum=10, maximum=50, value=40, step=1.0
)
btn = gr.Button("Generate image").style(
margin=False,
rounded=(False, True, True, False),
full_width=False,
)
gallery = gr.Gallery(
label="Generated images", show_label=False, elem_id="gallery"
).style(grid=[2], height="auto")
with gr.Group(elem_id="container-advanced-btns"):
with gr.Group(elem_id="share-btn-container"):
community_icon = gr.HTML(community_icon_html)
loading_icon = gr.HTML(loading_icon_html)
share_button = gr.Button("Share to community", elem_id="share-btn")
ex = gr.Examples(
examples=examples,
fn=generate_image_fn,
inputs=[text, guidance_scale],
outputs=[gallery, community_icon, loading_icon, share_button],
cache_examples=False,
)
ex.dataset.headers = [""]
btn.click(
generate_image_fn,
inputs=[text, guidance_scale],
outputs=[gallery],
postprocess=False,
)
share_button.click(
None,
[],
[],
_js=share_js,
)
gr.HTML(
"""
LICENSE
The original model is licensed with a
CreativeML OpenRAIL M license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please
read the license.
Biases and content acknowledgment
Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The oiginal model is meant for research purposes. You can read more in the
model card.
"""
)
block.queue(concurrency_count=80, max_size=100).launch(max_threads=150)