Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ from prophet import Prophet
|
|
| 10 |
from tensorflow import keras
|
| 11 |
from sklearn.preprocessing import MinMaxScaler
|
| 12 |
import warnings
|
|
|
|
|
|
|
| 13 |
warnings.filterwarnings('ignore')
|
| 14 |
|
| 15 |
# Load your saved models (update paths as needed)
|
|
@@ -39,20 +41,54 @@ def load_models():
|
|
| 39 |
arima_model, prophet_model, lstm_model, scaler = load_models()
|
| 40 |
SEQ_LENGTH = 60 # Should match your training
|
| 41 |
|
|
|
|
| 42 |
def fetch_stock_data(ticker, days=365):
|
| 43 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
start_date = end_date - timedelta(days=days)
|
| 47 |
-
df =
|
| 48 |
-
|
| 49 |
-
|
| 50 |
df = df[['Close']].copy()
|
| 51 |
df.columns = ['Price']
|
|
|
|
| 52 |
return df, None
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
return None, str(e)
|
| 55 |
|
|
|
|
| 56 |
def make_arima_forecast(data, days):
|
| 57 |
"""Make ARIMA forecast"""
|
| 58 |
try:
|
|
|
|
| 10 |
from tensorflow import keras
|
| 11 |
from sklearn.preprocessing import MinMaxScaler
|
| 12 |
import warnings
|
| 13 |
+
import os
|
| 14 |
+
from datetime import datetime, timedelta
|
| 15 |
warnings.filterwarnings('ignore')
|
| 16 |
|
| 17 |
# Load your saved models (update paths as needed)
|
|
|
|
| 41 |
arima_model, prophet_model, lstm_model, scaler = load_models()
|
| 42 |
SEQ_LENGTH = 60 # Should match your training
|
| 43 |
|
| 44 |
+
|
| 45 |
def fetch_stock_data(ticker, days=365):
|
| 46 |
+
"""
|
| 47 |
+
Load stock data from a local CSV file in the project root folder.
|
| 48 |
+
If the file doesn't exist, returns an error message.
|
| 49 |
+
|
| 50 |
+
Parameters:
|
| 51 |
+
ticker (str): Stock ticker symbol (used as filename prefix)
|
| 52 |
+
days (int): Number of days of recent data to keep (optional)
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
tuple: (DataFrame, error_message)
|
| 56 |
+
"""
|
| 57 |
try:
|
| 58 |
+
# Construct the file path (e.g., ./AAPL.csv)
|
| 59 |
+
filename = f"{ticker.upper()}.csv"
|
| 60 |
+
file_path = os.path.join(os.getcwd(), filename)
|
| 61 |
+
|
| 62 |
+
# Check if file exists
|
| 63 |
+
if not os.path.exists(file_path):
|
| 64 |
+
return None, f"Dataset file not found in root folder: {filename}"
|
| 65 |
+
|
| 66 |
+
# Load dataset
|
| 67 |
+
df = pd.read_csv(file_path)
|
| 68 |
+
|
| 69 |
+
# Ensure 'Date' and 'Close' columns exist
|
| 70 |
+
if 'Date' not in df.columns or 'Close' not in df.columns:
|
| 71 |
+
return None, f"File must contain 'Date' and 'Close' columns in {filename}"
|
| 72 |
+
|
| 73 |
+
# Parse dates and set index
|
| 74 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 75 |
+
df.set_index('Date', inplace=True)
|
| 76 |
+
|
| 77 |
+
# Keep only recent `days`
|
| 78 |
+
end_date = df.index.max()
|
| 79 |
start_date = end_date - timedelta(days=days)
|
| 80 |
+
df = df.loc[df.index >= start_date]
|
| 81 |
+
|
| 82 |
+
# Rename column for consistency
|
| 83 |
df = df[['Close']].copy()
|
| 84 |
df.columns = ['Price']
|
| 85 |
+
|
| 86 |
return df, None
|
| 87 |
+
|
| 88 |
except Exception as e:
|
| 89 |
return None, str(e)
|
| 90 |
|
| 91 |
+
|
| 92 |
def make_arima_forecast(data, days):
|
| 93 |
"""Make ARIMA forecast"""
|
| 94 |
try:
|