sbasu2512 commited on
Commit
d94d5cf
·
1 Parent(s): b52a149

Release model versions in onnx

Browse files
.gitignore CHANGED
@@ -19,7 +19,6 @@ sentiment_env/
19
  gguf_env/
20
  dataset/
21
  export_model_script/
22
- financial_sentiment_analyzer_v1/
23
  # =========================================
24
  # Jupyter Notebook
25
  # =========================================
 
19
  gguf_env/
20
  dataset/
21
  export_model_script/
 
22
  # =========================================
23
  # Jupyter Notebook
24
  # =========================================
README.md CHANGED
@@ -89,6 +89,191 @@ model = AutoModelForSequenceClassification.from_pretrained(
89
  )
90
  ```
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  ## 🧩 Intended Use
93
 
94
  - Real-time sentiment analysis for Indian and global stock market news.
 
89
  )
90
  ```
91
 
92
+ # 🚀 Using the ONNX Model
93
+
94
+ This repository contains an optimized **ONNX Runtime** version of the Financial Sentiment Analyzer for fast CPU and GPU inference.
95
+
96
+ ## Installation
97
+
98
+ ```bash
99
+ pip install onnxruntime optimum transformers
100
+ ```
101
+
102
+ For NVIDIA GPU inference:
103
+
104
+ ```bash
105
+ pip install onnxruntime-gpu optimum transformers
106
+ ```
107
+
108
+ ---
109
+
110
+ ## Download the Model
111
+
112
+ Clone the repository:
113
+
114
+ ```bash
115
+ git clone https://huggingface.co/sbasu2512/financial_sentiment_model
116
+ ```
117
+
118
+ or download the model directly from Hugging Face:
119
+
120
+ ```python
121
+ from optimum.onnxruntime import ORTModelForSequenceClassification
122
+ from transformers import AutoTokenizer
123
+
124
+ MODEL_NAME = "sbasu2512/financial_sentiment_analyzer_v2"
125
+
126
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
127
+ model = ORTModelForSequenceClassification.from_pretrained(MODEL_NAME)
128
+ ```
129
+
130
+ ---
131
+
132
+ # Local Usage
133
+
134
+ ```python
135
+ from optimum.onnxruntime import ORTModelForSequenceClassification
136
+ from transformers import AutoTokenizer
137
+
138
+ MODEL_PATH = "./financial_sentiment_analyzer_v2"
139
+
140
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
141
+ model = ORTModelForSequenceClassification.from_pretrained(MODEL_PATH)
142
+ ```
143
+
144
+ ---
145
+
146
+ # Basic Inference
147
+
148
+ ```python
149
+ import torch
150
+ from optimum.onnxruntime import ORTModelForSequenceClassification
151
+ from transformers import AutoTokenizer
152
+
153
+ MODEL_PATH = "./financial_sentiment_analyzer_v2"
154
+
155
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
156
+ model = ORTModelForSequenceClassification.from_pretrained(MODEL_PATH)
157
+
158
+ text = """
159
+ Reliance Industries reported record quarterly profits,
160
+ beating analyst expectations.
161
+ """
162
+
163
+ inputs = tokenizer(
164
+ text,
165
+ return_tensors="pt",
166
+ truncation=True,
167
+ max_length=512,
168
+ )
169
+
170
+ outputs = model(**inputs)
171
+
172
+ prediction = torch.argmax(outputs.logits, dim=1).item()
173
+
174
+ labels = {
175
+ 0: "Negative",
176
+ 1: "Neutral",
177
+ 2: "Positive",
178
+ }
179
+
180
+ print(labels[prediction])
181
+ ```
182
+
183
+ Example output:
184
+
185
+ ```
186
+ Positive
187
+ ```
188
+
189
+ ---
190
+
191
+ # Confidence Scores
192
+
193
+ ```python
194
+ import torch
195
+
196
+ probabilities = torch.softmax(outputs.logits, dim=1)[0]
197
+
198
+ labels = ["Negative", "Neutral", "Positive"]
199
+
200
+ for label, probability in zip(labels, probabilities):
201
+ print(f"{label}: {probability:.4f}")
202
+ ```
203
+
204
+ Example output:
205
+
206
+ ```
207
+ Negative : 0.0124
208
+ Neutral : 0.0836
209
+ Positive : 0.9040
210
+ ```
211
+
212
+ ---
213
+
214
+ # Predict Multiple Headlines
215
+
216
+ ```python
217
+ headlines = [
218
+ "Tata Motors reports record EV sales.",
219
+ "Markets remained largely unchanged today.",
220
+ "Company files for bankruptcy protection.",
221
+ ]
222
+
223
+ inputs = tokenizer(
224
+ headlines,
225
+ padding=True,
226
+ truncation=True,
227
+ max_length=512,
228
+ return_tensors="pt",
229
+ )
230
+
231
+ outputs = model(**inputs)
232
+
233
+ predictions = torch.argmax(outputs.logits, dim=1)
234
+
235
+ labels = ["Negative", "Neutral", "Positive"]
236
+
237
+ for headline, pred in zip(headlines, predictions):
238
+ print(f"{headline}\n→ {labels[pred.item()]}\n")
239
+ ```
240
+
241
+ Example output:
242
+
243
+ ```
244
+ Tata Motors reports record EV sales.
245
+ → Positive
246
+
247
+ Markets remained largely unchanged today.
248
+ → Neutral
249
+
250
+ Company files for bankruptcy protection.
251
+ → Negative
252
+ ```
253
+
254
+ ---
255
+
256
+ # Output Labels
257
+
258
+ | ID | Sentiment |
259
+ |---:|------------|
260
+ | 0 | Negative |
261
+ | 1 | Neutral |
262
+ | 2 | Positive |
263
+
264
+ ---
265
+
266
+ # Performance
267
+
268
+ The ONNX version provides significantly faster inference than the original PyTorch model while maintaining identical predictions. It is suitable for:
269
+
270
+ - Real-time news sentiment analysis
271
+ - Trading pipelines
272
+ - Financial research
273
+ - Batch inference
274
+ - REST APIs
275
+ - Production deployment
276
+
277
  ## 🧩 Intended Use
