chkp-talexm
commited on
Commit
·
c50bbfb
1
Parent(s):
6c67532
update
Browse files
app.py
CHANGED
|
@@ -1,69 +1,93 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
if
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
+
import joblib
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
# Hugging Face Model Repo
|
| 8 |
+
MODEL_REPO = "chagu13/is_click_predictor"
|
| 9 |
+
MODEL_DIR = "models"
|
| 10 |
+
os.makedirs(MODEL_DIR, exist_ok=True) # Ensure directory exists
|
| 11 |
+
|
| 12 |
+
# Model Paths
|
| 13 |
+
CATBOOST_MODEL_PATH = os.path.join(MODEL_DIR, "catboost_model.pkl")
|
| 14 |
+
XGB_MODEL_PATH = os.path.join(MODEL_DIR, "xgb_model.pkl")
|
| 15 |
+
RF_MODEL_PATH = os.path.join(MODEL_DIR, "rf_model.pkl")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Function to download models from Hugging Face if not found locally
|
| 19 |
+
def load_models():
|
| 20 |
+
if not os.path.exists(CATBOOST_MODEL_PATH):
|
| 21 |
+
hf_hub_download(repo_id=MODEL_REPO, filename="models/catboost_model.pkl", local_dir=MODEL_DIR)
|
| 22 |
+
if not os.path.exists(XGB_MODEL_PATH):
|
| 23 |
+
hf_hub_download(repo_id=MODEL_REPO, filename="models/xgb_model.pkl", local_dir=MODEL_DIR)
|
| 24 |
+
if not os.path.exists(RF_MODEL_PATH):
|
| 25 |
+
hf_hub_download(repo_id=MODEL_REPO, filename="models/rf_model.pkl", local_dir=MODEL_DIR)
|
| 26 |
+
|
| 27 |
+
catboost_model = joblib.load(CATBOOST_MODEL_PATH)
|
| 28 |
+
xgb_model = joblib.load(XGB_MODEL_PATH)
|
| 29 |
+
rf_model = joblib.load(RF_MODEL_PATH)
|
| 30 |
+
|
| 31 |
+
return catboost_model, xgb_model, rf_model
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Load models at startup
|
| 35 |
+
st.title("Is_Click Predictor - ML Model Inference")
|
| 36 |
+
st.info("Upload a CSV file, and the trained models will predict click probability.")
|
| 37 |
+
|
| 38 |
+
catboost, xgb, rf = load_models()
|
| 39 |
+
|
| 40 |
+
# Upload File
|
| 41 |
+
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
| 42 |
+
if uploaded_file:
|
| 43 |
+
input_df = pd.read_csv(uploaded_file)
|
| 44 |
+
st.success("File uploaded successfully!")
|
| 45 |
+
|
| 46 |
+
# Make Predictions
|
| 47 |
+
st.subheader("Predictions in Progress...")
|
| 48 |
+
|
| 49 |
+
catboost_preds = catboost.predict(input_df)
|
| 50 |
+
xgb_preds = xgb.predict(input_df)
|
| 51 |
+
rf_preds = rf.predict(input_df)
|
| 52 |
+
|
| 53 |
+
catboost_probs = catboost.predict_proba(input_df)[:, 1]
|
| 54 |
+
xgb_probs = xgb.predict_proba(input_df)[:, 1]
|
| 55 |
+
rf_probs = rf.predict_proba(input_df)[:, 1]
|
| 56 |
+
|
| 57 |
+
# Combine results
|
| 58 |
+
predictions_df = pd.DataFrame({
|
| 59 |
+
"CatBoost": catboost_preds,
|
| 60 |
+
"XGBoost": xgb_preds,
|
| 61 |
+
"RandomForest": rf_preds
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
# Apply "at least one model predicts 1" rule
|
| 65 |
+
predictions_df["is_click_predicted"] = predictions_df.max(axis=1)
|
| 66 |
+
|
| 67 |
+
# Generate probability file
|
| 68 |
+
probabilities_df = pd.DataFrame({
|
| 69 |
+
"CatBoost_Prob": catboost_probs,
|
| 70 |
+
"XGBoost_Prob": xgb_probs,
|
| 71 |
+
"RandomForest_Prob": rf_probs
|
| 72 |
+
})
|
| 73 |
+
|
| 74 |
+
# Save results
|
| 75 |
+
binary_predictions_path = "binary_predictions.csv"
|
| 76 |
+
filtered_predictions_path = "filtered_predictions.csv"
|
| 77 |
+
probabilities_path = "model_probabilities.csv"
|
| 78 |
+
|
| 79 |
+
predictions_df.to_csv(binary_predictions_path, index=False)
|
| 80 |
+
predictions_df[predictions_df["is_click_predicted"] == 1].to_csv(filtered_predictions_path, index=False)
|
| 81 |
+
probabilities_df.to_csv(probabilities_path, index=False)
|
| 82 |
+
|
| 83 |
+
st.success("Predictions completed! Download results below.")
|
| 84 |
+
|
| 85 |
+
# Download Buttons
|
| 86 |
+
with open(binary_predictions_path, "rb") as f:
|
| 87 |
+
st.download_button("Download Binary Predictions (0/1)", f, file_name="binary_predictions.csv")
|
| 88 |
+
|
| 89 |
+
with open(filtered_predictions_path, "rb") as f:
|
| 90 |
+
st.download_button("Download Clicked Predictions (Only 1s)", f, file_name="filtered_predictions.csv")
|
| 91 |
+
|
| 92 |
+
with open(probabilities_path, "rb") as f:
|
| 93 |
+
st.download_button("Download Probability Predictions", f, file_name="model_probabilities.csv")
|