chkp-talexm commited on
Commit ·
2d3f7d4
1
Parent(s): 2a7c621
update
Browse files
app.py
CHANGED
|
@@ -2,6 +2,17 @@ import os
|
|
| 2 |
import joblib
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Hugging Face Model Repo
|
| 6 |
MODEL_REPO = "chagu13/is_click_predictor"
|
| 7 |
MODEL_DIR = "models"
|
|
@@ -48,3 +59,65 @@ def load_models():
|
|
| 48 |
except Exception as e:
|
| 49 |
print(f"❌ Error loading models: {e}")
|
| 50 |
return None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import joblib
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import os
|
| 8 |
+
import joblib
|
| 9 |
+
from huggingface_hub import hf_hub_download
|
| 10 |
+
|
| 11 |
+
# Hugging Face Model Repo
|
| 12 |
+
MODEL_REPO = "chagu13/is_click_predictor"
|
| 13 |
+
MODEL_DIR = "models"
|
| 14 |
+
os.makedirs(MODEL_DIR, exist_ok=True) # Ensure directory exists
|
| 15 |
+
|
| 16 |
# Hugging Face Model Repo
|
| 17 |
MODEL_REPO = "chagu13/is_click_predictor"
|
| 18 |
MODEL_DIR = "models"
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
print(f"❌ Error loading models: {e}")
|
| 61 |
return None, None, None
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Load models at startup
|
| 65 |
+
st.title("Is_Click Predictor - ML Model Inference")
|
| 66 |
+
st.info("Upload a CSV file, and the trained models will predict click probability.")
|
| 67 |
+
|
| 68 |
+
catboost, xgb, rf = load_models()
|
| 69 |
+
|
| 70 |
+
# Upload File
|
| 71 |
+
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
| 72 |
+
if uploaded_file:
|
| 73 |
+
input_df = pd.read_csv(uploaded_file)
|
| 74 |
+
st.success("File uploaded successfully!")
|
| 75 |
+
|
| 76 |
+
# Make Predictions
|
| 77 |
+
st.subheader("Predictions in Progress...")
|
| 78 |
+
|
| 79 |
+
catboost_preds = catboost.predict(input_df)
|
| 80 |
+
xgb_preds = xgb.predict(input_df)
|
| 81 |
+
rf_preds = rf.predict(input_df)
|
| 82 |
+
|
| 83 |
+
catboost_probs = catboost.predict_proba(input_df)[:, 1]
|
| 84 |
+
xgb_probs = xgb.predict_proba(input_df)[:, 1]
|
| 85 |
+
rf_probs = rf.predict_proba(input_df)[:, 1]
|
| 86 |
+
|
| 87 |
+
# Combine results
|
| 88 |
+
predictions_df = pd.DataFrame({
|
| 89 |
+
"CatBoost": catboost_preds,
|
| 90 |
+
"XGBoost": xgb_preds,
|
| 91 |
+
"RandomForest": rf_preds
|
| 92 |
+
})
|
| 93 |
+
|
| 94 |
+
# Apply "at least one model predicts 1" rule
|
| 95 |
+
predictions_df["is_click_predicted"] = predictions_df.max(axis=1)
|
| 96 |
+
|
| 97 |
+
# Generate probability file
|
| 98 |
+
probabilities_df = pd.DataFrame({
|
| 99 |
+
"CatBoost_Prob": catboost_probs,
|
| 100 |
+
"XGBoost_Prob": xgb_probs,
|
| 101 |
+
"RandomForest_Prob": rf_probs
|
| 102 |
+
})
|
| 103 |
+
|
| 104 |
+
# Save results
|
| 105 |
+
binary_predictions_path = "binary_predictions.csv"
|
| 106 |
+
filtered_predictions_path = "filtered_predictions.csv"
|
| 107 |
+
probabilities_path = "model_probabilities.csv"
|
| 108 |
+
|
| 109 |
+
predictions_df.to_csv(binary_predictions_path, index=False)
|
| 110 |
+
predictions_df[predictions_df["is_click_predicted"] == 1].to_csv(filtered_predictions_path, index=False)
|
| 111 |
+
probabilities_df.to_csv(probabilities_path, index=False)
|
| 112 |
+
|
| 113 |
+
st.success("Predictions completed! Download results below.")
|
| 114 |
+
|
| 115 |
+
# Download Buttons
|
| 116 |
+
with open(binary_predictions_path, "rb") as f:
|
| 117 |
+
st.download_button("Download Binary Predictions (0/1)", f, file_name="binary_predictions.csv")
|
| 118 |
+
|
| 119 |
+
with open(filtered_predictions_path, "rb") as f:
|
| 120 |
+
st.download_button("Download Clicked Predictions (Only 1s)", f, file_name="filtered_predictions.csv")
|
| 121 |
+
|
| 122 |
+
with open(probabilities_path, "rb") as f:
|
| 123 |
+
st.download_button("Download Probability Predictions", f, file_name="model_probabilities.csv")
|