Skip to content

Commit ad6caf0

Browse files
committed
initial commit
1 parent 329d30d commit ad6caf0

16 files changed

+1630
-0
lines changed

.github/workflows/build_push.yaml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Build and push ml endpoint
2+
3+
env:
4+
TAG: $(echo ${GITHUB_SHA::8}-$(date +'%Y-%m-%d'))
5+
REPO_NAME: portfolio
6+
APP_NAME: ml-api
7+
8+
on:
9+
push:
10+
branches:
11+
- main
12+
pull_request:
13+
branches:
14+
- main
15+
types:
16+
- closed
17+
18+
jobs:
19+
build:
20+
runs-on: self-hosted
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v2
24+
25+
- name: Set up Docker Buildx
26+
uses: docker/setup-buildx-action@v1
27+
28+
- name: Login to DockerHub
29+
uses: docker/login-action@v1
30+
with:
31+
username: ${{ secrets.DOCKERHUB_LOGIN }}
32+
password: ${{ secrets.DOCKERHUB_TOKEN }}
33+
registry: srv17.mikr.us:40073
34+
35+
- name: build and push
36+
run: |
37+
docker build \
38+
-t srv17.mikr.us:40073/${{ env.REPO_NAME }}/${{ env.APP_NAME}}:${{ env.TAG }} \
39+
-f Dockerfile .
40+
41+
docker push srv17.mikr.us:40073/${{ env.REPO_NAME }}/${{ env.APP_NAME}}:${{ env.TAG }}
42+
43+
docker build \
44+
-t srv17.mikr.us:40073/${{ env.REPO_NAME }}/${{ env.APP_NAME}}:latest \
45+
-f Dockerfile .
46+
47+
docker push srv17.mikr.us:40073/${{ env.REPO_NAME }}/${{ env.APP_NAME}}:latest
48+
redeploy:
49+
runs-on: self-hosted
50+
needs: build
51+
steps:
52+
- name: Checkout
53+
uses: actions/checkout@v2
54+
55+
- name: Deploy
56+
run: |
57+
cd kubernetes && sh create_secrets_and_deploy.sh \
58+
${{ secrets.DOCKERHUB_LOGIN }} \
59+
${{ secrets.DOCKERHUB_TOKEN }}

.gitignore copy

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/
161+
demo.ipynb
162+
kubernetes/secret.yaml

.pre-commit-config.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v2.3.0
4+
hooks:
5+
- id: check-builtin-literals
6+
args: ['--no-allow-dict-kwargs']
7+
- id: check-docstring-first
8+
- id: debug-statements
9+
- id: double-quote-string-fixer
10+
- id: end-of-file-fixer
11+
exclude: '.*\.txt$'
12+
- id: name-tests-test
13+
- id: flake8
14+
args: ['--max-line-length', '120']
15+
- id: trailing-whitespace
16+
- repo: https://github.com/asottile/reorder_python_imports
17+
rev: v1.6.1
18+
hooks:
19+
- id: reorder-python-imports
20+
language_version: python3
21+
- repo: https://github.com/asottile/add-trailing-comma
22+
rev: v1.4.1
23+
hooks:
24+
- id: add-trailing-comma
25+
- repo: https://github.com/Yelp/detect-secrets.git
26+
rev: v1.4.0
27+
hooks:
28+
- id: detect-secrets
29+
name: Detect secrets
30+
language: python
31+
entry: detect-secrets-hook
32+
args: ['scan']

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM python:3.10-slim-buster
2+
3+
WORKDIR /app
4+
5+
RUN apt-get update && apt-get install -y libgomp1
6+
7+
COPY requirements.txt .
8+
RUN pip install -r requirements.txt
9+
ENV GIT_PYTHON_REFRESH=quiet
10+
11+
EXPOSE 8000
12+
COPY . .
13+
14+
ENTRYPOINT ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]

api.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from fastapi import FastAPI
2+
from pydantic import BaseModel
3+
import pandas as pd
4+
from otomoto.preprocessing import Preprocessing
5+
from otomoto.model_training import ModelTrainer
6+
from otomoto.utils import get_unique_values
7+
from otomoto.model_predictions import Predictor
8+
from otomoto.input_models import OtomotoInputData
9+
from fastapi import Body
10+
from fastapi import HTTPException
11+
12+
app = FastAPI()
13+
14+
15+
@app.post("/api/v1/otomoto/train_model",tags=['model_training'])
16+
async def train_model():
17+
try:
18+
data_processor = Preprocessing()
19+
data_processor.preprocess_data()
20+
21+
input_data = data_processor.get_data()
22+
23+
model_trainer = ModelTrainer(input_data, target_column='price')
24+
model_trainer.preprocess()
25+
model_trainer.train_models()
26+
27+
except Exception as error:
28+
raise HTTPException(status_code=500, detail=f"Error occurred: {error}")
29+
30+
return {"message": "Models trained and registered successfully"}
31+
32+
33+
@app.post("/api/v1/otomoto/get_dropdown_values",tags=['dropdown_values'])
34+
async def get_dropdown_values():
35+
try:
36+
unique_values = get_unique_values()
37+
except Exception as error:
38+
raise HTTPException(status_code=500, detail=f"Error occurred: {error}")
39+
40+
return unique_values
41+
42+
43+
@app.post("/api/v1/otomoto/predict",tags=['model_predictions'])
44+
async def predict(data: OtomotoInputData = Body(...)):
45+
json_data = data.model_dump()
46+
model_name = json_data['model_name']
47+
transformer_name = 'otomoto_data_encoder'
48+
features = {k: v for k, v in json_data.items() if k != 'model_name'}
49+
try:
50+
predictor = Predictor(model_name, transformer_name)
51+
pred = predictor.predict(features)
52+
53+
except Exception as error:
54+
raise HTTPException(status_code=500, detail=f"Error occurred: {error}")
55+
56+
return {"message": pred}
57+
58+

0 commit comments

Comments
 (0)