abnsol commited on
Commit
0b7fcdd
·
verified ·
1 Parent(s): e8a8481

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +8 -21
src/streamlit_app.py CHANGED
@@ -3,7 +3,6 @@ import pandas as pd
3
  import joblib
4
  from tensorflow import keras
5
  from datetime import date
6
- from huggingface_hub import hf_hub_download
7
  import os
8
 
9
  from utils import forecast_to_annual_return, plot_forecast_vs_actual
@@ -20,17 +19,11 @@ from forecast import forecast_lstm
20
  tickers = ["TSLA", "BND", "SPY"]
21
  sequence_length = 60
22
  start_date_data = "2015-01-01"
23
- hf_repo_id = "abnsol/tsla_price_lstm_model"
24
- model_filename = "tsla_price_lstm_model.keras"
25
- scaler_filename = "scaler.joblib"
26
 
27
  APP_DIR = os.path.dirname(os.path.abspath(__file__))
28
  PROJECT_ROOT = os.path.dirname(APP_DIR)
29
-
30
- proc_dir = os.path.join(PROJECT_ROOT,"src","data", "processed")
31
- os.environ["HF_HOME"] = "./.hf_cache"
32
- os.environ["HF_HUB_CACHE"] = "./.hf_cache"
33
- os.environ["TRANSFORMERS_CACHE"] = "./.hf_cache"
34
 
35
  # --- App Layout ---
36
  st.set_page_config(layout="wide")
@@ -47,17 +40,12 @@ if st.sidebar.button("Run Optimization"):
47
  end_date_data = date.today().strftime("%Y-%m-%d")
48
  data = load_processed_prices(proc_dir, tickers)
49
 
50
- # --- 2. Load trained LSTM model & scaler from Hugging Face Hub ---
51
- with st.spinner("Loading forecasting model from Hugging Face Hub..."):
52
- # Load the Keras model directly using the hf:// protocol
53
- # model = keras.models.load_model(f"hf://{hf_repo_id}/{model_filename}")
54
- # Available backend options are: "jax", "torch", "tensorflow".
55
- os.environ["KERAS_BACKEND"] = "tensorflow"
56
- model = keras.models.load_model("hf://abnsol/tsla_price_lstm_model")
57
-
58
-
59
- # Download the scaler file from the Hub and then load it with joblib
60
- scaler_path = hf_hub_download(repo_id=hf_repo_id, filename=scaler_filename)
61
  scaler = joblib.load(scaler_path)
62
 
63
  # --- 3. Prepare TSLA data & forecast ---
@@ -111,7 +99,6 @@ if st.sidebar.button("Run Optimization"):
111
  allocation_df = (weights_df * investment_capital).map('${:,.2f}'.format)
112
  st.dataframe(allocation_df)
113
 
114
-
115
  # --- 8. Efficient Frontier Plot ---
116
  st.subheader("Efficient Frontier")
117
  fig_frontier = plot_efficient_frontier(mu, cov, general_results)
 
3
  import joblib
4
  from tensorflow import keras
5
  from datetime import date
 
6
  import os
7
 
8
  from utils import forecast_to_annual_return, plot_forecast_vs_actual
 
19
  tickers = ["TSLA", "BND", "SPY"]
20
  sequence_length = 60
21
  start_date_data = "2015-01-01"
 
 
 
22
 
23
  APP_DIR = os.path.dirname(os.path.abspath(__file__))
24
  PROJECT_ROOT = os.path.dirname(APP_DIR)
25
+ proc_dir = os.path.join(PROJECT_ROOT, "src", "data", "processed")
26
+ models_dir = os.path.join(PROJECT_ROOT, "models")
 
 
 
27
 
28
  # --- App Layout ---
29
  st.set_page_config(layout="wide")
 
40
  end_date_data = date.today().strftime("%Y-%m-%d")
41
  data = load_processed_prices(proc_dir, tickers)
42
 
43
+ # --- 2. Load trained LSTM model & scaler from local models folder ---
44
+ with st.spinner("Loading forecasting model..."):
45
+ model_path = os.path.join(models_dir, "lstm_model.keras")
46
+ scaler_path = os.path.join(models_dir, "scaler.joblib")
47
+
48
+ model = keras.models.load_model(model_path)
 
 
 
 
 
49
  scaler = joblib.load(scaler_path)
50
 
51
  # --- 3. Prepare TSLA data & forecast ---
 
99
  allocation_df = (weights_df * investment_capital).map('${:,.2f}'.format)
100
  st.dataframe(allocation_df)
101
 
 
102
  # --- 8. Efficient Frontier Plot ---
103
  st.subheader("Efficient Frontier")
104
  fig_frontier = plot_efficient_frontier(mu, cov, general_results)