cloudwp commited on
Commit
f41e92b
·
1 Parent(s): b248e97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +216 -1
app.py CHANGED
@@ -69,4 +69,219 @@ with capture.capture_output() as cap:
69
  else:
70
  !pip install gradio==3.23 --no-deps -qq
71
  clear_output()
72
- inf('\u2714 Done','success', '50px')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  else:
70
  !pip install gradio==3.23 --no-deps -qq
71
  clear_output()
72
+ inf('\u2714 Done','success', '50px')
73
+
74
+ #@markdown # Requirements
75
+
76
+ print('Installing requirements...')
77
+
78
+ with capture.capture_output() as cap:
79
+ %cd /content/
80
+ !wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/A1111.txt
81
+ !dpkg -i *.deb
82
+ if not os.path.exists('/content/gdrive/'+mainpth+'/sd/stablediffusion'):
83
+ !tar -C /content/gdrive/$mainpth --zstd -xf sd_rep.tar.zst
84
+ !tar -C / --zstd -xf gcolabdeps.tar.zst
85
+ !rm *.deb | rm *.zst | rm *.txt
86
+ %env LD_PRELOAD=libtcmalloc.so
87
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
88
+ os.environ['PYTHONWARNINGS'] = 'ignore'
89
+ if Use_Latest_Working_Commit:
90
+ !pip install gradio==3.16.2 --no-deps -qq
91
+ else:
92
+ !pip install gradio==3.23 --no-deps -qq
93
+
94
+ clear_output()
95
+ inf('\u2714 Done','success', '50px')
96
+
97
+ #@markdown # Model Download/Load
98
+ Model_Version = "1.5" #@param [ "1.5", "v1.5 Inpainting ", "V2.1-512px", "V2.1-768px"]
99
+
100
+ Redownload_the_original_model = False #@param {type:"boolean"}
101
+
102
+ def rmv():
103
+ !wget -q -O /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/models/Stable-diffusion/model.ckpt https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/precompiled/README.md
104
+ !mv /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/models/Stable-diffusion/model.ckpt /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/models/Stable-diffusion/trashfile.f
105
+ time.sleep(2)
106
+ !rm /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/models/Stable-diffusion/trashfile.f
107
+ clear_output()
108
+
109
+ if Redownload_the_original_model:
110
+ with capture.capture_output() as cap:
111
+ rmv()
112
+
113
+ #@markdown Or
114
+ Path_to_MODEL = "" #@param {type:"string"}
115
+ #@markdown - Insert the full path of your trained model or to a folder containing multiple models
116
+
117
+ #@markdown Or
118
+ MODEL_LINK = "" #@param {type:"string"}
119
+ safetensors = False #@param {type:"boolean"}
120
+ Use_temp_storage = False #@param {type:"boolean"}
121
+
122
+
123
+
124
+ def newmdl():
125
+
126
+ if not os.path.exists('/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/model.ckpt'):
127
+ %cd /content/
128
+ clear_output()
129
+ !git init
130
+ !git lfs install --system --skip-repo
131
+ !$fgitclone --branch fp16 "https://huggingface.co/runwayml/stable-diffusion-v1-5"
132
+ if os.path.exists('/content/stable-diffusion-v1-5/unet/diffusion_pytorch_model.bin'):
133
+ !wget -q -O stable-diffusion-v1-5/vae/diffusion_pytorch_model.bin https://huggingface.co/stabilityai/sd-vae-ft-mse/resolve/main/diffusion_pytorch_model.bin
134
+ !wget -q -O convertosd.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosd.py
135
+ !sed -i '201s@.*@ model_path = "/content/stable-diffusion-v1-5"@' /content/convertosd.py
136
+ !sed -i '202s@.*@ checkpoint_path= "/content/gdrive/{mainpth}/sd/stable-diffusion-{blasphemy}/models/Stable-diffusion/model.ckpt"@' /content/convertosd.py
137
+ clear_output()
138
+ !python /content/convertosd.py
139
+ !rm /content/convertosd.py
140
+ if os.path.exists('/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/model.ckpt'):
141
+ model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/model.ckpt'
142
+ clear_output()
143
+ inf('\u2714 Done','success', '50px')
144
+ else:
145
+ inf('\u2718 Something went wrong, try again','danger', "250px")
146
+ else:
147
+ inf('\u2718 Something went wrong','danger', "200px")
148
+
149
+ else:
150
+ model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/model.ckpt'
151
+ clear_output()
152
+ inf('\u2714 Model already exists, check the box "Redownload_the_original_model" to redownload/download the V1.5','primary', '700px')
153
+
154
+ if os.path.exists('/content/.git'):
155
+ !rm -r /content/.git
156
+
157
+ if os.path.exists('/content/stable-diffusion-v1-5'):
158
+ !rm -r /content/stable-diffusion-v1-5
159
+
160
+ return model
161
+
162
+ def V2():
163
+
164
+ if not os.path.exists('/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/model.ckpt'):
165
+ %cd /content/
166
+ clear_output()
167
+ !mkdir "/content/stable-diffusion-V2"
168
+ %cd "/content/stable-diffusion-V2"
169
+ !git init
170
+ !git lfs install --system --skip-repo
171
+ if Model_Version == "V2.1-768px":
172
+ !git remote add -f origin "https://huggingface.co/stabilityai/stable-diffusion-2-1"
173
+ elif Model_Version == "V2.1-512px":
174
+ !git remote add -f origin "https://huggingface.co/stabilityai/stable-diffusion-2-1-base"
175
+ !git config core.sparsecheckout true
176
+ !echo -e "scheduler\ntext_encoder\ntokenizer\nunet\nvae\nfeature_extractor\nmodel_index.json\n!*.safetensors" > .git/info/sparse-checkout
177
+ !git pull origin fp16
178
+ %cd /content
179
+ !wget -O convertosdv2.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosdv2.py
180
+ clear_output()
181
+ !python /content/convertosdv2.py --fp16 /content/stable-diffusion-V2 /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/models/Stable-diffusion/model.ckpt
182
+ !rm /content/convertosdv2.py
183
+ if os.path.exists('/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/model.ckpt'):
184
+ model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/model.ckpt'
185
+ clear_output()
186
+ inf('\u2714 Done','success', '50px')
187
+ else:
188
+ inf('\u2718 Something went wrong, try again','danger', "250px")
189
+
190
+ else:
191
+ model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/model.ckpt'
192
+ clear_output()
193
+ inf('\u2714 Model already exists, check the box "Redownload_the_original_model" to redownload/download the V2','primary', '700px')
194
+
195
+ if os.path.exists('/content/.git'):
196
+ !rm -r /content/.git
197
+ !rm -r /content/convertosdv2.py
198
+ if os.path.exists('/content/stable-diffusion-V2'):
199
+ !rm -r /content/stable-diffusion-V2
200
+
201
+ return model
202
+
203
+ def inpmdl():
204
+
205
+ if not os.path.exists('/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'):
206
+ %cd /content/
207
+ clear_output()
208
+ !git init
209
+ !git lfs install --system --skip-repo
210
+ !$fgitclone --branch fp16 "https://huggingface.co/runwayml/stable-diffusion-inpainting"
211
+ if os.path.exists('/content/stable-diffusion-inpainting'):
212
+ !$fgitclone "https://huggingface.co/stabilityai/sd-vae-ft-mse"
213
+ !rm -r /content/stable-diffusion-inpainting/vae
214
+ !mv /content/sd-vae-ft-mse /content/stable-diffusion-inpainting/vae
215
+ !wget -O convertosd.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosd.py
216
+ !sed -i '201s@.*@ model_path = "/content/stable-diffusion-inpainting"@' /content/convertosd.py
217
+ !sed -i '202s@.*@ checkpoint_path= "/content/gdrive/{mainpth}/sd/stable-diffusion-{blasphemy}/models/Stable-diffusion/sd-v1-5-inpainting.ckpt"@' /content/convertosd.py
218
+ clear_output()
219
+ !python /content/convertosd.py
220
+ !rm /content/convertosd.py
221
+ if os.path.exists('/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'):
222
+ model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'
223
+ clear_output()
224
+ inf('\u2714 Done','success', '50px')
225
+ else:
226
+ inf('\u2718 Something went wrong, try again','danger', "250px")
227
+ else:
228
+ inf('\u2718 Something went wrong','danger', "200px")
229
+
230
+
231
+ else:
232
+ model='/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'
233
+ clear_output()
234
+ inf('\u2714 inpainting model already exists','primary', '250px')
235
+
236
+ if os.path.exists('/content/.git'):
237
+ !rm -r /content/.git
238
+
239
+ if os.path.exists('/content/stable-diffusion-inpainting'):
240
+ !rm -r /content/stable-diffusion-inpainting
241
+
242
+ return model
243
+
244
+ if (Path_to_MODEL !=''):
245
+ if os.path.exists(str(Path_to_MODEL)):
246
+ inf('\u2714 Using the trained model.','success', '200px')
247
+
248
+ else:
249
+ while not os.path.exists(str(Path_to_MODEL)):
250
+ inf('\u2718 Wrong path, use the colab file explorer to copy the path : ','danger', "400px")
251
+ Path_to_MODEL=input()
252
+ if os.path.exists(str(Path_to_MODEL)):
253
+ inf('\u2714 Using the trained model.','success', '200px')
254
+
255
+ model=Path_to_MODEL
256
+
257
+ elif MODEL_LINK != "":
258
+ gdrv='/content/gdrive/'+mainpth+'/sd/stable-diffusion-'+blasphemy+'/models/Stable-diffusion'
259
+ tmp="/content"
260
+ pth=tmp if Use_temp_storage else gdrv
261
+ %cd $pth
262
+ clear_output()
263
+ if not safetensors:
264
+ modelname="model.ckpt"
265
+ else:
266
+ modelname="model.safetensors"
267
+ !gdown --fuzzy -O $modelname "$MODEL_LINK"
268
+ if os.path.exists(f'{pth}/{modelname}') and os.path.getsize(f'{pth}/{modelname}') > 1810671599:
269
+ model=f'{pth}/{modelname}'
270
+ clear_output()
271
+ inf('\u2714 Model downloaded, using the trained model.','success', '350px')
272
+ else:
273
+ if Use_temp_storage:
274
+ !rm $pth/$modelname
275
+ else:
276
+ rmv()
277
+ inf('\u2718 Wrong link, check that the link is valid','danger', "300px")
278
+
279
+
280
+ elif Model_Version=="1.5":
281
+ model=newmdl()
282
+
283
+ elif Model_Version=="V2.1-512px" or Model_Version=="V2.1-768px":
284
+ model=V2()
285
+
286
+ else:
287
+ model=inpmdl()