File size: 6,984 Bytes
4f317ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
---
title: Creditcard-Fraud-Detection
app_file: gradio_app.py
sdk: gradio
sdk_version: 4.37.1
---

<!-- markdownlint-disable -->
<p align="center">
<a href = "https://github.com/Sibikrish3000/Creditcard-Fraud-Detection" > <img src = "https://github.com/Sibikrish3000/Creditcard-Fraud-Detection/blob/main/static/images/creditcard1.jpg?raw=true" alt = "fraud detection image"  width=500 height=280> </a>
</p>
<h1 align="center"> Credit Card Fraud Detection Application </h1>

<p align="center">
This application leverages machine learning to detect fraudulent credit card transactions.
</p>

<p align="center">
<a href="https://github.com/Sibikrish3000/Creditcard-Fraud-Detection/blob/main/LICENSE"><img src="https://img.shields.io/github/license/Sibikrish3000/Creditcard-Fraud-Detection" alt="GitHub license"></a>
<a href="https://github.com/Sibikrish3000/Creditcard-Fraud-Detection/stargazers"><img src="https://img.shields.io/github/stars/Sibikrish3000/Creditcard-Fraud-Detection?style=social" alt="GitHub stars"></a>
<a href="https://github.com/Sibikrish3000/Creditcard-Fraud-Detection/issues"><img src="https://img.shields.io/github/issues/Sibikrish3000/Creditcard-Fraud-Detection" alt="GitHub issues"></a>
<a href="https://github.com/Sibikrish3000/Creditcard-Fraud-Detection/actions/workflows/quality.yml"><img src="https://github.com/Sibikrish3000/Creditcard-Fraud-Detection/actions/workflows/quality.yml/badge.svg" alt="Code Quality"></a>
</p>
<p align="center">
<a href="https://scikit-learn.org/"><img src=https://img.shields.io/badge/sklearn-darkorange.svg?style=flat&logo=scikit-learn&logoColor=white alt="sklearn"></a>
<a href="https://www.python.org"><img src="https://img.shields.io/badge/Python-darkblue.svg?style=flat&logo=python&logoColor=white" alt="language"></a>
<a href="https://fastapi.tiangolo.com/" ><img src="https://img.shields.io/badge/FastAPI-darkgreen.svg?style=flat&logo=fastapi&logoColor=white " alt="fastapi"></a> <a href="https://hub.docker.com/repository/docker/sibikrish3000/creditcard-fraud-detection/"><img src="https://img.shields.io/badge/Docker-blue?style=flat&logo=docker&logoColor=white" alt= "docker"></a>





</p>


This project contains a Fraud Detection application that includes a FastAPI server for the backend and a Gradio interface for the frontend. The application can predict if a transaction is fraudulent using either XGBoost or RandomForest models.

[Dataset](https://www.kaggle.com/datasets/kartik2112/fraud-detection)
## Overview

1. **FastAPI Backend**: Handles prediction requests using machine learning models.
2. **Gradio Frontend**: Provides a user-friendly web interface for users to input transaction details and get predictions.


## Project Structure

```

/Creditcard-Fraud-Detection
β”‚
β”œβ”€β”€/model
β”‚   β”œβ”€β”€ xgboost.pkl
β”‚   └── randomforest.pkl
β”œβ”€β”€/Encoder
β”‚   └── WOEEncoder.pkl
β”‚
β”œβ”€β”€/static
β”‚   └──/images
β”‚       β”œβ”€β”€ github.svg
β”‚       └── api.png
β”‚
β”œβ”€β”€ app.py
β”œβ”€β”€ gradio_app.py
β”œβ”€β”€ docker_app.py
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ features.py

````

- `app.py`: Defines the FastAPI application.
- `gradio_app.py`: Defines the Gradio interface.
- `docker_app.py`: Gradio interface for docker
- `Dockerfile`: Dockerfile for building the Docker image.
- `docker-compose.yml`: Docker Compose file for orchestrating the services.
- `requirements.txt`: List of dependencies.
- `features.py`: List of features.
-  `model/`: Directory containing pre-trained machine learning models.
- `Encoder/`: Directory containing encoders used for data preprocessing.
- `static/`: Directory containing static files such as images used in the interface.

## Getting Started

### Prerequisites

- Docker
- Docker Compose

### Installation

**Clone the repository:**

   ```bash
   git clone https://github.com/Sibikrish3000/Creditcard-Fraud-Detection.git
   cd Creditcard-Fraud-Detection
   ```
   ```
   git install lfs
   git lfs ls-files
   ```
   ```
   git lfs pull
   ```


## Running Locally

### Using Docker Compose

1. Build and start the containers:
   ```sh
   docker network create AIservice
   ```
    ```sh
    docker-compose up --build
    ```

2. Access the Gradio interface at [http://localhost:7860](http://localhost:7860).

### Using Docker image

```sh
docker network create AIservice
```
```sh
docker pull sibikrish/creditcard-fraud-detection:latest
docker run sibikrish/creditcard-fraud-detection:latest #or 
docker run -d -p 7860:7860 sibikrish/creditcard-fraud-detection:latest
 ``` 

### Manually

To run the application locally without Docker, ensure you have Python installed and follow these steps:

1. **Install the dependencies:**

   ```bash
   pip install -r requirements.txt
   ```

2. **Run the FastAPI server:**

   ```bash
   uvicorn app:app --host 0.0.0.0 --port 8000
   ```

3. **Run the Gradio interface:**

   ```bash
   python gradio_app.py
   ```

## Development
### Running in a Gitpod Cloud Environment

**Click the button below to start a new development environment:**

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Sibikrish3000/Creditcard-Fraud-Detection)
## Deployment

### Using Vercel

1. Create a `vercel.json` file in the project root:
    ```json
    {
      "version": 2,
      "builds": [
        { "src": "app.py", "use": "@vercel/python" },
        { "src": "gradio_app.py", "use": "@vercel/python" }
      ],
      "routes": [
        { "src": "/api/(.*)", "dest": "app.py" },
        { "src": "/(.*)", "dest": "gradio_app.py" }
      ]
    }
    ```

2. Deploy using the Vercel CLI:
    ```sh
    vercel
    ```



## Usage

1. **Access the Gradio Interface:**

   Open your web browser and navigate to `http://localhost:7860` to access the Gradio interface.

    - **Inputs**: Users can input transaction details such as credit card frequency, job, age, gender, category, distance, hour, hours difference between transactions, amount, and choose a model.
    - **Output**: The application returns a prediction indicating whether the transaction is legitimate or fraudulent.
    - **Flag Option**: Users can enable a flag option to provide feedback on incorrect or suspicious predictions.


2. **Access the FastAPI Documentation:**

   Open your web browser and navigate to `http://localhost:8000/docs` to access the FastAPI documentation.

### API Endpoints

- **POST /predict**

  Predict if a transaction is fraudulent.

  **Request:**

  ```json
  {
    "cc_freq": int,
    "cc_freq_class": int,
    "job": str,
    "age": int,
    "gender_M": int,
    "category": str,
    "distance_km": float,
    "hour": str,
    "hours_diff_bet_trans": float,
    "amt": float
  }
  ```

  **Response:**

  ```json
  {
    "prediction": 0 for legitimate, 1 for fraudulent
  }
  ```

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.