reduce nesting, better error reporting

This commit is contained in:
Konstantin Gukov 2023-05-25 17:30:06 +02:00 committed by Richard Guo
parent 591a047204
commit 6e3c59d8b2

View File

@ -87,23 +87,23 @@ class GPT4All():
else: else:
model_path = model_path.replace("\\", "\\\\") model_path = model_path.replace("\\", "\\\\")
if os.path.exists(model_path): if not os.path.exists(model_path):
model_dest = os.path.join(model_path, model_filename).replace("\\", "\\\\") raise ValueError("Invalid model directory: {}".format(model_path))
if os.path.exists(model_dest):
print("Found model file at ", model_dest)
return model_dest
# If model file does not exist, download model_dest = os.path.join(model_path, model_filename).replace("\\", "\\\\")
elif allow_download: if os.path.exists(model_dest):
# Make sure valid model filename before attempting download print("Found model file at ", model_dest)
available_models = GPT4All.list_models() return model_dest
if model_filename not in (m["filename"] for m in available_models):
raise ValueError(f"Model filename not in model list: {model_filename}") # If model file does not exist, download
return GPT4All.download_model(model_filename, model_path) elif allow_download:
else: # Make sure valid model filename before attempting download
raise ValueError("Failed to retrieve model") available_models = GPT4All.list_models()
if model_filename not in (m["filename"] for m in available_models):
raise ValueError(f"Model filename not in model list: {model_filename}")
return GPT4All.download_model(model_filename, model_path)
else: else:
raise ValueError("Invalid model directory") raise ValueError("Failed to retrieve model")
@staticmethod @staticmethod
def download_model(model_filename: str, model_path: str) -> str: def download_model(model_filename: str, model_path: str) -> str: