eolecvk commited on
Commit
6aea3f3
·
1 Parent(s): 4997f36

gradio app

Browse files
Files changed (1) hide show
  1. app.py +196 -0
app.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from contextlib import nullcontext
2
+ import gradio as gr
3
+ import torch
4
+ from torch import autocast
5
+ from diffusers import StableDiffusionPipeline
6
+
7
+
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ context = autocast if device == "cuda" else nullcontext
10
+ dtype = torch.float16 if device == "cuda" else torch.float32
11
+
12
+ model_id = 'eolecvk/dreambooth-avatar'
13
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=dtype)
14
+ pipe = pipe.to(device)
15
+
16
+
17
+
18
+ def infer(prompt, n_samples, steps, scale):
19
+
20
+ with context("cuda"):
21
+ images = pipe(n_samples*[prompt], guidance_scale=scale, num_inference_steps=steps).images
22
+
23
+ return images
24
+
25
+ css = """
26
+ a {
27
+ color: inherit;
28
+ text-decoration: underline;
29
+ }
30
+ .gradio-container {
31
+ font-family: 'IBM Plex Sans', sans-serif;
32
+ }
33
+ .gr-button {
34
+ color: white;
35
+ border-color: #9d66e5;
36
+ background: #9d66e5;
37
+ }
38
+ input[type='range'] {
39
+ accent-color: #9d66e5;
40
+ }
41
+ .dark input[type='range'] {
42
+ accent-color: #dfdfdf;
43
+ }
44
+ .container {
45
+ max-width: 730px;
46
+ margin: auto;
47
+ padding-top: 1.5rem;
48
+ }
49
+ #gallery {
50
+ min-height: 22rem;
51
+ margin-bottom: 15px;
52
+ margin-left: auto;
53
+ margin-right: auto;
54
+ border-bottom-right-radius: .5rem !important;
55
+ border-bottom-left-radius: .5rem !important;
56
+ }
57
+ #gallery>div>.h-full {
58
+ min-height: 20rem;
59
+ }
60
+ .details:hover {
61
+ text-decoration: underline;
62
+ }
63
+ .gr-button {
64
+ white-space: nowrap;
65
+ }
66
+ .gr-button:focus {
67
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
68
+ outline: none;
69
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
70
+ --tw-border-opacity: 1;
71
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
72
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
73
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
74
+ --tw-ring-opacity: .5;
75
+ }
76
+ #advanced-options {
77
+ margin-bottom: 20px;
78
+ }
79
+ .footer {
80
+ margin-bottom: 45px;
81
+ margin-top: 35px;
82
+ text-align: center;
83
+ border-bottom: 1px solid #e5e5e5;
84
+ }
85
+ .footer>p {
86
+ font-size: .8rem;
87
+ display: inline-block;
88
+ padding: 0 10px;
89
+ transform: translateY(10px);
90
+ background: white;
91
+ }
92
+ .dark .logo{ filter: invert(1); }
93
+ .dark .footer {
94
+ border-color: #303030;
95
+ }
96
+ .dark .footer>p {
97
+ background: #0b0f19;
98
+ }
99
+ .acknowledgments h4{
100
+ margin: 1.25em 0 .25em 0;
101
+ font-weight: bold;
102
+ font-size: 115%;
103
+ }
104
+ """
105
+
106
+ block = gr.Blocks(css=css)
107
+
108
+ examples = [
109
+ [
110
+ 'Yoda',
111
+ 2,
112
+ 7.5,
113
+ ],
114
+ [
115
+ 'Abraham Lincoln',
116
+ 2,
117
+ 7.5,
118
+ ],
119
+ [
120
+ 'George Washington',
121
+ 2,
122
+ 7,
123
+ ],
124
+ ]
125
+
126
+ with block:
127
+ gr.HTML(
128
+ """
129
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
130
+ <div>
131
+ <img class="logo" src="https://lambdalabs.com/static/images/lambda-logo.svg" alt="Lambda Logo"
132
+ style="margin: auto; max-width: 7rem;">
133
+ <h1 style="font-weight: 900; font-size: 3rem;">
134
+ Naruto text to image
135
+ </h1>
136
+ </div>
137
+ <p style="margin-bottom: 10px; font-size: 94%">
138
+ Generate new Naruto anime character from a text description,
139
+ <a href="https://lambdalabs.com/blog/how-to-fine-tune-stable-diffusion-how-we-made-the-text-to-pokemon-model-at-lambda/">created by Lambda Labs</a>.
140
+ </p>
141
+ </div>
142
+ """
143
+ )
144
+ with gr.Group():
145
+ with gr.Box():
146
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
147
+ text = gr.Textbox(
148
+ label="Enter your prompt",
149
+ show_label=False,
150
+ max_lines=1,
151
+ placeholder="Enter your prompt",
152
+ ).style(
153
+ border=(True, False, True, True),
154
+ rounded=(True, False, False, True),
155
+ container=False,
156
+ )
157
+ btn = gr.Button("Generate image").style(
158
+ margin=False,
159
+ rounded=(False, True, True, False),
160
+ )
161
+
162
+ gallery = gr.Gallery(
163
+ label="Generated images", show_label=False, elem_id="gallery"
164
+ ).style(grid=[2], height="auto")
165
+
166
+
167
+ with gr.Row(elem_id="advanced-options"):
168
+ samples = gr.Slider(label="Images", minimum=1, maximum=4, value=2, step=1)
169
+ steps = gr.Slider(label="Steps", minimum=5, maximum=50, value=50, step=5)
170
+ scale = gr.Slider(
171
+ label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
172
+ )
173
+
174
+
175
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[text, samples, scale], outputs=gallery, cache_examples=False)
176
+ ex.dataset.headers = [""]
177
+
178
+
179
+ text.submit(infer, inputs=[text, samples, steps, scale], outputs=gallery)
180
+ btn.click(infer, inputs=[text, samples, steps, scale], outputs=gallery)
181
+ gr.HTML(
182
+ """
183
+ <div class="footer">
184
+ <p> Gradio Demo by 🤗 Hugging Face and Lambda Labs
185
+ </p>
186
+ </div>
187
+ <div class="acknowledgments">
188
+ <p> Put in a text prompt and generate your own Naruto anime character, no "prompt engineering" required!
189
+ <p>If you want to find out how we made this model read about it in <a href="https://lambdalabs.com/blog/how-to-fine-tune-stable-diffusion-how-we-made-the-text-to-pokemon-model-at-lambda/">this blog post</a>.
190
+ <p>And if you want to train your own Stable Diffusion variants, see our <a href="https://github.com/LambdaLabsML/examples/tree/main/stable-diffusion-finetuning">Examples Repo</a>!
191
+ <p>Trained by Eole Cervenka at <a href="https://lambdalabs.com/">Lambda Labs</a>.</p>
192
+ </div>
193
+ """
194
+ )
195
+
196
+ block.launch()