Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import yfinance as yf
|
| 3 |
+
from prophet import Prophet
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# Stock ticker options
|
| 9 |
+
tickers = ['AAPL', 'GOOGL', 'MSFT', 'TSLA', 'AMZN', 'NFLX', 'NVDA', 'FB', 'INTC', 'AMD']
|
| 10 |
+
|
| 11 |
+
# Function to fetch stock data from Yahoo Finance
|
| 12 |
+
def fetch_stock_data(ticker, start_date, end_date):
|
| 13 |
+
stock_data = yf.download(ticker, start=start_date, end=end_date)
|
| 14 |
+
return stock_data
|
| 15 |
+
|
| 16 |
+
# Function to train the model and predict future prices
|
| 17 |
+
def predict_stock(ticker, start_date, end_date):
|
| 18 |
+
# Fetch data
|
| 19 |
+
stock_data = fetch_stock_data(ticker, start_date, end_date)
|
| 20 |
+
|
| 21 |
+
# Prepare data for Prophet model
|
| 22 |
+
stock_df = stock_data[['Close']].reset_index()
|
| 23 |
+
stock_df = stock_df.rename(columns={'Date': 'ds', 'Close': 'y'})
|
| 24 |
+
|
| 25 |
+
# Train Prophet model
|
| 26 |
+
model = Prophet()
|
| 27 |
+
model.fit(stock_df)
|
| 28 |
+
|
| 29 |
+
# Make future dataframe for 3 months (90 days) prediction
|
| 30 |
+
future = model.make_future_dataframe(periods=90)
|
| 31 |
+
forecast = model.predict(future)
|
| 32 |
+
|
| 33 |
+
# Extract key data points
|
| 34 |
+
current_price = stock_data['Close'][-1]
|
| 35 |
+
highest_price = stock_data['Close'].max()
|
| 36 |
+
lowest_price = stock_data['Close'].min()
|
| 37 |
+
percent_change = ((current_price - stock_data['Close'][0]) / stock_data['Close'][0]) * 100
|
| 38 |
+
|
| 39 |
+
# Generate graphs
|
| 40 |
+
historical_plot = stock_data['Close'].plot(title=f"{ticker} Historical Prices").get_figure()
|
| 41 |
+
future_plot = model.plot(forecast).get_figure()
|
| 42 |
+
|
| 43 |
+
# Determine buy/sell prediction (naive strategy: buy if current < future mean, sell otherwise)
|
| 44 |
+
future_avg = forecast['yhat'].mean()
|
| 45 |
+
buy_sell = "Buy" if current_price < future_avg else "Sell"
|
| 46 |
+
|
| 47 |
+
# Return results
|
| 48 |
+
return {
|
| 49 |
+
"Current Price": current_price,
|
| 50 |
+
"Highest Price": highest_price,
|
| 51 |
+
"Lowest Price": lowest_price,
|
| 52 |
+
"Percent Change": percent_change,
|
| 53 |
+
"Recommendation": buy_sell,
|
| 54 |
+
"Historical Plot": historical_plot,
|
| 55 |
+
"Future Plot": future_plot
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
# Gradio Interface
|
| 59 |
+
def app_interface(ticker, start_date, end_date):
|
| 60 |
+
result = predict_stock(ticker, start_date, end_date)
|
| 61 |
+
|
| 62 |
+
return result["Historical Plot"], result["Future Plot"], result["Current Price"], result["Highest Price"], result["Lowest Price"], result["Percent Change"], result["Recommendation"]
|
| 63 |
+
|
| 64 |
+
# Gradio UI Setup
|
| 65 |
+
gr_interface = gr.Interface(
|
| 66 |
+
fn=app_interface,
|
| 67 |
+
inputs=[
|
| 68 |
+
gr.Dropdown(label="Select Stock Ticker", choices=tickers, value='AAPL'),
|
| 69 |
+
gr.inputs.Date(label="Start Date", default=datetime(2020, 1, 1).strftime('%Y-%m-%d')),
|
| 70 |
+
gr.inputs.Date(label="End Date", default=datetime.now().strftime('%Y-%m-%d'))
|
| 71 |
+
],
|
| 72 |
+
outputs=[
|
| 73 |
+
gr.Plot(label="Historical Stock Prices"),
|
| 74 |
+
gr.Plot(label="Future Stock Predictions"),
|
| 75 |
+
gr.Textbox(label="Current Price"),
|
| 76 |
+
gr.Textbox(label="Highest Price"),
|
| 77 |
+
gr.Textbox(label="Lowest Price"),
|
| 78 |
+
gr.Textbox(label="Percentage Change"),
|
| 79 |
+
gr.Textbox(label="Buy/Sell Recommendation")
|
| 80 |
+
],
|
| 81 |
+
title="Stock Prediction App",
|
| 82 |
+
description="Select a stock, start and end date to predict future performance and get buy/sell recommendations."
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Launch the app
|
| 86 |
+
gr_interface.launch()
|