import os import sys import subprocess import importlib.metadata def config_env(): packages_to_install = [ ('gradio',None), ('numpy',None), ('tensorflow', '2.9'), ('keras', '2.9'), ('opencv-python-headless', '4.5.5.64'), ('python-dotenv', None), ('torch', None), ('torchvision', None), ('xplique', None), ('segment_anything', None), ('panopticapi', None), ('keras_cv_attention_models',None) ] name_to_command = {'segment_anything':'git+https://github.com/facebookresearch/segment-anything.git', 'panopticapi':'git+https://github.com/cocodataset/panopticapi.git', } env_name = os.path.basename(sys.prefix) print(env_name) for package, version in packages_to_install: package_spec = f"{package}=={version}" if version else package package_spec = name_to_command[package_spec] if package_spec in name_to_command else package_spec if not is_pkg_installed(package): subprocess.call(f"pip install {package_spec}".split()) else: print(f"{package_spec} is already installed.") #print(os.getenv('SYSTEM')) ''' if os.getenv('SYSTEM') == 'spaces': subprocess.call('pip install tensorflow==2.9'.split()) subprocess.call('pip install keras==2.9'.split()) subprocess.call('pip install git+https://github.com/facebookresearch/segment-anything.git'.split()) subprocess.call('pip install opencv-python-headless==4.5.5.64'.split()) subprocess.call('pip install git+https://github.com/cocodataset/panopticapi.git'.split()) subprocess.call('pip install python-dotenv'.split()) subprocess.call('pip install torch torchvision '.split()) subprocess.call('pip install xplique'.split()) ''' def is_pkg_installed(pkg_name): try: importlib.metadata.distribution(pkg_name) return True except importlib.metadata.PackageNotFoundError: return False