Spaces:
Sleeping
Sleeping
| 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" | |
| } | |
| 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) | |