dibend commited on
Commit
1e716d6
1 Parent(s): 3fb3ad5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gradio_app_builder import app
2
+ import yfinance as yf
3
+ from sklearn.linear_model import LinearRegression
4
+ import json
5
+
6
+ @app.route("/")
7
+ def train_predict_wrapper(ticker, start_date, end_date, prediction_days):
8
+ """
9
+ Downloads stock data, trains a linear regression model, and predicts future prices.
10
+
11
+ Args:
12
+ ticker: The ticker symbol of the stock.
13
+ start_date: The start date for the data (YYYY-MM-DD format).
14
+ end_date: The end date for the data (YYYY-MM-DD format).
15
+ prediction_days: The number of days to predict.
16
+
17
+ Returns:
18
+ A list of predicted closing prices for the next `prediction_days`.
19
+ """
20
+ # Download stock data
21
+ data = yf.download(ticker, start=start_date, end=end_date)
22
+ # Extract closing price and set as index
23
+ data = data["Close"].to_frame().set_index(data.index.values)
24
+
25
+ # Train linear regression model
26
+ X = data.index.values[:-prediction_days].reshape(-1, 1)
27
+ y = data.values[:-prediction_days]
28
+ model = LinearRegression()
29
+ model.fit(X, y)
30
+
31
+ # Predict future prices
32
+ future_dates = data.index.values[-prediction_days:]
33
+ X_future = future_dates.reshape(-1, 1)
34
+ predicted_prices = model.predict(X_future).tolist()
35
+
36
+ # Return predicted prices as JSON
37
+ return json.dumps(predicted_prices)
38
+
39
+ # Launch the Gradio application
40
+ app.launch()