updated usage code in README.md
Browse files
README.md
CHANGED
|
@@ -150,6 +150,28 @@ During fine-tuning, the following hyperparameters were used to optimize model pe
|
|
| 150 |
---
|
| 151 |
## Usage
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
```python
|
| 154 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 155 |
import torch
|
|
@@ -168,6 +190,7 @@ print(f"Text : {text}")
|
|
| 168 |
print(f"Sentiment: {label_map[predicted_class]}")
|
| 169 |
|
| 170 |
```
|
|
|
|
| 171 |
---
|
| 172 |
## Acknowledgements
|
| 173 |
|
|
|
|
| 150 |
---
|
| 151 |
## Usage
|
| 152 |
|
| 153 |
+
### Pipeline Approach
|
| 154 |
+
```python
|
| 155 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 156 |
+
import torch
|
| 157 |
+
|
| 158 |
+
model_name = "project-aps/finbert-finetune"
|
| 159 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 160 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 161 |
+
|
| 162 |
+
# Override the config's id2label and label2id
|
| 163 |
+
label_map = {0: "neutral", 1: "negative", 2: "positive"}
|
| 164 |
+
model.config.id2label = label_map
|
| 165 |
+
model.config.label2id = {v: k for k, v in label_map.items()}
|
| 166 |
+
|
| 167 |
+
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 168 |
+
|
| 169 |
+
text = "Earnings smashed expectations AAPL posts $0.89 EPS vs $0.78 est. Bullish momentum incoming! #EarningsSeason"
|
| 170 |
+
print(pipe(text)) #Output: [{'label': 'positive', 'score': 0.9997484087944031}]
|
| 171 |
+
|
| 172 |
+
```
|
| 173 |
+
|
| 174 |
+
### Simple Approach
|
| 175 |
```python
|
| 176 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 177 |
import torch
|
|
|
|
| 190 |
print(f"Sentiment: {label_map[predicted_class]}")
|
| 191 |
|
| 192 |
```
|
| 193 |
+
|
| 194 |
---
|
| 195 |
## Acknowledgements
|
| 196 |
|