Giter VIP home page Giter VIP logo

wey-gu / nebulagraph-fraud-detection-gnn Goto Github PK

View Code? Open in Web Editor NEW
32.0 1.0 3.0 499 KB

An example project for training a GraphSAGE Model, and setup a Real-time Fraud Detection Web Service(Frontend and Backend) with NebulaGraph Database and DGL.

Home Page: https://siwei.io/fraud-detection-with-nebulagraph/

License: Apache License 2.0

Jupyter Notebook 66.40% Python 26.91% JavaScript 0.22% HTML 0.75% Vue 5.71%
dgl gnn graphdatabase nebulagraph fraud-detection graphsage

nebulagraph-fraud-detection-gnn's Introduction

Arch and Flow

GraphSAGE_FraudDetection

Model Training

Check https://github.com/wey-gu/NebulaGraph-Fraud-Detection-GNN/tree/main/notebooks/Train_GraphSAGE.ipynb for details.

  • Input: Graph of Historical Yelp Reviews
  • Output: a GraphSAGE Node Classification Model, could be inductive
                     ┌──────────────────────────────────────────────┐                     
                     │   ┌──────────────────────────────────────┐   │                     
                     │   │     Graph of Historical Reviews      │   │                     
                     │   └──────────────────────────────────────┘   │                     
                     │                      .─.              .      │                     
                     │                     (   )◀───────────( )     │                     
                     │                      `─'              '      │                     
                     │     .       .─.       ╲             ◁        │                     
                     │    ( )◀────(   )       ╲        .  ╱         │                     
                     │     '       `─'         ╲      ( )╱          │                     
                     │     ╲       ◀            ╲      '            │                     
                     │      ╲  .  ╱              ◁                  │                     
                     │       ◀( )╱               .─.         .─.    │                     
                     │         '                (   )◀──────(   )   │                     
                     │                           `─'         `─'    │                     
                     │                                              │                     
                     └──────────────────────────────────────────────┘                     
                                             ┃   (Nebula-DGL: NebulaLoader)                                         
                                             ▼                                            
┌────────────────────────────────────────────────────────────────────────────────────────┐
│ ┌────┐           ┌ ─ ─ ─ ─ ─ ─ ─ ─               ┌ ─ ─ ─ ─ ─ ─ ─ ─                     │
│ │GNN │                            │                               │                    │
│ └────┘           │                               │                                     │
│                                   │                               │                    │
│                  │           ◀                   │           ◀                         │
│                      .   .  ╱.─.  │                  .   .  ╱.─.  │                    │
│                  │  ( )◀───╱(   )                │  ( )◀───╱(   )                      │
│ .       .─.          '╱  '   `─'  │  ┌────────┐      '╱  '   `─'  │        .       .─. │
│( )◀────(   )     │   .       .─.     │ ReLU  ╱│  │   .       .─.          ( )◀────(   )│
│ '       `─'         ( )◀────(   ) │  │      ╱ │     ( )◀────(   ) │        '       `─' │
│ ╲       ◀   ══▶  │   '       `─'     │     ╱  │  │   '       `─'   ... ══▶ ╲       ◀   │
│  ╲  .  ╱             ╲       ◀    │  │─────   │      ╲       ◀    │         ╲  .  ╱    │
│   ◀( )╱          │    ╲  .  ╱        └────────┘  │    ╲  .  ╱                ◀( )╱     │
│     '                  ◀( )╱      │                    ◀( )╱      │            '       │
│                  │       '                       │       '                             │
│                      .       .─.  │                  .       .─.  │                    │
│                  │  ( )◀────(   )                │  ( )◀────(   )                      │
│                      '       `─'  │                  '       `─'  │                    │
│                  │   ╲       ◀                   │   ╲       ◀                         │
│                       ╲  .  ╱     │                   ╲  .  ╱     │                    │
│                  │     ◀( )╱                     │     ◀( )╱                           │
│                          '        │                      '        │                    │
│                  └ ─ ─ ─ ─ ─ ─ ─ ─               └ ─ ─ ─ ─ ─ ─ ─ ─                     │
└────────────────────────────────────────────────────────────────────────────────────────┘
                                            ┃                                             
                                            ▼                                             
                          ┌──────────────────────────────────┐                            
                          │                 Λ                │                            
                      ┌───┴─┐  GNN Model   ╱ ╲            ┌──┴──┐                         
                      ├─────┤             ╱   ╲           ├─────┤                         
                      ├─────┼────────────▶    ───────────▶├─────┤                         
                      ├─────┤             ╲   ╱           ├─────┤                         
                      └───┬─┘              ╲ ╱            └──┬──┘                         
                          │                 V                │                            
                          └──────────────────────────────────┘      

