├── .gitignore ├── LICENSE ├── README.md └── lmstudio_hf.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # UV 98 | # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | #uv.lock 102 | 103 | # poetry 104 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 105 | # This is especially recommended for binary packages to ensure reproducibility, and is more 106 | # commonly ignored for libraries. 107 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 108 | #poetry.lock 109 | 110 | # pdm 111 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 112 | #pdm.lock 113 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 114 | # in version control. 115 | # https://pdm.fming.dev/latest/usage/project/#working-with-version-control 116 | .pdm.toml 117 | .pdm-python 118 | .pdm-build/ 119 | 120 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 121 | __pypackages__/ 122 | 123 | # Celery stuff 124 | celerybeat-schedule 125 | celerybeat.pid 126 | 127 | # SageMath parsed files 128 | *.sage.py 129 | 130 | # Environments 131 | .env 132 | .venv 133 | env/ 134 | venv/ 135 | ENV/ 136 | env.bak/ 137 | venv.bak/ 138 | 139 | # Spyder project settings 140 | .spyderproject 141 | .spyproject 142 | 143 | # Rope project settings 144 | .ropeproject 145 | 146 | # mkdocs documentation 147 | /site 148 | 149 | # mypy 150 | .mypy_cache/ 151 | .dmypy.json 152 | dmypy.json 153 | 154 | # Pyre type checker 155 | .pyre/ 156 | 157 | # pytype static type analyzer 158 | .pytype/ 159 | 160 | # Cython debug symbols 161 | cython_debug/ 162 | 163 | # PyCharm 164 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 165 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 166 | # and can be added to the global gitignore or merged into this file. For a more nuclear 167 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 168 | #.idea/ 169 | 170 | # PyPI configuration file 171 | .pypirc 172 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Ivan Fioravanti 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LM Studio - Hugging Face Model Manager 2 | 3 | A command-line utility to manage MLX models between your Hugging Face cache and LM Studio. This tool makes it easy to import and manage models you've downloaded from Hugging Face into LM Studio. 4 | 5 | ## Features 6 | 7 | - Interactive model selection interface with keyboard navigation 8 | - Automatic detection of MLX models in your Hugging Face cache 9 | - Smart handling of model imports via symbolic links 10 | - Support for model replacement and removal 11 | - Terminal-based UI with scrolling for large model lists 12 | 13 | ## Prerequisites 14 | 15 | - Python 3.x 16 | - LM Studio installed 17 | - Hugging Face models downloaded locally 18 | 19 | ## Installation 20 | 21 | 1. Clone this repository: 22 | ```bash 23 | git clone https://github.com/ivanfioravanti/lmstudio_hf.git 24 | cd lmstudio_hf 25 | ``` 26 | 27 | ## Usage 28 | 29 | Run the script using Python: 30 | 31 | ```bash 32 | python lmstudio_hf.py 33 | ``` 34 | 35 | ### Navigation Controls 36 | 37 | - ↑/↓ arrows: Navigate through the model list 38 | - SPACE: Select/deselect a model 39 | - ENTER: Confirm selection and proceed with import 40 | - Ctrl+C: Cancel operation 41 | 42 | ## How It Works 43 | 44 | 1. The tool scans your Hugging Face cache directory (`~/.cache/huggingface` by default) 45 | 2. Identifies MLX-compatible models (excluding specific types like Whisper, LLaVA, etc.) 46 | 3. Creates symbolic links in the LM Studio models directory (`~/.cache/lm-studio/models`) 47 | 4. Allows for easy management of existing imports 48 | 49 | ## Environment Variables 50 | 51 | - `HF_HOME`: Optional. Set this to customize your Hugging Face cache location. 52 | 53 | ## Notes 54 | 55 | - Models are imported using symbolic links to save disk space 56 | - Already imported models are marked in the selection interface 57 | - Selecting an already imported model will remove it from LM Studio 58 | 59 | ## Contributing 60 | 61 | Feel free to open issues or submit pull requests for any improvements or bug fixes. 62 | 63 | ## License 64 | 65 | [MIT License](LICENSE) -------------------------------------------------------------------------------- /lmstudio_hf.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | from pathlib import Path 4 | import sys 5 | import shutil 6 | 7 | def select_models(model_choices): 8 | selected = [False] * len(model_choices) 9 | idx = 0 10 | window_size = os.get_terminal_size().lines - 5 11 | 12 | while True: 13 | print("\033[H\033[J", end="") 14 | print("❯ lm-studio - Hugging Face Manage models \nAvailable models (↑/↓ to navigate, SPACE to select, ENTER to confirm, Ctrl+C to quit):") 15 | 16 | window_start = max(0, min(idx - window_size + 3, len(model_choices) - window_size)) 17 | window_end = min(window_start + window_size, len(model_choices)) 18 | 19 | for i in range(window_start, window_end): 20 | display_name, _, _, _ = model_choices[i] 21 | print(f"{'>' if i == idx else ' '} {'◉' if selected[i] else '○'} {display_name}") 22 | 23 | key = get_key() 24 | if key == "\x1b[A": # Up arrow 25 | idx = max(0, idx - 1) 26 | elif key == "\x1b[B": # Down arrow 27 | idx = min(len(model_choices) - 1, idx + 1) 28 | elif key == " ": 29 | selected[idx] = not selected[idx] 30 | elif key == "\r": # Enter key 31 | break 32 | elif key == "\x03": # Ctrl+C 33 | print("\nImport is cancelled. Do nothing.") 34 | sys.exit(0) 35 | 36 | return [choice for choice, is_selected in zip(model_choices, selected) if is_selected] 37 | 38 | def get_key(): 39 | """Get a single keypress from the user.""" 40 | import tty, termios 41 | 42 | fd = sys.stdin.fileno() 43 | old_settings = termios.tcgetattr(fd) 44 | try: 45 | tty.setraw(sys.stdin.fileno()) 46 | ch = sys.stdin.read(1) 47 | if ch == "\x1b": 48 | ch += sys.stdin.read(2) 49 | finally: 50 | termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) 51 | return ch 52 | 53 | def manage_models(): 54 | "Import MLX models from the Hugging Face cache." 55 | cache_dir = Path( 56 | os.environ.get("HF_HOME", os.path.expanduser("~/.cache/huggingface")) 57 | ) 58 | lm_studio_dir = Path(os.path.expanduser("~/.cache/lm-studio/models")) 59 | 60 | found_models = set() 61 | for root, dirs, _ in os.walk(cache_dir): 62 | for d in dirs: 63 | if "mlx-community" in d: 64 | model_dir = Path(root) / d 65 | snapshots_dir = model_dir / "snapshots" 66 | if not snapshots_dir.exists(): 67 | continue 68 | 69 | # Search for config.json in any subfolder under snapshots 70 | config_found = False 71 | snapshot_path = None 72 | for config_root, _, config_files in os.walk(snapshots_dir): 73 | if "config.json" in config_files: 74 | config_path = Path(config_root) / "config.json" 75 | try: 76 | with open(config_path) as f: 77 | config = json.load(f) 78 | model_type = config.get("model_type", "").lower() 79 | config_found = True 80 | snapshot_path = Path(config_root) 81 | break 82 | except (json.JSONDecodeError, FileNotFoundError): 83 | continue 84 | 85 | if not config_found or not snapshot_path: 86 | continue 87 | 88 | parts = d.split("--") 89 | model_name = "/".join(parts[1:]) 90 | if model_name: 91 | # Store model_type, model_name, and snapshot_path 92 | found_models.add((model_type, model_name, snapshot_path)) 93 | 94 | if not found_models: 95 | print("No MLX models found in Hugging Face cache") 96 | return 97 | 98 | # Create list of models with their current import status 99 | model_choices = [] 100 | for model_type, model, snapshot_path in sorted(found_models): 101 | target_path = lm_studio_dir / f"{model}" 102 | is_imported = target_path.exists() 103 | status = " (already imported)" if is_imported else "" 104 | display_name = f"({model_type}) {model}{status}" 105 | model_choices.append((display_name, model, is_imported, snapshot_path)) 106 | 107 | # Show interactive selection menu 108 | selected = select_models(model_choices) 109 | print("\nImporting models...\n") 110 | 111 | for display_name, model_name, is_imported, snapshot_path in selected: 112 | target_path = lm_studio_dir / f"{model_name}" 113 | 114 | if is_imported: 115 | # Remove existing directory or symlink 116 | if target_path.is_symlink() or target_path.exists(): 117 | if target_path.is_dir(): 118 | shutil.rmtree(target_path) 119 | else: 120 | target_path.unlink() 121 | print(f"Removed {model_name}") 122 | 123 | else: 124 | # Create parent directories and target directory 125 | target_path.mkdir(parents=True, exist_ok=True) 126 | 127 | # Create symbolic links for all files in the snapshot directory 128 | for item in snapshot_path.iterdir(): 129 | link_path = target_path / item.name 130 | os.symlink(item, link_path) 131 | 132 | print(f"Imported {model_name} (symlinked files)") 133 | 134 | if __name__ == "__main__": 135 | manage_models() 136 | 137 | --------------------------------------------------------------------------------