Spaces:
Sleeping
Sleeping
Upload 3 files
Browse filesFiles changed (3)
- app.pyAdded+56
- fraud_data.csvAdded+83
- requirements.txtAdded+3
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import pandas as pd
import gradio as gr
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
# Load dataset
df = pd.read_csv("fraud_data.csv")
# Preprocess data
le_country = LabelEncoder()
le_device = LabelEncoder()
df['country'] = le_country.fit_transform(df['country'])
df['device'] = le_device.fit_transform(df['device'])
# Convert transaction_time to minutes
df['transaction_time'] = df['transaction_time'].apply(lambda x: int(x.split(":")[0]) * 60 + int(x.split(":")[1]))
# Features and target
X = df.drop('is_fraud', axis=1)
y = df['is_fraud']
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train model
model = RandomForestClassifier()
model.fit(X_train, y_train)
# Prediction function
def predict_fraud(amount, country, device, transaction_time):
country_encoded = le_country.transform([country])[0]
device_encoded = le_device.transform([device])[0]
time_in_minutes = int(transaction_time.split(":")[0]) * 60 + int(transaction_time.split(":")[1])
input_data = [[amount, country_encoded, device_encoded, time_in_minutes]]
prediction = model.predict(input_data)[0]
return "Fraudulent" if prediction == 1 else "Legitimate"
# Gradio interface
countries = df['country'].unique()
devices = df['device'].unique()
iface = gr.Interface(
fn=predict_fraud,
inputs=[
gr.Number(label="Amount"),
gr.Dropdown(choices=le_country.classes_.tolist(), label="Country"),
gr.Dropdown(choices=le_device.classes_.tolist(), label="Device"),
gr.Textbox(label="Transaction Time (HH:MM)")
],
outputs="text",
title="Fraud Detection System",
description="Enter transaction details to predict if it's fraudulent."
)
iface.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283amount,country,device,transaction_time,is_fraud
100.00,USA,mobile,12:05,0
9500.00,Nigeria,web,02:13,1
200.00,Canada,mobile,14:22,0
4500.00,India,web,08:45,1
50.00,USA,tablet,16:30,0
7000.00,Russia,web,03:50,1
120.00,UK,mobile,18:10,0
8000.00,Nigeria,mobile,01:15,1
75.00,Germany,tablet,13:40,0
6000.00,Brazil,web,23:20,1
30.00,France,mobile,10:00,0
5000.00,China,web,04:25,1
150.00,USA,web,11:45,0
9000.00,Russia,mobile,05:05,1
250.00,Canada,tablet,19:30,0
8500.00,India,web,02:50,1
45.00,UK,mobile,17:15,0
7000.00,Nigeria,web,00:30,1
100.00,Germany,mobile,15:55,0
5500.00,Brazil,web,22:10,1
60.00,France,tablet,09:05,0
4900.00,China,mobile,06:40,1
130.00,USA,web,13:20,0
7200.00,Russia,web,01:55,1
270.00,Canada,mobile,20:30,0
9000.00,India,tablet,03:00,1
55.00,UK,web,16:45,0
6300.00,Nigeria,mobile,02:25,1
110.00,Germany,tablet,14:50,0
4800.00,Brazil,web,23:45,1
40.00,France,mobile,10:30,0
5200.00,China,web,05:15,1
140.00,USA,mobile,12:00,0
7700.00,Russia,tablet,02:05,1
230.00,Canada,web,19:00,0
8800.00,India,mobile,04:40,1
50.00,UK,mobile,15:20,0
6700.00,Nigeria,web,01:00,1
115.00,Germany,mobile,13:30,0
5300.00,Brazil,tablet,22:35,1
65.00,France,web,09:50,0
4600.00,China,mobile,06:10,1
125.00,USA,web,13:05,0
7000.00,Russia,mobile,02:35,1
260.00,Canada,tablet,20:45,0
8900.00,India,web,03:30,1
48.00,UK,mobile,16:10,0
6500.00,Nigeria,mobile,00:50,1
105.00,Germany,web,14:25,0
5100.00,Brazil,tablet,23:00,1
55.00,France,mobile,10:20,0
4700.00,China,web,05:50,1
135.00,USA,mobile,12:15,0
7200.00,Russia,tablet,02:00,1
240.00,Canada,web,19:15,0
8500.00,India,mobile,04:55,1
52.00,UK,mobile,15:40,0
6800.00,Nigeria,web,01:20,1
120.00,Germany,mobile,13:50,0
5400.00,Brazil,tablet,22:50,1
70.00,France,web,09:40,0
4800.00,China,mobile,06:25,1
110.00,USA,web,13:10,0
7500.00,Russia,mobile,02:10,1
250.00,Canada,tablet,20:20,0
9000.00,India,web,03:45,1
47.00,UK,mobile,16:30,0
6700.00,Nigeria,mobile,00:40,1
100.00,Germany,web,14:15,0
5200.00,Brazil,tablet,23:15,1
60.00,France,mobile,10:45,0
4600.00,China,web,05:40,1
130.00,USA,mobile,12:25,0
7000.00,Russia,tablet,01:50,1
270.00,Canada,web,19:30,0
8800.00,India,mobile,04:30,1
50.00,UK,mobile,15:10,0
6600.00,Nigeria,web,01:10,1
115.00,Germany,mobile,13:40,0
5300.00,Brazil,tablet,22:30,1
65.00,France,web,09:30,0
4700.00,China,mobile,06:00,1123pandas
scikit-learn
gradioapp.pyAdded+56
fraud_data.csvAdded+83
requirements.txtAdded+3