Mishal23 commited on
Commit
5544e11
·
verified ·
1 Parent(s): cfb86d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -28
app.py CHANGED
@@ -8,18 +8,18 @@ from sklearn.linear_model import LogisticRegression
8
  from imblearn.over_sampling import SMOTE
9
  from transformers import pipeline
10
  import gradio as gr
11
- from google.colab import files
12
 
13
- # Load the creditcard.csv dataset from Google Drive or Colab local file upload
14
- uploaded = files.upload() # This will prompt you to upload your file
15
- df = pd.read_csv('creditcard.csv')
 
 
16
 
17
  # Display basic information
18
  print("Columns in the dataset:", df.columns)
19
  print(df.head())
20
 
21
  # Preprocessing: Selecting relevant columns
22
- # Assuming the dataset has 'Time', 'Amount', and 'Class' columns along with 'V1' to 'V28' features
23
  time_col = 'Time'
24
  amount_col = 'Amount'
25
  class_col = 'Class'
@@ -63,36 +63,24 @@ retrieval_pipeline = pipeline("feature-extraction", model="distilbert-base-uncas
63
 
64
  def retrieve_explanation(prediction):
65
  if prediction == 1:
66
- explanation = "The transaction is classified as fraudulent based on the provided features."
67
- else:
68
- explanation = "The transaction is classified as non-fraudulent based on the provided features."
69
- return explanation
70
 
71
- # Gradio prediction function with complete feature padding
72
  def fraud_detection_predictor(V1, V2, V3, Amount):
73
- # Create a list of features with default zero values for missing ones
74
  input_features = [0] * len(feature_cols)
75
-
76
- # Map the provided features to their indices (ensure they are in correct feature_cols)
77
- v1_index = feature_cols.index('V1') # Ensure these columns exist in feature_cols
78
- v2_index = feature_cols.index('V2')
79
- v3_index = feature_cols.index('V3')
80
- amount_index = feature_cols.index('Amount')
81
-
82
- # Assign user inputs to the correct feature indices
83
- input_features[v1_index] = V1
84
- input_features[v2_index] = V2
85
- input_features[v3_index] = V3
86
- input_features[amount_index] = Amount
87
 
88
- # Scale input data using the pre-fitted scaler
89
  input_data = scaler.transform([input_features])
90
 
91
  # Make a prediction
92
  prediction = model.predict(input_data)[0]
93
  fraud_status = "Fraudulent" if prediction == 1 else "Non-Fraudulent"
94
-
95
- # Get explanation
96
  explanation = retrieve_explanation(prediction)
97
  return fraud_status, explanation
98
 
@@ -109,8 +97,8 @@ interface = gr.Interface(
109
  gr.Textbox(label="Fraud Status"),
110
  gr.Textbox(label="Explanation")
111
  ],
112
- title="Simplified Credit Card Fraud Detection",
113
- description="Enter a few transaction features (V1, V2, V3, Amount) to predict fraud status."
114
  )
115
 
116
  # Launch Gradio Interface
 
8
  from imblearn.over_sampling import SMOTE
9
  from transformers import pipeline
10
  import gradio as gr
 
11
 
12
+ # Load the creditcard.csv dataset from your local directory
13
+ file_path = 'creditcard.csv' # Make sure this file is in the same directory as your script
14
+
15
+ # Load the dataset
16
+ df = pd.read_csv(file_path)
17
 
18
  # Display basic information
19
  print("Columns in the dataset:", df.columns)
20
  print(df.head())
21
 
22
  # Preprocessing: Selecting relevant columns
 
23
  time_col = 'Time'
24
  amount_col = 'Amount'
25
  class_col = 'Class'
 
63
 
64
  def retrieve_explanation(prediction):
65
  if prediction == 1:
66
+ return "The transaction is classified as fraudulent based on the provided features."
67
+ return "The transaction is classified as non-fraudulent based on the provided features."
 
 
68
 
69
+ # Gradio prediction function
70
  def fraud_detection_predictor(V1, V2, V3, Amount):
71
+ # Prepare input features
72
  input_features = [0] * len(feature_cols)
73
+ input_features[feature_cols.index('V1')] = V1
74
+ input_features[feature_cols.index('V2')] = V2
75
+ input_features[feature_cols.index('V3')] = V3
76
+ input_features[feature_cols.index('Amount')] = Amount
 
 
 
 
 
 
 
 
77
 
78
+ # Scale input data
79
  input_data = scaler.transform([input_features])
80
 
81
  # Make a prediction
82
  prediction = model.predict(input_data)[0]
83
  fraud_status = "Fraudulent" if prediction == 1 else "Non-Fraudulent"
 
 
84
  explanation = retrieve_explanation(prediction)
85
  return fraud_status, explanation
86
 
 
97
  gr.Textbox(label="Fraud Status"),
98
  gr.Textbox(label="Explanation")
99
  ],
100
+ title="Credit Card Fraud Detection",
101
+ description="Enter transaction features (V1, V2, V3, Amount) to predict fraud status."
102
  )
103
 
104
  # Launch Gradio Interface