pcuenq HF staff commited on
Commit
d4d6eb8
β€’
1 Parent(s): c868b88

Use LoRA fine-tuned model.

Browse files
Files changed (1) hide show
  1. app.py +269 -0
app.py ADDED
@@ -0,0 +1,269 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+
4
+ from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
5
+ from huggingface_hub.repocard import RepoCard
6
+
7
+ model_path = "sayakpaul/sd-model-finetuned-lora-t4"
8
+ # model_path = "pcuenq/pokemon-lora"
9
+
10
+ card = RepoCard.load(model_path)
11
+ model_base = card.data.to_dict()["base_model"]
12
+ print(model_base)
13
+
14
+ pipe = StableDiffusionPipeline.from_pretrained(model_base, torch_dtype=torch.float16)
15
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
16
+ pipe.unet.load_attn_procs(model_path)
17
+ pipe.to("cuda")
18
+
19
+ def infer(prompt, negative, scale):
20
+ images = pipe(
21
+ prompt, negative_prompt=negative, guidance_scale=scale,
22
+ num_images_per_prompt=4, num_inference_steps=25
23
+ ).images
24
+ return images
25
+
26
+
27
+ css = """
28
+ .gradio-container {
29
+ font-family: 'IBM Plex Sans', sans-serif;
30
+ }
31
+ .gr-button {
32
+ color: white;
33
+ border-color: black;
34
+ background: black;
35
+ }
36
+ input[type='range'] {
37
+ accent-color: black;
38
+ }
39
+ .dark input[type='range'] {
40
+ accent-color: #dfdfdf;
41
+ }
42
+ .container {
43
+ max-width: 730px;
44
+ margin: auto;
45
+ padding-top: 1.5rem;
46
+ }
47
+ #gallery {
48
+ min-height: 22rem;
49
+ margin-bottom: 15px;
50
+ margin-left: auto;
51
+ margin-right: auto;
52
+ border-bottom-right-radius: .5rem !important;
53
+ border-bottom-left-radius: .5rem !important;
54
+ }
55
+ #gallery>div>.h-full {
56
+ min-height: 20rem;
57
+ }
58
+ .details:hover {
59
+ text-decoration: underline;
60
+ }
61
+ .gr-button {
62
+ white-space: nowrap;
63
+ }
64
+ .gr-button:focus {
65
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
66
+ outline: none;
67
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
68
+ --tw-border-opacity: 1;
69
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
70
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
71
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
72
+ --tw-ring-opacity: .5;
73
+ }
74
+ #advanced-btn {
75
+ font-size: .7rem !important;
76
+ line-height: 19px;
77
+ margin-top: 12px;
78
+ margin-bottom: 12px;
79
+ padding: 2px 8px;
80
+ border-radius: 14px !important;
81
+ }
82
+ #advanced-options {
83
+ display: none;
84
+ margin-bottom: 20px;
85
+ }
86
+ .footer {
87
+ margin-bottom: 45px;
88
+ margin-top: 35px;
89
+ text-align: center;
90
+ border-bottom: 1px solid #e5e5e5;
91
+ }
92
+ .footer>p {
93
+ font-size: .8rem;
94
+ display: inline-block;
95
+ padding: 0 10px;
96
+ transform: translateY(10px);
97
+ background: white;
98
+ }
99
+ .dark .footer {
100
+ border-color: #303030;
101
+ }
102
+ .dark .footer>p {
103
+ background: #0b0f19;
104
+ }
105
+ .acknowledgments h4{
106
+ margin: 1.25em 0 .25em 0;
107
+ font-weight: bold;
108
+ font-size: 115%;
109
+ }
110
+ .animate-spin {
111
+ animation: spin 1s linear infinite;
112
+ }
113
+ @keyframes spin {
114
+ from {
115
+ transform: rotate(0deg);
116
+ }
117
+ to {
118
+ transform: rotate(360deg);
119
+ }
120
+ }
121
+ #share-btn-container {
122
+ display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
123
+ margin-top: 10px;
124
+ margin-left: auto;
125
+ }
126
+ #share-btn {
127
+ all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;right:0;
128
+ }
129
+ #share-btn * {
130
+ all: unset;
131
+ }
132
+ #share-btn-container div:nth-child(-n+2){
133
+ width: auto !important;
134
+ min-height: 0px !important;
135
+ }
136
+ #share-btn-container .wrap {
137
+ display: none !important;
138
+ }
139
+
140
+ .gr-form{
141
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
142
+ }
143
+ #prompt-container{
144
+ gap: 0;
145
+ }
146
+ #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
147
+ #component-16{border-top-width: 1px!important;margin-top: 1em}
148
+ .image_duplication{position: absolute; width: 100px; left: 50px}
149
+ """
150
+
151
+ block = gr.Blocks(css=css)
152
+
153
+ with block:
154
+ gr.HTML(
155
+ """
156
+ <div style="text-align: center; margin: 0 auto;">
157
+ <div
158
+ style="
159
+ display: inline-flex;
160
+ align-items: center;
161
+ gap: 0.8rem;
162
+ font-size: 1.75rem;
163
+ "
164
+ >
165
+ <svg
166
+ width="0.65em"
167
+ height="0.65em"
168
+ viewBox="0 0 115 115"
169
+ fill="none"
170
+ xmlns="http://www.w3.org/2000/svg"
171
+ >
172
+ <rect width="23" height="23" fill="white"></rect>
173
+ <rect y="69" width="23" height="23" fill="white"></rect>
174
+ <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
175
+ <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
176
+ <rect x="46" width="23" height="23" fill="white"></rect>
177
+ <rect x="46" y="69" width="23" height="23" fill="white"></rect>
178
+ <rect x="69" width="23" height="23" fill="black"></rect>
179
+ <rect x="69" y="69" width="23" height="23" fill="black"></rect>
180
+ <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
181
+ <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
182
+ <rect x="115" y="46" width="23" height="23" fill="white"></rect>
183
+ <rect x="115" y="115" width="23" height="23" fill="white"></rect>
184
+ <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
185
+ <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
186
+ <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
187
+ <rect x="92" y="69" width="23" height="23" fill="white"></rect>
188
+ <rect x="69" y="46" width="23" height="23" fill="white"></rect>
189
+ <rect x="69" y="115" width="23" height="23" fill="white"></rect>
190
+ <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
191
+ <rect x="46" y="46" width="23" height="23" fill="black"></rect>
192
+ <rect x="46" y="115" width="23" height="23" fill="black"></rect>
193
+ <rect x="46" y="69" width="23" height="23" fill="black"></rect>
194
+ <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
195
+ <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
196
+ <rect x="23" y="69" width="23" height="23" fill="black"></rect>
197
+ </svg>
198
+ <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
199
+ LoRA fine-tuning of Stable Diffusion
200
+ </h1>
201
+ </div>
202
+ <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
203
+ This demo showcases a Stable Diffusion model fine-tuned on <a href="https://huggingface.co/datasets/lambdalabs/pokemon-blip-captions" style="text-decoration: underline;" target="_blank">Lambda Labs PokΓ©mon Dataset</a>, using a fast and efficient technique known as LoRA. The <a href="https://huggingface.co/sayakpaul/sd-model-finetuned-lora-t4" style="text-decoration: underline;" target="_blank">training result</a> is just a small (~3 MB) file that can be easily shared and downloaded. Feel free to check the source code to see how to use it!
204
+ </p>
205
+ </div>
206
+ """
207
+ )
208
+ with gr.Group():
209
+ with gr.Box():
210
+ with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
211
+ with gr.Column():
212
+ text = gr.Textbox(
213
+ label="Enter your prompt",
214
+ show_label=False,
215
+ max_lines=1,
216
+ placeholder="Enter your prompt",
217
+ elem_id="prompt-text-input",
218
+ ).style(
219
+ border=(True, False, True, True),
220
+ rounded=(True, False, False, True),
221
+ container=False,
222
+ )
223
+ negative = gr.Textbox(
224
+ label="Enter your negative prompt",
225
+ show_label=False,
226
+ max_lines=1,
227
+ placeholder="Enter a negative prompt",
228
+ elem_id="negative-prompt-text-input",
229
+ ).style(
230
+ border=(True, False, True, True),
231
+ rounded=(True, False, False, True),
232
+ container=False,
233
+ )
234
+ btn = gr.Button("Generate image").style(
235
+ margin=False,
236
+ rounded=(False, True, True, False),
237
+ full_width=False,
238
+ )
239
+
240
+ # output_image = gr.Image(label=f"Generated Image", show_label=False, type="pil", interactive=False)
241
+ gallery = gr.Gallery(
242
+ label="Generated images", show_label=False, type="pil", elem_id="gallery"
243
+ ).style(grid=[2], height="auto")
244
+
245
+ with gr.Accordion("Advanced settings", open=False):
246
+ guidance_scale = gr.Slider(
247
+ label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
248
+ )
249
+
250
+ negative.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery])
251
+ text.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery])
252
+ btn.click(infer, inputs=[text, negative, guidance_scale], outputs=[gallery])
253
+
254
+ gr.HTML(
255
+ """
256
+ <div class="footer">
257
+ <p>Original model by <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">StabilityAI</a> – Fine-tuned by <a href="https://huggingface.co/sayakpaul/sd-model-finetuned-lora-t4" style="text-decoration: underline;" target="_blank">sayakpaul</a> on a T4 GPU – Gradio Demo by πŸ€— <a href="https://huggingface.co/" style="text-decoration: underline;" target="_blank">Hugging Face</a>
258
+ </p>
259
+ </div>
260
+ <div class="acknowledgments">
261
+ <p><h4>LICENSE</h4>
262
+ The model is licensed with a <a href="https://huggingface.co/stabilityai/stable-diffusion-2/blob/main/LICENSE-MODEL" style="text-decoration: underline;" target="_blank">CreativeML OpenRAIL++</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
263
+ <p><h4>Biases and content acknowledgment</h4>
264
+ Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
265
+ </div>
266
+ """
267
+ )
268
+
269
+ block.queue(concurrency_count=1).launch()