A simple Python tool for converting Hugging Face NLP models into Apple's Core ML format (.mlmodel or .mlpackage) for seamless integration into iOS/macOS apps, running inference on device.
This converter allows you to quickly transform pre-trained Hugging Face models (such as GPT-2 for text generation) into Core ML models suitable for on-device inference on Apple devices.
Check out our project roadmap for upcoming features and development plans. Interested in contributing to any of these features? We welcome your help! See the Contributing section below or open an issue to discuss your ideas.
- Python 3.12+
- uv for dependency management
Clone and initialize the project:
git clone <repo_link>
cd mlmodel-converter
uv installuv run python main.pyThe CLI will prompt you to enter the following parameters:
model_name: The Hugging Face model name to convert (e.g., 'sarahai/your-inspiration')file_extension: The target file extension to use for the output Core ML model (mlmodel or mlpackage)output_filename: The output Core ML model filename (without .mlmodel or .mlpackage extension)
The tool now supports fine-tuning models using Low-Rank Adaptation (LoRA) before converting them to CoreML format. This is particularly useful for customizing models for specific tasks while keeping them small enough for mobile deployment.
uv run python lora_fine_tune.py --model_id distilgpt2 --num_train_epochs 3 --batch_size 4--model_id: The Hugging Face model ID to fine-tune (default: 'distilgpt2')--output_dir: Directory to save training results (default: './results')--merged_model_dir: Directory to save the merged model (default: './merged_model')--coreml_model_path: Path to save the CoreML model (default: './Motivator.mlmodel')--num_train_epochs: Number of training epochs (default: 3)--batch_size: Training batch size (default: 4)--lora_r: LoRA attention dimension (default: 8)--lora_alpha: LoRA alpha parameter (default: 16)--lora_dropout: LoRA dropout rate (default: 0.05)--max_length: Maximum sequence length (default: 64)
The script will:
- Load the specified model
- Fine-tune it on a dataset of motivational quotes using LoRA
- Merge the LoRA weights with the base model
- Convert the fine-tuned model to CoreML format
- Test the converted model with a sample input
This feature is optimized for Apple Silicon (M-series chips) and produces models ready for iOS deployment.
This generates a .mlmodel or .mlpackage file in the project directory (e.g., <output_filename>.mlmodel or <output_filename>.mlpackage) ready for integration into Xcode.
Drag the generated .mlmodel file into your Xcode project’s file navigator. Xcode automatically generates a Swift class to interact with the model.
Contributions are welcome! Please open an issue or submit a pull request.
