File size: 8,485 Bytes
e450b04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import os
from IPython.display import clear_output
from subprocess import call, getoutput
import time
import sys
import fileinput
import ipywidgets as widgets



def Deps(force_reinstall):

    if not force_reinstall and os.path.exists('/usr/local/lib/python3.9/dist-packages/safetensors'):
        os.chdir('/notebooks')
        if not os.path.exists('Latest_Notebooks'):
            call('mkdir Latest_Notebooks', shell=True)
        else:
            call('rm -r Latest_Notebooks', shell=True)
            call('mkdir Latest_Notebooks', shell=True)
        os.chdir('/notebooks/Latest_Notebooks')
        call('wget -q -i https://huggingface.co/datasets/TheLastBen/PPS/raw/main/Notebooks.txt', shell=True)
        call('rm Notebooks.txt', shell=True)
        os.chdir('/notebooks')
        print('Modules and notebooks updated, dependencies already installed')

    else:
        print('Installing the dependencies...')
        call("pip install --root-user-action=ignore --no-deps -q accelerate==0.12.0", shell=True, stdout=open('/dev/null', 'w'))
        if not os.path.exists('/usr/local/lib/python3.9/dist-packages/safetensors'):
            os.chdir('/usr/local/lib/python3.9/dist-packages')
            call("rm -r torch torch-1.12.0+cu116.dist-info torchaudio* torchvision* PIL Pillow* transformers* numpy* gdown*", shell=True, stdout=open('/dev/null', 'w'))

        os.chdir('/notebooks')
        if not os.path.exists('Latest_Notebooks'):
            call('mkdir Latest_Notebooks', shell=True)
        else:
            call('rm -r Latest_Notebooks', shell=True)
            call('mkdir Latest_Notebooks', shell=True)
        os.chdir('/notebooks/Latest_Notebooks')
        call('wget -q -i https://huggingface.co/datasets/TheLastBen/PPS/raw/main/Notebooks.txt', shell=True)
        call('rm Notebooks.txt', shell=True)
        os.chdir('/notebooks')
        call('wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/aptdeps.txt', shell=True)
        os.chdir('/notebooks')
        if not os.path.exists('/models'):
            call('mkdir /models', shell=True)
        if not os.path.exists('/notebooks/models'):
            call('ln -s /models /notebooks', shell=True)
        if os.path.exists('/deps'):
            call("rm -r /deps", shell=True)
        call('mkdir /deps', shell=True)
        if not os.path.exists('cache'):
            call('mkdir cache', shell=True)
        os.chdir('/deps')
        call('wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/aptdeps.txt', shell=True)
        call('dpkg -i *.deb', shell=True, stdout=open('/dev/null', 'w'))
        call('wget -q https://huggingface.co/TheLastBen/dependencies/resolve/main/pps.tar.zst', shell=True, stdout=open('/dev/null', 'w'))
        call('tar -C / --zstd -xf pps.tar.zst', shell=True, stdout=open('/dev/null', 'w'))
        call("sed -i 's@~/.cache@/notebooks/cache@' /usr/local/lib/python3.9/dist-packages/transformers/utils/hub.py", shell=True)
        os.chdir('/notebooks')
        call("git clone --depth 1 -q --branch updt https://github.com/TheLastBen/diffusers /diffusers", shell=True, stdout=open('/dev/null', 'w'))
        if not os.path.exists('/notebooks/diffusers'):
            call('ln -s /diffusers /notebooks', shell=True)
        call("rm -r /deps", shell=True)
        os.chdir('/notebooks')
        clear_output()

        done()



def repo():

    print('Installing/Updating the repo...')
    os.chdir('/notebooks')
    if not os.path.exists('/notebooks/sd/stablediffusion'):
       call('wget -q -O sd_rep.tar.zst https://huggingface.co/TheLastBen/dependencies/resolve/main/sd_rep.tar.zst', shell=True)
       call('tar --zstd -xf sd_rep.tar.zst', shell=True)
       call('rm sd_rep.tar.zst', shell=True)        
        
    os.chdir('/notebooks/sd')
    if not os.path.exists('stable-diffusion-webui'):
        call('git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui', shell=True)
    
    os.chdir('/notebooks/sd/stable-diffusion-webui/')
    call('git reset --hard', shell=True, stdout=open('/dev/null', 'w'))
    print('')
    call('git pull', shell=True, stdout=open('/dev/null', 'w'))
    os.chdir('/notebooks')
    clear_output()
    done()


