Inhanced model installation, Working hugging face binding

This commit is contained in:
Saifeddine ALOUI 2023-08-23 17:59:52 +02:00
parent 1c80ba41b4
commit a6deac5f9c
3 changed files with 144 additions and 119 deletions

3
.gitignore vendored
View File

@ -203,3 +203,6 @@ src/taming-transformers
# Temporary arguments to restart file # Temporary arguments to restart file
temp_args.txt temp_args.txt
# Hugging face offloading folder
offload

View File

@ -223,131 +223,148 @@ class LoLLMsAPPI(LollmsApplication):
binding_folder = self.config["binding_name"] binding_folder = self.config["binding_name"]
model_url = model_path model_url = model_path
signature = f"{model_name}_{binding_folder}_{model_url}" signature = f"{model_name}_{binding_folder}_{model_url}"
self.download_infos[signature]={ try:
"start_time":datetime.now(), self.download_infos[signature]={
"total_size":self.binding.get_file_size(model_path), "start_time":datetime.now(),
"downloaded_size":0, "total_size":self.binding.get_file_size(model_path),
"progress":0, "downloaded_size":0,
"speed":0, "progress":0,
"cancel":False "speed":0,
} "cancel":False
}
if installation_path.exists():
print("Error: Model already exists. please remove it first")
socketio.emit('install_progress',{
'status': False,
'error': f'model already exists. Please remove it first.\nThe model can be found here:{installation_path}',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id
)
if installation_path.exists():
print("Error: Model already exists")
socketio.emit('install_progress',{ socketio.emit('install_progress',{
'status': False, 'status': True,
'error': 'model already exists', 'progress': progress,
'model_name' : model_name, 'model_name' : model_name,
'binding_folder' : binding_folder, 'binding_folder' : binding_folder,
'model_url' : model_url, 'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"), 'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'], 'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'], 'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'], 'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'], 'speed': self.download_infos[signature]['speed'],
}, room=room_id
)
socketio.emit('install_progress',{ }, room=room_id)
'status': True,
'progress': progress,
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id) def callback(downloaded_size, total_size):
progress = (downloaded_size / total_size) * 100
now = datetime.now()
dt = (now - self.download_infos[signature]['start_time']).total_seconds()
speed = downloaded_size/dt
self.download_infos[signature]['downloaded_size'] = downloaded_size
self.download_infos[signature]['speed'] = speed
def callback(downloaded_size, total_size): if progress - self.download_infos[signature]['progress']>2:
progress = (downloaded_size / total_size) * 100 self.download_infos[signature]['progress'] = progress
now = datetime.now() socketio.emit('install_progress',{
dt = (now - self.download_infos[signature]['start_time']).total_seconds() 'status': True,
speed = downloaded_size/dt 'model_name' : model_name,
self.download_infos[signature]['downloaded_size'] = downloaded_size 'binding_folder' : binding_folder,
self.download_infos[signature]['speed'] = speed 'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id)
if progress - self.download_infos[signature]['progress']>2: if self.download_infos[signature]["cancel"]:
self.download_infos[signature]['progress'] = progress raise Exception("canceled")
socketio.emit('install_progress',{
'status': True,
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id)
if self.download_infos[signature]["cancel"]:
raise Exception("canceled")
if hasattr(self.binding, "download_model"): if hasattr(self.binding, "download_model"):
try:
self.binding.download_model(model_path, installation_path, callback)
except Exception as ex:
ASCIIColors.warning(str(ex))
socketio.emit('install_progress',{
'status': False,
'error': 'canceled',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id
)
del self.download_infos[signature]
try: try:
installation_path.unlink() self.binding.download_model(model_path, installation_path, callback)
except Exception as ex: except Exception as ex:
ASCIIColors.error(f"Couldn't delete file. Please try to remove it manually.\n{installation_path}") ASCIIColors.warning(str(ex))
return trace_exception(ex)
socketio.emit('install_progress',{
else: 'status': False,
try: 'error': 'canceled',
self.download_file(model_path, installation_path, callback) 'model_name' : model_name,
except Exception as ex: 'binding_folder' : binding_folder,
ASCIIColors.warning(str(ex)) 'model_url' : model_url,
socketio.emit('install_progress',{ 'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'status': False, 'total_size': self.download_infos[signature]['total_size'],
'error': 'canceled', 'downloaded_size': self.download_infos[signature]['downloaded_size'],
'model_name' : model_name, 'progress': self.download_infos[signature]['progress'],
'binding_folder' : binding_folder, 'speed': self.download_infos[signature]['speed'],
'model_url' : model_url, }, room=room_id
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"), )
'total_size': self.download_infos[signature]['total_size'], del self.download_infos[signature]
'downloaded_size': self.download_infos[signature]['downloaded_size'], try:
'progress': self.download_infos[signature]['progress'], installation_path.unlink()
'speed': self.download_infos[signature]['speed'], except Exception as ex:
}, room=room_id ASCIIColors.error(f"Couldn't delete file. Please try to remove it manually.\n{installation_path}")
) return
del self.download_infos[signature]
installation_path.unlink()
return
socketio.emit('install_progress',{
'status': True,
'error': '',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': 100,
'speed': self.download_infos[signature]['speed'],
}, room=room_id)
del self.download_infos[signature]
else:
try:
self.download_file(model_path, installation_path, callback)
except Exception as ex:
ASCIIColors.warning(str(ex))
trace_exception(ex)
socketio.emit('install_progress',{
'status': False,
'error': 'canceled',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id
)
del self.download_infos[signature]
installation_path.unlink()
return
socketio.emit('install_progress',{
'status': True,
'error': '',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': 100,
'speed': self.download_infos[signature]['speed'],
}, room=room_id)
del self.download_infos[signature]
except Exception as ex:
trace_exception(ex)
socketio.emit('install_progress',{
'status': False,
'error': str(ex),
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': '',
'total_size': 0,
'downloaded_size': 0,
'progress': 0,
'speed': 0,
}, room=room_id
)
tpe = threading.Thread(target=install_model_, args=()) tpe = threading.Thread(target=install_model_, args=())
tpe.start() tpe.start()

5
app.py
View File

@ -1646,6 +1646,11 @@ class LoLLMsWebUI(LoLLMsAPPI):
path = f'{server}{filename}' path = f'{server}{filename}'
else: else:
path = f'{server}/{filename}' path = f'{server}/{filename}'
blocs = filename.split("/")
# Special case, if hugging face model format
if len(blocs)==2:
filename = blocs[1]
local_path = lollms_paths.personal_models_path/f'{self.config["binding_name"]}/{filename}' local_path = lollms_paths.personal_models_path/f'{self.config["binding_name"]}/{filename}'
is_installed = local_path.exists() or model_type.lower()=="api" is_installed = local_path.exists() or model_type.lower()=="api"
models.append({ models.append({