Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yfinance as yf
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from sklearn.model_selection import train_test_split
|
6 |
+
from sklearn.linear_model import LinearRegression
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Define stock tickers
|
10 |
+
stock_tickers = ["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA", "FB", "NFLX", "NVDA", "BRK.B", "DIS"]
|
11 |
+
|
12 |
+
def fetch_data(ticker, start_date, end_date):
|
13 |
+
"""Fetch historical stock data."""
|
14 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
15 |
+
return data
|
16 |
+
|
17 |
+
def train_model(data):
|
18 |
+
"""Train a Linear Regression model on the stock data."""
|
19 |
+
data['Date'] = data.index.map(pd.Timestamp.timestamp)
|
20 |
+
X = data[['Date']]
|
21 |
+
y = data['Close']
|
22 |
+
|
23 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
24 |
+
model = LinearRegression()
|
25 |
+
model.fit(X_train, y_train)
|
26 |
+
|
27 |
+
return model
|
28 |
+
|
29 |
+
def predict_price(model, today):
|
30 |
+
"""Predict the stock price using the trained model."""
|
31 |
+
prediction = model.predict(np.array([[pd.Timestamp(today).timestamp()]]))
|
32 |
+
return prediction[0]
|
33 |
+
|
34 |
+
def stock_analysis(ticker, start_date, end_date):
|
35 |
+
"""Perform stock analysis and predictions."""
|
36 |
+
# Fetch data
|
37 |
+
data = fetch_data(ticker, start_date, end_date)
|
38 |
+
|
39 |
+
# Train model
|
40 |
+
model = train_model(data)
|
41 |
+
|
42 |
+
# Calculate statistics
|
43 |
+
today = pd.Timestamp.today()
|
44 |
+
predicted_price = predict_price(model, today)
|
45 |
+
|
46 |
+
percentage_change = ((data['Close'][-1] - data['Close'][0]) / data['Close'][0]) * 100
|
47 |
+
highest_value = data['Close'].max()
|
48 |
+
lowest_value = data['Close'].min()
|
49 |
+
|
50 |
+
# Create a plot for historical and predicted performance
|
51 |
+
plt.figure(figsize=(12, 6))
|
52 |
+
plt.plot(data.index, data['Close'], label='Historical Prices', color='blue')
|
53 |
+
future_dates = pd.date_range(start=today, periods=90, freq='D')
|
54 |
+
future_prices = [predict_price(model, date) for date in future_dates]
|
55 |
+
plt.plot(future_dates, future_prices, label='Predicted Prices', color='orange')
|
56 |
+
plt.title(f"{ticker} Stock Price Prediction")
|
57 |
+
plt.xlabel("Date")
|
58 |
+
plt.ylabel("Price")
|
59 |
+
plt.legend()
|
60 |
+
plt.grid()
|
61 |
+
plt.xticks(rotation=45)
|
62 |
+
plt.tight_layout()
|
63 |
+
plt.savefig('stock_analysis.png')
|
64 |
+
|
65 |
+
return (
|
66 |
+
predicted_price,
|
67 |
+
percentage_change,
|
68 |
+
highest_value,
|
69 |
+
lowest_value,
|
70 |
+
'Buy' if predicted_price > data['Close'][-1] else 'Sell',
|
71 |
+
'stock_analysis.png'
|
72 |
+
)
|
73 |
+
|
74 |
+
# Gradio UI
|
75 |
+
inputs = [
|
76 |
+
gr.inputs.Dropdown(choices=stock_tickers, label="Select Stock Ticker"),
|
77 |
+
gr.inputs.Date(label="Start Date"),
|
78 |
+
gr.inputs.Date(label="End Date"),
|
79 |
+
]
|
80 |
+
|
81 |
+
outputs = [
|
82 |
+
gr.outputs.Textbox(label="Predicted Price"),
|
83 |
+
gr.outputs.Textbox(label="Percentage Change (%)"),
|
84 |
+
gr.outputs.Textbox(label="Highest Price"),
|
85 |
+
gr.outputs.Textbox(label="Lowest Price"),
|
86 |
+
gr.outputs.Textbox(label="Recommendation (Buy/Sell)"),
|
87 |
+
gr.outputs.Image(label="Stock Price Analysis")
|
88 |
+
]
|
89 |
+
|
90 |
+
gr.Interface(fn=stock_analysis, inputs=inputs, outputs=outputs, title="Stock Price Predictor").launch()
|