Spaces:
Sleeping
Sleeping
File size: 3,448 Bytes
24d09a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
from flask import Flask, render_template, request
import joblib
import pandas as pd
app = Flask(__name__)
model = joblib.load("churn_model.pkl")
model_features = joblib.load("model_features.pkl")
value_map = {
"Aydan aya": "Month-to-month",
"1 yıllık": "One year",
"2 yıllık": "Two year",
"Elektronik çek": "Electronic check",
"Posta çeki": "Mailed check",
"Banka havalesi (otomatik)": "Bank transfer (automatic)",
"Kredi kartı (otomatik)": "Credit card (automatic)",
"Hayır": "No",
"Evet": "Yes",
"Yok": "No internet service",
"Telefon hizmeti yok": "No phone service",
"Fiber optik": "Fiber optic"
}
@app.route("/", methods=["GET", "POST"])
def index():
result = None
if request.method == "POST":
form = request.form
input_dict = {}
# Sayısal alanlar
tenure = float(form.get("tenure"))
monthly = float(form.get("MonthlyCharges"))
total = float(form.get("TotalCharges"))
# Temel sayısal değişkenler
input_dict["tenure"] = tenure
input_dict["PhoneService"] = form.get("PhoneService") == "Evet"
input_dict["avg_charge_per_month"] = total / tenure if tenure > 0 else 0
input_dict["charge_ratio"] = total / (monthly * tenure) if monthly > 0 and tenure > 0 else 1
# tenure_bin
tenure_label = "0-12" if tenure <= 12 else "12-24" if tenure <= 24 else "24+"
for bin_label in ["0-12", "12-24", "24+"]:
input_dict[f"tenure_bin_{bin_label}"] = (tenure_label == bin_label)
# is_long_term_contract
contract_value = value_map.get(form.get("Contract"), form.get("Contract"))
input_dict["is_long_term_contract"] = contract_value in ["One year", "Two year"]
# One-hot kategorik değişkenler
categorical_fields = [
"gender", "SeniorCitizen", "Partner", "Dependents", "PaperlessBilling",
"MultipleLines", "InternetService", "OnlineSecurity", "OnlineBackup",
"DeviceProtection", "TechSupport", "StreamingTV", "StreamingMovies",
"Contract", "PaymentMethod"
]
for field in categorical_fields:
raw_value = form.get(field)
mapped_value = value_map.get(raw_value, raw_value)
for col in model_features:
if col.startswith(f"{field}_"):
input_dict[col] = (col == f"{field}_{mapped_value}")
# Eksik kalan tüm feature'lar tamamlanır
for col in model_features:
if col not in input_dict:
input_dict[col] = 0 if col == "tenure" or "charge" in col or "avg" in col else False
# DataFrame oluştur ve tahmin yap
input_df = pd.DataFrame([[input_dict[col] for col in model_features]], columns=model_features)
print("💬 MODELE GİDEN VERİLER:", flush=True)
print(input_df.to_dict(orient="records")[0], flush=True)
prediction = model.predict_proba(input_df)[0][1]
score = round(prediction * 100, 2)
if score >= 50:
comment = "Müşteri Kaybedilebilir."
else:
comment = "Müşteri Kayıp Riski Taşımıyor."
result = f"Churn Riski: %{score} — {comment}"
return render_template("index.html", result=result)
if __name__ == "__main__":
app.run(debug=False)
|