Spaces:
Runtime error
Runtime error
Commit ·
b6ced55
1
Parent(s): 64e0a91
Added streamlit app, dockerfile and requirements
Browse files- .github/workflows/pipeline.yml +36 -0
- Dockerfile +7 -14
- app.py +35 -0
- deployment/app.py +35 -0
- deployment/predict.py +23 -0
- deployment/push_to_hf.py +30 -0
- deployment/requirements.txt +7 -0
- predict.py +23 -0
- requirements.txt +6 -2
- src/streamlit_app.py +0 -40
.github/workflows/pipeline.yml
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: SuperKart-MLOps-Pipeline
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches: [ main ]
|
| 6 |
+
|
| 7 |
+
jobs:
|
| 8 |
+
train-and-upload:
|
| 9 |
+
runs-on: ubuntu-latest
|
| 10 |
+
|
| 11 |
+
steps:
|
| 12 |
+
- name: Checkout Repo
|
| 13 |
+
uses: actions/checkout@v4
|
| 14 |
+
|
| 15 |
+
- name: Setup Python
|
| 16 |
+
uses: actions/setup-python@v5
|
| 17 |
+
with:
|
| 18 |
+
python-version: '3.10'
|
| 19 |
+
|
| 20 |
+
- name: Install Dependencies
|
| 21 |
+
run: |
|
| 22 |
+
pip install -U pip
|
| 23 |
+
pip install pandas numpy scikit-learn xgboost joblib huggingface_hub
|
| 24 |
+
|
| 25 |
+
- name: Train Model
|
| 26 |
+
env:
|
| 27 |
+
HF_DATASET_REPO: ${{ secrets.HF_DATASET_REPO }}
|
| 28 |
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
| 29 |
+
run: |
|
| 30 |
+
python train.py
|
| 31 |
+
|
| 32 |
+
- name: Upload Artifacts
|
| 33 |
+
uses: actions/upload-artifact@v4
|
| 34 |
+
with:
|
| 35 |
+
name: model-artifacts
|
| 36 |
+
path: artifacts/
|
Dockerfile
CHANGED
|
@@ -1,20 +1,13 @@
|
|
| 1 |
-
FROM python:3.
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
curl \
|
| 8 |
-
git \
|
| 9 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
-
COPY
|
| 12 |
-
COPY src/ ./src/
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
| 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 |
+
FROM python:3.10-slim
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
COPY . .
|
|
|
|
| 9 |
|
| 10 |
+
ENV HF_MODEL_REPO="manoj112025/SuperKartSalesModel"
|
| 11 |
+
EXPOSE 7860
|
| 12 |
|
| 13 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from predict import predict_one
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="SuperKart Sales Predictor", page_icon="🛒", layout="centered")
|
| 6 |
+
st.title("🛒 SuperKart Sales Prediction App")
|
| 7 |
+
|
| 8 |
+
st.markdown("Enter product & store details to predict **Product_Store_Sales_Total**")
|
| 9 |
+
|
| 10 |
+
# Input UI
|
| 11 |
+
inputs = {
|
| 12 |
+
"Product_Weight": st.number_input("Product Weight", min_value=0.0, value=12.0, step=0.1),
|
| 13 |
+
"Product_Sugar_Content": st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"]),
|
| 14 |
+
"Product_Allocated_Area": st.number_input("Product Allocated Area", min_value=0.0, max_value=1.0, value=0.05),
|
| 15 |
+
"Product_Type": st.selectbox("Product Type", [
|
| 16 |
+
"Frozen Foods", "Dairy", "Canned", "Baking Goods", "Health and Hygiene", "Snack Foods",
|
| 17 |
+
"Meat", "Household", "Hard Drinks", "Fruits and Vegetables", "Breads",
|
| 18 |
+
"Breakfast", "Seafood", "Starchy Foods", "Soft Drinks", "Others",
|
| 19 |
+
"Food Mart", "Departmental Store", "Supermarket Type1", "Supermarket Type2"
|
| 20 |
+
]),
|
| 21 |
+
"Product_MRP": st.number_input("Product MRP", min_value=0.0, value=150.0),
|
| 22 |
+
"Store_Id": st.selectbox("Store ID", ["OUT001", "OUT002", "OUT003", "OUT004"]),
|
| 23 |
+
"Store_Establishment_Year": st.number_input("Store Establishment Year", value=2000, step=1),
|
| 24 |
+
"Store_Size": st.selectbox("Store Size", ["Small", "Medium", "High"]),
|
| 25 |
+
"Store_Location_City_Type": st.selectbox("Store City Type", ["Tier 1", "Tier 2", "Tier 3"]),
|
| 26 |
+
"Store_Type": st.selectbox("Store Type", ["Departmental Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"])
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
if st.button("Predict Sales"):
|
| 30 |
+
df = pd.DataFrame([inputs])
|
| 31 |
+
try:
|
| 32 |
+
prediction = predict_one(df)
|
| 33 |
+
st.success(f"Predicted Sales: **{float(prediction[0]):,.2f}**")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
st.error(f"Prediction Failed: {e}")
|
deployment/app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from predict import predict_one
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="SuperKart Sales Predictor", page_icon="🛒", layout="centered")
|
| 6 |
+
st.title("🛒 SuperKart Sales Prediction App")
|
| 7 |
+
|
| 8 |
+
st.markdown("Enter product & store details to predict **Product_Store_Sales_Total**")
|
| 9 |
+
|
| 10 |
+
# Input UI
|
| 11 |
+
inputs = {
|
| 12 |
+
"Product_Weight": st.number_input("Product Weight", min_value=0.0, value=12.0, step=0.1),
|
| 13 |
+
"Product_Sugar_Content": st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"]),
|
| 14 |
+
"Product_Allocated_Area": st.number_input("Product Allocated Area", min_value=0.0, max_value=1.0, value=0.05),
|
| 15 |
+
"Product_Type": st.selectbox("Product Type", [
|
| 16 |
+
"Frozen Foods", "Dairy", "Canned", "Baking Goods", "Health and Hygiene", "Snack Foods",
|
| 17 |
+
"Meat", "Household", "Hard Drinks", "Fruits and Vegetables", "Breads",
|
| 18 |
+
"Breakfast", "Seafood", "Starchy Foods", "Soft Drinks", "Others",
|
| 19 |
+
"Food Mart", "Departmental Store", "Supermarket Type1", "Supermarket Type2"
|
| 20 |
+
]),
|
| 21 |
+
"Product_MRP": st.number_input("Product MRP", min_value=0.0, value=150.0),
|
| 22 |
+
"Store_Id": st.selectbox("Store ID", ["OUT001", "OUT002", "OUT003", "OUT004"]),
|
| 23 |
+
"Store_Establishment_Year": st.number_input("Store Establishment Year", value=2000, step=1),
|
| 24 |
+
"Store_Size": st.selectbox("Store Size", ["Small", "Medium", "High"]),
|
| 25 |
+
"Store_Location_City_Type": st.selectbox("Store City Type", ["Tier 1", "Tier 2", "Tier 3"]),
|
| 26 |
+
"Store_Type": st.selectbox("Store Type", ["Departmental Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"])
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
if st.button("Predict Sales"):
|
| 30 |
+
df = pd.DataFrame([inputs])
|
| 31 |
+
try:
|
| 32 |
+
prediction = predict_one(df)
|
| 33 |
+
st.success(f"Predicted Sales: **{float(prediction[0]):,.2f}**")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
st.error(f"Prediction Failed: {e}")
|
deployment/predict.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
|
| 6 |
+
MODEL_REPO = os.getenv("HF_MODEL_REPO", "manoj112025/SuperKartSalesModel")
|
| 7 |
+
MODEL_FILE = "model.joblib"
|
| 8 |
+
PREPROCESSOR_FILE = "preprocessor.joblib"
|
| 9 |
+
|
| 10 |
+
def load_artifacts():
|
| 11 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE, repo_type="model")
|
| 12 |
+
pre_path = hf_hub_download(repo_id=MODEL_REPO, filename=PREPROCESSOR_FILE, repo_type="model")
|
| 13 |
+
|
| 14 |
+
model = joblib.load(model_path)
|
| 15 |
+
preprocessor = joblib.load(pre_path)
|
| 16 |
+
|
| 17 |
+
return preprocessor, model
|
| 18 |
+
|
| 19 |
+
def predict_one(df: pd.DataFrame):
|
| 20 |
+
preprocessor, model = load_artifacts()
|
| 21 |
+
X = preprocessor.transform(df)
|
| 22 |
+
y = model.predict(X)
|
| 23 |
+
return y
|
deployment/push_to_hf.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def move_files_to_hf():
|
| 6 |
+
|
| 7 |
+
api = HfApi()
|
| 8 |
+
space_id = "manoj112025/superkart-sales-app"
|
| 9 |
+
|
| 10 |
+
files = {
|
| 11 |
+
"app.py": "app.py",
|
| 12 |
+
"predict.py": "predict.py",
|
| 13 |
+
"requirements.txt": "requirements.txt",
|
| 14 |
+
"Dockerfile": "Dockerfile"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
operations = []
|
| 18 |
+
|
| 19 |
+
for src, dst in files.items():
|
| 20 |
+
with open(src, "rb") as f:
|
| 21 |
+
operations.append(("add_or_update", dst, f.read()))
|
| 22 |
+
|
| 23 |
+
api.create_commit(
|
| 24 |
+
repo_id=space_id,
|
| 25 |
+
repo_type="space",
|
| 26 |
+
commit_message="Update deployment files",
|
| 27 |
+
operations=operations
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
print("Files uploaded to HF Space:", space_id)
|
deployment/requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.31.0
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
xgboost
|
| 6 |
+
joblib
|
| 7 |
+
huggingface_hub
|
predict.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
|
| 6 |
+
MODEL_REPO = os.getenv("HF_MODEL_REPO", "manoj112025/SuperKartSalesModel")
|
| 7 |
+
MODEL_FILE = "model.joblib"
|
| 8 |
+
PREPROCESSOR_FILE = "preprocessor.joblib"
|
| 9 |
+
|
| 10 |
+
def load_artifacts():
|
| 11 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE, repo_type="model")
|
| 12 |
+
pre_path = hf_hub_download(repo_id=MODEL_REPO, filename=PREPROCESSOR_FILE, repo_type="model")
|
| 13 |
+
|
| 14 |
+
model = joblib.load(model_path)
|
| 15 |
+
preprocessor = joblib.load(pre_path)
|
| 16 |
+
|
| 17 |
+
return preprocessor, model
|
| 18 |
+
|
| 19 |
+
def predict_one(df: pd.DataFrame):
|
| 20 |
+
preprocessor, model = load_artifacts()
|
| 21 |
+
X = preprocessor.transform(df)
|
| 22 |
+
y = model.predict(X)
|
| 23 |
+
return y
|
requirements.txt
CHANGED
|
@@ -1,3 +1,7 @@
|
|
| 1 |
-
|
| 2 |
pandas
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.31.0
|
| 2 |
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
xgboost
|
| 6 |
+
joblib
|
| 7 |
+
huggingface_hub
|
src/streamlit_app.py
DELETED
|
@@ -1,40 +0,0 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import streamlit as st
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
# Welcome to Streamlit!
|
| 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 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|