Location>code7788 >text

Python 3.11 Binary AI Project Program Packaging for Apple Mac App (DMG)-Application pyinstaller Production Process (AppleSilicon)

Popularity:362 ℃/2024-08-26 13:49:22

As we all know, although Apple MacOs system is expensive for the Unix kernel system, but because the system does not support N card, so if you want to run AI projects locally, you also need to customize the relevant AI modules, this time we demonstrate how to package the Python3.11 based AI project program into a DMG installer that can be run directly on MacOS, you can Apple system in the One click to run the AI project.

MacOs Local Deployment of AI Projects

First of all, make sure that the Python 3.11 program for the arm kernel is installed locally, you can download and install it from the Python website:.

Let's take LivePortrait, the famous emoji migration project of Racer Team, as an example. First, clone the official project of Racer Team.

git clone /KwaiVGI/

Go to the project's directory:

cd LivePortrait

Install relevant dependencies for Mac-based systems:

# for macOS with Apple Silicon users  
pip install -r requirements_macOS.txt

The file was then modified to include the environment variable setting at the top of the code:

# coding: utf-8  
  
"""  
The entrance of the gradio for human  
"""  
  
import os  
["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"

PYTORCH_ENABLE_MPS_FALLBACK=1 This environment variable is used for MPS (Metal Performance Shaders) acceleration in PyTorch.

MPS is Apple's graphics processing unit (GPU) framework for macOS and iOS devices that accelerates the training and inference of machine learning models.

PYTORCH_ENABLE_MPS_FALLBACK=1 means MPS fallback is enabled. When PyTorch detects that a device supports MPS, it will prioritize MPS for acceleration. If MPS is not available, it will fallback to the CPU.
In short, setting this environment variable helps PyTorch take advantage of GPU acceleration on devices that support MPS and run properly on devices that do not.

If this variable is not set separately, an error will be reported when reasoning is enabled.

The reasoning page was then launched for testing:

python3 

Note that since xpose doesn't support mps reasoning for the time being, the mac version doesn't support animal emoji drivers, only emoji drivers for characters.

If there is no problem with the reasoning, then the packing operation can begin.

MacOs locally packaged AI projects

First, install the pyinstaller library:

pip3 install -U pyinstaller

Afterwards, create the project configuration file:

# -*- mode: python ; coding: utf-8 -*-  
import sys  
(5000)  
from  import collect_data_files  
  
datas = []  
datas += collect_data_files('gradio_client')  
datas += collect_data_files('gradio')  
  
  
  
a = Analysis(  
    ['',  
      
      
    ],  
    pathex=['/Users/liuyue/Downloads/LivePortrait_For_Mac'],  
    binaries=[],  
    datas=datas,  
    hiddenimports=[],  
    hookspath=[],  
    hooksconfig={},  
    runtime_hooks=[],  
    excludes=[],  
    noarchive=False,  
    optimize=0,  
    module_collection_mode={ 'gradio': 'py'}  
)  
pyz = PYZ()  
  
exe = EXE(  
    pyz,  
    ,  
    [],  
    exclude_binaries=True,  
    name='LivePortrait',  
    icon='AnyConv.com__paints_logo.icns',  
    debug=False,  
    bootloader_ignore_signals=False,  
    strip=False,  
    upx=True,  
    console=True,  
    disable_windowed_traceback=False,  
    argv_emulation=False,  
    target_arch=None,  
    codesign_identity=None,  
    entitlements_file=None,  
)  
  
 += Tree('./pretrained_weights', prefix='pretrained_weights')  
  
  
  
coll = COLLECT(  
    exe,  
    ,  
    ,  
    strip=False,  
    upx=True,  
    upx_exclude=[],  
    name='LivePortrait',  
)

The project's entry files, dependencies, and tripartite directories are declared according to the official pyinstaller documentation.

Then run the pack command.

pyinstaller 

The program returns:

98124 INFO: Rewriting the executable's macOS SDK version (13.1.0) to match the SDK version of the Python library (12.1.0) in order to avoid inconsistent behavior and potential UI issues in the frozen application.  
98125 INFO: Re-signing the EXE  
98243 INFO: Building EXE from  completed successfully.  
98244 INFO: checking Tree  
98244 INFO: Building Tree because  is non existent  
98244 INFO: Building Tree   
98265 INFO: checking COLLECT  
98266 INFO: Building COLLECT because  is non existent  
98266 INFO: Building COLLECT   
108930 INFO: Building COLLECT  completed successfully.

This means that the package was successful and an executable will be generated in the dist directory of the project:

Just double-click the LivePortrait icon to test it.

At this point, the program is packaged.

MacOs build DMG installers locally

Afterwards, run Disk Utility and create a new disk file:

Note that the format must be mac os extended (logged) and the size needs to be larger than 2G.

Then just copy the project files you just packaged to the newly created disk.

Subsequently launch the disk and click on Image - Convert to compress the disk file.

Finally we get a compressed DMG installation file:

➜ mac ll
total 5328720
-rw-r--r--@ 1 liuyue staff 2.5G 8 20 19:49 LivePortrait(converted).dmg

Just double-click the installer on another Mac computer to open and run it.

At this point we have gone through the entire MacOS AI project program production process, and finally, to present the packaged program files, with the folks with the feast:

New LivePortrait Integration Pack (Apple MacOsAppleSilicon) Image Engine /s/53c24cd845b9