Olivier-52 commited on
Commit
9cdea9f
·
1 Parent(s): 1c6c7be

Initial commit

Browse files
Files changed (4) hide show
  1. .gitignore +181 -0
  2. Dockerfile +16 -0
  3. app.py +109 -0
  4. requirements.txt +13 -0
.gitignore ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+ # Ruff stuff:
171
+ .ruff_cache/
172
+
173
+ # PyPI configuration file
174
+ .pypirc
175
+
176
+ # Cursor
177
+ # Cursor is an AI-powered code editor.`.cursorignore` specifies files/directories to
178
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
179
+ # refer to https://docs.cursor.com/context/ignore-files
180
+ .cursorignore
181
+ .cursorindexingignore
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /home/app
4
+
5
+ RUN apt-get update -y
6
+ RUN apt-get install nano unzip -y
7
+ RUN apt install curl -y
8
+
9
+ RUN curl -fsSL https://get.deta.dev/cli.sh | sh
10
+
11
+ COPY requirements.txt /dependencies/requirements.txt
12
+ RUN pip install -r /dependencies/requirements.txt
13
+
14
+ COPY . /home/app
15
+
16
+ CMD gunicorn app:app --bind 0.0.0.0:$PORT --worker-class uvicorn.workers.UvicornWorker
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uvicorn
3
+ import pandas as pd
4
+ from pydantic import BaseModel
5
+ from fastapi import FastAPI
6
+ import mlflow
7
+ import os
8
+ from dotenv import load_dotenv
9
+
10
+ description = """
11
+
12
+ # [Fraud Detection](https://github.com/Olivier-52/fraud_detection.git)
13
+
14
+ Fast API backend to predict if a payment is fraud or not from : https://huggingface.co/spaces/sdacelo/real-time-fraud-detection
15
+
16
+ ## Machine-Learning
17
+
18
+ Where you can:
19
+ * `/predict` : prediction for a single value
20
+
21
+ Check out documentation for more information on each endpoint.
22
+ """
23
+
24
+ tags_metadata = [
25
+ {
26
+ "name": "Predictions",
27
+ "description": "Endpoints that uses our Machine Learning model",
28
+ },
29
+ ]
30
+
31
+ load_dotenv()
32
+
33
+ # Variables MLflow : URI de tracking, nom du modèle et stage
34
+ MLFLOW_TRACKING_APP_URI = os.getenv("MLFLOW_TRACKING_APP_URI")
35
+ MODEL_NAME = os.getenv("MODEL_NAME", "Fraud_Detection_Project")
36
+ STAGE = os.getenv("STAGE", "production")
37
+
38
+ # Variables AWS pour accéder au bucket S3 qui contient les artifacts de MLflow
39
+ os.environ["AWS_ACCESS_KEY_ID"] = os.getenv("AWS_ACCESS_KEY_ID")
40
+ os.environ["AWS_SECRET_ACCESS_KEY"] = os.getenv("AWS_SECRET_ACCESS_KEY")
41
+
42
+ # Variables globales pour stocker le modèle
43
+ mlflow.set_tracking_uri(MLFLOW_TRACKING_APP_URI)
44
+ model_uri = f"models:/{MODEL_NAME}@{STAGE}"
45
+ model = mlflow.sklearn.load_model(model_uri)
46
+
47
+ app = FastAPI(
48
+ title="API for Fraud Detection Project",
49
+ description=description,
50
+ version="1.0",
51
+ contact={
52
+ "name": "Olivier-52",
53
+ "url": "https://huggingface.co/Olivier-52",
54
+ },
55
+ openapi_tags=tags_metadata,)
56
+
57
+ @app.get("/")
58
+ def index():
59
+ """Return a message to the user.
60
+
61
+ This endpoint does not take any parameters and returns a message
62
+ to the user. It is used to test the API.
63
+
64
+ Returns:
65
+ str: A message to the user.
66
+ """
67
+ return "Hello world! Go to /docs to try the API."
68
+
69
+ class PredictionFeatures(BaseModel):
70
+ category : str
71
+ amt : float
72
+ merch_fraud_level :str
73
+ city_fraud_level : str
74
+ state_fraud_level : str
75
+ zip_fraud_level : str
76
+ job_fraud_level : str
77
+ age_fraud_level : str
78
+
79
+ @app.post("/predict", tags=["Predictions"])
80
+ def predict(features: PredictionFeatures):
81
+
82
+ """
83
+ Predict if a payment is fraud or not.
84
+
85
+ This endpoint takes a set of features as input and returns a prediction
86
+ of whether the payment is fraud or not.
87
+
88
+ Parameters:
89
+ features (PredictionFeatures): A set of features describing the payment
90
+
91
+ Returns:
92
+ dict: A dictionary containing the prediction of whether the payment is fraud or not
93
+ """
94
+ df = pd.DataFrame({
95
+ "category" : [features.category],
96
+ "amt" : [features.amt],
97
+ "merch_fraud_level" : [features.merch_fraud_level],
98
+ "city_fraud_level" : [features.city_fraud_level],
99
+ "state_fraud_level" : [features.state_fraud_level],
100
+ "zip_fraud_level" : [features.zip_fraud_level],
101
+ "job_fraud_level" : [features.job_fraud_level],
102
+ "age_fraud_level" : [features.age_fraud_level]
103
+ })
104
+
105
+ prediction = model.predict(df)[0]
106
+ return {"prediction": float(prediction)}
107
+
108
+ if __name__ == "__main__":
109
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ mlflow==2.21.3
2
+ scikit-learn==1.4.2
3
+ requests>=2.31.0,<3
4
+ fastapi
5
+ uvicorn[standard]
6
+ pydantic
7
+ typing
8
+ pandas
9
+ gunicorn
10
+ openpyxl
11
+ boto3
12
+ python-multipart
13
+ dotenv