Download app.py from CHETAN004/Digital: direct link, hf CLI and curl.
- Browser
- Download file 2.13 kB
-
https://huggingface.co/spaces/CHETAN004/Digital/resolve/main/app.py
- Command line
-
hf download hf://spaces/CHETAN004/Digital/app.py
-
curl -L -o app.py https://huggingface.co/spaces/CHETAN004/Digital/resolve/main/app.py
2.13 kB
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.model_selection import train_test_split | |
| st.set_page_config(page_title="Marketing Conversion Predictor") | |
| st.title("๐ Digital Marketing Conversion Predictor") | |
| # Load and preprocess dataset | |
| def load_data(): | |
| data = pd.read_csv("digital_marketing_campaign_dataset.csv") | |
| # Drop Customer ID if present | |
| if "CustomerID" in data.columns: | |
| data = data.drop("CustomerID", axis=1) | |
| # One-hot encode categorical variables | |
| data = pd.get_dummies(data, drop_first=True) | |
| return data | |
| # Load data | |
| df = load_data() | |
| # Separate features and target | |
| X = df.drop("Conversion", axis=1) | |
| y = df["Conversion"] | |
| # Scale features | |
| scaler = StandardScaler() | |
| X_scaled = scaler.fit_transform(X) | |
| # Train model | |
| model = RandomForestClassifier(n_estimators=100, random_state=42) | |
| model.fit(X_scaled, y) | |
| # User Input Form | |
| st.subheader("๐ง Input Campaign & Customer Info") | |
| input_data = {} | |
| for col in X.columns: | |
| if "Gender_" in col: | |
| gender = st.selectbox("Gender", ["Male", "Female"]) | |
| input_data[col] = 1 if gender == "Male" else 0 | |
| elif X[col].nunique() < 10 and not np.issubdtype(X[col].dtype, np.floating): | |
| input_data[col] = st.slider(f"{col}", int(df[col].min()), int(df[col].max()), int(df[col].mean())) | |
| else: | |
| input_data[col] = st.number_input(f"{col}", float(df[col].min()), float(df[col].max()), float(df[col].mean())) | |
| # Convert input to DataFrame | |
| input_df = pd.DataFrame([input_data]) | |
| # Match input features to training columns | |
| input_df = input_df.reindex(columns=X.columns, fill_value=0) | |
| input_scaled = scaler.transform(input_df) | |
| # Predict | |
| prob = model.predict_proba(input_scaled)[0][1] | |
| pred = model.predict(input_scaled)[0] | |
| # Output | |
| st.subheader("๐ฏ Prediction Result") | |
| st.write(f"๐ฎ Predicted Conversion Probability: **{prob:.2%}**") | |
| if pred == 1: | |
| st.success("โ This customer is likely to convert.") | |
| else: | |
| st.warning("โ This customer is unlikely to convert.") | |