Skip to content

Commit

Permalink
Merge branch 'dev-async-ai-model-comms' into 'main'
Browse files Browse the repository at this point in the history
Automated Python virtual environments for our `core-gui` environment.

See merge request contribute/betterprompts/prompt-dev-env!21
  • Loading branch information
p3nGu1nZz committed Jun 28, 2024
2 parents 4b051c5 + dc17951 commit 5faad6a
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 0 deletions.
Empty file added gui/bin/PLACEHOLDER
Empty file.
23 changes: 23 additions & 0 deletions gui/scripts/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Use an official Python runtime as a parent image
FROM continuumio/miniconda3

# Set the working directory in the container to /app
WORKDIR /app

# Add metadata to an image
LABEL maintainer="[email protected]"

# Copy the current directory contents into the container at /app
COPY . /app

# Ensure the script has execution permissions
RUN chmod +x /app/activate_conda.sh

# Ensure Conda is initialized
RUN conda init bash

# Make port 80 available to the world outside this container
EXPOSE 80

# Run the command to execute your script
CMD ["./activate_conda.sh"]
15 changes: 15 additions & 0 deletions gui/scripts/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Python 3",
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"runArgs": [
"-u",
"root"
],
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
"extensions": []
}
69 changes: 69 additions & 0 deletions gui/scripts/activate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
:: scripts/activate.bat
@echo off
setlocal

:: Check for help argument or no arguments
if "%~1"=="" goto :help
if /I "%~1"=="--help" goto :help
if /I not "%~1"=="--env" goto :help

:: Extract the environment name from the argument
set "VENV_NAME=%~2"
set "BETTERPROMPTS_PATH=%USERPROFILE%\.betterprompts"
set "VENV_DIR=venv"
set "VENV_PATH=%BETTERPROMPTS_PATH%\%VENV_DIR%\%VENV_NAME%"

call :create_venv
call :activate_venv
call :display_python_info
call :display_venv_info
goto :eof

:create_venv
if not exist "%VENV_PATH%" (
echo Creating a virtual environment for %VENV_NAME%...
python -m venv "%VENV_PATH%"
if %ERRORLEVEL% NEQ 0 (
echo Failed to create a virtual environment for %VENV_NAME%.
exit /b 1
)
)
exit /b 0

:activate_venv
echo Activating the virtual environment for %VENV_NAME%...
call "%VENV_PATH%\Scripts\activate.bat"
if %ERRORLEVEL% NEQ 0 (
echo Failed to activate the virtual environment for %VENV_NAME%.
exit /b 1
)
echo Virtual environment for %VENV_NAME% activated.
exit /b 0

:display_python_info
"%VENV_PATH%\Scripts\python.exe" --version
"%VENV_PATH%\Scripts\python.exe" -m site
if %ERRORLEVEL% NEQ 0 (
echo Failed to display Python version and site information.
exit /b 1
)
exit /b 0

:display_venv_info
echo Virtual Environment Information:
echo - Name: %VENV_NAME%
echo - Path: %VENV_PATH%
echo - Python Executable: "%VENV_PATH%\Scripts\python.exe"
echo - Site-packages Directory: "%VENV_PATH%\Lib\site-packages"
exit /b 0

:help
echo.
echo Usage:
echo activate.bat --env [environment-name] : Activates the specified virtual environment.
echo activate.bat --help : Displays this help message.
echo.
exit /b 0

:: End of the script
endlocal
134 changes: 134 additions & 0 deletions gui/scripts/initialize.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
:: scripts/initialize.bat
@echo off
setlocal

:: Define the required Python version and installer details
set "REQUIRED_PYTHON_VERSION=3.9.13"
set "PYTHON_INSTALLER_FILENAME=python-%REQUIRED_PYTHON_VERSION%-amd64.exe"
set "PYTHON_INSTALLER_URL=https://www.python.org/ftp/python/%REQUIRED_PYTHON_VERSION%/%PYTHON_INSTALLER_FILENAME%"
set "PYTHON_INSTALL_DIR=%USERPROFILE%\.betterprompts\python"
set "INSTALLATION_ERROR=0"

call :check_python_installation
if %ERRORLEVEL% NEQ 0 call :install_python
if %INSTALLATION_ERROR% NEQ 0 goto :eof

