-
-
Notifications
You must be signed in to change notification settings - Fork 890
/
ModelManager.py
170 lines (147 loc) · 6.89 KB
/
ModelManager.py
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
# This file is part of sygil-webui (https://github.com/Sygil-Dev/sygil-webui/).
# Copyright 2022 Sygil-Dev team.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# base webui import and utils.
from sd_utils import st, logger
# streamlit imports
# other imports
import os, requests
from requests.auth import HTTPBasicAuth
from requests import HTTPError
from stqdm import stqdm
# Temp imports
# end of imports
# ---------------------------------------------------------------------------------------------------------------
def download_file(file_name, file_path, file_url):
if not os.path.exists(file_path):
os.makedirs(file_path)
if not os.path.exists(os.path.join(file_path, file_name)):
print("Downloading " + file_name + "...")
# TODO - add progress bar in streamlit
# download file with `requests``
if file_name == "Stable Diffusion v1.5":
if (
"huggingface_token" not in st.session_state
or st.session_state["defaults"].general.huggingface_token == "None"
):
if "progress_bar_text" in st.session_state:
st.session_state["progress_bar_text"].error(
"You need a huggingface token in order to use the Text to Video tab. Use the Settings page from the sidebar on the left to add your token."
)
raise OSError(
"You need a huggingface token in order to use the Text to Video tab. Use the Settings page from the sidebar on the left to add your token."
)
try:
with requests.get(
file_url,
auth=HTTPBasicAuth(
"token", st.session_state.defaults.general.huggingface_token
)
if "huggingface.co" in file_url
else None,
stream=True,
) as r:
r.raise_for_status()
with open(os.path.join(file_path, file_name), "wb") as f:
for chunk in stqdm(
r.iter_content(chunk_size=8192), backend=True, unit="kb"
):
f.write(chunk)
except HTTPError as e:
if "huggingface.co" in file_url:
if "resolve" in file_url:
repo_url = file_url.split("resolve")[0]
st.session_state["progress_bar_text"].error(
f"You need to accept the license for the model in order to be able to download it. "
f"Please visit {repo_url} and accept the lincense there, then try again to download the model."
)
logger.error(e)
else:
print(file_name + " already exists.")
def download_model(models, model_name):
"""Download all files from model_list[model_name]"""
for file in models[model_name]:
download_file(file["file_name"], file["file_path"], file["file_url"])
return
def layout():
# search = st.text_input(label="Search", placeholder="Type the name of the model you want to search for.", help="")
colms = st.columns((1, 3, 3, 5, 5))
columns = ["№", "Model Name", "Save Location", "Download", "Download Link"]
models = st.session_state["defaults"].model_manager.models
for col, field_name in zip(colms, columns):
# table header
col.write(field_name)
for x, model_name in enumerate(models):
col1, col2, col3, col4, col5 = st.columns((1, 3, 3, 3, 6))
col1.write(x) # index
col2.write(models[model_name]["model_name"])
col3.write(models[model_name]["save_location"])
with col4:
files_exist = 0
for file in models[model_name]["files"]:
if "save_location" in models[model_name]["files"][file]:
os.path.exists(
os.path.join(
models[model_name]["files"][file]["save_location"],
models[model_name]["files"][file]["file_name"],
)
)
files_exist += 1
elif os.path.exists(
os.path.join(
models[model_name]["save_location"],
models[model_name]["files"][file]["file_name"],
)
):
files_exist += 1
files_needed = []
for file in models[model_name]["files"]:
if "save_location" in models[model_name]["files"][file]:
if not os.path.exists(
os.path.join(
models[model_name]["files"][file]["save_location"],
models[model_name]["files"][file]["file_name"],
)
):
files_needed.append(file)
elif not os.path.exists(
os.path.join(
models[model_name]["save_location"],
models[model_name]["files"][file]["file_name"],
)
):
files_needed.append(file)
if len(files_needed) > 0:
if st.button(
"Download",
key=models[model_name]["model_name"],
help="Download " + models[model_name]["model_name"],
):
for file in files_needed:
if "save_location" in models[model_name]["files"][file]:
download_file(
models[model_name]["files"][file]["file_name"],
models[model_name]["files"][file]["save_location"],
models[model_name]["files"][file]["download_link"],
)
else:
download_file(
models[model_name]["files"][file]["file_name"],
models[model_name]["save_location"],
models[model_name]["files"][file]["download_link"],
)
st.experimental_rerun()
else:
st.empty()
else:
st.write("✅")
#