Online Fraud Inference System

Backend

  • Input: a new review
  • Output: is_fraud prediction
  • Flow: 0. A review will be inserted to NebulaGraph
    1. A SubGraph Query will be called
    2. SubGraph will be sent to Inference API
    3. Inference API will predict its is_fraud label on the trained model
      ┌─────────────────────┐                          ┌─────────────────┐      
      │                     │                          │                 │
─────▶│ Transaction Record  ├──────2. Fraud Risk ─────▶│  Inference API  │◀────┐
      │                     │◀────Prediction with ─────┤                 │     │
      │                     │        Sub Graph.        │                 │     │
      └─────────────────────┘                          └─────────────────┘     │
           │           ▲                                        │              │
           │           │                                        │              │
       0. Insert   1. Get New                              3.req: Node         │
         Record.   Record Sub                            Classification.       │
           │         Graph.                                     │              │
           ▼           │                                        │              │
┌──────────────────────┴─────────────────┐ ┌────────────────────┘      3.resp: │
│┌──────────────────────────────────────┐│ │                          Predicted│
││   Graph of Historical Transactions   ││ │                             Risk. │
│└──────────────────────────────────────┘│ │                                   │
│                   .─.              .   │ │                                   │
│                  (   )◀───────────( )  │ │                                   │
│                   `─'              '   │ │      ┌──────────────────────┐     │
│  .       .─.       ╲             ◁     │ │      │ GNN Model Λ          │     │
│ ( )◀────(   )       ╲           ╱      │ │  ┌───┴─┐        ╱ ╲      ┌──┴──┐  │
│  '       `─'         ╲       . ╱       │ │  ├─────┤       ╱   ╲     ├─────┤  │
│  ╲       ◀            ╲     ( )        │ └─▶├─────┼─────▶▕     ─────├─────┤──┘
│   ╲  .  ╱              ◁     '         │    ├─────┤       ╲   ╱     ├─────┤   
│    ◀( )╱               .─.         .─. │    └───┬─┘        ╲ ╱      └──┬──┘   
│      '                (   )◀──────(   )│        │           V          │      
│                        `─'         `─' │        └──────────────────────┘      
└────────────────────────────────────────┘                                      

Frontend

As the review request being sent to Graph Database and Inference API, when fraud predict is responded to the Inference API caller, in parallel, the result will be broadcast to Real Time Fraud Monitor Dashboards, too.

The dashbard are tables subscribing to the flow of reviews sending in, and when some of the records are highlighted with hi risk in fraud, corresponding party will be notified and inovlved for follow-up actions.

Demo Video 👉🏻:

GraphSAGE_FraudDetection_demo.mov
         ┌────────────────────────────────────────────────────────────────────┐         
         │   ┌──────────────────────────────────────────────────────────┐     │         
         │   │        Real-Time Online Fraud Monitor Web Service        │     │         
         │   └──────────────────────────────────────────────────────────┘     │         
         │                                                                    │         
         │   ┌────┬────┬──────┬────┬────┬────┬────┬────┬────┬────┬──────┐     │         
         │   │    │    │      │    │    │    │    │    │    │    │  OK  │     │         
         │   ├────┼────┼──────┼────┼────┼────┼────┼────┼────┼────┼──────┤     │         
         │   │    │    │      │    │    │    │    │    │    │    │  OK  │     │         
         │   ├────┼────┼──────┼────┼────┼────┼────┼────┼────┼────┼──────┤     │         
         │   │    │    │      │    │    │    │    │    │    │    │ NOK  │     │         
         │   └────┴────┴──────┴────┴────┴────┴────┴────┴────┴────┴──────┘     │         
         └─────────────────────────────────▲──────────────────────────────────┘         
                                           ┃                                            
┌───────────────────────┐                  ┃                                            
│  New Review/Requests  │                  ┃                                            
│Generated Continuously │                  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓               
└───────────────────────┘                                               ┃               
            │                                                           ┃               
            │                                                           ┃               
            │ ┌─────────────────────┐                          ┌────────┻────────┐      
            │ │                     │                          │                 │      
            │ │                     │                          │                 │      
            └▶│ Transaction Record  ├──────2. Fraud Risk ─────▶│  Inference API  │◀────┐
              │                     │◀────Prediction with ─────┤                 │     │
              │                     │        Sub Graph         │                 │     │
              └─────────────────────┘                          └─────────────────┘     │
