Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle an app that use MLX VLM with pyinstaller #201

Closed
thoddnn opened this issue Feb 6, 2025 · 1 comment
Closed

Bundle an app that use MLX VLM with pyinstaller #201

thoddnn opened this issue Feb 6, 2025 · 1 comment

Comments

@thoddnn
Copy link

thoddnn commented Feb 6, 2025

I'm trying to bundle this simple Python app using pyinstaller

from flask import Flask, request, jsonify
from flask_cors import CORS
from flask import Response

from mlx_vlm import load as load_vlm, generate as generate_vlm
from mlx_vlm.prompt_utils import apply_chat_template
from mlx_vlm.utils import load_config

import mlx.core as mx
from mlx_vlm import load, generate
from mlx_vlm.prompt_utils import apply_chat_template
from mlx_vlm.utils import load_config


app = Flask(__name__)
CORS(app)

def main():

    # Load the model
    model_path = "mlx-community/Qwen2-VL-2B-Instruct-8bit" 
    model, processor = load(model_path)
    config = load_config(model_path)

    # Prepare input
    image = ["http://images.cocodataset.org/val2017/000000039769.jpg"]
    prompt = "Describe this image."

    # Apply chat template
    formatted_prompt = apply_chat_template(
        processor, config, prompt, num_images=len(image)
    )

    # Generate output
    output = generate(model, processor, formatted_prompt, image, verbose=False,  max_tokens=40, temp=0)
    print(output)

if __name__ == "__main__":
    main()
    app.run(debug=False, port=5001, host='0.0.0.0')


with this .spec file

# -*- mode: python ; coding: utf-8 -*-


a = Analysis(
    ['test.py'],
    pathex=[],
    binaries=[
        ('venv/lib/python3.11/site-packages/mlx/lib/mlx.metallib', 'mlx'),
        ('venv/lib/python3.11/site-packages/mlx/lib/libmlx.dylib', 'mlx')
    ],
    datas=[
        ('venv/lib/python3.11/site-packages/mlx', './mlx'),
        ('venv/lib/python3.11/site-packages/mlx_lm', './mlx_lm'),
        ('venv/lib/python3.11/site-packages/mlx_vlm', './mlx_vlm'),
    ],
    hiddenimports=["mlx", "mlx._reprlib_fix","mlx._os_warning","mlx_vlm", "mlx_lm", "cv2"],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.datas,
    [],
    name='test',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

But I'm running through the following error and I don't really know how to solve this.

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    from mlx_vlm import load as load_vlm, generate as generate_vlm
  File "PyInstaller/loader/pyimod02_importers.py", line 384, in exec_module
  File "mlx_vlm/__init__.py", line 2, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 384, in exec_module
  File "mlx_vlm/utils.py", line 14, in <module>
ImportError: dlopen(/var/folders/9c/2h7fdr5d1t7c_683ggm09d4m0000gn/T/_MEIzKUAdu/mlx/core.cpython-311-darwin.so, 0x0002): Symbol not found: __ZN3mlx4core10as_stridedENS0_5arrayENSt3__16vectorIiNS2_9allocatorIiEEEENS3_IxNS4_IxEEEEmNS2_7variantIJNS2_9monostateENS0_6StreamENS0_6DeviceEEEE
  Referenced from: <5F7C5896-A1A9-38F8-AE41-B6473D4651F5> /private/var/folders/9c/2h7fdr5d1t7c_683ggm09d4m0000gn/T/_MEIzKUAdu/mlx/core.cpython-311-darwin.so (built for macOS 14.1 which is newer than running OS)
  Expected in:     <538F1A67-EF07-35A4-88C5-B273BE7CD612> /private/var/folders/9c/2h7fdr5d1t7c_683ggm09d4m0000gn/T/_MEIzKUAdu/libmlx.dylib
[PYI-37963:ERROR] Failed to execute script 'test' due to unhandled exception!

Has someone already managed to bundle an app that uses mlx-vlm using pyinstaller ?

Thanks !

@thoddnn
Copy link
Author

thoddnn commented Feb 9, 2025

It was a conflict issue with conda and venv . Closing the issue

@thoddnn thoddnn closed this as completed Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant