File size: 1,147 Bytes
ee9bf22
 
 
 
 
 
 
bc90a93
f9f6184
ee9bf22
 
f9f6184
 
ee9bf22
4a25498
ee9bf22
 
 
bc90a93
f9f6184
4a25498
ee9bf22
4a25498
 
ee9bf22
 
4a25498
 
 
bc90a93
 
 
 
 
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
import torch
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from datasets import load_dataset

def predict_btc():
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    dataset = load_dataset("KavinduHansaka/btc-minute-data")
    df = dataset["train"].to_pandas()

    df["price"] = df["price"].astype(float)
    prices = df["price"].values.reshape(-1, 1)

    # Normalize Data
    scaler = MinMaxScaler(feature_range=(0, 1))
    prices_scaled = scaler.fit_transform(prices)

    # Ensure input shape is exactly (1, 60, 1)
    input_seq = np.array(prices_scaled[-60:]).reshape(1, 60, 1)
    input_tensor = torch.tensor(input_seq, dtype=torch.float32).to(device)

    # Load optimized model
    model = torch.jit.load("btc_lstm_optimized.pth").to(device)
    model.eval()

    # Predict BTC price
    with torch.no_grad():
        predicted_scaled = model(input_tensor).cpu().numpy()

    # Ensure output shape is exactly (15,)
    predicted_prices = scaler.inverse_transform(predicted_scaled.reshape(-1, 1)).flatten()

    return f"Next 15 min BTC Prices: {predicted_prices.tolist()}"