mwitiderrick's picture
Create app.py
1797d2c
raw
history blame
2.41 kB
from huggingface_hub import from_pretrained_keras
import keras_cv
import gradio as gr
from tensorflow import keras
keras.mixed_precision.set_global_policy("mixed_float16")
# load keras model
resolution = 512
dreambooth_model = keras_cv.models.StableDiffusion(
img_width=resolution, img_height=resolution, jit_compile=True,
)
loaded_diffusion_model = from_pretrained_keras("keras-dreambooth/dreambooth_fantasy")
dreambooth_model._diffusion_model = loaded_diffusion_model
def generate_images(prompt: str, negative_prompt:str, num_imgs_to_gen: int, num_steps: int):
"""
This function is used to generate images using our fine-tuned keras dreambooth stable diffusion model.
Args:
prompt (str): The text input given by the user based on which images will be generated.
num_imgs_to_gen (int): The number of images to be generated using given prompt.
num_steps (int): The number of denoising steps
Returns:
generated_img (List): List of images that were generated using the model
"""
generated_img = dreambooth_model.text_to_image(
prompt,
negative_prompt=negative_prompt,
batch_size=num_imgs_to_gen,
num_steps=num_steps,
)
return generated_img
with gr.Blocks() as demo:
gr.HTML("<h2 style=\"font-size: 2em; font-weight: bold\" align=\"center\">Keras Dreambooth - Dreambooth Fantasy Demo</h2>")
with gr.Row():
with gr.Column():
prompt = gr.Textbox(lines=1, value="sks fantasy_world that is colorful with green scenary", label="Base Prompt")
negative_prompt = gr.Textbox(lines=1, value="deformed", label="Negative Prompt")
samples = gr.Slider(minimum=1, maximum=10, default=1, step=1, label="Number of Image")
num_steps = gr.Slider(label="Inference Steps",value=50)
run = gr.Button(value="Run")
with gr.Column():
gallery = gr.Gallery(label="Outputs").style(grid=(1,2))
run.click(generate_images, inputs=[prompt,negative_prompt, samples, num_steps], outputs=gallery)
gr.Examples([["A phot of sks fantasy_world that is colorful with green scenary","deformed", 1, 50]],
[prompt,negative_prompt, samples,num_steps], gallery, generate_images)
gr.Markdown('\n Demo created by: <a href=\"https://huggingface.co/mwitiderrick/\">Derrick Mwiti</a>')
demo.launch(debug=True)