Spaces:
Sleeping
Sleeping
Download app.py from FuurythumbsCPT/fraud-detection-app: direct link, hf CLI and curl.
- Browser
- Download file 1.91 kB
-
https://huggingface.co/spaces/FuurythumbsCPT/fraud-detection-app/resolve/main/app.py
- Command line
-
hf download hf://spaces/FuurythumbsCPT/fraud-detection-app/app.py
-
curl -L -o app.py https://huggingface.co/spaces/FuurythumbsCPT/fraud-detection-app/resolve/main/app.py
1.91 kB
| import 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() |