278
 
279
  - Real-time sentiment analysis for Indian and global stock market news.
model_exports/financial_sentiment_analyzer_v1.0.0/config.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BertForSequenceClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "classifier_dropout": null,
7
+ "dtype": "float32",
8
+ "gradient_checkpointing": false,
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 768,
12
+ "id2label": {
13
+ "0": "negative",
14
+ "1": "neutral",
15
+ "2": "positive"
16
+ },
17
+ "initializer_range": 0.02,
18
+ "intermediate_size": 3072,
19
+ "label2id": {
20
+ "negative": 0,
21
+ "neutral": 1,
22
+ "positive": 2
23
+ },
24
+ "layer_norm_eps": 1e-12,
25
+ "max_position_embeddings": 512,
26
+ "model_type": "bert",
27
+ "num_attention_heads": 12,
28
+ "num_hidden_layers": 12,
29
+ "pad_token_id": 0,
30
+ "position_embedding_type": "absolute",
31
+ "problem_type": "single_label_classification",
32
+ "transformers_version": "4.57.6",
33
+ "type_vocab_size": 2,
34
+ "use_cache": false,
35
+ "vocab_size": 30522
36
+ }
model_exports/financial_sentiment_analyzer_v1.0.0/model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ba99fd8667d0fb95e297a4a41e37477bd073aaa27c546492e233868e9d0245a6
3
+ size 438148594
model_exports/financial_sentiment_analyzer_v1.0.0/special_tokens_map.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": {
3
+ "content": "[CLS]",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "mask_token": {
10
+ "content": "[MASK]",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": {
17
+ "content": "[PAD]",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "sep_token": {
24
+ "content": "[SEP]",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "unk_token": {
31
+ "content": "[UNK]",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ }
37
+ }
model_exports/financial_sentiment_analyzer_v1.0.0/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
model_exports/financial_sentiment_analyzer_v1.0.0/tokenizer_config.json ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "100": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "101": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "102": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "103": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "backend": "tokenizers",
45
+ "clean_up_tokenization_spaces": false,
46
+ "cls_token": "[CLS]",
47
+ "do_lower_case": true,
48
+ "extra_special_tokens": {},
49
+ "is_local": true,
50
+ "local_files_only": false,
51
+ "mask_token": "[MASK]",
52
+ "max_length": 256,
53
+ "model_max_length": 512,
54
+ "pad_to_multiple_of": null,
55
+ "pad_token": "[PAD]",
56
+ "pad_token_type_id": 0,
57
+ "padding_side": "right",
58
+ "sep_token": "[SEP]",
59
+ "stride": 0,
60
+ "strip_accents": null,
61
+ "tokenize_chinese_chars": true,
62
+ "tokenizer_class": "BertTokenizer",
63
+ "truncation_side": "right",
64
+ "truncation_strategy": "longest_first",
65
+ "unk_token": "[UNK]"
66
+ }
model_exports/financial_sentiment_analyzer_v1.0.0/vocab.txt ADDED
The diff for this file is too large to render. See raw diff
 
