kadirnar commited on
Commit
8d61545
1 Parent(s): 91d15e0

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +25 -0
  2. requirements.txt +2 -0
  3. utils_app.py +123 -0
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils_app import keras_stable_diffusion_app
2
+ import gradio as gr
3
+
4
+ app = gr.Blocks()
5
+ with app:
6
+ gr.HTML(
7
+ """
8
+ <h1 style='text-align: center'>
9
+ Keras Diffusion WebUI
10
+ </h1>
11
+ """
12
+ )
13
+ gr.Markdown(
14
+ """
15
+ <h4 style='text-align: center'>
16
+ Follow me for more!
17
+ <a href='https://twitter.com/kadirnar_ai' target='_blank'>Twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>Github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>Linkedin</a>
18
+ </h4>
19
+ """
20
+ )
21
+ with gr.Row():
22
+ with gr.Column():
23
+ keras_diffusion_app = keras_stable_diffusion_app()
24
+
25
+ app.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ keras_cv
2
+ tensorflow
utils_app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import from_pretrained_keras
2
+ from keras_cv import models
3
+ from tensorflow import keras
4
+ import tensorflow as tf
5
+ import gradio as gr
6
+
7
+ keras_model_list = [
8
+ "keras-dreambooth/keras_diffusion_lowpoly_world",
9
+
10
+ ]
11
+
12
+ stable_prompt_list = [
13
+ "a photo of lowpoly_world",
14
+ ]
15
+
16
+ stable_negative_prompt_list = [
17
+ "bad, ugly",
18
+ "deformed"
19
+ ]
20
+
21
+ def keras_stable_diffusion(
22
+ model_path:str,
23
+ prompt:str,
24
+ negative_prompt:str,
25
+ guidance_scale:int,
26
+ num_inference_step:int,
27
+ height:int,
28
+ width:int,
29
+ ):
30
+
31
+ with tf.device('/GPU:0'):
32
+ keras.mixed_precision.set_global_policy("mixed_float16")
33
+
34
+ sd_dreambooth_model = models.StableDiffusion(
35
+ img_width=height,
36
+ img_height=width
37
+ )
38
+
39
+ db_diffusion_model = from_pretrained_keras(model_path)
40
+ sd_dreambooth_model._diffusion_model = db_diffusion_model
41
+
42
+ generated_images = sd_dreambooth_model.text_to_image(
43
+ prompt=prompt,
44
+ negative_prompt=negative_prompt,
45
+ num_steps=num_inference_step,
46
+ unconditional_guidance_scale=guidance_scale
47
+ )
48
+
49
+ return generated_images
50
+
51
+ def keras_stable_diffusion_app():
52
+ with gr.Blocks():
53
+ with gr.Row():
54
+ with gr.Column():
55
+ keras_text2image_model_path = gr.Dropdown(
56
+ choices=keras_model_list,
57
+ value=keras_model_list[0],
58
+ label='Text-Image Model Id'
59
+ )
60
+
61
+ keras_text2image_prompt = gr.Textbox(
62
+ lines=1,
63
+ value=stable_prompt_list[0],
64
+ label='Prompt'
65
+ )
66
+
67
+ keras_text2image_negative_prompt = gr.Textbox(
68
+ lines=1,
69
+ value=stable_negative_prompt_list[0],
70
+ label='Negative Prompt'
71
+ )
72
+
73
+ with gr.Accordion("Advanced Options", open=False):
74
+ keras_text2image_guidance_scale = gr.Slider(
75
+ minimum=0.1,
76
+ maximum=15,
77
+ step=0.1,
78
+ value=7.5,
79
+ label='Guidance Scale'
80
+ )
81
+
82
+ keras_text2image_num_inference_step = gr.Slider(
83
+ minimum=1,
84
+ maximum=100,
85
+ step=1,
86
+ value=50,
87
+ label='Num Inference Step'
88
+ )
89
+
90
+ keras_text2image_height = gr.Slider(
91
+ minimum=128,
92
+ maximum=1280,
93
+ step=32,
94
+ value=512,
95
+ label='Image Height'
96
+ )
97
+
98
+ keras_text2image_width = gr.Slider(
99
+ minimum=128,
100
+ maximum=1280,
101
+ step=32,
102
+ value=512,
103
+ label='Image Height'
104
+ )
105
+
106
+ keras_text2image_predict = gr.Button(value='Generator')
107
+
108
+ with gr.Column():
109
+ output_image = gr.Gallery(label='Output')
110
+
111
+ keras_text2image_predict.click(
112
+ fn=keras_stable_diffusion,
113
+ inputs=[
114
+ keras_text2image_model_path,
115
+ keras_text2image_prompt,
116
+ keras_text2image_negative_prompt,
117
+ keras_text2image_guidance_scale,
118
+ keras_text2image_num_inference_step,
119
+ keras_text2image_height,
120
+ keras_text2image_width
121
+ ],
122
+ outputs=output_image
123
+ )