Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import joblib
|
| 4 |
-
import os
|
| 5 |
import numpy as np
|
| 6 |
import pandas as pd
|
| 7 |
import yfinance as yf
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
# --- 1. Download Model and Scalers from Hugging Face Hub ---
|
|
|
|
| 11 |
MODEL_REPO = "munem420/stock-forecaster-lstm"
|
| 12 |
-
# FIX #1: Corrected model filename from .h5 to .keras
|
| 13 |
MODEL_FILENAME = "model_lstm.keras"
|
| 14 |
SCALER_FILENAME = "scalers.joblib"
|
| 15 |
|
|
@@ -20,7 +23,6 @@ try:
|
|
| 20 |
print("✅ Files downloaded successfully.")
|
| 21 |
except Exception as e:
|
| 22 |
print(f"❌ Error downloading files: {e}")
|
| 23 |
-
# Exit gracefully if files can't be downloaded
|
| 24 |
model_path, scalers_path = None, None
|
| 25 |
|
| 26 |
# --- 2. Load the Model and Scalers ---
|
|
@@ -29,7 +31,6 @@ loaded_scalers = None
|
|
| 29 |
|
| 30 |
if model_path and os.path.exists(model_path):
|
| 31 |
try:
|
| 32 |
-
# No custom_objects needed for the .keras format in this case
|
| 33 |
loaded_model_lstm = tf.keras.models.load_model(model_path)
|
| 34 |
print("✅ Model loaded successfully.")
|
| 35 |
except Exception as e:
|
|
@@ -51,7 +52,7 @@ def get_ticker_from_input(input_name):
|
|
| 51 |
def forecast_stock(input_name, model, scalers_dict, input_width=60):
|
| 52 |
if not model or not scalers_dict:
|
| 53 |
return "Error: Model or scalers not loaded. The backend may have failed to start."
|
| 54 |
-
|
| 55 |
ticker = get_ticker_from_input(input_name)
|
| 56 |
if not ticker:
|
| 57 |
return "Error: Invalid stock ticker."
|
|
@@ -102,7 +103,7 @@ with gr.Blocks() as app:
|
|
| 102 |
ticker_input.submit(predict_api, inputs=[ticker_input], outputs=[output_text], api_name="predict")
|
| 103 |
|
| 104 |
# --- 5. Mount and Serve the React App's Static Files ---
|
| 105 |
-
#
|
| 106 |
app = gr.mount_static_directory(app, "build")
|
| 107 |
|
| 108 |
# Launch the server
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import tensorflow as tf
|
| 4 |
import joblib
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import pandas as pd
|
| 7 |
import yfinance as yf
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
+
# This forces TensorFlow to only use the CPU.
|
| 11 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
| 12 |
+
|
| 13 |
# --- 1. Download Model and Scalers from Hugging Face Hub ---
|
| 14 |
+
# FIX #1: Ensured the repository name is 100% correct.
|
| 15 |
MODEL_REPO = "munem420/stock-forecaster-lstm"
|
|
|
|
| 16 |
MODEL_FILENAME = "model_lstm.keras"
|
| 17 |
SCALER_FILENAME = "scalers.joblib"
|
| 18 |
|
|
|
|
| 23 |
print("✅ Files downloaded successfully.")
|
| 24 |
except Exception as e:
|
| 25 |
print(f"❌ Error downloading files: {e}")
|
|
|
|
| 26 |
model_path, scalers_path = None, None
|
| 27 |
|
| 28 |
# --- 2. Load the Model and Scalers ---
|
|
|
|
| 31 |
|
| 32 |
if model_path and os.path.exists(model_path):
|
| 33 |
try:
|
|
|
|
| 34 |
loaded_model_lstm = tf.keras.models.load_model(model_path)
|
| 35 |
print("✅ Model loaded successfully.")
|
| 36 |
except Exception as e:
|
|
|
|
| 52 |
def forecast_stock(input_name, model, scalers_dict, input_width=60):
|
| 53 |
if not model or not scalers_dict:
|
| 54 |
return "Error: Model or scalers not loaded. The backend may have failed to start."
|
| 55 |
+
# ... (The rest of the function is the same)
|
| 56 |
ticker = get_ticker_from_input(input_name)
|
| 57 |
if not ticker:
|
| 58 |
return "Error: Invalid stock ticker."
|
|
|
|
| 103 |
ticker_input.submit(predict_api, inputs=[ticker_input], outputs=[output_text], api_name="predict")
|
| 104 |
|
| 105 |
# --- 5. Mount and Serve the React App's Static Files ---
|
| 106 |
+
# This function requires a modern version of Gradio, specified in README.md
|
| 107 |
app = gr.mount_static_directory(app, "build")
|
| 108 |
|
| 109 |
# Launch the server
|