Spaces:
Sleeping
Sleeping
Add files via upload
Browse files
backend/reproduce_sentiment.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import BertTokenizer, BertForSequenceClassification, AutoConfig
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
MODEL_NAME = "ProsusAI/finbert"
|
| 7 |
+
|
| 8 |
+
print(f"Loading {MODEL_NAME}...")
|
| 9 |
+
tokenizer = BertTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
+
model = BertForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 11 |
+
config = AutoConfig.from_pretrained(MODEL_NAME)
|
| 12 |
+
|
| 13 |
+
print("\n--- Label Mapping ---")
|
| 14 |
+
print(f"id2label: {config.id2label}")
|
| 15 |
+
print(f"label2id: {config.label2id}")
|
| 16 |
+
|
| 17 |
+
# The code in sentiment.py uses: FINBERT_LABELS = ["negative", "neutral", "positive"]
|
| 18 |
+
# Let's see if that matches id2label.
|
| 19 |
+
|
| 20 |
+
headlines = [
|
| 21 |
+
"RBI approves ICICI Group stake hike in eight banks",
|
| 22 |
+
"This Emerging Markets ETF Charges Just 0.07% and Ran Way Past The S&P 500",
|
| 23 |
+
"Asian Equities Traded in the US as American Depositary Receipts Edge Higher in Thursday Trading"
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
print("\n--- Testing Headlines ---")
|
| 27 |
+
for text in headlines:
|
| 28 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
logits = model(**inputs).logits
|
| 31 |
+
|
| 32 |
+
probs = torch.nn.functional.softmax(logits, dim=-1).squeeze().tolist()
|
| 33 |
+
|
| 34 |
+
print(f"\nHeadline: {text}")
|
| 35 |
+
print(f"Raw Probabilities: {probs}")
|
| 36 |
+
|
| 37 |
+
# Current logic in sentiment.py
|
| 38 |
+
# FINBERT_LABELS = ["negative", "neutral", "positive"]
|
| 39 |
+
# signed_score = probs[2] - probs[0]
|
| 40 |
+
|
| 41 |
+
# Check what the model actually thinks
|
| 42 |
+
predicted_id = int(np.argmax(probs))
|
| 43 |
+
actual_label = config.id2label[predicted_id]
|
| 44 |
+
print(f"Model Predicted ID: {predicted_id} -> Label: {actual_label}")
|
backend/requirements-enterprise.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Enterprise ML ensemble (40% of prediction blend)
|
| 2 |
+
# Optional: install for full H2O.ai and DataRobot API support
|
| 3 |
+
# Without these, local sklearn equivalents are used automatically
|
| 4 |
+
|
| 5 |
+
h2o>=3.42.0
|
| 6 |
+
datarobot>=3.6.0
|