r/Python • u/ZachVorhies • Jan 11 '24
Intermediate Showcase isolated-environment: Package Isolation Designed for AI app developers to prevent pytorch conflicts
isolated-environment: Package Isolation Designed for AI app developers
This is a package isolation library designed specifically for AI developers to solve the problems of AI dependency conflicts introduced by the various pytorch incompatibilities within and between AI apps.
Install it like this:
pip install isolated-environment
In plain words, this package allows you to install your AI apps globally without pytorch conflicts. Such dependencies are moved out of the requirements.txt and into the runtime of your app within a privately scoped virtual environment. This is very similar to pipx, but without the downsides, enumerated in the readme here.
Example Usage:
from pathlib import Path
import subprocess
CUDA_VERSION = "cu121"
EXTRA_INDEX_URL = f"https://download.pytorch.org/whl/{CUDA_VERSION}"
HERE = Path(os.path.abspath(os.path.dirname(__file__)))
from isolated_environment import IsolatedEnvironment
iso_env = IsolatedEnvironment(HERE / 'whisper_env')
iso_env.install_environment()
iso_env.pip_install('torch==2.1.2', EXTRA_INDEX_URL)
iso_env.pip_install('openai-whisper')
venv = iso_env.environment()
subprocess.run(['whisper', '--help'], env=venv, shell=True, check=True)
If you want to see this package in action, checkout transcribe-anything by installing it globally using pip install transcribe-anything and then invoking it on the "Never Gonna Give You Up" song on youtube:
transcribe-anything https://www.youtube.com/watch?v=dQw4w9WgXcQ
6
u/ZachVorhies Jan 11 '24 edited Jan 12 '24
I'm thinking people are confused about what this does.
venvis typically created and activated before your app is invoked.isolated-environmentis invoked by your code when your app runs to create it's ownvenvto invoke a complex AI app subcommand that has a specificpytorchversion requirement that would interfere if stored globally.In this way,
isolated-environmentinverts the relationship between a venv and an app. Instead of the app being launched after venv, your app is launched first and then creates a venv for a complex AI dependency chain that you don't want to leak out because of dependency hell that would entail.Like this:
status quo: venv -> your app + ai subcommand/dependencies
isolate-environment: your app -> private venv -> ai subcommand/dependencies
If you want to get an idea of this problem, look at every wrapper around
openai-whisper. They all have massive conflicts and recommend you install and run them from their own virtual environments. So what if you want to duct tape all these ai programs together? What if you want to swapwhisperwithinsanely-fast-whispervia a command line switch which uses a different dependency chain? Congrats, you are now in dependency hell.isolated-environmentsolves this problem. If all these whisper frontend apps usedisolated-environmentthen they could all be installed globally with pip install and just work.If you want to emulate
isolated-environmentby hand rolling your own private venv creation, then go for it. But be prepared to hit every platform specific footgun that exists, which I've solved with this library.