Spaces:
Build error
Build error
Download predict_live.py from Hamza012bce21/PSX_fyp: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/Hamza012bce21/PSX_fyp/resolve/main/predict_live.py
- Command line
-
hf download hf://spaces/Hamza012bce21/PSX_fyp/predict_live.py
-
curl -L -o predict_live.py https://huggingface.co/spaces/Hamza012bce21/PSX_fyp/resolve/main/predict_live.py
1.06 kB
| import yfinance as yf | |
| import numpy as np | |
| import joblib | |
| from tensorflow.keras.models import load_model | |
| model = load_model("model.h5") | |
| scaler = joblib.load("scaler.pkl") | |
| TICKER = "HBL.KA" | |
| WINDOW_SIZE = 60 | |
| def get_live_data(ticker, days=90): | |
| """Fetch last N days candles from yfinance""" | |
| data = yf.Ticker(ticker).history(period=f"{days}d") | |
| return data["Close"].values | |
| def prepare_features(close_prices): | |
| """Prepare last window as model input""" | |
| scaled = scaler.transform(close_prices.reshape(-1, 1)) | |
| window = scaled[-WINDOW_SIZE:] # last 60 prices | |
| return window.reshape(1, WINDOW_SIZE, 1) | |
| def predict_next_price(ticker): | |
| # latest prices | |
| close_prices = get_live_data(ticker) | |
| # input | |
| X = prepare_features(close_prices) | |
| # Predict | |
| pred_scaled = model.predict(X)[0][0] | |
| # Inverse scale | |
| pred_price = scaler.inverse_transform([[pred_scaled]])[0][0] | |
| return pred_price | |
| pred = predict_next_price(TICKER) | |
| print("Next Price Prediction:", pred) | |