lollms-webui/installations/install_backend.py

61 lines
2.1 KiB
Python
Raw Normal View History

2023-05-05 22:28:17 +00:00
import argparse
import subprocess
import shutil
import yaml
from pathlib import Path
2023-05-25 21:24:14 +00:00
def install_binding(binding_name):
# Load the list of available bindings from bindinglist.yaml
with open('bindinglist.yaml', 'r') as f:
binding_list = yaml.safe_load(f)
2023-05-05 22:28:17 +00:00
2023-05-25 21:24:14 +00:00
# Get the Github repository URL for the selected binding
2023-05-05 22:28:17 +00:00
try:
2023-05-25 21:24:14 +00:00
binding_url = binding_list[binding_name]
2023-05-05 22:28:17 +00:00
except KeyError:
2023-05-25 21:24:14 +00:00
print(f"Binding '{binding_name}' not found in bindinglist.yaml")
2023-05-05 22:28:17 +00:00
return
# Clone the Github repository to a tmp folder
tmp_folder = Path('tmp')
if tmp_folder.exists():
shutil.rmtree(tmp_folder)
2023-05-25 21:24:14 +00:00
subprocess.run(['git', 'clone', binding_url, tmp_folder])
2023-05-05 22:28:17 +00:00
# Install the requirements.txt from the cloned project
requirements_file = tmp_folder / 'requirements.txt'
subprocess.run(['pip', 'install', '-r', str(requirements_file)])
2023-05-25 21:24:14 +00:00
# Copy the folder found inside the binding to ../bindings
2023-05-05 22:28:17 +00:00
folders = [f for f in tmp_folder.iterdir() if f.is_dir() and not f.stem.startswith(".")]
src_folder = folders[0]
2023-05-25 21:24:14 +00:00
dst_folder = Path('../bindings') / src_folder.stem
2023-05-05 22:28:17 +00:00
print(f"coipying from {src_folder} to {dst_folder}")
# Delete the destination directory if it already exists
if dst_folder.exists():
shutil.rmtree(dst_folder)
shutil.copytree(src_folder, dst_folder)
# Create an empty folder in ../models with the same name
models_folder = Path('../models')
models_folder.mkdir(exist_ok=True)
2023-05-25 21:24:14 +00:00
(models_folder / binding_name).mkdir(exist_ok=True, parents=True)
2023-05-05 22:28:17 +00:00
if tmp_folder.exists():
shutil.rmtree(tmp_folder)
if __name__ == '__main__':
2023-05-25 21:24:14 +00:00
# Load the list of available bindings from bindinglist.yaml
with open('bindinglist.yaml', 'r') as f:
binding_list = yaml.safe_load(f)
2023-05-05 22:28:17 +00:00
2023-05-25 21:24:14 +00:00
# Print the list of available bindings and prompt the user to select one
print("Available bindings:")
for binding_id, binding_name in enumerate(binding_list):
print(f" {binding_id} - {binding_name}")
binding_id = int(input("Select a binding to install: "))
2023-05-05 22:28:17 +00:00
2023-05-25 21:24:14 +00:00
install_binding(list(binding_list.keys())[binding_id])