Skip to content

Commit

Permalink
Merge pull request #27 from iurwpoietknckvjndfsm-gndvkd/authentication
Browse files Browse the repository at this point in the history
added create_superuser method, Added ML dir and dependacies
  • Loading branch information
shroukhegazi committed Mar 3, 2023
2 parents d6ee4a6 + 35cfbe2 commit 46244cc
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 0 deletions.
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions agriwise/crop_recomendation/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class CropRecomendationConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "agriwise.crop_recomendation"
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
55 changes: 55 additions & 0 deletions agriwise/ml/crop_recommendation/crop_recommendation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import joblib
import numpy
import pandas as pd


class RandomForestClassifier:
def __init__(self):
self.model = joblib.load("./random_forest.joblib")

def preprocessing(self, input_data):
# JSON to pandas DataFrame
input_data = pd.DataFrame(input_data, index=[0])

return input_data

def predict(self, input_data):
return self.model.predict_proba(input_data)

def postprocessing(self, prediction):
categories = [
"apple",
"banana",
"blackgram",
"chickpea",
"coconut",
"coffee",
"cotton",
"grapes",
"jute",
"kidneybeans",
"lentil",
"maize",
"mango",
"mothbeans",
"mungbean",
"muskmelon",
"orange",
"papaya",
"pigeonpeas",
"pomegranate",
"rice",
"watermelon",
]
index_max_predict = numpy.argmax(prediction)
return categories[index_max_predict]

def compute_prediction(self, input_data):
try:
input_data = self.preprocessing(input_data)
prediction = self.predict(input_data)[0] # only one sample
prediction = self.postprocessing(prediction)
except Exception as e:
return {"status": "Error", "message": str(e)}

return prediction
Binary file not shown.
16 changes: 16 additions & 0 deletions agriwise/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ def create_user(self, username, email, password=None):
user.save()
return user

def create_superuser(self, username, email, password=None):
if not username:
raise TypeError("superuser must have username")
if not email:
raise TypeError("superuser must have email")
if not password:
raise TypeError("superuser must have password")
email = self.normalize_email(email)
user = self.model(email=email, username=username)
user.set_password(password)
user.is_superuser = True
user.is_staff = True
user.is_active = True
user.save()
return user


class User(AbstractUser):
name = CharField(_("Name of User"), blank=True, max_length=255)
Expand Down
2 changes: 2 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

LOCAL_APPS = [
"agriwise.users",
"agriwise.crop_recomendation",
# Your stuff: custom apps go here
]
# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
Expand Down Expand Up @@ -110,6 +111,7 @@
"SERIALIZERS": {
"user_create": "agriwise.users.api.serializers.UserCreateSerializer"
},
"PASSWORD_RESET_CONFIRM_URL": "users/auth/api/users/password/reset/confirm/{uid}/{token}",
"ACTIVATION_URL": "users/auth/api/users/activation/{uid}/{token}",
"SEND_ACTIVATION_EMAIL": True,
"LOGIN_FIELD": "email",
Expand Down
4 changes: 4 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ django-cors-headers==3.13.0 # https://github.com/adamchainz/django-cors-headers
drf-spectacular==0.25.1 # https://github.com/tfranzel/drf-spectacular
#New
djoser==2.1.0
#ML dependancies
pandas==1.5.3
joblib==1.2.0
numpy==1.24.2

0 comments on commit 46244cc

Please sign in to comment.