Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import xgboost as xgb
|
| 4 |
+
import numpy as np
|
| 5 |
+
import joblib
|
| 6 |
+
|
| 7 |
+
# Load pre-trained XGBoost model (saved as xgb_model.pkl in repo)
|
| 8 |
+
model = joblib.load("xgb_model.pkl")
|
| 9 |
+
|
| 10 |
+
# Define probability thresholds
|
| 11 |
+
def classify(prob):
|
| 12 |
+
if prob >= 0.7:
|
| 13 |
+
return "Most Likely"
|
| 14 |
+
elif prob >= 0.4:
|
| 15 |
+
return "Likely"
|
| 16 |
+
else:
|
| 17 |
+
return "Not Preferred"
|
| 18 |
+
|
| 19 |
+
def predict_leads(file):
|
| 20 |
+
# Read uploaded CSV
|
| 21 |
+
df = pd.read_csv(file.name)
|
| 22 |
+
|
| 23 |
+
# Drop non-feature columns if present
|
| 24 |
+
features = df.drop(columns=[c for c in ["Converted", "Name"] if c in df.columns])
|
| 25 |
+
|
| 26 |
+
# Ensure numeric encoding (simplified for demo)
|
| 27 |
+
features_encoded = pd.get_dummies(features, dummy_na=True)
|
| 28 |
+
|
| 29 |
+
# Align with model features
|
| 30 |
+
model_features = model.get_booster().feature_names
|
| 31 |
+
for col in model_features:
|
| 32 |
+
if col not in features_encoded.columns:
|
| 33 |
+
features_encoded[col] = 0
|
| 34 |
+
features_encoded = features_encoded[model_features]
|
| 35 |
+
|
| 36 |
+
# Predict probabilities
|
| 37 |
+
dmatrix = xgb.DMatrix(features_encoded)
|
| 38 |
+
probs = model.predict(dmatrix)
|
| 39 |
+
|
| 40 |
+
# Add predictions
|
| 41 |
+
df["AI_Probability"] = probs
|
| 42 |
+
df["AI_Category"] = df["AI_Probability"].apply(classify)
|
| 43 |
+
|
| 44 |
+
# Return results
|
| 45 |
+
return df, df
|
| 46 |
+
|
| 47 |
+
# Gradio interface
|
| 48 |
+
demo = gr.Interface(
|
| 49 |
+
fn=predict_leads,
|
| 50 |
+
inputs=gr.File(type="file", file_types=[".csv"]),
|
| 51 |
+
outputs=[
|
| 52 |
+
gr.Dataframe(label="Predicted Leads"),
|
| 53 |
+
gr.Dataframe(label="Raw Predictions")
|
| 54 |
+
],
|
| 55 |
+
title="AI Lead Scoring",
|
| 56 |
+
description="Upload a CSV of leads to predict Most Likely, Likely, and Not Preferred leads."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|