Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +61 -0
- scaler.pkl +3 -0
- stock_model.h5 +3 -0
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
from tensorflow.keras.metrics import MeanAbsoluteError
|
| 7 |
+
from tensorflow.keras.losses import MeanSquaredError
|
| 8 |
+
|
| 9 |
+
model = load_model("stock_model.h5", custom_objects={'mae': MeanAbsoluteError(), 'mse': MeanSquaredError()})
|
| 10 |
+
scaler = joblib.load("scaler.pkl")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
FEATURES = ['open', 'high', 'low', 'close', 'volume', 'MA5', 'MA10', 'MA20']
|
| 14 |
+
|
| 15 |
+
def predict_next_day(open_price, high_price, low_price, close_price, volume, ma5, ma10, ma20):
|
| 16 |
+
"""
|
| 17 |
+
Predicts the next day's closing price using the trained LSTM model.
|
| 18 |
+
"""
|
| 19 |
+
dummy_df = pd.DataFrame([[open_price, high_price, low_price, close_price, volume, ma5, ma10, ma20]],
|
| 20 |
+
columns=FEATURES)
|
| 21 |
+
|
| 22 |
+
scaled_input = scaler.transform(dummy_df)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
reshaped_input = scaled_input.reshape(1, 1, len(FEATURES))
|
| 26 |
+
|
| 27 |
+
scaled_prediction = model.predict(reshaped_input)[0][0]
|
| 28 |
+
|
| 29 |
+
# To get the actual price, we need to inverse transform the prediction.
|
| 30 |
+
# The scaler was trained on all features, so we need to create a dummy
|
| 31 |
+
# array with zeros for all other features. The 'close' price is at index 3.
|
| 32 |
+
# The inverse_close function from the original notebook is implemented here.
|
| 33 |
+
def inverse_close(scaled_value, close_index=3, total_features=len(FEATURES)):
|
| 34 |
+
dummy = np.zeros((1, total_features))
|
| 35 |
+
dummy[:, close_index] = scaled_value
|
| 36 |
+
return scaler.inverse_transform(dummy)[:, close_index]
|
| 37 |
+
|
| 38 |
+
# Inverse transform the scaled prediction to get the real price
|
| 39 |
+
predicted_price = inverse_close(scaled_prediction)[0]
|
| 40 |
+
|
| 41 |
+
return f"${predicted_price:.2f}"
|
| 42 |
+
|
| 43 |
+
# Create the Gradio interface
|
| 44 |
+
iface = gr.Interface(
|
| 45 |
+
fn=predict_next_day,
|
| 46 |
+
inputs=[
|
| 47 |
+
gr.Number(label="Open Price", value=150.0),
|
| 48 |
+
gr.Number(label="High Price", value=155.0),
|
| 49 |
+
gr.Number(label="Low Price", value=149.0),
|
| 50 |
+
gr.Number(label="Close Price", value=152.0),
|
| 51 |
+
gr.Number(label="Volume", value=1000000),
|
| 52 |
+
gr.Number(label="5-Day MA", value=151.5),
|
| 53 |
+
gr.Number(label="10-Day MA", value=151.0),
|
| 54 |
+
gr.Number(label="20-Day MA", value=150.5),
|
| 55 |
+
],
|
| 56 |
+
outputs=gr.Textbox(label="Predicted Next Day's Close Price"),
|
| 57 |
+
title="Stock Price Prediction",
|
| 58 |
+
description="Enter today's stock data to predict tomorrow's closing price."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
iface.launch(debug=True)
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3db8e1f6408ec42285079039a7eb44ee8b555a936088eac3e9bffce2728689fe
|
| 3 |
+
size 983
|
stock_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2627cfa3926dea2d334cd66e3f4491ca09c0e422795dc6d0766c0b65bced4866
|
| 3 |
+
size 548424
|