def mdl(Original_Model_Version, Path_to_MODEL, MODEL_LINK, safetensors, Temporary_Storage):
    import gdown
    if Path_to_MODEL !='':
      if os.path.exists(str(Path_to_MODEL)):
        print('Using the trained model.')
        model=Path_to_MODEL
      else:
          print('Wrong path, check that the path to the model is correct')

    elif MODEL_LINK != "":
      modelname="model.safetensors" if safetensors else "model.ckpt"
      if Temporary_Storage:
         model=f'/models/{modelname}'
      else:
         model=f'/notebooks/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}'
      if os.path.exists(model):
        call('rm '+model, shell=True)
      gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)

      if os.path.exists(model) and os.path.getsize(model) > 1810671599:
        clear_output()
        print('Model downloaded, using the trained model.')
      else:
        print('Wrong link, check that the link is valid')

    else:
        if Original_Model_Version == "v1.5":
           model="/datasets/stable-diffusion-classic/v1-5-pruned-emaonly.ckpt"
           print('Using the original V1.5 model')
        elif Original_Model_Version == "v2-512":
           model="dataset"
           print('Using the original V2-512 model')
        elif Original_Model_Version == "v2-768":
           model="/datasets/stable-diffusion-v2-1/stable-diffusion-2-1/v2-1_768-nonema-pruned.safetensors"
           print('Using the original V2-768 model')
        else:
            model=""
            print('Wrong model version')

    return model


def sd(User, Password, Use_localtunnel):

    auth=f"--gradio-auth {User}:{Password}"
    if User =="" or Password=="":
      auth=""
      
    if not os.path.exists('/usr/lib/node_modules/localtunnel'):
       call('npm install -g localtunnel  --silent', shell=True, stdout=open('/dev/null', 'w'))
       clear_output()
       
       
    share=''
    call('wget -q -O /usr/local/lib/python3.9/dist-packages/gradio/blocks.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/blocks.py', shell=True)
    
    if not Use_localtunnel:
      share='--share'
      
    else:
      share=''
      os.chdir('/notebooks')
      call('nohup lt --port 7860 > srv.txt 2>&1 &', shell=True)
      time.sleep(2)
      call("grep -o 'https[^ ]*' /notebooks/srv.txt >srvr.txt", shell=True)
      time.sleep(2)
      srv= getoutput('cat /notebooks/srvr.txt')

      for line in fileinput.input('/usr/local/lib/python3.9/dist-packages/gradio/blocks.py', inplace=True):
        if line.strip().startswith('self.server_name ='):
            line = f'            self.server_name = "{srv[8:]}"\n'
        if line.strip().startswith('self.server_port ='):
            line = '            self.server_port = 443\n'
        if line.strip().startswith('self.protocol = "https"'):
            line = '            self.protocol = "https"\n'
        if line.strip().startswith('if self.local_url.startswith("https") or self.is_colab'):
            line = ''
        if line.strip().startswith('else "http"'):
            line = ''
        sys.stdout.write(line)

      call('rm /notebooks/srv.txt', shell=True)
      call('rm /notebooks/srvr.txt', shell=True)
      
    os.chdir('/notebooks/sd/stable-diffusion-webui/modules')
    call('wget -q -O paths.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/paths.py', shell=True) 
    call("sed -i 's@/content/gdrive/MyDrive/sd/stablediffusion@/notebooks/sd/stablediffusion@' /notebooks/sd/stable-diffusion-webui/modules/paths.py", shell=True)   
    os.chdir('/notebooks/sd/stable-diffusion-webui')
    clear_output()

    configf="--disable-console-progressbars --no-half-vae --disable-safe-unpickle --api --xformers --medvram --skip-version-check "+auth+" "+share

    return configf, auth, share
    
    
def done():
    done = widgets.Button(
        description='Done!',
        disabled=True,
        button_style='success',
        tooltip='',
        icon='check'
    )
    display(done)