Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 5 |
+
import pickle
|
| 6 |
+
|
| 7 |
+
# Load the trained model
|
| 8 |
+
with open('knn_model.pkl', 'rb') as file:
|
| 9 |
+
kn_class = pickle.load(file)
|
| 10 |
+
|
| 11 |
+
# Load the fitted MinMaxScaler
|
| 12 |
+
with open('scaler.pkl', 'rb') as file:
|
| 13 |
+
scaler = pickle.load(file)
|
| 14 |
+
|
| 15 |
+
def predict_fraud(cc_num, gender, lat, long, city_pop, unix_time, amount):
|
| 16 |
+
# Handle categorical feature 'Gender'
|
| 17 |
+
gender = 1 if gender == 'M' else 0
|
| 18 |
+
|
| 19 |
+
# Scale the amount feature
|
| 20 |
+
amount_scaled = scaler.transform([[amount]])[0][0]
|
| 21 |
+
|
| 22 |
+
# Create input dataframe for the model
|
| 23 |
+
input_data = pd.DataFrame({
|
| 24 |
+
'cc_num': [cc_num],
|
| 25 |
+
'Gender': [gender],
|
| 26 |
+
'lat': [lat],
|
| 27 |
+
'long': [long],
|
| 28 |
+
'city_pop': [city_pop],
|
| 29 |
+
'unix_time': [unix_time],
|
| 30 |
+
'Amount_Scaled': [amount_scaled]
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
# Predict using the loaded model
|
| 34 |
+
prediction = kn_class.predict(input_data)
|
| 35 |
+
|
| 36 |
+
# Return the result
|
| 37 |
+
return 'Fraudulent Transaction' if prediction[0] == 1 else 'Legitimate Transaction'
|
| 38 |
+
|
| 39 |
+
# Define examples, including one example of fraud
|
| 40 |
+
examples = [
|
| 41 |
+
[1234567890123456, 'M', 40.712776, -74.005974, 8398748, 1614575732, 100.0], # Legitimate transaction
|
| 42 |
+
[2345678901234567, 'F', 34.052235, -118.243683, 3990456, 1614575832, 200.0], # Legitimate transaction
|
| 43 |
+
[3456789012345678, 'M', 37.774929, -122.419416, 883305, 1614575932, 5000.0] # Fraudulent transaction
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
# Define Gradio interface
|
| 47 |
+
interface = gr.Interface(
|
| 48 |
+
fn=predict_fraud,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Number(label="Credit Card Number"),
|
| 51 |
+
gr.Radio(['M', 'F'], label="Gender"),
|
| 52 |
+
gr.Number(label="Latitude"),
|
| 53 |
+
gr.Number(label="Longitude"),
|
| 54 |
+
gr.Number(label="City Population"),
|
| 55 |
+
gr.Number(label="Unix Time"),
|
| 56 |
+
gr.Number(label="Transaction Amount")
|
| 57 |
+
],
|
| 58 |
+
outputs="text",
|
| 59 |
+
title="Fraud Detection Application",
|
| 60 |
+
description="Enter the transaction details to predict if it is fraudulent or legitimate.",
|
| 61 |
+
examples=examples
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Launch the interface
|
| 65 |
+
interface.launch()
|