Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import tensorflow as tf
|
|
| 6 |
from sklearn.preprocessing import MinMaxScaler
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
import gradio as gr
|
| 9 |
-
from datetime import datetime
|
| 10 |
|
| 11 |
# Step 1: Fetch stock data from yfinance
|
| 12 |
def fetch_stock_data(ticker, start_date, end_date):
|
|
@@ -109,11 +109,30 @@ def stock_prediction_app(ticker, start_date_str, end_date_str):
|
|
| 109 |
|
| 110 |
# Predict stock price for tomorrow (1 day)
|
| 111 |
predicted_price = predict_future(model, scaled_data, scaler, steps=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# Generate and return the plot with historical and predicted prices
|
| 114 |
plot_path = plot_predictions(data, predicted_price)
|
| 115 |
|
| 116 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Step 8: Gradio UI setup
|
| 119 |
tickers = ["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA", "META", "NFLX", "NVDA", "BABA", "BA"]
|
|
@@ -128,7 +147,12 @@ ui = gr.Interface(
|
|
| 128 |
],
|
| 129 |
outputs=[
|
| 130 |
gr.Image(type="filepath"), # Return the file path for the generated graph
|
| 131 |
-
gr.Number(label="Predicted Price for Tomorrow (USD)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
],
|
| 133 |
title="Stock Price Prediction App",
|
| 134 |
description="Predict future stock price for tomorrow based on historical data."
|
|
|
|
| 6 |
from sklearn.preprocessing import MinMaxScaler
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
import gradio as gr
|
| 9 |
+
from datetime import datetime
|
| 10 |
|
| 11 |
# Step 1: Fetch stock data from yfinance
|
| 12 |
def fetch_stock_data(ticker, start_date, end_date):
|
|
|
|
| 109 |
|
| 110 |
# Predict stock price for tomorrow (1 day)
|
| 111 |
predicted_price = predict_future(model, scaled_data, scaler, steps=1)
|
| 112 |
+
|
| 113 |
+
# Current, highest, lowest prices, and percentage change
|
| 114 |
+
current_price = data['Close'].iloc[-1]
|
| 115 |
+
highest_price = data['Close'].max()
|
| 116 |
+
lowest_price = data['Close'].min()
|
| 117 |
|
| 118 |
+
# Calculate percentage change
|
| 119 |
+
percentage_change = ((predicted_price[0][0] - current_price) / current_price) * 100
|
| 120 |
+
|
| 121 |
+
# Buy/Sell recommendation
|
| 122 |
+
recommendation = "Buy" if predicted_price[0][0] > current_price else "Sell"
|
| 123 |
+
|
| 124 |
# Generate and return the plot with historical and predicted prices
|
| 125 |
plot_path = plot_predictions(data, predicted_price)
|
| 126 |
|
| 127 |
+
return (
|
| 128 |
+
plot_path,
|
| 129 |
+
predicted_price[0][0], # Tomorrow's predicted price
|
| 130 |
+
current_price, # Current price
|
| 131 |
+
highest_price, # Highest price
|
| 132 |
+
lowest_price, # Lowest price
|
| 133 |
+
percentage_change, # Percentage change
|
| 134 |
+
recommendation # Buy/Sell recommendation
|
| 135 |
+
)
|
| 136 |
|
| 137 |
# Step 8: Gradio UI setup
|
| 138 |
tickers = ["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA", "META", "NFLX", "NVDA", "BABA", "BA"]
|
|
|
|
| 147 |
],
|
| 148 |
outputs=[
|
| 149 |
gr.Image(type="filepath"), # Return the file path for the generated graph
|
| 150 |
+
gr.Number(label="Predicted Price for Tomorrow (USD)"),
|
| 151 |
+
gr.Number(label="Current Price (USD)"),
|
| 152 |
+
gr.Number(label="Highest Price (USD)"),
|
| 153 |
+
gr.Number(label="Lowest Price (USD)"),
|
| 154 |
+
gr.Number(label="Percentage Change (%)"),
|
| 155 |
+
gr.Textbox(label="Recommendation")
|
| 156 |
],
|
| 157 |
title="Stock Price Prediction App",
|
| 158 |
description="Predict future stock price for tomorrow based on historical data."
|