File size: 3,009 Bytes
befbd95
df1443b
befbd95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b87396
befbd95
 
 
 
 
 
 
 
 
 
 
 
 
4b87396
befbd95
02d6aa6
befbd95
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
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/ignatius")
dreambooth_model._diffusion_model = loaded_diffusion_model

# generate images
def generate_images(prompt, negative_prompt, num_imgs_to_gen, num_steps, guidance_scale):
    """
    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.
        negative_prompt (srt): The text to eliminate from the generation some concepts.
        num_imgs_to_gen (int): The number of images to be generated using given prompt.
        num_steps (int): The number of denoising steps
        guidance_scale (double): Increasing guidance makes generation follow more closely to the prompt.
    Returns:
        generated_img (List): List of images that were generated using the model
    """
    generated_images = dreambooth_model.text_to_image(
        prompt,
        negative_prompt=negative_prompt,
        batch_size=num_imgs_to_gen,
        num_steps=num_steps,
        unconditional_guidance_scale=guidance_scale
    )
    return generated_images 

with gr.Blocks() as demo:
    gr.HTML("<h2 style=\"font-size: 2em; font-weight: bold\" align=\"center\">Ignatius Farray - The cavern of the muffled scream</h2>")    
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(lines=1, value="ignatius in a standup comedy spectacle", label="Base Prompt")
            negative_prompt = gr.Textbox(lines=1, value="bad anatomy, blurry, ugly, 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, maximum=450)
            guidance_scale = gr.Number(label="Guidance scale", value=7.5)
            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([["ignatius on the moon","bad anatomy, blurry, ugly", 2, 150, 15],
                 ["A photo of ignatius person inside a box","bad anatomy, blurry, ugly", 2, 150, 15],
                 ["A closeup portrait of ignatius, highly detailed, high qulity","bad anatomy, blurry, ugly", 2, 150, 15]],
                [prompt, negative_prompt, samples, num_steps, guidance_scale], gallery, generate_images)
    gr.Markdown('\n Demo created by: <a href=\"https://huggingface.co/matallanas/\">Eduardo Matallanas</a>')

demo.launch(debug=True)