Commit ecdbfa36 authored by Nipun Lakshan Perera's avatar Nipun Lakshan Perera

Backend

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/app.iml" filepath="$PROJECT_DIR$/.idea/app.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
3.jpeg

25.6 KB

"""
Django settings for app project.
Generated by 'django-admin startproject' using Django 2.2.8.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'jnbty*(xl$zl1xu63f2t9sxg3n4kl$pc75mpl$0tef)b*87&*l'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
'core',
'staff',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'app.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'app.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db1.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
AUTH_USER_MODEL = 'core.User'
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
"""app URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('core.urls')),
path('staffapi/', include('staff.urls')),
]
"""
WSGI config for app project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
application = get_wsgi_application()
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class CoreConfig(AppConfig):
name = 'core'
import jwt, datetime
from rest_framework import exceptions
def create_access_token(id):
return jwt.encode({
'user_id': id,
'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=30),
'iat': datetime.datetime.utcnow()
}, 'access_secret', algorithm='HS256')
def decode_access_token(token):
try:
payload = jwt.decode(token, 'access_secret', algorithms='HS256')
return payload['user_id']
except:
raise exceptions.AuthenticationFailed('unauthenticated')
def create_refresh_token(id):
return jwt.encode({
'user_id': id,
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=7),
'iat': datetime.datetime.utcnow()
}, 'refresh_secret', algorithm='HS256')
def decode_refresh_token(token):
try:
payload = jwt.decode(token, 'refresh_secret', algorithms='HS256')
return payload['user_id']
except:
raise exceptions.AuthenticationFailed('unauthenticated')
\ No newline at end of file
from rest_framework.views import exception_handler
def status_code_handler(exc, context):
response = exception_handler(exc, context)
if response is not None and response.status_code == 403:
response.status_code = 401
return response
\ No newline at end of file
# Generated by Django 3.0.8 on 2022-10-08 19:13
import django.contrib.auth.models
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0011_update_proxy_permissions'),
]
operations = [
migrations.CreateModel(
name='Appointment',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('fullName', models.CharField(max_length=100)),
('gender', models.CharField(choices=[('1', 'One'), ('2', 'Two'), ('3', 'Three')], max_length=300)),
('email', models.CharField(max_length=255)),
('doctorName', models.CharField(max_length=255)),
('appointmentTime', models.DateTimeField()),
],
),
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('name', models.CharField(max_length=255)),
('email', models.CharField(max_length=255, unique=True)),
('password', models.CharField(max_length=255)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]
# Generated by Django 3.0.8 on 2022-10-08 20:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='File',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('file', models.FileField(upload_to='')),
],
),
]
# Generated by Django 3.0.8 on 2022-10-10 06:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0002_file'),
]
operations = [
migrations.CreateModel(
name='specificAppointment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.CharField(max_length=100)),
],
),
]
import email
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
name = models.CharField(max_length=255)
email = models.CharField(max_length=255, unique=True)
password = models.CharField(max_length=255)
first_name = None
last_name = None
username = None
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
genderChoices =(
("1", "One"),
("2", "Two"),
("3", "Three"),
)
class Appointment(models.Model):
id = models.AutoField(primary_key=True)
fullName = models.CharField(max_length=100)
gender = models.CharField(max_length=300, choices=genderChoices)
email = models.CharField(max_length=255)
doctorName = models.CharField(max_length=255)
appointmentTime =models.DateTimeField()
class specificAppointment(models.Model):
email = models.CharField(max_length=100)
class File(models.Model):
file = models.FileField(blank=False, null=False)
def __str__(self):
return self.file.name
\ No newline at end of file
import easyocr
import cv2
from matplotlib import pyplot as plt
import numpy as np
def main(file):
IMAGE_PATH = file
reader = easyocr.Reader(['en'])
result = reader.readtext(IMAGE_PATH)
# print(result)
image_values = []
for x in result:
# print(x[1])
image_values.append(x[1])
final_result = []
medicines = []
final_result.append(('Date', str(image_values[4].replace('Date:', ''))))
final_result.append(('Name', str(image_values[7].replace('Your Name:', ''))))
final_result.append(('Address', str(image_values[8].replace('Address:', ''))))
final_result.append(('Medicines', image_values[9:]))
# out final result
for f in final_result:
print(f)
img = cv2.imread(IMAGE_PATH)
font = cv2.FONT_HERSHEY_SIMPLEX
# Date
top_left = tuple(result[4][0][0])
bottom_right = tuple(result[4][0][2])
date_text = result[4][1]
img = cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 5)
img = cv2.putText(img, date_text, top_left, font, 0.5, (0, 0, 0), 3, cv2.LINE_AA)
# Name
top_left = tuple(result[7][0][0])
bottom_right = tuple(result[7][0][2])
text = result[7][1]
font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 5)
img = cv2.putText(img, text, top_left, font, 0.5, (0, 0, 0), 3, cv2.LINE_AA)
# Address
top_left = tuple(result[8][0][0])
bottom_right = tuple(result[8][0][2])
text = result[8][1]
font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 5)
img = cv2.putText(img, text, top_left, font, 0.5, (0, 0, 0), 3, cv2.LINE_AA)
# Medicine
for i in range(9, len(result)-1):
top_left = tuple(result[i][0][0])
bottom_right = tuple(result[i][0][2])
text = result[i][1]
font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 5)
img = cv2.putText(img, text, top_left, font, 0.5, (0, 0, 0), 3, cv2.LINE_AA)
return final_result
# main('new/3.jpeg')
# plt.imshow(img)
# plt.show()
from rest_framework.serializers import ModelSerializer
from .models import User,Appointment, specificAppointment
from .models import File
class UserSerializer(ModelSerializer):
class Meta:
model = User
fields = ['id', 'name', 'email', 'password']
extra_kwargs = {
'password': {'write_only': True}
}
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
class AppointmentSerializer(ModelSerializer):
class Meta:
model = Appointment
fields = ['id', 'fullName', 'gender', 'email', 'doctorName','appointmentTime']
class SpecificAppointmentSerilaizer(ModelSerializer):
class Meta:
model = specificAppointment
fields = "__all__"
class FileSerializer(ModelSerializer):
class Meta:
model = File
fields = "__all__"
from django.test import TestCase
# Create your tests here.
from django.urls import path
from .views import RegisterAPIView, LoginAPIView, UserAPIView, RefreshAPIView, LogoutAPIView, AppointmentAPIView, FileUploadView, SpecificAppointmentAPIView
urlpatterns = [
path('register', RegisterAPIView.as_view()),
path('login', LoginAPIView.as_view()),
path('user', UserAPIView.as_view()),
path('refresh', RefreshAPIView.as_view()),
path('logout', LogoutAPIView.as_view()),
path('appointment', AppointmentAPIView.as_view()),
path('medicine', FileUploadView.as_view()),
path('specificAppointment', SpecificAppointmentAPIView.as_view())
]
from rest_framework.authentication import get_authorization_header
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.exceptions import APIException, AuthenticationFailed
from rest_framework import status
from rest_framework.parsers import FileUploadParser
from .authentication import create_access_token, create_refresh_token, decode_access_token, decode_refresh_token
from .serializers import UserSerializer,AppointmentSerializer, SpecificAppointmentSerilaizer
from .models import User,Appointment, specificAppointment
from .serializers import FileSerializer
from .predict import main
class RegisterAPIView(APIView):
def post(self, request):
serializer = UserSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
class LoginAPIView(APIView):
def post(self, request):
user = User.objects.filter(email=request.data['email']).first()
if not user:
raise APIException('Invalid credentials!')
if not user.check_password(request.data['password']):
raise APIException('Invalid credentials!')
access_token = create_access_token(user.id)
refresh_token = create_refresh_token(user.id)
response = Response()
response.set_cookie(key='refreshToken', value=refresh_token, httponly=True)
response.data = {
'token': access_token
}
return response
class UserAPIView(APIView):
def get(self, request):
auth = get_authorization_header(request).split()
if auth and len(auth) == 2:
token = auth[1].decode('utf-8')
id = decode_access_token(token)
user = User.objects.filter(pk=id).first()
return Response(UserSerializer(user).data)
raise AuthenticationFailed('unauthenticated')
class RefreshAPIView(APIView):
def post(self, request):
refresh_token = request.COOKIES.get('refreshToken')
id = decode_refresh_token(refresh_token)
access_token = create_access_token(id)
return Response({
'token': access_token
})
class LogoutAPIView(APIView):
def post(self, _):
response = Response()
response.delete_cookie(key="refreshToken")
response.data = {
'message': 'success'
}
return response
class AppointmentAPIView(APIView):
def post(self,request):
serializer = AppointmentSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
def checkAppointment(email,arrayValue):
a = []
for x in range(len(arrayValue)):
# print(arrayValue[0]['email'])
if email == arrayValue[x]['doctorName']:
a.append(arrayValue[x])
return a
class SpecificAppointmentAPIView(APIView):
def post(self, request, *args, **kwargs):
serializer = SpecificAppointmentSerilaizer(data=request.data)
serializer.is_valid(raise_exception=True)
snippets = Appointment.objects.all()
serializer_1 = AppointmentSerializer(snippets, many=True)
value = checkAppointment(serializer.data['email'], serializer_1.data)
return Response(value)
class FileUploadView(APIView):
parser_class = (FileUploadParser,)
def post(self, request, *args, **kwargs):
file_serializer = FileSerializer(data=request.data)
if file_serializer.is_valid():
file_serializer.save()
result=file_serializer.data
result_class = main(result['file'][1:])
return Response(result_class, status=status.HTTP_201_CREATED)
else:
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
\ No newline at end of file
File added
File added
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class StaffConfig(AppConfig):
name = 'staff'
import jwt, datetime
from rest_framework import exceptions
def create_access_token(id):
return jwt.encode({
'user_id': id,
'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=30),
'iat': datetime.datetime.utcnow()
}, 'access_secret', algorithm='HS256')
def decode_access_token(token):
try:
payload = jwt.decode(token, 'access_secret', algorithms='HS256')
return payload['user_id']
except:
raise exceptions.AuthenticationFailed('unauthenticated')
def create_refresh_token(id):
return jwt.encode({
'user_id': id,
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=7),
'iat': datetime.datetime.utcnow()
}, 'refresh_secret', algorithm='HS256')
def decode_refresh_token(token):
try:
payload = jwt.decode(token, 'refresh_secret', algorithms='HS256')
return payload['user_id']
except:
raise exceptions.AuthenticationFailed('unauthenticated')
\ No newline at end of file
from rest_framework.views import exception_handler
def status_code_handler(exc, context):
response = exception_handler(exc, context)
if response is not None and response.status_code == 403:
response.status_code = 401
return response
\ No newline at end of file
# Generated by Django 3.0.8 on 2022-10-10 05:47
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='labReport',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('file', models.FileField(upload_to='')),
],
),
migrations.CreateModel(
name='Salary',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('Name', models.CharField(max_length=180)),
('Qualification', models.CharField(max_length=180)),
('Experience', models.CharField(max_length=180)),
('Rating', models.CharField(max_length=180)),
('Place', models.CharField(max_length=180)),
('Profile', models.CharField(max_length=180)),
],
),
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('name', models.CharField(max_length=255)),
('email', models.CharField(max_length=255, unique=True)),
('password', models.CharField(max_length=255)),
],
options={
'abstract': False,
},
),
]
This source diff could not be displayed because it is too large. You can view the blob instead.
from django.contrib.auth.models import AbstractBaseUser
from django.db import models
class User(AbstractBaseUser):
name = models.CharField(max_length=255)
email = models.CharField(max_length=255, unique=True)
password = models.CharField(max_length=255)
first_name = None
last_name = None
username = None
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class labReport(models.Model):
file = models.FileField(blank=False, null=False)
def __str__(self):
return self.file.name
class Salary(models.Model):
Name = models.CharField(max_length = 180)
Qualification = models.CharField(max_length = 180)
Experience = models.CharField(max_length = 180)
Rating = models.CharField(max_length = 180)
Place = models.CharField(max_length = 180)
Profile = models.CharField(max_length = 180)
def __str__(self):
return self.Qualification
import numpy as np
import cv2
import matplotlib.pyplot as plt
import pytesseract
import re
from pytesseract import Output
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
def plot_gray(image):
plt.figure(figsize=(16,10))
return plt.imshow(image, cmap='Greys_r')
def plot_rgb(image):
plt.figure(figsize=(16,10))
return plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
def readCBC(extractedText):
lineNumberStart = extractedText.index('Complete Blood Picture (CBC)')
lineNumberEnd = extractedText.index('Differential Count')
dicts = {}
i = 0
for x in range(lineNumberStart+3, lineNumberEnd-1):
resultLine = extractedText[x].split(" ")
dicts[i] = {'name': resultLine[0], 'result': resultLine[1], 'unit' : resultLine[2], 'refRnage': resultLine[3]}
i = i+1
return dicts
def main(file_path):
file_name = file_path
image = cv2.imread(file_name, cv2.IMREAD_GRAYSCALE)
d = pytesseract.image_to_data(image, output_type=Output.DICT)
n_boxes = len(d['level'])
boxes = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2RGB)
for i in range(n_boxes):
(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
boxes = cv2.rectangle(boxes, (x, y), (x + w, y + h), (0, 255, 0), 2)
extracted_text = pytesseract.image_to_string(image)
ls = extracted_text.split("\n")
return(readCBC(ls))
# print(main("/home/aloka/Documents/medical-final year project/LabReport/lab-sample.jpg"))
\ No newline at end of file
import pickle
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
def PreProcess(df):
################## 'Experience' ########################
df['Experience'] = df['Experience'].astype(str).str.split(' ').map(lambda x: x[0])
df['Experience'] = df['Experience'].astype(np.int64)
########################## 'Place' #################################
###### We drop the entire row for null values of 'Place' #######
df = df.dropna(axis=0, subset=['Place'])
###### Keeping only the city in 'Place' ######
City = df['Place'].str.split(', ', n = 1, expand = True)
df['Place'] = City[1]
df.drop(df[df['Place'] == 'Sector 5, Delhi'].index, inplace = True)
df.drop(df[df['Place'].isna()].index, inplace = True)
###### Resetting index after row deletion ###+##
df = df.reset_index(drop=True)
################## 'Qualification' column #################################
idx = df.index[df['Qualification'].str.contains('|'.join(['Fellowship','MRCS','FCGP', 'FRCS', 'FWFO', 'FPFA', 'FICD']))]
df['Qualification'][idx] = 'Fellowship'
idx = df.index[df['Qualification'].str.contains('|'.join(['MD', 'M.D','MS','MDS', 'PhD', 'DNB', 'DLO','DDV',
'DDVL', 'Diploma in Dermatology']))]
df['Qualification'][idx] = 'MD'
idx = df.index[df['Qualification'].str.contains('|'.join(['MBBS', '39 years experience']))]
df['Qualification'][idx] = 'MBBS'
idx = df.index[df['Qualification'].str.contains('BDS')]
df['Qualification'][idx] = 'BDS'
idx = df.index[df['Qualification'].str.contains('|'.join(['BHMS','LCEH', 'GCEH']))]
df['Qualification'][idx] = 'BHMS'
idx = df.index[df['Qualification'].str.contains('|'.join(['BAMS', 'BSAM', 'G.A.M.S', 'DAM']))]
df['Qualification'][idx] = 'BAMS'
####################### Working on Rating #################################
## Extracting the integer from the string
df['Rating'] = df.Rating.str.extract('(\d+)')
## Fill Nan with -1 and then convert to str and integer
df['Rating'] = df['Rating'].fillna(-1).astype(str).astype(np.float64)
df['Rating'] = df['Rating'].replace(-1, np.nan)
######## Imputing mean values depending upon 'Qualification' and 'Experience' #############
for qual in list(df.Qualification.unique()):
Mean = df[(df['Qualification'] == qual) & (df['Experience'] >= 30)]['Rating'].mean()
df.loc[(df['Qualification'] == qual) & (df['Experience'] >= 30), 'Rating'] = Mean
Mean = df[(df['Qualification']== qual) & (df['Experience']>=20) & (df['Experience']<30)]['Rating'].mean()
df.loc[(df['Qualification']==qual) & (df['Experience']>=20) & (df['Experience']<30),'Rating'] = Mean
Mean = df[(df['Qualification'] == qual) & (df['Experience'] >= 10) & (df['Experience'] < 20)]['Rating'].mean()
df.loc[(df['Qualification'] == qual) & (df['Experience'] >= 10) & (df['Experience'] < 20),'Rating'] = Mean
Mean = df[(df['Qualification'] == qual) & (df['Experience'] <10)]['Rating'].mean()
df.loc[(df['Qualification'] == qual) & (df['Experience'] <10),'Rating'] = Mean
########## Fill remaining null values, if any, with 0 ################################
df['Rating'] = df['Rating'].fillna(0)
return df
def main1(list_row):
data=pd.read_csv('D:\\FYP\\Update\\app\\staff\\model\\train.csv')
data.loc[len(data)] = list_row
data_new = PreProcess(data)
x=data_new.iloc[:,:5]
x = x.astype({"Qualification":'category',"Place":'category',"Profile":'category'})
x = pd.get_dummies(x,columns = ['Qualification', 'Place', 'Profile'],prefix=['Qualification', 'Place', 'Profile'])
MM = MinMaxScaler()
x[['Experience', 'Rating']] = MM.fit_transform(x[['Experience', 'Rating']])
import pickle
filename = "D:\\FYP\\Update\\app\\staff\\model\\final_model.pkl"
loaded_model=pickle.load(open(filename, 'rb'))
return(loaded_model.predict(x.iloc[:1]))
# print(main(['BHMS, MD - Homeopathy', '24 years experience', '100%', 'Kakkanad, Ernakulam', 'Homeopath','','']))
# Sample data
from rest_framework.serializers import ModelSerializer
from .models import Salary, User
from .models import labReport
class UserSerializer(ModelSerializer):
class Meta:
model = User
fields = ['id', 'name', 'email', 'password']
extra_kwargs = {
'password': {'write_only': True}
}
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
class LabReportSerializer(ModelSerializer):
class Meta:
model = labReport
fields = "__all__"
class salarySerializer(ModelSerializer):
class Meta:
model = Salary
fields = "__all__"
from django.test import TestCase
# Create your tests here.
from django.urls import path
from .views import RegisterAPIView, LoginAPIView, TodoListApiView, UserAPIView, RefreshAPIView, LogoutAPIView, LabReportView
urlpatterns = [
path('register', RegisterAPIView.as_view()),
path('login', LoginAPIView.as_view()),
path('user', UserAPIView.as_view()),
path('refresh', RefreshAPIView.as_view()),
path('logout', LogoutAPIView.as_view()),
path('logout', LogoutAPIView.as_view()),
path('labreport', LabReportView.as_view()),
path('salary', TodoListApiView.as_view())
]
from rest_framework.authentication import get_authorization_header
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.exceptions import APIException, AuthenticationFailed
from rest_framework.parsers import FileUploadParser
from rest_framework import status
from .authentication import create_access_token, create_refresh_token, decode_access_token, decode_refresh_token
from .serializers import UserSerializer, salarySerializer
from .models import Salary, User, labReport
from .serializers import LabReportSerializer
from .predict_Salary import main1
from .predict_LabReport import main
class RegisterAPIView(APIView):
def post(self, request):
serializer = UserSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
class LoginAPIView(APIView):
def post(self, request):
user = User.objects.filter(email=request.data['email']).first()
if not user:
raise APIException('Invalid credentials!')
if not user.check_password(request.data['password']):
raise APIException('Invalid credentials!')
access_token = create_access_token(user.id)
refresh_token = create_refresh_token(user.id)
response = Response()
response.set_cookie(key='refreshToken', value=refresh_token, httponly=True)
response.data = {
'token': access_token
}
return response
class UserAPIView(APIView):
def get(self, request):
auth = get_authorization_header(request).split()
if auth and len(auth) == 2:
token = auth[1].decode('utf-8')
id = decode_access_token(token)
user = User.objects.filter(pk=id).first()
return Response(UserSerializer(user).data)
raise AuthenticationFailed('unauthenticated')
class RefreshAPIView(APIView):
def post(self, request):
refresh_token = request.COOKIES.get('refreshToken')
id = decode_refresh_token(refresh_token)
access_token = create_access_token(id)
return Response({
'token': access_token
})
class LogoutAPIView(APIView):
def post(self, _):
response = Response()
response.delete_cookie(key="refreshToken")
response.data = {
'message': 'success'
}
return response
class LabReportView(APIView):
parser_class = (FileUploadParser,)
def post(self, request, *args, **kwargs):
file_serializer = LabReportSerializer(data=request.data)
if file_serializer.is_valid():
file_serializer.save()
result=file_serializer.data
result_class = main(result['file'][1:])
return Response(result_class, status=status.HTTP_201_CREATED)
else:
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class TodoListApiView(APIView):
def post(self, request, *args, **kwargs):
serializer = salarySerializer(data=request.data)
if serializer.is_valid():
serializer.save()
# print(serializer.data)
list_value =[serializer.data['Qualification'],
serializer.data['Experience'],
serializer.data['Rating'],
serializer.data['Place'],
serializer.data['Profile'],'',''
]
result_value = main1(list_row=list_value)
return Response(result_value, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, *args, **kwargs):
snippets = Salary.objects.all()
serializer = salarySerializer(snippets, many=True)
return Response(serializer.data)
test.jpeg

51.8 KB

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment