Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Sabit CSV dosya yolu
|
| 9 |
+
CSV_FILE_PATH = "payment_fraud.csv" # DOSYA ADI GÜNCELLENDİ
|
| 10 |
+
|
| 11 |
+
# Veri setini yükleme ve ön işleme fonksiyonu
|
| 12 |
+
def load_and_preprocess():
|
| 13 |
+
try:
|
| 14 |
+
data = pd.read_csv(CSV_FILE_PATH)
|
| 15 |
+
data["type"] = data["type"].map({"CASH_OUT": 1, "PAYMENT": 2, "CASH_IN": 3, "TRANSFER": 4, "DEBIT": 5})
|
| 16 |
+
data["isFraud"] = data["isFraud"].map({0: "No Fraud", 1: "Fraud"})
|
| 17 |
+
|
| 18 |
+
# Sahtekarlık oranını hesapla
|
| 19 |
+
fraud_rate = (data["isFraud"].value_counts(normalize=True) * 100).get("Fraud", 0)
|
| 20 |
+
|
| 21 |
+
return data, fraud_rate
|
| 22 |
+
except FileNotFoundError:
|
| 23 |
+
print(f"Hata: {CSV_FILE_PATH} dosyası bulunamadı!")
|
| 24 |
+
return None, 0 # Hata durumunda fraud_rate 0 olarak döner
|
| 25 |
+
|
| 26 |
+
# Modeli eğitme ve değerlendirme fonksiyonu
|
| 27 |
+
def train_and_evaluate(data):
|
| 28 |
+
if data is None:
|
| 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 |
+
demo.launch(debug=True, share=True)
|