File size: 2,498 Bytes
efe549b
 
 
 
 
 
 
 
 
 
 
4733b0e
efe549b
 
f5e0820
efe549b
 
 
 
 
 
f5e0820
efe549b
 
 
 
 
 
 
 
f5e0820
efe549b
 
 
 
 
 
 
 
 
 
 
 
f5e0820
efe549b
 
 
 
f5e0820
efe549b
d05f7b0
f5e0820
efe549b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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")

resolution = 512
dreambooth_model = keras_cv.models.StableDiffusion(
        img_width=resolution, img_height=resolution, jit_compile=True,
    )
loaded_diffusion_model = from_pretrained_keras("melanit/dreambooth_voyager_v3")
dreambooth_model._diffusion_model = loaded_diffusion_model

def generate_images(prompt: str, negative_prompt:str, batch_size: int, num_steps: int, guidance_scale: float):
    """
    This function will infer the trained dreambooth (stable diffusion) model
    Args:
        prompt (str): The input text
        batch_size (int): The number of images to be generated
        num_steps (int): The number of denoising steps
        guidance_scale (float): The Guidance Scale
    Returns:
        outputs (List): List of images that were generated using the model
    """
    outputs = dreambooth_model.text_to_image(
        prompt, 
        negative_prompt=negative_prompt,
        batch_size=batch_size,
        num_steps=num_steps,
        unconditional_guidance_scale=guidance_scale
    )
   
    return outputs
    
with gr.Blocks() as demo:
    gr.HTML("<h2 style=\"font-size: 2rem; font-weight: 700; text-align: center;\">Keras Dreambooth - Voyager Demo</h2>")    
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(lines=1, value="a photo of voyager spaceship", label="Prompt")
            negative_prompt = gr.Textbox(lines=1, value="", label="Negative Prompt")
            samples = gr.Slider(minimum=1, maximum=10, value=1, step=1, label="Number of Images")
            num_steps = gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Denoising Steps")
            guidance_scale = gr.Slider(value=7.5, step=0.5, label="Guidance scale")
            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, guidance_scale], outputs=gallery)
    
    gr.Examples([["photo of voyager spaceship in space, high quality, 8k","bad, ugly, malformed, deformed, out of frame, blurry, cropped, noisy", 4, 50, 7.5]],
                [prompt, negative_prompt, samples, num_steps, guidance_scale], gallery, generate_images)
    gr.Markdown('Demo created by [Lily Berkow](https://huggingface.co/melanit/)')

demo.launch()