:: Add Python Scripts to PATH after installation
set "PATH=%PYTHON_INSTALL_DIR%\Scripts;%PATH%"

call :create_directories
if %INSTALLATION_ERROR% NEQ 0 goto :eof

call :setup_virtual_environment
if %INSTALLATION_ERROR% NEQ 0 goto :eof

call :upgrade_pip
if %INSTALLATION_ERROR% NEQ 0 goto :eof

call :install_poetry
if %INSTALLATION_ERROR% NEQ 0 goto :eof

:: Confirm completion of the setup
echo Python environment setup for 'better-prompts' is complete.
goto :eof

:: Subroutine for checking Python installation
:check_python_installation
echo Checking for Python installation...
for /f "tokens=2" %%i in ('"%PYTHON_INSTALL_DIR%\python.exe" --version 2^>^&1') do set PYTHON_VERSION=%%i
if not defined PYTHON_VERSION (
echo Python is not installed. Please install Python before running this script.
exit /b 1
)
if not "%PYTHON_VERSION%"=="%REQUIRED_PYTHON_VERSION%" (
echo Required Python %REQUIRED_PYTHON_VERSION% is not installed.
exit /b 1
)
echo Found Python %REQUIRED_PYTHON_VERSION% installed.
exit /b 0

:: Subroutine for installing Python if not present
:install_python
echo Installing Python %REQUIRED_PYTHON_VERSION%...
set "PYTHON_INSTALL_DIR=%USERPROFILE%\.betterprompts\python"
echo Installation directory: %PYTHON_INSTALL_DIR%
if not exist "%PYTHON_INSTALL_DIR%" (
mkdir "%PYTHON_INSTALL_DIR%"
)
echo Downloading Python %REQUIRED_PYTHON_VERSION%...
curl -o "%PYTHON_INSTALL_DIR%\%PYTHON_INSTALLER_FILENAME%" %PYTHON_INSTALLER_URL%
echo Running Python %REQUIRED_PYTHON_VERSION% installer...
start /wait "" "%PYTHON_INSTALL_DIR%\%PYTHON_INSTALLER_FILENAME%" InstallAllUsers=0 PrependPath=1 TargetDir="%PYTHON_INSTALL_DIR%"
if not exist "%PYTHON_INSTALL_DIR%\python.exe" (
echo Installation cancelled by the user or failed. Please run the installer again.
set "INSTALLATION_ERROR=1"
exit /b 1
)
echo Python %REQUIRED_PYTHON_VERSION% has been installed.
set "PATH=%PYTHON_INSTALL_DIR%\Scripts;%PATH%"
echo Retrieving Python installation information...
"%PYTHON_INSTALL_DIR%\python.exe" -m site
call :check_python_installation
if %ERRORLEVEL% NEQ 0 (
echo Verification failed. Python may not have been installed correctly.
exit /b 1
)
echo Verification successful. Python is callable from the installation directory.
exit /b 0

:: Subroutine for upgrading pip
:upgrade_pip
echo Upgrading pip to the latest version...
"%PYTHON_INSTALL_DIR%\python.exe" -m pip install --upgrade pip
if %ERRORLEVEL% NEQ 0 (
echo Failed to upgrade pip. Please check your Python installation and try again.
exit /b 1
)
echo pip has been upgraded to the latest version.
goto :eof

:: Subroutine for creating directories
:create_directories
set "BETTERPROMPTS_DIR=.betterprompts"
set "BETTERPROMPTS_PATH=%USERPROFILE%\%BETTERPROMPTS_DIR%"
if not exist "%BETTERPROMPTS_PATH%" (
echo Creating directory "%BETTERPROMPTS_PATH%"...
mkdir "%BETTERPROMPTS_PATH%"
)
goto :eof

:: Subroutine for setting up the virtual environment
:setup_virtual_environment
set "VENV_DIR=venv\core-gui"
set "VENV_PATH=%BETTERPROMPTS_PATH%\%VENV_DIR%"
if not exist "%VENV_PATH%" (
echo Creating a virtual environment at "%VENV_PATH%"...
"%PYTHON_INSTALL_DIR%\python.exe" -m venv "%VENV_PATH%"
) else (
echo Virtual environment found at "%VENV_PATH%".
)
echo Activating the virtual environment...
call "%VENV_PATH%\Scripts\activate.bat"
echo Virtual environment activated at "%VENV_PATH%".
echo Upgrading pip in the virtual environment to the latest version...
"%VENV_PATH%\Scripts\python.exe" -m pip install --upgrade pip
if %ERRORLEVEL% NEQ 0 (
echo Failed to upgrade pip in the virtual environment. Please check your installation and try again.
exit /b 1
)
echo pip in the virtual environment has been upgraded to the latest version.
goto :eof

