gopichandra commited on
Commit
d48489f
·
verified ·
1 Parent(s): a621aaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -18
app.py CHANGED
@@ -3,6 +3,7 @@ from prophet import Prophet
3
  import gradio as gr
4
  import pandas as pd
5
  from datetime import datetime
 
6
 
7
  # List of stock tickers for the dropdown
8
  tickers = ['AAPL', 'GOOGL', 'MSFT', 'TSLA', 'AMZN', 'NFLX', 'NVDA', 'FB', 'INTC', 'AMD']
@@ -29,36 +30,42 @@ def predict_stock(ticker, start_date, end_date):
29
  future = model.make_future_dataframe(periods=90)
30
  forecast = model.predict(future)
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Calculate key statistics
33
  current_price = stock_data['Close'][-1]
34
  highest_price = stock_data['Close'].max()
35
  lowest_price = stock_data['Close'].min()
36
  percent_change = ((current_price - stock_data['Close'][0]) / stock_data['Close'][0]) * 100
37
 
38
- # Plot historical and future stock prices
39
- historical_plot = stock_data['Close'].plot(title=f"{ticker} Historical Prices").get_figure()
40
- future_plot = model.plot(forecast).get_figure()
41
-
42
  # Simple buy/sell recommendation
43
  future_avg = forecast['yhat'].mean()
44
  buy_sell = "Buy" if current_price < future_avg else "Sell"
45
 
46
- # Return results
47
- return {
48
- "Current Price": current_price,
49
- "Highest Price": highest_price,
50
- "Lowest Price": lowest_price,
51
- "Percent Change": percent_change,
52
- "Recommendation": buy_sell,
53
- "Historical Plot": historical_plot,
54
- "Future Plot": future_plot
55
- }
56
 
57
  # Interface function for the Gradio UI
58
  def app_interface(ticker, start_date, end_date):
59
- result = predict_stock(ticker, start_date, end_date)
60
 
61
- return result["Historical Plot"], result["Future Plot"], result["Current Price"], result["Highest Price"], result["Lowest Price"], result["Percent Change"], result["Recommendation"]
 
62
 
63
  # Updated Gradio UI using Textbox for date input instead of DatePicker
64
  gr_interface = gr.Interface(
@@ -69,8 +76,7 @@ gr_interface = gr.Interface(
69
  gr.Textbox(label="End Date (YYYY-MM-DD)", value=datetime.now().strftime('%Y-%m-%d'))
70
  ],
71
  outputs=[
72
- gr.Plot(label="Historical Stock Prices"),
73
- gr.Plot(label="Future Stock Predictions"),
74
  gr.Textbox(label="Current Price"),
75
  gr.Textbox(label="Highest Price"),
76
  gr.Textbox(label="Lowest Price"),
 
3
  import gradio as gr
4
  import pandas as pd
5
  from datetime import datetime
6
+ import matplotlib.pyplot as plt
7
 
8
  # List of stock tickers for the dropdown
9
  tickers = ['AAPL', 'GOOGL', 'MSFT', 'TSLA', 'AMZN', 'NFLX', 'NVDA', 'FB', 'INTC', 'AMD']
 
30
  future = model.make_future_dataframe(periods=90)
31
  forecast = model.predict(future)
32
 
33
+ # Plot the stock price history
34
+ fig, ax = plt.subplots(figsize=(10, 6))
35
+ ax.plot(stock_data.index, stock_data['Close'], label="Historical Prices")
36
+
37
+ # Plot the forecast
38
+ ax.plot(forecast['ds'], forecast['yhat'], label="Predicted Prices", linestyle='--')
39
+
40
+ # Customize the plot
41
+ ax.set_title(f"{ticker} Stock Price Prediction", fontsize=14)
42
+ ax.set_xlabel("Date", fontsize=12)
43
+ ax.set_ylabel("Price (USD)", fontsize=12)
44
+ ax.legend(loc="upper left")
45
+
46
+ # Save the plot to a file
47
+ plt.savefig('/tmp/stock_prediction.png')
48
+ plt.close(fig) # Close the plot to prevent display issues in some environments
49
+
50
  # Calculate key statistics
51
  current_price = stock_data['Close'][-1]
52
  highest_price = stock_data['Close'].max()
53
  lowest_price = stock_data['Close'].min()
54
  percent_change = ((current_price - stock_data['Close'][0]) / stock_data['Close'][0]) * 100
55
 
 
 
 
 
56
  # Simple buy/sell recommendation
57
  future_avg = forecast['yhat'].mean()
58
  buy_sell = "Buy" if current_price < future_avg else "Sell"
59
 
60
+ # Return the plot file path and statistics
61
+ return '/tmp/stock_prediction.png', current_price, highest_price, lowest_price, percent_change, buy_sell
 
 
 
 
 
 
 
 
62
 
63
  # Interface function for the Gradio UI
64
  def app_interface(ticker, start_date, end_date):
65
+ graph, current_price, highest_price, lowest_price, percent_change, buy_sell = predict_stock(ticker, start_date, end_date)
66
 
67
+ # Return the file path for the plot and text outputs
68
+ return graph, current_price, highest_price, lowest_price, percent_change, buy_sell
69
 
70
  # Updated Gradio UI using Textbox for date input instead of DatePicker
71
  gr_interface = gr.Interface(
 
76
  gr.Textbox(label="End Date (YYYY-MM-DD)", value=datetime.now().strftime('%Y-%m-%d'))
77
  ],
78
  outputs=[
79
+ gr.Image(label="Stock Price Prediction"), # Display the saved plot image
 
80
  gr.Textbox(label="Current Price"),
81
  gr.Textbox(label="Highest Price"),
82
  gr.Textbox(label="Lowest Price"),