Update app.py
Browse files
app.py
CHANGED
@@ -4,20 +4,41 @@ import numpy as np
|
|
4 |
import pandas as pd
|
5 |
import matplotlib.pyplot as plt
|
6 |
from sklearn.linear_model import LinearRegression
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Function to fetch data
|
9 |
def fetch_data(ticker, start_date, end_date):
|
|
|
|
|
|
|
|
|
10 |
# Convert input dates from strings to pandas datetime
|
11 |
start_date = pd.to_datetime(start_date)
|
12 |
end_date = pd.to_datetime(end_date)
|
13 |
|
14 |
stock_data = yf.download(ticker, start=start_date, end=end_date)
|
|
|
|
|
|
|
|
|
15 |
return stock_data
|
16 |
|
17 |
# Function to predict future prices
|
18 |
def predict_stock(ticker, start_date, end_date):
|
19 |
stock_data = fetch_data(ticker, start_date, end_date)
|
20 |
|
|
|
|
|
|
|
21 |
stock_data['Date'] = pd.to_datetime(stock_data.index)
|
22 |
stock_data['Days'] = (stock_data['Date'] - stock_data['Date'].min()).dt.days
|
23 |
|
@@ -36,6 +57,9 @@ def predict_stock(ticker, start_date, end_date):
|
|
36 |
def plot_stock_data(ticker, start_date, end_date):
|
37 |
future_prices, stock_data = predict_stock(ticker, start_date, end_date)
|
38 |
|
|
|
|
|
|
|
39 |
plt.figure(figsize=(10, 6))
|
40 |
plt.plot(stock_data['Date'], stock_data['Close'], label="Historical Prices")
|
41 |
future_dates = pd.date_range(stock_data['Date'].max() + pd.Timedelta(days=1), periods=90)
|
@@ -52,15 +76,26 @@ def plot_stock_data(ticker, start_date, end_date):
|
|
52 |
# Gradio Interface
|
53 |
def stock_prediction_interface(ticker, start_date, end_date):
|
54 |
stock_data = fetch_data(ticker, start_date, end_date)
|
|
|
|
|
|
|
|
|
55 |
price_change = (stock_data['Close'].iloc[-1] - stock_data['Close'].iloc[0]) / stock_data['Close'].iloc[0] * 100
|
56 |
highest_price = stock_data['High'].max()
|
57 |
lowest_price = stock_data['Low'].min()
|
58 |
|
59 |
future_prices, _ = predict_stock(ticker, start_date, end_date)
|
|
|
|
|
|
|
|
|
60 |
decision = "Buy" if future_prices[-1] > stock_data['Close'].iloc[-1] else "Sell"
|
61 |
|
62 |
graph = plot_stock_data(ticker, start_date, end_date)
|
63 |
|
|
|
|
|
|
|
64 |
return f"Percentage Change: {price_change:.2f}%\nHighest Price: {highest_price}\nLowest Price: {lowest_price}\nDecision: {decision}", graph
|
65 |
|
66 |
# Gradio app UI
|
@@ -81,4 +116,4 @@ with gr.Blocks() as demo:
|
|
81 |
outputs=[output_text, output_plot]
|
82 |
)
|
83 |
|
84 |
-
demo.launch()
|
|
|
4 |
import pandas as pd
|
5 |
import matplotlib.pyplot as plt
|
6 |
from sklearn.linear_model import LinearRegression
|
7 |
+
import matplotlib
|
8 |
+
matplotlib.use('Agg') # Use non-interactive backend
|
9 |
+
|
10 |
+
# Date validation function
|
11 |
+
def validate_date(date_text):
|
12 |
+
try:
|
13 |
+
pd.to_datetime(date_text)
|
14 |
+
return True
|
15 |
+
except ValueError:
|
16 |
+
return False
|
17 |
|
18 |
# Function to fetch data
|
19 |
def fetch_data(ticker, start_date, end_date):
|
20 |
+
# Validate the dates
|
21 |
+
if not validate_date(start_date) or not validate_date(end_date):
|
22 |
+
return "Invalid date format. Please use YYYY-MM-DD.", None
|
23 |
+
|
24 |
# Convert input dates from strings to pandas datetime
|
25 |
start_date = pd.to_datetime(start_date)
|
26 |
end_date = pd.to_datetime(end_date)
|
27 |
|
28 |
stock_data = yf.download(ticker, start=start_date, end=end_date)
|
29 |
+
|
30 |
+
if stock_data.empty:
|
31 |
+
return f"No data found for ticker {ticker} in the specified date range.", None
|
32 |
+
|
33 |
return stock_data
|
34 |
|
35 |
# Function to predict future prices
|
36 |
def predict_stock(ticker, start_date, end_date):
|
37 |
stock_data = fetch_data(ticker, start_date, end_date)
|
38 |
|
39 |
+
if isinstance(stock_data, str): # In case of an error message from fetch_data
|
40 |
+
return stock_data, None
|
41 |
+
|
42 |
stock_data['Date'] = pd.to_datetime(stock_data.index)
|
43 |
stock_data['Days'] = (stock_data['Date'] - stock_data['Date'].min()).dt.days
|
44 |
|
|
|
57 |
def plot_stock_data(ticker, start_date, end_date):
|
58 |
future_prices, stock_data = predict_stock(ticker, start_date, end_date)
|
59 |
|
60 |
+
if isinstance(future_prices, str): # In case of an error message
|
61 |
+
return future_prices
|
62 |
+
|
63 |
plt.figure(figsize=(10, 6))
|
64 |
plt.plot(stock_data['Date'], stock_data['Close'], label="Historical Prices")
|
65 |
future_dates = pd.date_range(stock_data['Date'].max() + pd.Timedelta(days=1), periods=90)
|
|
|
76 |
# Gradio Interface
|
77 |
def stock_prediction_interface(ticker, start_date, end_date):
|
78 |
stock_data = fetch_data(ticker, start_date, end_date)
|
79 |
+
|
80 |
+
if isinstance(stock_data, str): # If there was an error fetching data
|
81 |
+
return stock_data, None
|
82 |
+
|
83 |
price_change = (stock_data['Close'].iloc[-1] - stock_data['Close'].iloc[0]) / stock_data['Close'].iloc[0] * 100
|
84 |
highest_price = stock_data['High'].max()
|
85 |
lowest_price = stock_data['Low'].min()
|
86 |
|
87 |
future_prices, _ = predict_stock(ticker, start_date, end_date)
|
88 |
+
|
89 |
+
if isinstance(future_prices, str): # If there was an error in prediction
|
90 |
+
return future_prices, None
|
91 |
+
|
92 |
decision = "Buy" if future_prices[-1] > stock_data['Close'].iloc[-1] else "Sell"
|
93 |
|
94 |
graph = plot_stock_data(ticker, start_date, end_date)
|
95 |
|
96 |
+
if isinstance(graph, str): # If there was an error in plotting
|
97 |
+
return graph, None
|
98 |
+
|
99 |
return f"Percentage Change: {price_change:.2f}%\nHighest Price: {highest_price}\nLowest Price: {lowest_price}\nDecision: {decision}", graph
|
100 |
|
101 |
# Gradio app UI
|
|
|
116 |
outputs=[output_text, output_plot]
|
117 |
)
|
118 |
|
119 |
+
demo.launch()
|