Snigs98 commited on
Commit
a26b0b0
·
verified ·
1 Parent(s): 2d50a2a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import joblib
5
+ from sklearn.ensemble import RandomForestClassifier
6
+ from sklearn.preprocessing import StandardScaler
7
+
8
+ # Load the dataset for feature extraction
9
+ csv_file_path = "creditcard.csv" # Ensure this file is uploaded to Hugging Face
10
+ df = pd.read_csv(csv_file_path)
11
+
12
+ # Select important features
13
+ selected_features = ["Amount", "V1", "V2", "V3", "Class"]
14
+ df = df[selected_features]
15
+
16
+ # Train-test split
17
+ X = df.drop(columns=["Class"])
18
+ y = df["Class"]
19
+
20
+ # Standardize "Amount"
21
+ scaler = StandardScaler()
22
+ X["Amount"] = scaler.fit_transform(X[["Amount"]])
23
+
24
+ # Train model
25
+ model = RandomForestClassifier(n_estimators=100, random_state=42)
26
+ model.fit(X, y)
27
+
28
+ # Save model and scaler
29
+ joblib.dump(model, "fraud_model.pkl")
30
+ joblib.dump(scaler, "scaler.pkl")
31
+
32
+
33
+ # Define function for prediction
34
+ def predict_fraud(amount, v1, v2, v3):
35
+ scaler = joblib.load("scaler.pkl")
36
+ model = joblib.load("fraud_model.pkl")
37
+
38
+ input_data = np.array([[amount, v1, v2, v3]])
39
+ input_data[:, 0] = scaler.transform(input_data[:, [0]]) # Scale amount
40
+
41
+ prediction = model.predict(input_data)
42
+ return "Fraudulent Transaction" if prediction[0] == 1 else "Non-Fraudulent Transaction"
43
+
44
+
45
+ # Gradio UI
46
+ iface = gr.Interface(
47
+ fn=predict_fraud,
48
+ inputs=[
49
+ gr.Number(label="Transaction Amount"),
50
+ gr.Number(label="V1"),
51
+ gr.Number(label="V2"),
52
+ gr.Number(label="V3"),
53
+ ],
54
+ outputs="text",
55
+ title="Credit Card Fraud Detection",
56
+ description="Enter transaction details to check if it's fraudulent or not.",
57
+ )
58
+
59
+ if __name__ == "__main__":
60
+ iface.launch()