File size: 2,998 Bytes
f3ecf25
e92430f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3880c18
e92430f
 
f3ecf25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'''import gradio as gr
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import joblib

# Load training data
data = pd.read_csv("fraud_data.csv")

# Preprocessing
X = data.drop("is_fraud", axis=1)
y = data["is_fraud"]
X = pd.get_dummies(X)
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)

# Save model and columns
joblib.dump(model, "model.pkl")
joblib.dump(X.columns, "columns.pkl")

# Load model and columns
model = joblib.load("model.pkl")
columns = joblib.load("columns.pkl")

def predict(file):
    df = pd.read_csv(file.name)
    input_df = pd.get_dummies(df)
    input_df = input_df.reindex(columns=columns, fill_value=0)
    preds = model.predict(input_df)
    df["prediction"] = preds
    return df

iface = gr.Interface(
    fn=predict,
    inputs=gr.File(label="Upload a CSV File with Transactions"),
    outputs="dataframe",
    title="Fraud Detection with CSV Upload",
    description="**Upload a CSV file** with columns like **'amount', 'country', 'device', 'transaction_time'**. The model will predict fraud (**1 = fraud**, **0 = not fraud**)."
)

iface.launch()'''

import gradio as gr
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import joblib

# Load training data
data = pd.read_csv("fraud_data.csv")

# Preprocessing
X = data.drop("is_fraud", axis=1)
y = data["is_fraud"]
X = pd.get_dummies(X)
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)

# Save model and columns
joblib.dump(model, "model.pkl")
joblib.dump(X.columns, "columns.pkl")

# Load model and columns
model = joblib.load("model.pkl")
columns = joblib.load("columns.pkl")

def predict(file):
    df = pd.read_csv(file.name)
    input_df = pd.get_dummies(df)
    input_df = input_df.reindex(columns=columns, fill_value=0)
    preds = model.predict(input_df)
    df["prediction"] = preds
    return df

with gr.Blocks() as demo:
    gr.Markdown("# Fraud Detection with CSV Upload")
    gr.Markdown("**Upload a CSV file** with columns like **'amount', 'country', 'device', 'transaction_time'**. The model will predict fraud (**1 = fraud**, **0 = not fraud**).")

    file_input = gr.File(label="Upload a CSV File with Transactions")
    output_df = gr.Dataframe()

    submit = gr.Button("Predict Fraud")
    submit.click(fn=predict, inputs=file_input, outputs=output_df)

    # Embed Dialogflow bot using raw HTML
    gr.HTML("""
    <df-messenger
      intent="WELCOME"
      chat-title="AI_teacher"
      agent-id="24215fc6-555e-4ebe-88b0-130a096887ac"
      language-code="en"
    ></df-messenger>
    <script src="https://www.gstatic.com/dialogflow-console/fast/messenger/bootstrap.js?v=1"></script>
    """)

demo.launch()