import yfinance as yf import pandas as pd import numpy as np import gradio as gr from prophet import Prophet import plotly.graph_objs as go # Stock tickers for dropdown tickers = ["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA", "META", "NFLX", "NVDA", "JPM", "BAC"] # Function to fetch and process stock data def get_stock_data(ticker, start_date, end_date): data = yf.download(ticker, start=start_date, end=end_date) data.reset_index(inplace=True) return data # Train Prophet model def train_model(data): df = data[['Date', 'Close']].rename(columns={"Date": "ds", "Close": "y"}) model = Prophet() model.fit(df) return model # Predict future stock prices def make_predictions(model, periods=90): future = model.make_future_dataframe(periods=periods) forecast = model.predict(future) return forecast # Function to generate buy/sell prediction def predict_stock(ticker, start_date, end_date): data = get_stock_data(ticker, start_date, end_date) # Train the model model = train_model(data) # Make predictions forecast = make_predictions(model, periods=90) # Calculate statistics highest_value = data['Close'].max() lowest_value = data['Close'].min() percentage_change = ((data['Close'].iloc[-1] - data['Close'].iloc[0]) / data['Close'].iloc[0]) * 100 last_close_price = data['Close'].iloc[-1] future_price = forecast['yhat'].iloc[-1] # Recommendation logic recommendation = "Buy" if future_price > last_close_price else "Sell" # Plotting the historical and predicted prices fig = go.Figure() fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], mode='lines', name='Historical Data')) fig.add_trace(go.Scatter(x=forecast['ds'], y=forecast['yhat'], mode='lines', name='Forecast Data')) fig.update_layout(title=f'Stock Price Prediction for {ticker}', xaxis_title='Date', yaxis_title='Stock Price') return { "Highest Price": highest_value, "Lowest Price": lowest_value, "Percentage Change": f"{percentage_change:.2f}%", "Buy/Sell Recommendation": recommendation, "Graph": fig } # Gradio UI Components def app_ui(): stock_ticker = gr.Dropdown(choices=tickers, label="Select Stock Ticker") start_date = gr.DatePicker(label="Select Start Date") end_date = gr.DatePicker(label="Select End Date") output = gr.outputs.JSON(label="Stock Prediction Results") # Define Gradio Interface interface = gr.Interface( fn=predict_stock, inputs=[stock_ticker, start_date, end_date], outputs=[output, gr.Plot(label="Stock Performance Graph")], live=True ) interface.launch() if __name__ == "__main__": app_ui()