...

Graph Model and Data Set

We will leverage Yelp-Fraud dataset comes from Enhancing Graph Neural Network-based Fraud Detectors against Camouflaged Fraudsters.

There will be one type of node and three types of edges:

  • Node: review on restaurant, hotel. With Label and Feature Properties:
    • is_fraud to be the label
    • 32 features being feature-engineered
  • Edge: in 3-typed between the nodes. Without Properties:
    • R-U-R: share same reviewer, named shares_user_with
    • R-S-R: share same rate for same object, named shares_restaurant_rating_with
    • R-T-R: share same review submitting month for same object, named shares_restaurant_in_one_month_with

Before the project, I made the playground to ingest the Yelp Data Graph into NebulaGraph, see more from https://github.com/wey-gu/nebulagraph-yelp-frauddetection.

Playground Setup with Data Ingestion

You could quickly run the following lines to make it ready:

# Deploy NebulaGraph for Playground
curl -fsSL nebula-up.siwei.io/install.sh | bash

# Clone the data downloader repo
git clone https://github.com/wey-gu/nebulagraph-yelp-frauddetection && cd nebulagraph-yelp-frauddetection

# Install requirement, then download the data ready for NebulaGraph
python3 -m pip install -r requirements.txt
python3 data_download.py

# Import it to NebulaGraph
docker run --rm -ti \
 --network=nebula-net \
 -v ${PWD}/yelp_nebulagraph_importer.yaml:/root/importer.yaml \
 -v ${PWD}/data:/root \
 vesoft/nebula-importer:v3.1.0 \
 --config /root/importer.yaml

Then refer to notebooks/* for Training Model and the Real-time Fraud Detection Web Service itself, and refer to src/* for the Real-time Fraud Detection Web Service reference implementation.

Playground of Real-Time Fraud Monitor

Follow this, you should be able to run the Real-time Fraud Detection Web Service with my trained model being loaded.

Get your machine's IP (not the 127.0.0.1), say it's 10.0.0.5.

export MY_IP="10.0.0.5"

Run Backend:

git clone https://github.com/wey-gu/NebulaGraph-Fraud-Detection-GNN.git
cd NebulaGraph-Fraud-Detection-GNN/src

# ADD MY_IP into CORS & Frontend file, nginx.conf
sed -i "s/nebula-demo.siwei.io/$MY_IP/g" fraudd_backend/fraudd/__init__.py
sed -i "s/nebula-demo.siwei.io/$MY_IP/g" fraudd_frontend/src/components/Table.vue
sed -i "s/nebula-demo.siwei.io/$MY_IP/g" nginx.conf

# install dep of backend
python3 -m pip install -r requirements.txt

export NG_ENDPOINTS="127.0.0.1:9669";
export FLASK_ENV=development;
export FLASK_APP=wsgi;

# run backend
cd fraudd_backend

python3 -m flask run --reload --host=0.0.0.0
# verify
$ curl localhost:5000/api
{
  "status": "ok"
}

From another terminal, build frontend:

cd NebulaGraph-Fraud-Detection-GNN/src
cd fraudd_frontend

# sudo apt install npm

npm install
npm run build

From another terminal, run Nginx:

cd NebulaGraph-Fraud-Detection-GNN/src
docker-compose up -d
# end-to-end verify backend
curl -X POST localhost:15000/api/add_review \
        -d '{"vertex_id": "2049"}' \
        -H 'Content-Type: application/json'
# return value
{
  "is_fraud": false
}

From web browser 👉🏻 http://10.0.0.5:8080/

You could check my demo: http://nebula-demo.siwei.io:8080

nebulagraph-fraud-detection-gnn's People

Contributors

wey-gu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nebulagraph-fraud-detection-gnn's Issues

About IEEE-CLS-Fraud dataset

Hello, how can I use the IEEE-cls-fraud dataset to implement this code? What parts of the code need to be modified?

Could you provide the trained model?

Hello, I only want to run the Real Time Fraud Monitor Dashboards. Could you provide the trained model?
I would appreciate it very much if you can provide it.

The URL problem

Hello, sorry to bother you. The URL is invalid. Can you update the latest URL address?
URLError: <urlopen error [Errno 99] Cannot assign requested address>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.