suhas1324 commited on
Commit
3cac466
·
verified ·
1 Parent(s): 3ef5364

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -1,29 +1,42 @@
1
  import gradio as gr
2
  import pickle
 
3
  from sklearn.preprocessing import StandardScaler
4
 
5
- # Load the model
6
  with open("fraud_detection_model.pkl", "rb") as f:
7
  model = pickle.load(f)
8
 
9
- # Define a function for prediction
 
 
 
 
10
  def predict(input_data):
 
11
  input_data = [float(x) for x in input_data.split(",")]
12
- scaler = StandardScaler()
13
- input_data = scaler.fit_transform([input_data])
 
 
 
 
14
  prediction = model.predict(input_data)
 
 
15
  return "Fraud" if prediction[0] == 1 else "Not Fraud"
16
 
17
- # Updated Gradio Interface
18
  input_text = gr.Textbox(label="Input Features (comma-separated)", placeholder="Enter features like 1.2, 3.4, ...")
19
  output_text = gr.Textbox(label="Prediction")
20
 
 
21
  interface = gr.Interface(
22
  fn=predict,
23
  inputs=input_text,
24
  outputs=output_text,
25
  title="Fraud Detection System",
26
- description="Enter the features to predict whether it is fraud or not."
27
  )
28
 
29
  # Run the application
 
1
  import gradio as gr
2
  import pickle
3
+ import numpy as np
4
  from sklearn.preprocessing import StandardScaler
5
 
6
+ # Load the trained RandomForest model
7
  with open("fraud_detection_model.pkl", "rb") as f:
8
  model = pickle.load(f)
9
 
10
+ # Load the scaler used during training (ensure it's the same one used for training the model)
11
+ with open("fraud_detection_model.pkl", "rb") as f:
12
+ scaler = pickle.load(f)
13
+
14
+ # Define the prediction function
15
  def predict(input_data):
16
+ # Convert input data to a list of floats
17
  input_data = [float(x) for x in input_data.split(",")]
18
+
19
+ # Preprocess the input data (e.g., scaling)
20
+ input_data = np.array(input_data).reshape(1, -1) # Reshape for a single sample
21
+ input_data = scaler.transform(input_data) # Apply the same scaling as during training
22
+
23
+ # Make the prediction using the loaded model
24
  prediction = model.predict(input_data)
25
+
26
+ # Return the result as "Fraud" or "Not Fraud"
27
  return "Fraud" if prediction[0] == 1 else "Not Fraud"
28
 
29
+ # Gradio Interface
30
  input_text = gr.Textbox(label="Input Features (comma-separated)", placeholder="Enter features like 1.2, 3.4, ...")
31
  output_text = gr.Textbox(label="Prediction")
32
 
33
+ # Create the Gradio interface
34
  interface = gr.Interface(
35
  fn=predict,
36
  inputs=input_text,
37
  outputs=output_text,
38
  title="Fraud Detection System",
39
+ description="Enter the features in comma-separated format to predict whether it is fraud or not."
40
  )
41
 
42
  # Run the application