File size: 1,911 Bytes
67e4c0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
library_name: coreml
tags:
- coreml
- sentiment-analysis
- distilbert
- text-classification
license: apache-2.0
---

# sentiment-analyzer-coreml

This is a CoreML version of the DistilBERT sentiment analysis model, converted from the Hugging Face model `distilbert-base-uncased-finetuned-sst-2-english`.

## Model Details

- **Original Model**: `distilbert-base-uncased-finetuned-sst-2-english`
- **Task**: Sentiment Analysis
- **Framework**: CoreML
- **Input**: Text (tokenized as input_ids and attention_mask)
- **Output**: Logits for sentiment classification (2 classes: negative, positive)

## Usage

### Python (CoreML)

```python
import coremltools as ct

# Load the model
model = ct.models.MLModel("sentiment_analyzer.mlpackage")

# Get model spec
spec = model.get_spec()
print("Model type:", spec.WhichOneof('Type'))

# Make predictions (you'll need to tokenize your input first)
# The model expects input_ids and attention_mask as inputs
```

### Swift (iOS/macOS)

```swift
import CoreML

// Load the model
guard let model = try? MLModel(contentsOf: URL(fileURLWithPath: "sentiment_analyzer.mlpackage")) else { return }

// Make predictions
// You'll need to convert your text to the required input format
```

## Input Format

The model expects two inputs:
- `input_ids`: Tokenized input text (shape: [1, sequence_length])
- `attention_mask`: Attention mask (shape: [1, sequence_length])

## Output Format

The model outputs logits for sentiment classification:
- Shape: [1, 2] (batch_size, num_classes)
- Classes: [negative, positive]

## Conversion Notes

This model was converted using coremltools from the original PyTorch model. The conversion process involved:
1. Loading the Hugging Face model
2. Wrapping it to return only logits (tensor output)
3. Tracing with PyTorch JIT
4. Converting to CoreML format

## Requirements

- iOS 15+ / macOS 12+ (for ML Program format)
- CoreML framework