:: Subroutine for installing Poetry
:install_poetry
"%VENV_PATH%\Scripts\poetry.exe" --version >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
echo Installing Poetry...
"%VENV_PATH%\Scripts\pip.exe" install poetry
) else (
echo Poetry is already installed.
)
goto :eof

:: End of the script
endlocal
24 changes: 24 additions & 0 deletions gui/scripts/model_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# scripts/model_info.py

class ModelInfo:
def __init__(self, user, model_name, model_type, model_uri, model_url, timestamp=None, description=None, version=None):
self.user = user
self.model_name = model_name
self.model_type = model_type
self.model_uri = model_uri
self.model_url = model_url
self.timestamp = timestamp
self.description = description
self.version = version

def to_string(self):
info_str = ""
info_str += f"User: {self.user}\n"
info_str += f"Name: {self.model_name}\n"
info_str += f"Type: {self.model_type}\n"
info_str += f"URI: {self.model_uri}\n"
info_str += f"URL: {self.model_url}\n"
info_str += f"Timestamp: {self.timestamp}\n"
info_str += f"Description: {self.description}\n"
info_str += f"Version: {self.version}"
return info_str
35 changes: 35 additions & 0 deletions gui/scripts/model_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# scripts/model_list.py
from model_metadata import model_metadata
from model_info import ModelInfo

class ModelList:
def __init__(self):
self.model_metadata = model_metadata

def get_model_info(self, model_name):
return self.model_metadata.get(model_name, None)

def get_model_uri(self, model_name):
model_info = self.get_model_info(model_name)
if model_info:
return model_info.get("model_uri")
else:
return None # Model not found in metadata

def list_models(self):
print("")
for idx, (model_name, model_info) in enumerate(self.model_metadata.items()):
if idx > 0:
print("\n" + "=" * 40 + "\n") # Add separator between models
model = ModelInfo(
user=model_info.get("user"),
model_name=model_info.get("model_name"),
model_type=model_info.get("model_type"),
model_uri=model_info.get("model_uri"),
model_url=model_info.get("model_url"),
timestamp=model_info.get("timestamp"),
description=model_info.get("description"),
version=model_info.get("version")
)
print(model.to_string())
print("")
42 changes: 42 additions & 0 deletions gui/scripts/model_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# scripts/conda_actions.py

import subprocess
import argparse
import logging

class CondaManager:
def __init__(self):
self.valid_actions = ["create", "activate", "deactivate", "list", "remove"]
self.parser = argparse.ArgumentParser(description="Conda Environment Manager")
self.parser.add_argument("action", choices=self.valid_actions, help="Action to perform")
self.parser.add_argument("--env", help="Name of the Conda environment")
self.parser.add_argument("--python-version", default="3.8", help="Python version for creating a new environment")
self.args = self.parser.parse_args()

def create_environment(self, env_name, python_version):
try:
subprocess.run(["conda", "create", "-n", env_name, f"python={python_version}", "-y"])
logging.info(f"Environment '{env_name}' created successfully.")
except subprocess.CalledProcessError:
logging.error(f"Error creating environment '{env_name}'.")

def activate_environment(self, env_name):
subprocess.run(["conda", "activate", env_name])

def deactivate_environment(self):
subprocess.run(["conda", "deactivate"])

def list_environments(self):
subprocess.run(["conda", "env", "list"])

def remove_environment(self, env_name):
try:
subprocess.run(["conda", "env", "remove", "-n", env_name, "-y"])
logging.info(f"Environment '{env_name}' removed successfully.")
except subprocess.CalledProcessError:
logging.error(f"Error removing environment '{env_name}'.")

if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
conda_manager = CondaManager()
conda_manager.handle_action(conda_manager.args.action)

0 comments on commit 5faad6a

Please sign in to comment.