|
from gradio_app_builder import app |
|
import yfinance as yf |
|
from sklearn.linear_model import LinearRegression |
|
import json |
|
|
|
@app.route("/") |
|
def train_predict_wrapper(ticker, start_date, end_date, prediction_days): |
|
""" |
|
Downloads stock data, trains a linear regression model, and predicts future prices. |
|
|
|
Args: |
|
ticker: The ticker symbol of the stock. |
|
start_date: The start date for the data (YYYY-MM-DD format). |
|
end_date: The end date for the data (YYYY-MM-DD format). |
|
prediction_days: The number of days to predict. |
|
|
|
Returns: |
|
A list of predicted closing prices for the next `prediction_days`. |
|
""" |
|
|
|
data = yf.download(ticker, start=start_date, end=end_date) |
|
|
|
data = data["Close"].to_frame().set_index(data.index.values) |
|
|
|
|
|
X = data.index.values[:-prediction_days].reshape(-1, 1) |
|
y = data.values[:-prediction_days] |
|
model = LinearRegression() |
|
model.fit(X, y) |
|
|
|
|
|
future_dates = data.index.values[-prediction_days:] |
|
X_future = future_dates.reshape(-1, 1) |
|
predicted_prices = model.predict(X_future).tolist() |
|
|
|
|
|
return json.dumps(predicted_prices) |
|
|
|
|
|
app.launch() |