Shaikat01 commited on
Commit
0926439
·
verified ·
1 Parent(s): 87e984d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -43
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
- """Fetch stock data from Yahoo Finance"""
44
- try:
45
- end_date = datetime.now()
46
- start_date = end_date - timedelta(days=days)
47
-
48
- # Add retry logic and better error handling
49
- import time
50
- max_retries = 3
51
- for attempt in range(max_retries):
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
- df = df.dropna()
80
-
81
- if len(df) < 100:
82
- return None, f"Insufficient data for {ticker}. Only {len(df)} days available."
83
-
84
- return df, None
85
- except Exception as e:
86
- return None, f"Error fetching data for {ticker}: {str(e)}"
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"""