JBond07 commited on
Commit
8dd3411
·
verified ·
1 Parent(s): 476a229

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. Requirements.txt +7 -0
  3. app.py +78 -0
  4. scaler.gz +3 -0
  5. stock_model.keras +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ stock_model.keras filter=lfs diff=lfs merge=lfs -text
Requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ tensorflow
2
+ yfinance
3
+ pandas
4
+ numpy
5
+ scikit-learn
6
+ joblib
7
+ gradio
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import yfinance as yf
3
+ import numpy as np
4
+ import pandas as pd
5
+ from keras.models import load_model
6
+ import joblib
7
+ import os
8
+
9
+ # Load the saved components
10
+ # Using a check to ensure the app doesn't crash during build if files are missing
11
+ MODEL_PATH = 'stock_model.keras'
12
+ SCALER_PATH = 'scaler.gz'
13
+
14
+ if os.path.exists(MODEL_PATH) and os.path.exists(SCALER_PATH):
15
+ model = load_model(MODEL_PATH)
16
+ scaler = joblib.load(SCALER_PATH)
17
+ else:
18
+ model = None
19
+ scaler = None
20
+
21
+ def predict_next_day(ticker):
22
+ if model is None or scaler is None:
23
+ return "Error: Model or Scaler files not found in the repository."
24
+
25
+ try:
26
+ # 1. Fetch data
27
+ # We use multi_level_index=False to fix the common yfinance MultiIndex error
28
+ df = yf.download(ticker, period='90d', interval='1d', multi_level_index=False)
29
+
30
+ if df.empty:
31
+ return f"Error: No data found for ticker '{ticker}'."
32
+
33
+ # 2. Re-create technical indicators
34
+ df['MA7'] = df['Close'].rolling(window=7).mean()
35
+ df['MA21'] = df['Close'].rolling(window=21).mean()
36
+ df.dropna(inplace=True)
37
+
38
+ # 3. Prepare the sliding window (Last 60 days)
39
+ # Ensure we have enough rows after dropping NaNs
40
+ if len(df) < 60:
41
+ return "Error: Not enough historical data to generate a 60-day window."
42
+
43
+ features = df[['Close', 'MA7', 'MA21']].tail(60).values
44
+
45
+ # 4. Scale and Reshape
46
+ scaled_features = scaler.transform(features)
47
+ # Reshape to (1, 60, 3) -> 1 sample, 60 timesteps, 3 features
48
+ input_data = np.reshape(scaled_features, (1, 60, 3))
49
+
50
+ # 5. Predict
51
+ prediction_scaled = model.predict(input_data, verbose=0)
52
+
53
+ # 6. Inverse Transform
54
+ # Since the scaler was fit on 3 columns, we create a dummy with 3 cols
55
+ dummy = np.zeros((1, 3))
56
+ dummy[0, 0] = prediction_scaled[0, 0]
57
+ prediction_final = scaler.inverse_transform(dummy)[0, 0]
58
+
59
+ return f"Predicted Next Closing Price for {ticker}: ₹{prediction_final:.2f}"
60
+
61
+ except Exception as e:
62
+ return f"Technical Error: {str(e)}"
63
+
64
+ # Professional Gradio Interface
65
+ interface = gr.Interface(
66
+ fn=predict_next_day,
67
+ inputs=gr.Textbox(
68
+ label="Stock Ticker",
69
+ placeholder="Enter Ticker (e.g., TCS.NS for NSE, AAPL for NASDAQ)..."
70
+ ),
71
+ outputs=gr.Textbox(label="Forecasted Price"),
72
+ title="📈 StockPulse: LSTM Market Predictor",
73
+ description="This AI model uses Long Short-Term Memory (LSTM) networks to predict the next day's closing price based on a 60-day window of historical prices and Moving Averages.",
74
+ theme="soft"
75
+ )
76
+
77
+ if __name__ == "__main__":
78
+ interface.launch()
scaler.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f703de8e33ce32de0fd864c748977d4096da844352397c81f28064705daccb72
3
+ size 974
stock_model.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5cdd032ee98f5f7ad32221888007b62d6cb4b3a71c64f72dd59e7a77f1bf9bd6
3
+ size 1648465