Abhisesh7 commited on
Commit
0dfdfb5
·
verified ·
1 Parent(s): 34fe5b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -31
app.py CHANGED
@@ -1,27 +1,16 @@
1
- import sys
2
  import pandas as pd
3
  import numpy as np
4
  import tensorflow as tf
5
  import gradio as gr
6
-
7
- print(f"Python version: {sys.version}")
8
- print(f"Pandas version: {pd.__version__}")
9
- print(f"Numpy version: {np.__version__}")
10
- print(f"TensorFlow version: {tf.__version__}")
11
- print(f"Gradio version: {gr.__version__}")
12
- import yfinance as yf
13
- import pandas as pd
14
 
15
  def fetch_data(ticker, start_date, end_date):
16
  # Fetch historical data for the given ticker symbol
17
  data = yf.download(ticker, start=start_date, end=end_date)
18
  return data
19
 
20
- # Example usage
21
- # data = fetch_data("AAPL", "2023-01-01", "2023-10-01")
22
- from sklearn.preprocessing import MinMaxScaler
23
- import numpy as np
24
-
25
  def prepare_data(data):
26
  # Preprocessing
27
  scaler = MinMaxScaler(feature_range=(0, 1))
@@ -43,10 +32,6 @@ def create_model(input_shape):
43
  model.compile(optimizer='adam', loss='mean_squared_error')
44
  return model
45
 
46
- # Example usage
47
- # x_train, y_train, scaler = prepare_data(data)
48
- # model = create_model((x_train.shape[1], 1))
49
- # model.fit(x_train, y_train, batch_size=1, epochs=1)
50
  def predict_next_days(model, last_60_days, scaler):
51
  last_60_days = np.array(last_60_days).reshape(-1, 1)
52
  last_60_days_scaled = scaler.transform(last_60_days)
@@ -58,6 +43,17 @@ def predict_next_days(model, last_60_days, scaler):
58
  predicted_price = model.predict(X_test)
59
  predicted_price = scaler.inverse_transform(predicted_price) # Reverse scaling
60
  return predicted_price[0][0]
 
 
 
 
 
 
 
 
 
 
 
61
  def stock_prediction(ticker, start_date, end_date):
62
  data = fetch_data(ticker, start_date, end_date)
63
  x_train, y_train, scaler = prepare_data(data)
@@ -73,6 +69,8 @@ def stock_prediction(ticker, start_date, end_date):
73
  highest_value = data['Close'].max()
74
  lowest_value = data['Close'].min()
75
 
 
 
76
  return {
77
  "Predicted Price": predicted_price,
78
  "Percentage Change": percentage_change,
@@ -92,16 +90,3 @@ gr.Interface(
92
  ],
93
  outputs=["json"],
94
  ).launch()
95
- import matplotlib.pyplot as plt
96
-
97
- def plot_graph(data, predicted_prices):
98
- plt.figure(figsize=(14, 5))
99
- plt.plot(data['Close'], label='Historical Prices', color='blue')
100
- plt.plot(predicted_prices, label='Predicted Prices', color='red')
101
- plt.title('Stock Price Prediction')
102
- plt.xlabel('Date')
103
- plt.ylabel('Stock Price')
104
- plt.legend()
105
- plt.show()
106
-
107
- # Call this function in your `stock_prediction` function to plot the graph
 
1
+ import yfinance as yf
2
  import pandas as pd
3
  import numpy as np
4
  import tensorflow as tf
5
  import gradio as gr
6
+ import matplotlib.pyplot as plt
7
+ from sklearn.preprocessing import MinMaxScaler
 
 
 
 
 
 
8
 
9
  def fetch_data(ticker, start_date, end_date):
10
  # Fetch historical data for the given ticker symbol
11
  data = yf.download(ticker, start=start_date, end=end_date)
12
  return data
13
 
 
 
 
 
 
14
  def prepare_data(data):
15
  # Preprocessing
16
  scaler = MinMaxScaler(feature_range=(0, 1))
 
32
  model.compile(optimizer='adam', loss='mean_squared_error')
33
  return model
34
 
 
 
 
 
35
  def predict_next_days(model, last_60_days, scaler):
36
  last_60_days = np.array(last_60_days).reshape(-1, 1)
37
  last_60_days_scaled = scaler.transform(last_60_days)
 
43
  predicted_price = model.predict(X_test)
44
  predicted_price = scaler.inverse_transform(predicted_price) # Reverse scaling
45
  return predicted_price[0][0]
46
+
47
+ def plot_graph(data, predicted_prices):
48
+ plt.figure(figsize=(14, 5))
49
+ plt.plot(data['Close'], label='Historical Prices', color='blue')
50
+ plt.plot(predicted_prices, label='Predicted Prices', color='red')
51
+ plt.title('Stock Price Prediction')
52
+ plt.xlabel('Date')
53
+ plt.ylabel('Stock Price')
54
+ plt.legend()
55
+ plt.show()
56
+
57
  def stock_prediction(ticker, start_date, end_date):
58
  data = fetch_data(ticker, start_date, end_date)
59
  x_train, y_train, scaler = prepare_data(data)
 
69
  highest_value = data['Close'].max()
70
  lowest_value = data['Close'].min()
71
 
72
+ plot_graph(data, predicted_price)
73
+
74
  return {
75
  "Predicted Price": predicted_price,
76
  "Percentage Change": percentage_change,
 
90
  ],
91
  outputs=["json"],
92
  ).launch()