# -*- 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 )