Upload folder using huggingface_hub
Browse files
Apple-Stock-Price-Forecasting-LSTM-Model/inference.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
!pip install yfinance --quiet
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import joblib
|
| 6 |
+
import numpy as np
|
| 7 |
+
import yfinance as yf
|
| 8 |
+
import pandas as pd
|
| 9 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 10 |
+
|
| 11 |
+
def predict_apple_stock(hf_token, days=7):
|
| 12 |
+
"""
|
| 13 |
+
AAPL Stock Price Prediction using LSTM
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
hf_token: Your Hugging Face token
|
| 17 |
+
days: Number of days to predict (default: 7)
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
DataFrame with predictions
|
| 21 |
+
"""
|
| 22 |
+
# Download model and scaler
|
| 23 |
+
model_path = hf_hub_download(
|
| 24 |
+
repo_id="EsferSami/DataSynthis_ML_JobTask",
|
| 25 |
+
filename="Apple-Stock-Price-Forecasting-LSTM-Model/apple_stock_lstm.h5",
|
| 26 |
+
token=hf_token
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
scaler_path = hf_hub_download(
|
| 30 |
+
repo_id="EsferSami/DataSynthis_ML_JobTask",
|
| 31 |
+
filename="Apple-Stock-Price-Forecasting-LSTM-Model/scaler.joblib",
|
| 32 |
+
token=hf_token
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Load model and scaler
|
| 36 |
+
model = tf.keras.models.load_model(model_path)
|
| 37 |
+
scaler = joblib.load(scaler_path)
|
| 38 |
+
print("Model loaded successfully!")
|
| 39 |
+
|
| 40 |
+
# Get recent data
|
| 41 |
+
data = yf.download("AAPL", period="3mo", auto_adjust=False)
|
| 42 |
+
recent_prices = data['Adj Close'].values
|
| 43 |
+
|
| 44 |
+
# Prepare last 60 days
|
| 45 |
+
last_60_days = recent_prices[-60:].reshape(-1, 1)
|
| 46 |
+
last_60_days_scaled = scaler.transform(last_60_days)
|
| 47 |
+
|
| 48 |
+
# Make predictions
|
| 49 |
+
predictions = []
|
| 50 |
+
current_sequence = last_60_days_scaled.copy()
|
| 51 |
+
|
| 52 |
+
for day in range(days):
|
| 53 |
+
input_data = current_sequence.reshape(1, 60, 1)
|
| 54 |
+
predicted_scaled = model.predict(input_data, verbose=0)
|
| 55 |
+
predicted_price = scaler.inverse_transform(predicted_scaled)[0][0]
|
| 56 |
+
predictions.append(predicted_price)
|
| 57 |
+
|
| 58 |
+
predicted_scaled_reshaped = predicted_scaled.reshape(1, 1)
|
| 59 |
+
current_sequence = np.append(current_sequence[1:], predicted_scaled_reshaped, axis=0)
|
| 60 |
+
|
| 61 |
+
# Create results
|
| 62 |
+
prediction_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=days)
|
| 63 |
+
results_df = pd.DataFrame({
|
| 64 |
+
'Date': prediction_dates,
|
| 65 |
+
'Predicted_Price': predictions
|
| 66 |
+
})
|
| 67 |
+
|
| 68 |
+
print(f"\nAAPL Stock Price - {days}-Day Forecast")
|
| 69 |
+
print("=" * 40)
|
| 70 |
+
print(results_df.to_string(index=False))
|
| 71 |
+
|
| 72 |
+
current_price = float(recent_prices[-1])
|
| 73 |
+
print(f"\nCurrent Price: ${current_price:.2f}")
|
| 74 |
+
print(f"Prediction Trend: {'UP ↗' if predictions[-1] > current_price else 'DOWN ↘'}")
|
| 75 |
+
|
| 76 |
+
return results_df
|
| 77 |
+
|
| 78 |
+
# Example usage:
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
# Replace with your HF token
|
| 81 |
+
YOUR_HF_TOKEN = "your_huggingface_token_here"
|
| 82 |
+
|
| 83 |
+
# Get 7-day prediction
|
| 84 |
+
predictions = predict_apple_stock(YOUR_HF_TOKEN, days=7)
|
Apple-Stock-Price-Forecasting-LSTM-Model/lstm_model/apple_stock_lstm.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:246fb15049f05699372843c6d8db55d95dd97fe1d09f82ca3e8ab90981d4c079
|
| 3 |
+
size 661264
|
Apple-Stock-Price-Forecasting-LSTM-Model/lstm_model/scaler.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:307a16526b5c9260d34cdcdeff7c50269403ff32196e119268e2d8409158bcbf
|
| 3 |
+
size 719
|