Commit
·
feb2463
1
Parent(s):
0eea681
config.json inference.py
Browse files- config.json +20 -0
- inference.py +39 -0
config.json
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"BertModel"
|
4 |
+
],
|
5 |
+
"attention_probs_dropout_prob": 0.1,
|
6 |
+
"hidden_act": "gelu",
|
7 |
+
"hidden_size": 312,
|
8 |
+
"initializer_range": 0.02,
|
9 |
+
"intermediate_size": 1200,
|
10 |
+
"max_position_embeddings": 512,
|
11 |
+
"num_attention_heads": 12,
|
12 |
+
"num_hidden_layers": 4,
|
13 |
+
"type_vocab_size": 2,
|
14 |
+
"vocab_size": 30522,
|
15 |
+
"model_type": "bert",
|
16 |
+
"pad_token_id": 0,
|
17 |
+
"position_embedding_type": "absolute",
|
18 |
+
"layer_norm_eps": 1e-12,
|
19 |
+
"use_cache": true
|
20 |
+
}
|
inference.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoTokenizer
|
3 |
+
from fin_tinybert_pytorch import TinyFinBERTRegressor # You may need to rename or include this class here
|
4 |
+
|
5 |
+
# Load model
|
6 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
7 |
+
model = TinyFinBERTRegressor()
|
8 |
+
model.load_state_dict(torch.load("./saved_model/pytorch_model.bin", map_location=device))
|
9 |
+
model.to(device)
|
10 |
+
model.eval()
|
11 |
+
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained("./saved_model")
|
13 |
+
|
14 |
+
def predict(texts):
|
15 |
+
if isinstance(texts, str):
|
16 |
+
texts = [texts]
|
17 |
+
|
18 |
+
results = []
|
19 |
+
for text in texts:
|
20 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding='max_length', max_length=128)
|
21 |
+
inputs = {k: v.to(device) for k, v in inputs.items() if k != "token_type_ids"}
|
22 |
+
with torch.no_grad():
|
23 |
+
score = model(**inputs)["score"].item()
|
24 |
+
sentiment = "positive" if score > 0.3 else "negative" if score < -0.3 else "neutral"
|
25 |
+
results.append({"text": text, "score": score, "sentiment": sentiment})
|
26 |
+
return results
|
27 |
+
#
|
28 |
+
# if __name__ == "__main__":
|
29 |
+
# texts = [
|
30 |
+
# "The stock price soared after the earnings report.",
|
31 |
+
# "The company reported significant losses this quarter.",
|
32 |
+
# "There was no noticeable change in performance."
|
33 |
+
# ]
|
34 |
+
#
|
35 |
+
# predictions = predict(texts)
|
36 |
+
# for pred in predictions:
|
37 |
+
# print(f"Text: {pred['text']}")
|
38 |
+
# print(f"Score: {pred['score']:.3f}")
|
39 |
+
# print(f"Sentiment: {pred['sentiment']}\n")
|