Lalithas commited on
Commit
c2db7f2
·
verified ·
1 Parent(s): 5b5b7c1

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +30 -24
app.py CHANGED
@@ -2,26 +2,32 @@ import streamlit as st
2
  import pandas as pd
3
  import joblib
4
  import os
5
- from huggingface_hub import hf_hub_download # Added import for Hugging Face download
6
 
7
  # Get Hugging Face token from environment variables
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
  model_repo_id = "Lalithas/Superkart-Prediction-Model"
10
  model_filename = "best_random_forest_model.joblib"
11
 
12
- model = None # Initialize model to None
13
- try:
14
- # Download the model from Hugging Face Model Hub
15
- local_model_path = hf_hub_download(
16
- repo_id=model_repo_id,
17
- filename=model_filename,
18
- repo_type="model",
19
- token=HF_TOKEN # Use token for private models or if rate limits apply
20
- )
21
- model = joblib.load(local_model_path)
22
- st.success("Model loaded successfully from Hugging Face Model Hub!")
23
- except Exception as e:
24
- st.error(f"Error loading model from Hugging Face Model Hub: {e}")
 
 
 
 
 
 
25
 
26
  st.title("Superkart Sales Prediction")
27
 
@@ -55,16 +61,16 @@ store_type_map = {category: idx for idx, category in enumerate(store_type_option
55
  if st.button("Predict Sales"):
56
  if model is not None: # Only proceed if model was loaded successfully
57
  # Create a DataFrame for prediction
58
- input_data = pd.DataFrame([{
59
- "Product_Weight": product_weight,
60
- "Product_Sugar_Content": sugar_content_map.get(product_sugar_content, -1),
61
- "Product_Allocated_Area": allocated_area,
62
- "Product_MRP": product_mrp,
63
- "Product_Type": product_type_map.get(product_type, -1),
64
- "Store_Size": store_size_map.get(store_size, -1),
65
- "Store_Location_City_Type": city_type_map.get(store_location_city_type, -1),
66
- "Store_Type": store_type_map.get(store_type, -1)
67
- }])
68
  # Ensure columns are in the same order as training data (crucial for consistent predictions)
69
  # This assumes Xtrain.columns was saved or known.
70
  # For this example, we'll try to match the order from the `prep.py` script's output Xtrain.
 
2
  import pandas as pd
3
  import joblib
4
  import os
5
+ from huggingface_hub import hf_hub_download
6
 
7
  # Get Hugging Face token from environment variables
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
  model_repo_id = "Lalithas/Superkart-Prediction-Model"
10
  model_filename = "best_random_forest_model.joblib"
11
 
12
+ # Use st.cache_resource to cache the model loading
13
+ @st.cache_resource
14
+ def load_model():
15
+ try:
16
+ # Download the model from Hugging Face Model Hub
17
+ local_model_path = hf_hub_download(
18
+ repo_id=model_repo_id,
19
+ filename=model_filename,
20
+ repo_type="model",
21
+ token=HF_TOKEN # Use token for private models or if rate limits apply
22
+ )
23
+ model = joblib.load(local_model_path)
24
+ st.success("Model loaded successfully from Hugging Face Model Hub!")
25
+ return model
26
+ except Exception as e:
27
+ st.error(f"Error loading model from Hugging Face Model Hub: {e}")
28
+ return None
29
+
30
+ model = load_model()
31
 
32
  st.title("Superkart Sales Prediction")
33
 
 
61
  if st.button("Predict Sales"):
62
  if model is not None: # Only proceed if model was loaded successfully
63
  # Create a DataFrame for prediction
64
+ input_data = pd.DataFrame({
65
+ "Product_Weight": [product_weight],
66
+ "Product_Sugar_Content": [sugar_content_map.get(product_sugar_content, -1)],
67
+ "Product_Allocated_Area": [allocated_area],
68
+ "Product_MRP": [product_mrp],
69
+ "Product_Type": [product_type_map.get(product_type, -1)],
70
+ "Store_Size": [store_size_map.get(store_size, -1)],
71
+ "Store_Location_City_Type": [city_type_map.get(store_location_city_type, -1)],
72
+ "Store_Type": [store_type_map.get(store_type, -1)]
73
+ })
74
  # Ensure columns are in the same order as training data (crucial for consistent predictions)
75
  # This assumes Xtrain.columns was saved or known.
76
  # For this example, we'll try to match the order from the `prep.py` script's output Xtrain.