Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,55 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
from sklearn.model_selection import train_test_split
|
| 3 |
-
from sklearn.linear_model import LogisticRegression
|
| 4 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
-
from sklearn.metrics import classification_report
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return None, None, "Veri yükleme hatası nedeniyle model eğitilemedi."
|
| 30 |
-
|
| 31 |
-
x = data[["type", "amount", "oldbalanceOrg", "newbalanceOrig"]].values
|
| 32 |
-
y = data[["isFraud"]].values
|
| 33 |
-
xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.10, random_state=42)
|
| 34 |
-
|
| 35 |
-
# Logistic Regression Modeli
|
| 36 |
-
model_lr = LogisticRegression()
|
| 37 |
-
model_lr.fit(xtrain, ytrain.ravel())
|
| 38 |
-
y_pred_lr = model_lr.predict(xtest)
|
| 39 |
-
report_lr = classification_report(ytest, y_pred_lr)
|
| 40 |
-
|
| 41 |
-
# Random Forest Modeli
|
| 42 |
-
model_rf = RandomForestClassifier()
|
| 43 |
-
model_rf.fit(xtrain, ytrain.ravel())
|
| 44 |
-
y_pred_rf = model_rf.predict(xtest)
|
| 45 |
-
report_rf = classification_report(ytest, y_pred_rf)
|
| 46 |
-
|
| 47 |
-
return model_lr, model_rf, report_lr, report_rf
|
| 48 |
-
|
| 49 |
-
# Sahtekarlık tespiti ve olasılık hesabı
|
| 50 |
-
def fraud_detection(models, type, amount, oldbalanceOrg, newbalanceOrig):
|
| 51 |
-
model_lr, model_rf = models
|
| 52 |
-
if model_lr is None or model_rf is None:
|
| 53 |
-
return "Model eğitilemediği için tahmin yapılamıyor."
|
| 54 |
-
|
| 55 |
-
features = [[type, amount, oldbalanceOrg, newbalanceOrig]]
|
| 56 |
-
|
| 57 |
-
# Logistic Regression tahmini
|
| 58 |
-
prediction_lr = model_lr.predict(features)[0]
|
| 59 |
-
probability_lr = model_lr.predict_proba(features)[0][1] if hasattr(model_lr, "predict_proba") else "N/A"
|
| 60 |
-
|
| 61 |
-
# Random Forest tahmini
|
| 62 |
-
prediction_rf = model_rf.predict(features)[0]
|
| 63 |
-
probability_rf = model_rf.predict_proba(features)[0][1] if hasattr(model_rf, "predict_proba") else "N/A"
|
| 64 |
-
|
| 65 |
-
return f"Logistic Regression: {prediction_lr} (Olasılık: {probability_lr})\nRandom Forest: {prediction_rf} (Olasılık: {probability_rf})"
|
| 66 |
-
|
| 67 |
-
# Gradio Arayüzü
|
| 68 |
-
def analyze_and_predict(type, amount, oldbalanceOrg, newbalanceOrig):
|
| 69 |
-
data, fraud_rate = load_and_preprocess()
|
| 70 |
-
model_lr, model_rf, report_lr, report_rf = train_and_evaluate(data)
|
| 71 |
-
|
| 72 |
-
# Analiz sonuçları
|
| 73 |
-
analysis_results = f"Logistic Regression Raporu:\n{report_lr}\n\nRandom Forest Raporu:\n{report_rf}\n\nSahtekarlık Oranı: {fraud_rate:.2f}%"
|
| 74 |
-
|
| 75 |
-
# Tahmin Sonucu
|
| 76 |
-
prediction_result = fraud_detection((model_lr, model_rf), type, amount, oldbalanceOrg, newbalanceOrig)
|
| 77 |
-
|
| 78 |
-
return analysis_results, prediction_result
|
| 79 |
-
|
| 80 |
-
# Gradio Arayüzü
|
| 81 |
-
with gr.Blocks() as demo:
|
| 82 |
-
with gr.Row():
|
| 83 |
-
with gr.Column():
|
| 84 |
-
type = gr.Dropdown(label="İşlem Türü", choices=[("CASH_OUT", 1), ("PAYMENT", 2), ("CASH_IN", 3), ("TRANSFER", 4), ("DEBIT", 5)], type="value")
|
| 85 |
-
amount = gr.Number(label="İşlem Miktarı")
|
| 86 |
-
oldbalanceOrg = gr.Number(label="Gönderen Eski Bakiye")
|
| 87 |
-
newbalanceOrig = gr.Number(label="Gönderen Yeni Bakiye")
|
| 88 |
-
predict_button = gr.Button("Analiz Et ve Tahmin Yap")
|
| 89 |
-
with gr.Column():
|
| 90 |
-
analysis_output = gr.Textbox(label="Analiz Sonuçları", lines=5)
|
| 91 |
-
prediction_output = gr.Textbox(label="Tahmin Sonucu")
|
| 92 |
-
|
| 93 |
-
predict_button.click(
|
| 94 |
-
fn=analyze_and_predict,
|
| 95 |
-
inputs=[type, amount, oldbalanceOrg, newbalanceOrig],
|
| 96 |
-
outputs=[analysis_output, prediction_output]
|
| 97 |
)
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Gradio Arayüzü Fonksiyonu
|
| 4 |
+
def fraud_detection(accountAgeDays, numItems, localTime, paymentMethod, paymentMethodAgeDays):
|
| 5 |
+
# Öznitelikleri DataFrame'e dönüştürme
|
| 6 |
+
input_data = pd.DataFrame({
|
| 7 |
+
'accountAgeDays': [accountAgeDays],
|
| 8 |
+
'numItems': [numItems],
|
| 9 |
+
'localTime': [localTime],
|
| 10 |
+
'paymentMethod': [paymentMethod],
|
| 11 |
+
'paymentMethodAgeDays': [paymentMethodAgeDays]
|
| 12 |
+
})
|
| 13 |
+
|
| 14 |
+
# Kategorik sütunları one-hot kodlama ile dönüştürme
|
| 15 |
+
encoded_data = encoder.transform(input_data[categorical_cols])
|
| 16 |
+
encoded_df = pd.DataFrame(
|
| 17 |
+
encoded_data,
|
| 18 |
+
columns=encoder.get_feature_names_out(categorical_cols)
|
| 19 |
+
)
|
| 20 |
+
input_data = input_data.drop(categorical_cols, axis=1)
|
| 21 |
+
input_data = pd.concat(
|
| 22 |
+
[input_data.reset_index(drop=True), encoded_df.reset_index(drop=True)],
|
| 23 |
+
axis=1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Eksik değerleri doldurma
|
| 27 |
+
input_data = imputer.transform(input_data)
|
| 28 |
+
|
| 29 |
+
# Tahminleri yapma
|
| 30 |
+
logreg_prediction = logreg_model.predict(input_data)[0]
|
| 31 |
+
rf_prediction = rf_model.predict(input_data)[0]
|
| 32 |
+
|
| 33 |
+
# Sonuçları formatlama
|
| 34 |
+
logreg_result = "Şüpheli İşlem" if logreg_prediction == 0 else "Normal"
|
| 35 |
+
rf_result = "Şüpheli İşlem" if rf_prediction == 0 else "Normal"
|
| 36 |
+
|
| 37 |
+
return f"Logistic Regression: {logreg_result}\nRandom Forest: {rf_result}"
|
| 38 |
+
|
| 39 |
+
# Gradio Arayüzünü Oluşturma
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=fraud_detection,
|
| 42 |
+
inputs=[
|
| 43 |
+
gr.Number(label="Hesap Yaşı (Gün)"),
|
| 44 |
+
gr.Number(label="Ürün Sayısı"),
|
| 45 |
+
gr.Number(label="Yerel Saat (4.44 gibi)"),
|
| 46 |
+
gr.Dropdown(label="Ödeme Yöntemi", choices=["creditcard", "paypal", "storecredit", "UNKNOWN"]),
|
| 47 |
+
gr.Number(label="Ödeme Yöntemi Yaşı (Gün)"),
|
| 48 |
+
],
|
| 49 |
+
outputs=gr.Textbox(label="Tahmin Sonuçları"),
|
| 50 |
+
title="Ödeme Sahtekarlığı Tespit Sistemi",
|
| 51 |
+
description="Gerekli bilgileri girerek işlemin sahte olup olmadığını tahmin edin.",
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Arayüzü Başlatma
|
| 55 |
+
iface.launch(debug=True, share=True)
|