File size: 5,152 Bytes
533b51b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8edab2
533b51b
 
 
 
 
 
 
b8edab2
533b51b
 
 
b8edab2
533b51b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8edab2
 
 
 
533b51b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8edab2
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
from IPython.display import display, HTML, clear_output, Image
from IPython import get_ipython
from ipywidgets import widgets
from pathlib import Path
import subprocess
import shlex
import json

home = Path.home()
src = home / '.gutris1'
css = src / 'segsmaker.css'
startup = home / '.ipython/profile_default/startup'
nenen = startup / 'nenen88.py'
key_file = src / 'api-key.json'
img = src / 'loading.png'

R = '\033[0m'
T = f'▶{R}'
BLUE = f'\033[38;5;33m{T}'
CYAN = f'\033[36m{T}'
PURPLE = f'\033[38;5;135m{T}'
PINK = f'\033[38;5;201m{T}'
RED = f'\033[31m{T}'
GREEN = f'\033[38;5;35m{T}'

Path(src).mkdir(parents=True, exist_ok=True)

SyS = get_ipython().system

main_output = widgets.Output()
save_button = widgets.Button(description='Save')
civitai_key_box = widgets.Text(placeholder='Enter Your Civitai API Key Here', layout=widgets.Layout(width='350px'))
hf_token_box = widgets.Text(placeholder='Huggingface READ Token (optional)', layout=widgets.Layout(width='350px'))
input_widget = widgets.Box(
    [civitai_key_box, hf_token_box, save_button], 
    layout=widgets.Layout(
        width='500px',
        height='200px',
        display='flex',
        flex_flow='column',
        align_items='center',
        justify_content='space-around',
        padding='10px'
    )
)

save_button.add_class('save-button')
civitai_key_box.add_class('api-input')
hf_token_box.add_class('api-input')
input_widget.add_class('boxs')

def CondaInstall():
    try:
        display(Image(filename=str(img)))

        cmd_list = [
            (f'rm -rf {home}/.condarc', None),
            ('conda config --add channels conda-forge', None),
            ('conda install -qy mamba', f'{BLUE} Installing Anaconda'),
            ('mamba install -y conda', None),
            ('mamba install -y python=3.10.13', f'{CYAN} Installing Python 3.10'),
            ('mamba install -y glib gperftools openssh pv gputil curl', f'{PURPLE} Installing Conda Packages'),
            ('pip install psutil aria2 gdown', f'{PINK} Installing Python Packages'),
            ('conda clean -qy --all', None)
        ]

        for cmd, msg in cmd_list:
            if msg is not None: print(msg)
            subprocess.run(shlex.split(cmd), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

        SyS(f'rm -rf {home}/.cache/* {home}/.condarc')
        clear_output()
        print(f'{GREEN} Done')

        get_ipython().kernel.do_shutdown(True)

    except KeyboardInterrupt:
        clear_output()
        print('^ Canceled')

def LoadCSS(): 
    display(HTML(f'<style>{open(css).read()}</style>'))

def KeyInject(civitai_key, hf_token):
    SyS(f'curl -sLo {nenen} https://github.com/gutris1/segsmaker/raw/main/script/nenen88.py')

    p = Path(nenen)
    v = p.read_text()
    v = v.replace("TOKET = ''", f"TOKET = '{civitai_key}'")
    v = v.replace("TOBRUT = ''", f"TOBRUT = '{hf_token}'")
    p.write_text(v)

def KeyWidget(civitai_key='', hf_token=''):
    civitai_key_box.value = civitai_key
    hf_token_box.value = hf_token

    def KeyInputs(b):
        civitai_key = civitai_key_box.value.strip()
        hf_token = hf_token_box.value.strip()

        with main_output:
            if not civitai_key:
                print('Please enter your Civitai API Key')
                return

            if len(civitai_key) < 32:
                print('API key must be at least 32 characters long')
                return

            civitai_key_value = {'civitai-api-key': civitai_key}
            hf_token_value = {'huggingface-read-token': hf_token}

            secrets = {**civitai_key_value, **hf_token_value}
            key_file.write_text(json.dumps(secrets, indent=4))

        KeyInject(civitai_key, hf_token)

        input_widget.close()
        main_output.clear_output()

        p = subprocess.run(['conda', '--version'], capture_output=True, text=True, check=True)
        cv = p.stdout.strip().split()[1]
        mv = int(cv.split('.')[0])

        with main_output:
            CondaInstall()
    save_button.on_click(KeyInputs)

def KeyCheck():
    if key_file.exists():
        v = json.loads(key_file.read_text())

        civitai_key = v.get('civitai-api-key', '')
        hf_token = v.get('huggingface-read-token', '')

        if not civitai_key or not hf_token:
            display(input_widget, main_output)
            KeyWidget(civitai_key, hf_token)
        else:
            KeyInject(civitai_key, hf_token)
            display(main_output)
            CondaInstall()
    else:
        display(input_widget, main_output)
        KeyWidget()

def CondaMisc():
    for scr in [
        f'curl -sLo {css} https://github.com/gutris1/segsmaker/raw/main/script/SM/segsmaker.css',
        f'curl -sLo {startup}/00-startup.py https://github.com/gutris1/segsmaker/raw/main/script/SM/00-startup.py',
        f'curl -sLo {startup}/util.py https://github.com/gutris1/segsmaker/raw/main/script/SM/util.py',
        f'curl -sLo {img} https://github.com/gutris1/segsmaker/raw/main/script/loading.png',
        f'curl -sLo {startup}/cupang.py https://github.com/gutris1/segsmaker/raw/main/script/cupang.py'
    ]: SyS(scr)

CondaMisc()
LoadCSS()
KeyCheck()