Upload folder using huggingface_hub
Browse files- Dockerfile +3 -2
- app.py +84 -0
- requirements.txt +7 -3
- src/streamlit_app.py +81 -38
Dockerfile
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
|
@@ -17,4 +18,4 @@ EXPOSE 8501
|
|
| 17 |
|
| 18 |
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
|
| 19 |
|
| 20 |
-
ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
|
|
|
| 1 |
+
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
|
| 4 |
WORKDIR /app
|
| 5 |
|
|
|
|
| 18 |
|
| 19 |
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
|
| 20 |
|
| 21 |
+
ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.headless=true", "--browser.gatherUsageStats=false", "--server.address=0.0.0.0"]
|
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["STREAMLIT_SERVER_HEADLESS"] = "true"
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
import joblib
|
| 8 |
+
|
| 9 |
+
# ---- Streamlit bootstrap ----
|
| 10 |
+
st.empty()
|
| 11 |
+
st.set_page_config(page_title="SuperKart Sales Prediction")
|
| 12 |
+
st.set_option("browser.gatherUsageStats", False)
|
| 13 |
+
|
| 14 |
+
# ---- Read secrets ----
|
| 15 |
+
Repo_ID = os.getenv("Repo_ID")
|
| 16 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
+
|
| 18 |
+
if not Repo_ID:
|
| 19 |
+
st.error("❌ Repo_ID secret is missing in HF Space")
|
| 20 |
+
st.stop()
|
| 21 |
+
|
| 22 |
+
# ---- Render UI immediately ----
|
| 23 |
+
st.title("🛒 SuperKart Sales Prediction")
|
| 24 |
+
st.write("✅ UI rendered successfully")
|
| 25 |
+
|
| 26 |
+
# ---- Load model lazily ----
|
| 27 |
+
@st.cache_resource
|
| 28 |
+
def load_model():
|
| 29 |
+
model_path = hf_hub_download(
|
| 30 |
+
repo_id=Repo_ID,
|
| 31 |
+
filename="best_superkart_sales_model_v1.joblib",
|
| 32 |
+
repo_type="model",
|
| 33 |
+
token=HF_TOKEN
|
| 34 |
+
)
|
| 35 |
+
return joblib.load(model_path)
|
| 36 |
+
|
| 37 |
+
# ---- Load model AFTER UI ----
|
| 38 |
+
try:
|
| 39 |
+
with st.spinner("Loading ML model…"):
|
| 40 |
+
model = load_model()
|
| 41 |
+
st.success("✅ Model loaded successfully")
|
| 42 |
+
except Exception as e:
|
| 43 |
+
st.error("❌ Model failed to load")
|
| 44 |
+
st.exception(e)
|
| 45 |
+
st.stop()
|
| 46 |
+
|
| 47 |
+
# ---- UI ----
|
| 48 |
+
st.write("""
|
| 49 |
+
This application predicts the **total product sales** for SuperKart
|
| 50 |
+
based on product characteristics and store attributes.
|
| 51 |
+
""")
|
| 52 |
+
|
| 53 |
+
product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
|
| 54 |
+
product_type = st.selectbox("Product Type", [
|
| 55 |
+
"Dairy", "Soft Drinks", "Meat", "Fruits and Vegetables",
|
| 56 |
+
"Baking Goods", "Frozen Foods", "Health and Hygiene",
|
| 57 |
+
"Canned", "Household", "Snack Foods", "Others"
|
| 58 |
+
])
|
| 59 |
+
store_id = st.selectbox("Store ID", ["OUT001", "OUT002", "OUT003", "OUT004", "OUT005"])
|
| 60 |
+
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
|
| 61 |
+
store_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
|
| 62 |
+
store_type = st.selectbox("Store Type", ["Grocery Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"])
|
| 63 |
+
|
| 64 |
+
product_weight = st.number_input("Product Weight (kg)", 0.1, 50.0, 10.0)
|
| 65 |
+
product_allocated_area = st.number_input("Product Allocated Area", 0.001, 1.0, 0.05, step=0.001)
|
| 66 |
+
product_mrp = st.number_input("Product MRP", 1.0, 1000.0, 100.0)
|
| 67 |
+
store_est_year = st.number_input("Store Establishment Year", 1950, 2025, 2005)
|
| 68 |
+
|
| 69 |
+
input_data = pd.DataFrame([{
|
| 70 |
+
"Product_Weight": product_weight,
|
| 71 |
+
"Product_Allocated_Area": product_allocated_area,
|
| 72 |
+
"Product_MRP": product_mrp,
|
| 73 |
+
"Store_Establishment_Year": store_est_year,
|
| 74 |
+
"Product_Sugar_Content": product_sugar_content,
|
| 75 |
+
"Product_Type": product_type,
|
| 76 |
+
"Store_Id": store_id,
|
| 77 |
+
"Store_Size": store_size,
|
| 78 |
+
"Store_Location_City_Type": store_city_type,
|
| 79 |
+
"Store_Type": store_type
|
| 80 |
+
}])
|
| 81 |
+
|
| 82 |
+
if st.button("Predict Sales"):
|
| 83 |
+
prediction = model.predict(input_data)[0]
|
| 84 |
+
st.success(f"Estimated Product Sales: **₹ {prediction:,.2f}**")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,7 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.32.6
|
| 2 |
+
datasets==3.6.0
|
| 3 |
+
pandas==2.2.2
|
| 4 |
+
scikit-learn==1.6.1
|
| 5 |
+
xgboost==2.1.4
|
| 6 |
+
mlflow==3.0.1
|
| 7 |
+
streamlit==1.28.0
|
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,83 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["STREAMLIT_SERVER_HEADLESS"] = "true"
|
| 3 |
+
|
| 4 |
import streamlit as st
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
import joblib
|
| 8 |
+
|
| 9 |
+
# ---- Streamlit bootstrap ----
|
| 10 |
+
st.set_page_config(page_title="SuperKart Sales Prediction")
|
| 11 |
+
|
| 12 |
+
# ---- Read secrets ----
|
| 13 |
+
Repo_ID = os.getenv("Repo_ID")
|
| 14 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
+
|
| 16 |
+
if not Repo_ID:
|
| 17 |
+
st.error("Repo_ID secret is missing in HF Space")
|
| 18 |
+
st.stop()
|
| 19 |
+
|
| 20 |
+
# ---- Render UI immediately ----
|
| 21 |
+
st.title("🛒 SuperKart Sales Prediction")
|
| 22 |
+
st.write("UI rendered successfully")
|
| 23 |
+
|
| 24 |
+
# ---- Load model lazily ----
|
| 25 |
+
|
| 26 |
+
repo_path=f"{Repo_ID}/superkart-sales-model"
|
| 27 |
+
|
| 28 |
+
@st.cache_resource
|
| 29 |
+
def load_model():
|
| 30 |
+
try:
|
| 31 |
+
model_path = hf_hub_download(
|
| 32 |
+
repo_id=repo_path,
|
| 33 |
+
filename="best_superkart_sales_model_v1.joblib",
|
| 34 |
+
repo_type="model",
|
| 35 |
+
token=HF_TOKEN
|
| 36 |
+
)
|
| 37 |
+
return joblib.load(model_path)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
st.error("Failed to download model from Hugging Face")
|
| 40 |
+
st.exception(e)
|
| 41 |
+
st.stop()
|
| 42 |
+
|
| 43 |
+
model = load_model()
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# ---- UI ----
|
| 47 |
+
st.write("""
|
| 48 |
+
This application predicts the **total product sales** for SuperKart
|
| 49 |
+
based on product characteristics and store attributes.
|
| 50 |
+
""")
|
| 51 |
+
|
| 52 |
+
product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
|
| 53 |
+
product_type = st.selectbox("Product Type", [
|
| 54 |
+
"Dairy", "Soft Drinks", "Meat", "Fruits and Vegetables",
|
| 55 |
+
"Baking Goods", "Frozen Foods", "Health and Hygiene",
|
| 56 |
+
"Canned", "Household", "Snack Foods", "Others"
|
| 57 |
+
])
|
| 58 |
+
store_id = st.selectbox("Store ID", ["OUT001", "OUT002", "OUT003", "OUT004", "OUT005"])
|
| 59 |
+
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
|
| 60 |
+
store_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
|
| 61 |
+
store_type = st.selectbox("Store Type", ["Grocery Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"])
|
| 62 |
+
|
| 63 |
+
product_weight = st.number_input("Product Weight (kg)", 0.1, 50.0, 10.0)
|
| 64 |
+
product_allocated_area = st.number_input("Product Allocated Area", 0.001, 1.0, 0.05, step=0.001)
|
| 65 |
+
product_mrp = st.number_input("Product MRP", 1.0, 1000.0, 100.0)
|
| 66 |
+
store_est_year = st.number_input("Store Establishment Year", 1950, 2025, 2005)
|
| 67 |
+
|
| 68 |
+
input_data = pd.DataFrame([{
|
| 69 |
+
"Product_Weight": product_weight,
|
| 70 |
+
"Product_Allocated_Area": product_allocated_area,
|
| 71 |
+
"Product_MRP": product_mrp,
|
| 72 |
+
"Store_Establishment_Year": store_est_year,
|
| 73 |
+
"Product_Sugar_Content": product_sugar_content,
|
| 74 |
+
"Product_Type": product_type,
|
| 75 |
+
"Store_Id": store_id,
|
| 76 |
+
"Store_Size": store_size,
|
| 77 |
+
"Store_Location_City_Type": store_city_type,
|
| 78 |
+
"Store_Type": store_type
|
| 79 |
+
}])
|
| 80 |
|
| 81 |
+
if st.button("Predict Sales"):
|
| 82 |
+
prediction = model.predict(input_data)[0]
|
| 83 |
+
st.success(f"Estimated Product Sales: **₹ {prediction:,.2f}**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|