Add CoreML sentiment analysis model
Browse files
README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: coreml
|
| 3 |
+
tags:
|
| 4 |
+
- coreml
|
| 5 |
+
- sentiment-analysis
|
| 6 |
+
- distilbert
|
| 7 |
+
- text-classification
|
| 8 |
+
license: apache-2.0
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# sentiment-analyzer-coreml
|
| 12 |
+
|
| 13 |
+
This is a CoreML version of the DistilBERT sentiment analysis model, converted from the Hugging Face model `distilbert-base-uncased-finetuned-sst-2-english`.
|
| 14 |
+
|
| 15 |
+
## Model Details
|
| 16 |
+
|
| 17 |
+
- **Original Model**: `distilbert-base-uncased-finetuned-sst-2-english`
|
| 18 |
+
- **Task**: Sentiment Analysis
|
| 19 |
+
- **Framework**: CoreML
|
| 20 |
+
- **Input**: Text (tokenized as input_ids and attention_mask)
|
| 21 |
+
- **Output**: Logits for sentiment classification (2 classes: negative, positive)
|
| 22 |
+
|
| 23 |
+
## Usage
|
| 24 |
+
|
| 25 |
+
### Python (CoreML)
|
| 26 |
+
|
| 27 |
+
```python
|
| 28 |
+
import coremltools as ct
|
| 29 |
+
|
| 30 |
+
# Load the model
|
| 31 |
+
model = ct.models.MLModel("sentiment_analyzer.mlpackage")
|
| 32 |
+
|
| 33 |
+
# Get model spec
|
| 34 |
+
spec = model.get_spec()
|
| 35 |
+
print("Model type:", spec.WhichOneof('Type'))
|
| 36 |
+
|
| 37 |
+
# Make predictions (you'll need to tokenize your input first)
|
| 38 |
+
# The model expects input_ids and attention_mask as inputs
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
### Swift (iOS/macOS)
|
| 42 |
+
|
| 43 |
+
```swift
|
| 44 |
+
import CoreML
|
| 45 |
+
|
| 46 |
+
// Load the model
|
| 47 |
+
guard let model = try? MLModel(contentsOf: URL(fileURLWithPath: "sentiment_analyzer.mlpackage")) else { return }
|
| 48 |
+
|
| 49 |
+
// Make predictions
|
| 50 |
+
// You'll need to convert your text to the required input format
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
## Input Format
|
| 54 |
+
|
| 55 |
+
The model expects two inputs:
|
| 56 |
+
- `input_ids`: Tokenized input text (shape: [1, sequence_length])
|
| 57 |
+
- `attention_mask`: Attention mask (shape: [1, sequence_length])
|
| 58 |
+
|
| 59 |
+
## Output Format
|
| 60 |
+
|
| 61 |
+
The model outputs logits for sentiment classification:
|
| 62 |
+
- Shape: [1, 2] (batch_size, num_classes)
|
| 63 |
+
- Classes: [negative, positive]
|
| 64 |
+
|
| 65 |
+
## Conversion Notes
|
| 66 |
+
|
| 67 |
+
This model was converted using coremltools from the original PyTorch model. The conversion process involved:
|
| 68 |
+
1. Loading the Hugging Face model
|
| 69 |
+
2. Wrapping it to return only logits (tensor output)
|
| 70 |
+
3. Tracing with PyTorch JIT
|
| 71 |
+
4. Converting to CoreML format
|
| 72 |
+
|
| 73 |
+
## Requirements
|
| 74 |
+
|
| 75 |
+
- iOS 15+ / macOS 12+ (for ML Program format)
|
| 76 |
+
- CoreML framework
|
config.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "coreml",
|
| 3 |
+
"task": "text-classification",
|
| 4 |
+
"library_name": "coreml",
|
| 5 |
+
"tags": [
|
| 6 |
+
"coreml",
|
| 7 |
+
"sentiment-analysis",
|
| 8 |
+
"distilbert",
|
| 9 |
+
"text-classification"
|
| 10 |
+
],
|
| 11 |
+
"pipeline_tag": "text-classification",
|
| 12 |
+
"original_model": "distilbert-base-uncased-finetuned-sst-2-english",
|
| 13 |
+
"framework": "coreml"
|
| 14 |
+
}
|
sentiment_analyzer.mlpackage/Data/com.apple.CoreML/model.mlmodel
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c1aa11aff34987ce108498e4b097433862108ceaeeb5ecfba8c0894c72c18fa8
|
| 3 |
+
size 72807
|
sentiment_analyzer.mlpackage/Data/com.apple.CoreML/weights/weight.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fdda8f2fe4d312a43a583c007a51cb9955e14c28ca7aacb878e2e1e63a58380b
|
| 3 |
+
size 133140992
|
sentiment_analyzer.mlpackage/Manifest.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileFormatVersion": "1.0.0",
|
| 3 |
+
"itemInfoEntries": {
|
| 4 |
+
"02da44c3-3204-4c7f-940c-d0e3b357c7ab": {
|
| 5 |
+
"author": "com.apple.CoreML",
|
| 6 |
+
"description": "CoreML Model Weights",
|
| 7 |
+
"name": "weights",
|
| 8 |
+
"path": "com.apple.CoreML/weights"
|
| 9 |
+
},
|
| 10 |
+
"67433d37-6b4d-4083-96a8-daca42182fc0": {
|
| 11 |
+
"author": "com.apple.CoreML",
|
| 12 |
+
"description": "CoreML Model Specification",
|
| 13 |
+
"name": "model.mlmodel",
|
| 14 |
+
"path": "com.apple.CoreML/model.mlmodel"
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
+
"rootModelIdentifier": "67433d37-6b4d-4083-96a8-daca42182fc0"
|
| 18 |
+
}
|