0% found this document useful (0 votes)
24 views15 pages

ML Deployment Using Django

This document provides a step-by-step guide on how to install Django, create a Django project and app, and set up a machine learning model using Jupyter Notebook. It includes instructions for linking the app to the project, creating HTML templates for user interaction, and handling model predictions. Additionally, it highlights common errors and troubleshooting tips during the setup process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views15 pages

ML Deployment Using Django

This document provides a step-by-step guide on how to install Django, create a Django project and app, and set up a machine learning model using Jupyter Notebook. It includes instructions for linking the app to the project, creating HTML templates for user interaction, and handling model predictions. Additionally, it highlights common errors and troubleshooting tips during the setup process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

DJANGO

1. Installation : Open Anaconda command prompt, and type


 pip install Django
 pip install djangorestframework

2. Create a Django project : Move to the directory where you want to


create the project (for example if you want to create project inside
downloads directory, then change path of anaconda prompt according to
it using cd command)
 django-admin startproject your-project-name
(deploy is my project name)

3. Change the path to project folder, then open the project folder in VS
Code using code . command in Anaconda Prompt. See the structure of
Django project you had created now in VS Code.

DJANGO AKSHARA AS
Django project - just a container
for our project

Root node (same name as project). The


inner deploy/ directory is the actual
Python package for our project.

A command-line utility that lets us


interact with this Django project in
various ways

Directory Structure

4. Create Django App : Open Anaconda Command Prompt, type this code
to create a Django app ( a project can contain multiple apps).

 python manage.py startapp your-app-name


(irisapp is my app name)

DJANGO AKSHARA AS
App created now - irisapp

In views.py, we will add business


logic of our app.

 On Auto-Save option in your VS Code by File->Auto-Save.


 It is important to note that all names in code are case-sensitive. Your app
name and project name should remain the same throughout deployment.
Don't change the case of any letters in name.

5. Change the Root node settings.py (we are linking our app with root
node)

My app name is irisapp. So in


‘Deploy’ -> settings.py ->
Installed_apps -> added my app
name

DJANGO AKSHARA AS
6. Create a folder named Notebooks in base directory (by selecting project,
click second option – new folder) in VS Code.

First click in this


empty area to
select project,
then click
second option to
create new
folder and name
it as Notebooks

7. Inside notebooks folder create .ipynb file (by selecting Notebooks folder
and clicking first option to create a new file and then rename it as .ipynb
file) – build model and train it.

DJANGO AKSHARA AS
(If you created model in jupyter notebook. Simply copy and paste your
notebook into your project->Notebooks and run all by clicking run all
options)

After creating notebook as iris_classification.ipynb file. We want to select


kernel to run .ipynb file. After opening .ipynb file we able to see ‘Select
kernel’ option at right top-corner side.

Select Python Environments option

Select Conda Python – base(Python 3.9.13) (In my case, I had virtual


environment so I am selecting Virtual Env Python)

DJANGO AKSHARA AS
Then code your model and run it.

8. After building model and training model. Install Joblib in Anaconda


Prompt
 pip install joblib

9. Go to VS Code and Create another folder in project as savedModels


(same procedure as you did for creating Notebooks folder)

DJANGO AKSHARA AS
Selecting project (in empty area) and
click second option to create new
folder and name it as savedModels.

10. Then dump our trained model into joblib file as .joblib by inserting these
codes into your .ipynb file

 from joblib import dump


 dump(your-model-name, ‘./../savedModels/your-dumped-model-
name.joblib’)

After dumping, we able to see joblib file inside savedModels folder


DJANGO AKSHARA AS
11. Now our model (Backend) is ready. Next we want to create UI
(Frontend) to enable user to interact with backend. For that Create a
folder templates in project (same procedure as previous)

12. Create html files inside template folder and code for frontend. For my
Iris model I want html form to get user input and then predict the
species of flower according to user input.

DJANGO AKSHARA AS
Index.html -> html form -> gets user input for four attributes

Result.html -> to display my model prediction in different page

DJANGO AKSHARA AS
13. Now we need to link templates folder to root node. For that go to Rootnode->
settings.py -> templates and also import os in settings.py

DJANGO AKSHARA AS
14. Need to code business logic for our app. Go to app->views.py and write your
logic.

For iris classification , my logic is


 Getting input from html form in index.html – for this I defined a user
defined function predictor which renders index.html page once it
gets requested.
 Using my dumped trained ml model I predict the species of flower
according to given input –
o for this I defined another function forminfo which invokes
when the user clicks the submit button in index.html.
o in that function the dumped model is loaded and made it to
predict according to inputs.
o Once it predicted the function renders another html page
result.html to display the result

DJANGO AKSHARA AS
15. For html pages we need to define path address. For that create a file urls.py
inside app and define path for each html pages

For index.html I defined as base path (‘ ‘) and for result.html I defined as result
path(‘result’) along with their view’s functions.

DJANGO AKSHARA AS
16. Then we want to link our app urls with root node urls. So go to root node->
urls.py and add your app urls in it and also import these packages in it.

now we linked our app with rootnode .So run this command in anaconda
command prompt : click on that link

 python manage.py runserver


(http://127.0.0.1:8000/) – press ctrl key and left click on that link in prompt

DJANGO AKSHARA AS
Errors encountered during these :
1. While linking app with rootnode->settings.py in step 5 . We forget to
include comma(,) after entering app name.

Don’t forget to include comma in step 5 Rootnode->settings.py


2. While loading dumped model in app->views.py give correct path of
dumped model.

Give correct path for loading dumped models

3. Always keep correct path in Anaconda command prompt according to the


use.

DJANGO AKSHARA AS
Set correct path of Anaconda Prompt for each use

4. Check once if you run all codes in your .ipynb file including dumping code.
And also verify there is dumped model after running it inside savedmodels
folder.

Run all codes in your .ipynb file including dumping

5. Don’t select your system python. Select Conda python in kernel.

Select conda python as your kernel while running .ipynb file

DJANGO AKSHARA AS

You might also like