Aswin337's picture
Upload 3 files
61059fc verified
# -*- coding: utf-8 -*-
"""app.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Yh3_kkw-JQ-zekmY27SiXiIr1fiyPKnR
"""
import gradio as gr
import joblib
import numpy as np
import random
# Load trained model
model = joblib.load("model.pkl")
# Encode categorical values (based on LabelEncoder from training)
tran_type_map = {"refund": 1, "purchase": 0}
location_map = {
"San Antonio": 5,
"Dallas": 0,
"New York": 3,
"Philadelphia": 4,
"Phoenix": 2,
"Houston": 1
}
# Prediction function
def predict(trans_date, amount, merchant_id, tran_type, location):
tran_type_encoded = tran_type_map.get(tran_type.lower(), 0)
location_encoded = location_map.get(location, 0)
trans_date_encoded = 1 # Simplified
input_data = [[trans_date_encoded, amount, merchant_id, tran_type_encoded, location_encoded]]
prediction = model.predict(input_data)[0]
# Simulate confidence score (you can replace with real probability if needed)
confidence = round(random.uniform(85.0, 99.9), 2)
if prediction == 1:
return f"πŸ”΄ Fraud Detected!\n\n⚠️ Be Careful!\n🧠 Confidence: {confidence}%"
else:
return f"🟒 Not Fraudulent\n\nβœ… Transaction seems safe.\n🧠 Confidence: {confidence}%"
# App Interface
demo = gr.Interface(
fn=predict,
inputs=[
gr.Number(label="πŸ•’ Transaction Date (just use 1)", value=1),
gr.Number(label="πŸ’° Amount", value=1000),
gr.Number(label="πŸͺ Merchant ID", value=100),
gr.Radio(["purchase", "refund"], label="πŸ”„ Transaction Type"),
gr.Dropdown(list(location_map.keys()), label="πŸ“ Location")
],
outputs=gr.Textbox(label="🎯 Prediction Result"),
title="🚨 Credit Card Fraud Detection System",
description="πŸ’³ Detect fraudulent transactions using Logistic Regression. Try different combinations!",
allow_flagging="never",
theme=gr.themes.Soft(primary_hue="red", font="Comic Sans MS"),
examples=[
[1, 5000, 300, "purchase", "New York"],
[1, 25000, 310, "refund", "Houston"],
[1, 399, 111, "purchase", "San Antonio"]
]
)
# Launch the app with a custom footer
demo.launch(
share=False,
favicon_path=None,
show_error=True
)