Spaces:
Build error
Build error
| import pandas as pd | |
| import joblib | |
| import gradio as gr | |
| from sklearn.preprocessing import OneHotEncoder | |
| from sklearn.impute import SimpleImputer | |
| # Modeli, encoder'ı ve imputer'ı yükleme | |
| logreg_model = joblib.load('logreg_model.pkl') | |
| rf_model = joblib.load('rf_model.pkl') | |
| encoder = joblib.load('encoder.pkl') | |
| imputer = joblib.load('imputer.pkl') | |
| # Veri setini yükleme ve ön işleme fonksiyonu | |
| def load_and_preprocess(csv_file_path): | |
| df = pd.read_csv(csv_file_path) | |
| df.fillna({"paymentMethod": "UNKNOWN"}, inplace=True) | |
| df["paymentMethod"] = df["paymentMethod"].map({ | |
| "creditcard": 1, | |
| "paypal": 2, | |
| "storecredit": 3, | |
| "UNKNOWN": 0 | |
| }) | |
| return df | |
| # payment_fraud.csv dosyasını yükle | |
| data = load_and_preprocess("payment_fraud.csv") | |
| # Kategorik sütunları tanımla | |
| categorical_cols = ['paymentMethod'] | |
| # Gradio Arayüzü Fonksiyonu | |
| def fraud_detection(accountAgeDays, numItems, localTime, paymentMethod, paymentMethodAgeDays): | |
| # Öznitelikleri DataFrame'e dönüştürme | |
| input_data = pd.DataFrame({ | |
| 'accountAgeDays': [accountAgeDays], | |
| 'numItems': [numItems], | |
| 'localTime': [localTime], | |
| 'paymentMethod': [paymentMethod], | |
| 'paymentMethodAgeDays': [paymentMethodAgeDays] | |
| }) | |
| # Kategorik sütunları one-hot kodlama ile dönüştürme | |
| encoded_data = encoder.transform(input_data[categorical_cols]) | |
| encoded_df = pd.DataFrame( | |
| encoded_data, | |
| columns=encoder.get_feature_names_out(categorical_cols) | |
| ) | |
| input_data = input_data.drop(categorical_cols, axis=1) | |
| input_data = pd.concat( | |
| [input_data.reset_index(drop=True), encoded_df.reset_index(drop=True)], | |
| axis=1 | |
| ) | |
| # Eksik değerleri doldurma | |
| input_data = imputer.transform(input_data) | |
| # Tahminleri yapma | |
| logreg_prediction = logreg_model.predict(input_data)[0] | |
| rf_prediction = rf_model.predict(input_data)[0] | |
| # Sonuçları formatlama | |
| logreg_result = "Normal" if logreg_prediction == 0 else "Fraud" | |
| rf_result = "Normal" if rf_prediction == 0 else "Fraud" | |
| return f"Logistic Regression: {logreg_result}\nRandom Forest: {rf_result}" | |
| # Gradio Arayüzünü Oluşturma | |
| iface = gr.Interface( | |
| fn=fraud_detection, | |
| inputs=[ | |
| gr.Number(label="Hesap Yaşı (Gün)"), | |
| gr.Number(label="Ürün Sayısı"), | |
| gr.Number(label="Yerel Saat (4.44 gibi)"), | |
| gr.Dropdown(label="Ödeme Yöntemi", choices=["creditcard", "paypal", "storecredit", "UNKNOWN"]), | |
| gr.Number(label="Ödeme Yöntemi Yaşı (Gün)"), | |
| ], | |
| outputs=gr.Textbox(label="Tahmin Sonuçları"), | |
| title="Ödeme Sahtekarlığı Tespit Sistemi", | |
| description="Gerekli bilgileri girerek işlemin sahte olup olmadığını tahmin edin.", | |
| ) | |
| # Arayüzü Başlatma | |
| iface.launch(debug=True, share=True) |