model_exports/financial_sentiment_analyzer_v2.0.0/config.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BertForSequenceClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "classifier_dropout": null,
7
+ "dtype": "float32",
8
+ "gradient_checkpointing": false,
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 768,
12
+ "id2label": {
13
+ "0": "negative",
14
+ "1": "neutral",
15
+ "2": "positive"
16
+ },
17
+ "initializer_range": 0.02,
18
+ "intermediate_size": 3072,
19
+ "label2id": {
20
+ "negative": 0,
21
+ "neutral": 1,
22
+ "positive": 2
23
+ },
24
+ "layer_norm_eps": 1e-12,
25
+ "max_position_embeddings": 512,
26
+ "model_type": "bert",
27
+ "num_attention_heads": 12,
28
+ "num_hidden_layers": 12,
29
+ "pad_token_id": 0,
30
+ "position_embedding_type": "absolute",
31
+ "problem_type": "single_label_classification",
32
+ "transformers_version": "4.57.6",
33
+ "type_vocab_size": 2,
34
+ "use_cache": false,
35
+ "vocab_size": 30522
36
+ }
model_exports/financial_sentiment_analyzer_v2.0.0/model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a5201f5f5d769bc38a2b3f2f430e584458361701a245ea1a499bc6a10a7d197c
3
+ size 438148594
model_exports/financial_sentiment_analyzer_v2.0.0/special_tokens_map.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": {
3
+ "content": "[CLS]",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "mask_token": {
10
+ "content": "[MASK]",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": {
17
+ "content": "[PAD]",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "sep_token": {
24
+ "content": "[SEP]",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "unk_token": {
31
+ "content": "[UNK]",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ }
37
+ }
model_exports/financial_sentiment_analyzer_v2.0.0/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
model_exports/financial_sentiment_analyzer_v2.0.0/tokenizer_config.json ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "100": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "101": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "102": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "103": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "backend": "tokenizers",
45
+ "clean_up_tokenization_spaces": false,
46
+ "cls_token": "[CLS]",
47
+ "do_lower_case": true,
48
+ "extra_special_tokens": {},
49
+ "is_local": true,
50
+ "local_files_only": false,
51
+ "mask_token": "[MASK]",
52
+ "max_length": 256,
53
+ "model_max_length": 512,
54
+ "pad_to_multiple_of": null,
55
+ "pad_token": "[PAD]",
56
+ "pad_token_type_id": 0,
57
+ "padding_side": "right",
58
+ "sep_token": "[SEP]",
59
+ "stride": 0,
60
+ "strip_accents": null,
61
+ "tokenize_chinese_chars": true,
62
+ "tokenizer_class": "BertTokenizer",
63
+ "truncation_side": "right",
64
+ "truncation_strategy": "longest_first",
65
+ "unk_token": "[UNK]"
66
+ }
model_exports/financial_sentiment_analyzer_v2.0.0/vocab.txt ADDED
The diff for this file is too large to render. See raw diff