Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
from
|
| 5 |
-
import yfinance as yf
|
| 6 |
import datetime
|
|
|
|
| 7 |
|
| 8 |
# Function to fetch stock data
|
| 9 |
def get_stock_data(ticker, start_date, end_date):
|
|
@@ -11,18 +12,21 @@ def get_stock_data(ticker, start_date, end_date):
|
|
| 11 |
stock_data.reset_index(inplace=True)
|
| 12 |
return stock_data
|
| 13 |
|
| 14 |
-
# Function to prepare data for
|
| 15 |
def prepare_data(stock_data):
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Function to make predictions using
|
| 20 |
-
def make_predictions(
|
| 21 |
-
model =
|
| 22 |
-
model.fit(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
return
|
| 26 |
|
| 27 |
# Function to calculate percentage change
|
| 28 |
def calculate_percentage_change(stock_data):
|
|
@@ -37,6 +41,20 @@ def get_stock_stats(stock_data):
|
|
| 37 |
low_value = stock_data['Close'].min()
|
| 38 |
return high_value, low_value
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Main function for UI
|
| 41 |
def stock_prediction(ticker, start_date, end_date):
|
| 42 |
stock_data = get_stock_data(ticker, start_date, end_date)
|
|
@@ -46,21 +64,23 @@ def stock_prediction(ticker, start_date, end_date):
|
|
| 46 |
high_value, low_value = get_stock_stats(stock_data)
|
| 47 |
|
| 48 |
# Prepare data for prediction
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
# Generate buy/sell suggestion (simple rule-based)
|
| 53 |
suggestion = "Buy" if percentage_change < 0 else "Sell"
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
return
|
| 60 |
|
| 61 |
# UI setup
|
| 62 |
with gr.Blocks() as app:
|
| 63 |
-
gr.Markdown("# Stock Prediction App")
|
| 64 |
|
| 65 |
stock_ticker = gr.Dropdown(
|
| 66 |
label="Select Stock Ticker",
|
|
@@ -70,8 +90,7 @@ with gr.Blocks() as app:
|
|
| 70 |
start_date = gr.DatePicker(label="Start Date", value=datetime.date(2023, 1, 1))
|
| 71 |
end_date = gr.DatePicker(label="End Date", value=datetime.date.today())
|
| 72 |
|
| 73 |
-
|
| 74 |
-
predicted_chart_output = gr.LinePlot(label="Predicted Future Stock Performance")
|
| 75 |
percentage_change_output = gr.Textbox(label="Percentage Change")
|
| 76 |
high_value_output = gr.Textbox(label="Highest Stock Value")
|
| 77 |
low_value_output = gr.Textbox(label="Lowest Stock Value")
|
|
@@ -83,7 +102,7 @@ with gr.Blocks() as app:
|
|
| 83 |
predict_button.click(
|
| 84 |
stock_prediction,
|
| 85 |
inputs=[stock_ticker, start_date, end_date],
|
| 86 |
-
outputs=[
|
| 87 |
)
|
| 88 |
|
| 89 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
+
from sklearn.linear_model import LinearRegression
|
| 5 |
+
import yfinance as yf
|
| 6 |
import datetime
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
|
| 9 |
# Function to fetch stock data
|
| 10 |
def get_stock_data(ticker, start_date, end_date):
|
|
|
|
| 12 |
stock_data.reset_index(inplace=True)
|
| 13 |
return stock_data
|
| 14 |
|
| 15 |
+
# Function to prepare data for Linear Regression model
|
| 16 |
def prepare_data(stock_data):
|
| 17 |
+
stock_data['Date'] = pd.to_datetime(stock_data['Date'])
|
| 18 |
+
stock_data['Days'] = (stock_data['Date'] - stock_data['Date'].min()).dt.days
|
| 19 |
+
X = stock_data['Days'].values.reshape(-1, 1)
|
| 20 |
+
y = stock_data['Close'].values
|
| 21 |
+
return X, y
|
| 22 |
|
| 23 |
+
# Function to make predictions using Linear Regression
|
| 24 |
+
def make_predictions(X, y, periods=90):
|
| 25 |
+
model = LinearRegression()
|
| 26 |
+
model.fit(X, y)
|
| 27 |
+
future_days = np.array(range(X[-1][0] + 1, X[-1][0] + 1 + periods)).reshape(-1, 1)
|
| 28 |
+
predicted_values = model.predict(future_days)
|
| 29 |
+
return future_days, predicted_values
|
| 30 |
|
| 31 |
# Function to calculate percentage change
|
| 32 |
def calculate_percentage_change(stock_data):
|
|
|
|
| 41 |
low_value = stock_data['Close'].min()
|
| 42 |
return high_value, low_value
|
| 43 |
|
| 44 |
+
# Function to plot stock performance
|
| 45 |
+
def plot_stock_performance(stock_data, predicted_dates, predicted_values):
|
| 46 |
+
plt.figure(figsize=(10, 5))
|
| 47 |
+
plt.plot(stock_data['Date'], stock_data['Close'], label="Historical Data", color="blue")
|
| 48 |
+
plt.plot(predicted_dates, predicted_values, label="Predicted Data", color="orange")
|
| 49 |
+
plt.xlabel("Date")
|
| 50 |
+
plt.ylabel("Stock Price")
|
| 51 |
+
plt.title("Stock Performance (Historical vs Predicted)")
|
| 52 |
+
plt.legend()
|
| 53 |
+
plt.grid(True)
|
| 54 |
+
plt.tight_layout()
|
| 55 |
+
plt.savefig("stock_performance.png")
|
| 56 |
+
return "stock_performance.png"
|
| 57 |
+
|
| 58 |
# Main function for UI
|
| 59 |
def stock_prediction(ticker, start_date, end_date):
|
| 60 |
stock_data = get_stock_data(ticker, start_date, end_date)
|
|
|
|
| 64 |
high_value, low_value = get_stock_stats(stock_data)
|
| 65 |
|
| 66 |
# Prepare data for prediction
|
| 67 |
+
X, y = prepare_data(stock_data)
|
| 68 |
+
future_days, predicted_values = make_predictions(X, y)
|
| 69 |
|
| 70 |
# Generate buy/sell suggestion (simple rule-based)
|
| 71 |
suggestion = "Buy" if percentage_change < 0 else "Sell"
|
| 72 |
|
| 73 |
+
# Create prediction dates
|
| 74 |
+
predicted_dates = pd.date_range(start=stock_data['Date'].max(), periods=len(predicted_values), freq='D')
|
| 75 |
+
|
| 76 |
+
# Plot stock performance
|
| 77 |
+
plot_file = plot_stock_performance(stock_data, predicted_dates, predicted_values)
|
| 78 |
|
| 79 |
+
return plot_file, percentage_change, high_value, low_value, suggestion
|
| 80 |
|
| 81 |
# UI setup
|
| 82 |
with gr.Blocks() as app:
|
| 83 |
+
gr.Markdown("# Stock Prediction App using Linear Regression")
|
| 84 |
|
| 85 |
stock_ticker = gr.Dropdown(
|
| 86 |
label="Select Stock Ticker",
|
|
|
|
| 90 |
start_date = gr.DatePicker(label="Start Date", value=datetime.date(2023, 1, 1))
|
| 91 |
end_date = gr.DatePicker(label="End Date", value=datetime.date.today())
|
| 92 |
|
| 93 |
+
plot_output = gr.Image(label="Stock Performance (Historical vs Predicted)")
|
|
|
|
| 94 |
percentage_change_output = gr.Textbox(label="Percentage Change")
|
| 95 |
high_value_output = gr.Textbox(label="Highest Stock Value")
|
| 96 |
low_value_output = gr.Textbox(label="Lowest Stock Value")
|
|
|
|
| 102 |
predict_button.click(
|
| 103 |
stock_prediction,
|
| 104 |
inputs=[stock_ticker, start_date, end_date],
|
| 105 |
+
outputs=[plot_output, percentage_change_output, high_value_output, low_value_output, suggestion_output]
|
| 106 |
)
|
| 107 |
|
| 108 |
app.launch()
|