2022-08-21 23:57:48 +00:00
|
|
|
#!/usr/bin/env python3
|
2022-08-24 13:22:04 +00:00
|
|
|
# Copyright (c) 2022 Lincoln D. Stein (https://github.com/lstein)
|
2022-08-17 16:00:00 +00:00
|
|
|
# Before running stable-diffusion on an internet-isolated machine,
|
|
|
|
# run this script from one with internet connectivity. The
|
|
|
|
# two machines must share a common .cache directory.
|
2022-08-28 20:14:29 +00:00
|
|
|
from transformers import CLIPTokenizer, CLIPTextModel
|
|
|
|
import clip
|
|
|
|
from transformers import BertTokenizerFast
|
2022-08-22 19:33:27 +00:00
|
|
|
import sys
|
|
|
|
import transformers
|
2022-08-26 05:20:01 +00:00
|
|
|
import os
|
|
|
|
import warnings
|
2022-08-22 19:33:27 +00:00
|
|
|
|
|
|
|
transformers.logging.set_verbosity_error()
|
2022-08-17 16:00:00 +00:00
|
|
|
|
|
|
|
# this will preload the Bert tokenizer fles
|
2022-08-26 07:15:42 +00:00
|
|
|
print('preloading bert tokenizer...')
|
|
|
|
|
|
|
|
tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
|
|
|
|
print('...success')
|
2022-08-17 16:00:00 +00:00
|
|
|
|
|
|
|
# this will download requirements for Kornia
|
2022-08-26 07:15:42 +00:00
|
|
|
print('preloading Kornia requirements (ignore the deprecation warnings)...')
|
2022-08-26 05:20:01 +00:00
|
|
|
with warnings.catch_warnings():
|
2022-08-26 07:15:42 +00:00
|
|
|
warnings.filterwarnings('ignore', category=DeprecationWarning)
|
2022-08-26 05:20:01 +00:00
|
|
|
import kornia
|
2022-08-26 07:15:42 +00:00
|
|
|
print('...success')
|
2022-08-17 16:00:00 +00:00
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
version = 'openai/clip-vit-large-patch14'
|
2022-08-22 19:33:27 +00:00
|
|
|
|
2022-08-26 05:20:01 +00:00
|
|
|
print('preloading CLIP model (Ignore the deprecation warnings)...')
|
2022-08-22 19:33:27 +00:00
|
|
|
sys.stdout.flush()
|
2022-08-26 07:15:42 +00:00
|
|
|
|
|
|
|
tokenizer = CLIPTokenizer.from_pretrained(version)
|
|
|
|
transformer = CLIPTextModel.from_pretrained(version)
|
2022-08-22 19:33:27 +00:00
|
|
|
print('\n\n...success')
|
|
|
|
|
2022-08-26 05:20:01 +00:00
|
|
|
# In the event that the user has installed GFPGAN and also elected to use
|
|
|
|
# RealESRGAN, this will attempt to download the model needed by RealESRGANer
|
|
|
|
gfpgan = False
|
|
|
|
try:
|
|
|
|
from realesrgan import RealESRGANer
|
2022-08-26 07:15:42 +00:00
|
|
|
|
2022-08-26 05:20:01 +00:00
|
|
|
gfpgan = True
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if gfpgan:
|
2022-08-26 07:15:42 +00:00
|
|
|
print('Loading models from RealESRGAN and facexlib')
|
2022-08-26 05:20:01 +00:00
|
|
|
try:
|
|
|
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
|
|
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
2022-08-26 07:15:42 +00:00
|
|
|
|
|
|
|
RealESRGANer(
|
|
|
|
scale=2,
|
|
|
|
model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
|
|
|
|
model=RRDBNet(
|
|
|
|
num_in_ch=3,
|
|
|
|
num_out_ch=3,
|
|
|
|
num_feat=64,
|
|
|
|
num_block=23,
|
|
|
|
num_grow_ch=32,
|
|
|
|
scale=2,
|
|
|
|
),
|
|
|
|
)
|
2022-08-28 20:14:29 +00:00
|
|
|
|
|
|
|
RealESRGANer(
|
|
|
|
scale=4,
|
|
|
|
model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth',
|
|
|
|
model=RRDBNet(
|
|
|
|
num_in_ch=3,
|
|
|
|
num_out_ch=3,
|
|
|
|
num_feat=64,
|
|
|
|
num_block=23,
|
|
|
|
num_grow_ch=32,
|
|
|
|
scale=4,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
FaceRestoreHelper(1, det_model='retinaface_resnet50')
|
|
|
|
print('...success')
|
2022-08-26 05:20:01 +00:00
|
|
|
except Exception:
|
|
|
|
import traceback
|
2022-08-24 13:22:04 +00:00
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
print('Error loading GFPGAN:')
|
|
|
|
print(traceback.format_exc())
|