sayakpaul's picture
sayakpaul HF staff
add: initial files.
9cc0317
raw
history blame
9.23 kB
"""
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(
"""
<div style="text-align: center; margin: 0 auto;">
<div
style="
display: inline-flex;
align-items: center;
gap: 0.8rem;
font-size: 1.75rem;
"
>
<svg
width="0.65em"
height="0.65em"
viewBox="0 0 115 115"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<rect width="23" height="23" fill="white"></rect>
<rect y="69" width="23" height="23" fill="white"></rect>
<rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
<rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
<rect x="46" width="23" height="23" fill="white"></rect>
<rect x="46" y="69" width="23" height="23" fill="white"></rect>
<rect x="69" width="23" height="23" fill="black"></rect>
<rect x="69" y="69" width="23" height="23" fill="black"></rect>
<rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
<rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
<rect x="115" y="46" width="23" height="23" fill="white"></rect>
<rect x="115" y="115" width="23" height="23" fill="white"></rect>
<rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
<rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
<rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
<rect x="92" y="69" width="23" height="23" fill="white"></rect>
<rect x="69" y="46" width="23" height="23" fill="white"></rect>
<rect x="69" y="115" width="23" height="23" fill="white"></rect>
<rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
<rect x="46" y="46" width="23" height="23" fill="black"></rect>
<rect x="46" y="115" width="23" height="23" fill="black"></rect>
<rect x="46" y="69" width="23" height="23" fill="black"></rect>
<rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
<rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
<rect x="23" y="69" width="23" height="23" fill="black"></rect>
</svg>
<h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
Stable Diffusion (v1-4) fine-tuned on the Pokemon dataset
</h1>
</div>
<p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
This Space demonstrates a fine-tuned version of the <a style="text-decoration: underline;" href="https://github.com/keras-team/keras-cv/tree/master/keras_cv/models/stable_diffusion">Stable Diffusion (v1) model shipped by KerasCV</a>. For details on the fine-tuning procedure, check out the <a style="text-decoration: underline;" href="https://github.com/sayakpaul/stable-diffusion-keras-ft/">corresponding repository</a>. You can use the Space to generate custom Pokemon-like characters. To get started either enter a text prompt or try one from the ones given below. This Space is adapted from the <a style="text-decoration: underline;" href="https://huggingface.co/spaces/stabilityai/stable-diffusion">original
Stable Diffusion 2.1 Space</a>.
</p>
</div>
"""
)
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(
"""
<div class="footer">
<p>For details on the fine-tuning procedure, check out the <a style="text-decoration: underline;" href="https://github.com/sayakpaul/stable-diffusion-keras-ft/">corresponding repository</a>. This Space is adapted from the <a style="text-decoration: underline;" href="https://huggingface.co/spaces/stabilityai/stable-diffusion">original Stable Diffusion 2.1 Space</a>.</p>
</div>
<div class="acknowledgments">
<p><h4>LICENSE</h4>
The original model is licensed with a <a href="https://raw.githubusercontent.com/CompVis/stable-diffusion/main/LICENSE" style="text-decoration: underline;" target="_blank">CreativeML OpenRAIL M</a> 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 <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a>.</p>
<p><h4>Biases and content acknowledgment</h4>
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 <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a>.</p>
</div>
"""
)
block.queue(concurrency_count=80, max_size=100).launch(max_threads=150)