Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,8 @@ 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)
|
| 16 |
# For Hugging Face, these will be in the same directory as app.py
|
|
@@ -39,51 +41,26 @@ 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 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
try:
|
| 53 |
-
df = yf.download(ticker, start=start_date, end=end_date, progress=False, auto_adjust=True)
|
| 54 |
-
if not df.empty:
|
| 55 |
-
break
|
| 56 |
-
time.sleep(1) # Wait before retry
|
| 57 |
-
except Exception as e:
|
| 58 |
-
if attempt == max_retries - 1:
|
| 59 |
-
raise e
|
| 60 |
-
time.sleep(1)
|
| 61 |
-
|
| 62 |
-
if df.empty:
|
| 63 |
-
return None, f"No data found for ticker: {ticker}. Please verify the ticker symbol is correct."
|
| 64 |
-
|
| 65 |
-
# Handle both multi-level and single-level columns
|
| 66 |
-
if isinstance(df.columns, pd.MultiIndex):
|
| 67 |
-
df = df['Close'].to_frame()
|
| 68 |
-
elif 'Close' in df.columns:
|
| 69 |
-
df = df[['Close']].copy()
|
| 70 |
-
else:
|
| 71 |
-
# Try to find a price column
|
| 72 |
-
price_col = [col for col in df.columns if 'close' in col.lower()]
|
| 73 |
-
if price_col:
|
| 74 |
-
df = df[[price_col[0]]].copy()
|
| 75 |
-
else:
|
| 76 |
-
return None, f"Could not find price data for {ticker}"
|
| 77 |
-
|
| 78 |
df.columns = ['Price']
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
return
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
def make_arima_forecast(data, days):
|
| 89 |
"""Make ARIMA forecast"""
|
|
|
|
| 11 |
from sklearn.preprocessing import MinMaxScaler
|
| 12 |
import warnings
|
| 13 |
warnings.filterwarnings('ignore')
|
| 14 |
+
import os
|
| 15 |
+
from datetime import datetime, timedelta
|
| 16 |
|
| 17 |
# Load your saved models (update paths as needed)
|
| 18 |
# For Hugging Face, these will be in the same directory as app.py
|
|
|
|
| 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 |
+
ticker = ticker.upper()
|
| 47 |
+
filename = f"{ticker}.csv"
|
| 48 |
+
if not os.path.exists(filename):
|
| 49 |
+
return None, f"No data found for ticker {ticker}. Upload {ticker}.csv to use offline mode."
|
| 50 |
+
|
| 51 |
+
df = pd.read_csv(filename, index_col=0, parse_dates=True)
|
| 52 |
+
if 'Close' in df.columns:
|
| 53 |
+
df = df[['Close']].copy()
|
| 54 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
df.columns = ['Price']
|
| 56 |
+
df.columns = ['Price']
|
| 57 |
+
|
| 58 |
+
df = df.dropna()
|
| 59 |
+
df = df.tail(days)
|
| 60 |
+
if df.empty:
|
| 61 |
+
return None, f"No data available in {filename}"
|
| 62 |
+
return df, None
|
| 63 |
+
|
| 64 |
|
| 65 |
def make_arima_forecast(data, days):
|
| 66 |
"""Make ARIMA forecast"""
|