Skip to main content

ML model deployment of Advertising dataset.

Project description

1. Packaging

Python is dynamically typed and non-compiled language. Python requires that the environment you run in has an appropriate Python interpreter and the ability to install the libraries and packages you need.

2. Create a GitHub repo

https://github.com/erkansirin78/fastapi-advertising-prediction.git

3. Activate conda environment

conda activate fastapi

4. Install packaging related packages

pip install -r requirements

5. Add setup.cfg

[metadata]
name = fastapi_advertising_prediction
version = 0.0.1
author = Erkan SIRIN
author_email = erkansirin.datalonga@gmail.com
description = ML model deployment of Advertising dataset.
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/erkansirin78/fastapi-advertising-prediction
classifiers =
    Programming Language :: Python :: 3
    License :: OSI Approved :: MIT License
    Operating System :: OS Independent

[options]
packages = find:
python_requires = >=3.7
include_package_data = True
  • If you update, change version.

6. Add pyproject.toml

[build-system]
requires = [
    "setuptools>=54",
    "wheel"
]
build-backend = "setuptools.build_meta"

7. Add a license

8. Build

python -m build
  • Build will create new files
.
├── dist
│   ├── fastapi_advertising_prediction-0.0.1-py3-none-any.whl
│   └── fastapi_advertising_prediction-0.0.1.tar.gz
├── fastapi_advertising_prediction
│   ├── Dockerfile
│   ├── __init__.py
│   ├── main.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── main.cpython-38.pyc
│   │   ├── schemas.cpython-38.pyc
│   │   └── train.cpython-38.pyc
│   ├── requirements.txt
│   ├── saved_models
│   │   └── 03.randomforest_with_advertising.pkl
│   ├── schemas.py
│   └── train.py
├── fastapi_advertising_prediction.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── LICENSE
├── pyproject.toml
├── README.md
└── setup.cfg

5 directories, 21 files
  • Check dist folder
tree dist/


dist/
├── fastapi_advertising_prediction-0.0.1-py3-none-any.whl
└── fastapi_advertising_prediction-0.0.1.tar.gz

9. Create an account on test.pypi.org

  • Before sending packages to pypi, first we upload test.pypi to see everything is all right.

10. Upload package with twine

twine upload --repository testpypi --skip-existing dist/* --verbose
  • Expected output
Uploading distributions to https://test.pypi.org/legacy/
INFO     dist/fastapi_advertising_prediction-0.0.1-py3-none-any.whl (4.6 KB)
INFO     dist/fastapi_advertising_prediction-0.0.1.tar.gz (3.3 KB)
INFO     Querying keyring for username
Enter your username: erkansirin
INFO     Querying keyring for password
WARNING  No recommended backend was available. Install a recommended 3rd party backend
         package; or, install the keyrings.alt package if you want to use the
         non-recommended backends. See https://pypi.org/project/keyring for details.
Enter your password:
INFO     username: erkansirin
INFO     password: <hidden>
Uploading fastapi_advertising_prediction-0.0.1-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.9/8.9 kB • 00:00 • 1.6 MB/s
INFO     Response from https://test.pypi.org/legacy/:
         200 OK
Uploading fastapi_advertising_prediction-0.0.1.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.6/7.6 kB • 00:00 • ?
INFO     Response from https://test.pypi.org/legacy/:
         200 OK

View at:
https://test.pypi.org/project/fastapi-advertising-prediction/0.0.1/

11. Install from test.pypi.org

pip install -i https://test.pypi.org/simple/ fastapi-advertising-prediction==0.0.1
  • Ignore following error and try again.
ERROR: No matching distribution found for fastapi-advertising-prediction==0.0.1

12. Test package

  • Create test directory
mkdir /tmp/fasttest
cd /tmp/fasttest
  • Create a module for training and saving model.
cat <<EOF > train_run.py
from fastapi_advertising_prediction import train

if __name__=='__main__':
    train.read_and_train()
EOF
  • Run: python train_run.py

  • Expected output

   ID     TV  Radio  Newspaper  Sales
0   1  230.1   37.8       69.2   22.1
1   2   44.5   39.3       45.1   10.4
2   3   17.2   45.9       69.3    9.3
3   4  151.5   41.3       58.5   18.5
4   5  180.8   10.8       58.4   12.9
(200, 3)
[[230.1  37.8  69.2]
 [ 44.5  39.3  45.1]
 [ 17.2  45.9  69.3]]
(200,)
0    22.1
1    10.4
2     9.3
3    18.5
4    12.9
5     7.2
Name: Sales, dtype: float64
R2: 0.9825966330409427
current_dir: /home/train/miniconda3/envs/fastapi/lib/python3.8/site-packages/fastapi_advertising_prediction
FileExistsError: File exists.
/home/train/miniconda3/envs/fastapi/lib/python3.8/site-packages/fastapi_advertising_prediction/saved_models
X_manual_test [[230.1, 37.8, 69.2]]
prediction [21.986]

13. Run uvicorn

  • create a module
cat<<EOF > main.py
from fastapi_advertising_prediction import main
import uvicorn

app = main.app

if __name__ == "__main__":
   uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True,
                log_level="debug", debug=True,
                workers=4, limit_concurrency=10, limit_max_requests=10)
EOF

14. Update package

# Uninstall
pip uninstall fastapi-advertising-prediction

# Delete all files in the dist folder.
rm -rf dist/
 
# Update the version number in the setup.cfg file.

# Re-create the wheels:
python -m build

# Re-upload the new files:
twine upload --repository testpypi dist/* --verbose

15. Install new version

pip install -i https://test.pypi.org/simple/ fastapi-advertising-prediction==0.0.2

16. Create an account on pypi

17. Upload to pypi

twine upload --repository pypi dist/* --verbose

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_advertising_prediction-0.0.7.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file fastapi_advertising_prediction-0.0.7.tar.gz.

File metadata

File hashes

Hashes for fastapi_advertising_prediction-0.0.7.tar.gz
Algorithm Hash digest
SHA256 6449f4dddcf09c95b4558aae8a1c56842c726c88463b315c2b758e3093a21b96
MD5 1bc61e2e461ed65db2060dcc87ac4930
BLAKE2b-256 7adc6197fdbe7645635ad574f8e7e3d1c802698479aa8e3b3acbeae6259bd2e0

See more details on using hashes here.

File details

Details for the file fastapi_advertising_prediction-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_advertising_prediction-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d7629ea07f3515a33d1942b011f0036d7dcb41ec0bcc2b491dcd30d3a4ea2e47
MD5 d429a1cf2cc98cc6ff4c653adb3a0e85
BLAKE2b-256 2731d9e2f45b8a0751af5012f0dca605da9cb0c7904457459019f3c05eb5e784

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page