- Install django using conda.
c:\dev\admission>conda install django
-
Create a new project using django-admin.
c:\dev\admission>django-admin startproject web
-
Create a new application after getting into web folder.
c:\dev\admission\web>python manage.py startapp admission
-
We need to add our application and INSTALLED_APPS list in settings.py, which is in web folder within web folder.
INSTALLED_APPS = [
'admission', # Our Application
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
-
Create the following function views in admission\views.py module.
from django.shortcuts import render
from django.http import HttpResponse
import pandas as pd
def admission_client(request):
return render(request, 'admission.html')
def predict_chances(request):
# Receive data from client
gre = int(request.GET['gre'])
toefl = int(request.GET['toefl'])
cgpa = float(request.GET['cgpa'])
model = pd.read_pickle(r"c:\dev\admission\lr_model.pickle")
chances = model.predict([[gre, toefl, cgpa]])
return HttpResponse(f"{chances[0] * 100:.2f}%")
-
Create the following template - admission.html inadmission\templates folder.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Predict Admission Chances</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
function predict_chances() {
url = "http://localhost:8000/predict?gre=" + $("#gre_score").val() +
"&toefl=" + $("#toefl_score").val() +
"&cgpa=" + $("#cgpa").val();
// Make AJAX request
$.get(url,
null,
function(result) {
$("#chances").text(result);
}
);
}
</script>
</head>
<body>
<h1>Predict Admission Chances</h1>
GRE Score <br/>
<input type="number" id="gre_score"/>
<p></p>
TOEFL Score <br/>
<input type="number" id="toefl_score"/>
<p></p>
CGPA <br/>
<input type="number" id="cgpa"/>
<p></p>
<button onclick="predict_chances()">Predict Chances</button>
<h2 id="chances"></h2>
</body>
</html>
-
Add the following entries in urls.py in web\web folder.
from django.contrib import admin
from django.urls import path
from admission import views
urlpatterns = [
path('admission/', views.admission_client),
path('predict/', views.predict_chances)
]
Comments
|
Posted By Tamanbir On 15-May-20 01:05:52 AM
Could you please provide the file structure, where did you keep the model.py and predict.py . Also if we build a model using pickle, don't we need to add the data.csv file in the file structure?
|