Spaces:
Runtime error
Runtime error
Create gradio app
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import from_pretrained_keras
|
2 |
+
import keras_cv
|
3 |
+
import gradio as gr
|
4 |
+
from tensorflow import keras
|
5 |
+
|
6 |
+
keras.mixed_precision.set_global_policy("mixed_float16")
|
7 |
+
|
8 |
+
resolution = 512
|
9 |
+
dreambooth_model = keras_cv.models.StableDiffusion(
|
10 |
+
img_width=resolution, img_height=resolution, jit_compile=True,
|
11 |
+
)
|
12 |
+
loaded_diffusion_model = from_pretrained_keras("melanit/dreambooth_voyager_v2")
|
13 |
+
dreambooth_model._diffusion_model = loaded_diffusion_model
|
14 |
+
|
15 |
+
def generate_images(prompt: str, negative_prompt:str, batch_size: int, num_steps: int):
|
16 |
+
"""
|
17 |
+
This function will infer the trained dreambooth (stable diffusion) model
|
18 |
+
Args:
|
19 |
+
prompt (str): The input text
|
20 |
+
batch_size (int): The number of images to be generated
|
21 |
+
num_steps (int): The number of denoising steps
|
22 |
+
Returns:
|
23 |
+
outputs (List): List of images that were generated using the model
|
24 |
+
"""
|
25 |
+
outputs = dreambooth_model.text_to_image(
|
26 |
+
prompt,
|
27 |
+
negative_prompt=negative_prompt,
|
28 |
+
batch_size=batch_size,
|
29 |
+
num_steps=num_steps,
|
30 |
+
)
|
31 |
+
|
32 |
+
return outputs
|
33 |
+
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
gr.HTML("<h2 style=\"font-size: 2rem; font-weight: 700; text-align: center;\">Keras Dreambooth - Voyager Demo</h2>")
|
36 |
+
with gr.Row():
|
37 |
+
with gr.Column():
|
38 |
+
prompt = gr.Textbox(lines=1, value="a photo of voyager spaceship", label="Prompt")
|
39 |
+
negative_prompt = gr.Textbox(lines=1, value="", label="Negative Prompt")
|
40 |
+
samples = gr.Slider(minimum=1, maximum=10, value=1, step=1, label="Number of Images")
|
41 |
+
num_steps = gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Denoising Steps")
|
42 |
+
run = gr.Button(value="Run")
|
43 |
+
with gr.Column():
|
44 |
+
gallery = gr.Gallery(label="Outputs").style(grid=(1,2))
|
45 |
+
|
46 |
+
run.click(generate_images, inputs=[prompt,negative_prompt, samples, num_steps], outputs=gallery)
|
47 |
+
|
48 |
+
gr.Examples([["photo of voyager spaceship in space, high quality, blender, 3d, trending on artstation, 8k","bad, ugly, malformed, deformed, out of frame, blurry", 1, 50]],
|
49 |
+
[prompt,negative_prompt, samples,num_steps], gallery, generate_images)
|
50 |
+
gr.Markdown('Demo created by [Lily Berkow](https://huggingface.co/melanit/)')
|
51 |
+
|
52 |
+
demo.launch()
|