2022-10-24 03:31:41 +03:00
# This file is part of sygil-webui (https://github.com/Sygil-Dev/sygil-webui/).
2022-09-26 16:02:48 +03:00
2022-10-24 03:17:50 +03:00
# Copyright 2022 Sygil-Dev team.
2022-09-26 16:02:48 +03:00
# 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/>.
2022-09-22 10:02:44 +03:00
import os , subprocess , shutil
2022-09-19 04:29:19 +03:00
from huggingface_hub import HfApi
from git import Repo , RemoteProgress
class CloneProgress ( RemoteProgress ) :
def update ( self , op_code , cur_count , max_count = None , message = ' ' ) :
if message :
print ( message )
api = HfApi ( )
models_list = api . list_models ( author = " sd-concepts-library " , sort = " likes " , direction = - 1 )
models = [ ]
print ( " Downloading the sd concept library from the huggingface site. " )
for model in models_list :
model_content = { }
model_id = model . modelId
url = f " https://huggingface.co/ { model_id } "
try :
2022-09-22 10:02:44 +03:00
if not os . path . exists ( os . path . join ( " ../models/custom " , model_id ) ) :
subprocess . run ( [ ' git ' , ' lfs ' , ' install ' ] , stdout = subprocess . DEVNULL )
Repo . clone_from ( url , os . path . join ( " ../models/custom " , model_id ) , progress = CloneProgress ( ) )
#else:
#repo = Repo(os.path.join("../models/custom", model_id))
#repo.git.stash('save')
#repo.git.pull()
subprocess . run ( [ ' git ' , ' lfs ' , ' uninstall ' ] , stdout = subprocess . DEVNULL ) # uninstall LFS
os . remove ( os . path . join ( " ../models/custom " , model_id , ' .gitattributes ' ) ) if os . path . exists ( os . path . join ( " ../models/custom " , model_id , ' .gitattributes ' ) ) else None # remove the .gitattributes so files don't use LFS
subprocess . run ( [ ' rm ' , ' -rf ' , os . path . join ( " ../models/custom " , model_id , ' .git ' ) ] ) if os . path . exists ( os . path . join ( " ../models/custom " , model_id , ' .git ' ) ) else None # remove all the .git folders as we dont need them.
# get the folder size and delete it if its larger than 100mb
size = 0
for ele in os . scandir ( os . path . join ( " ../models/custom " , model_id ) ) : # get folder size
size + = os . stat ( ele ) . st_size
if size > 100000000 : # if the folder is larger than 100mb delete it.
shutil . rmtree ( os . path . join ( " ../models/custom " , model_id ) , ignore_errors = True )
2022-09-19 04:29:19 +03:00
except :
pass