Spaces:
Sleeping
Sleeping
File size: 1,559 Bytes
e2b2661 |
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 |
import joblib
import torch
import numpy as np
from pathlib import Path
from sentiment import compute_sentiment
class MLP(torch.nn.Module):
def __init__(self, n):
super().__init__()
self.net = torch.nn.Sequential(
torch.nn.Linear(n, 32),
torch.nn.ReLU(),
torch.nn.Linear(32, 1)
)
def forward(self, x):
return self.net(x)
def load_models(ticker):
base = Path("models") / ticker
rf = joblib.load(base / "rf.joblib")
sx = joblib.load(base / "scaler_x.joblib")
sy = joblib.load(base / "scaler_y.joblib")
mlp = MLP(3)
mlp.load_state_dict(torch.load(base / "mlp.pth", map_location="cpu"))
mlp.eval()
return rf, mlp, sx, sy
if __name__ == "__main__":
ticker = input("Ticker (AAPL / GOOGL / TSLA): ").upper()
prev_return = float(input("Previous day return: "))
prev_volume = float(input("Previous day volume: "))
text = input("Enter tweet/news sentence: ")
sentiment = compute_sentiment(text)
print(f"Computed sentiment score: {sentiment:.4f}")
rf, mlp, sx, sy = load_models(ticker)
X = np.array([[prev_return, prev_volume, sentiment]])
Xs = sx.transform(X)
rf_pred = sy.inverse_transform(
rf.predict(Xs).reshape(-1, 1)
)[0, 0]
mlp_pred = sy.inverse_transform(
mlp(torch.tensor(Xs, dtype=torch.float32)).detach().numpy()
)[0, 0]
print("\n====== LIVE PREDICTION ======")
print(f"RF predicted return : {rf_pred:.6f}")
print(f"MLP predicted return: {mlp_pred:.6f}")
|