Spaces:
Sleeping
Sleeping
File size: 4,341 Bytes
e50f22e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model
from .models import UserProfile
from datetime import date
SCALER_CHOICES = [
("standard", "Standard Scaler"),
("minmax", "MinMax Scaler"),
]
MODEL_CHOICES = [
("rf", "Random Forest"),
("svm", "Support Vector Machine"),
("knn", "K-Nearest Neighbors"),
("lr", "Logistic Regression"),
("gb", "Gradient Boosting"),
("xgb", "XGBoost"),
("mlp", "MLP (Neural Network)"),
]
META_CHOICES = [
("lr", "Logistic Regression"),
("rf", "Random Forest"),
("gb", "Gradient Boosting"),
]
ENSEMBLE_CHOICES = [
("none", "No Ensemble"),
("voting", "Voting Classifier"),
("stacking","Stacking Classifier"),
]
VOTING_CHOICES = [
("hard", "Hard Voting"),
("soft", "Soft Voting"),
]
BALANCING_CHOICES = [
("none", "None"),
("over", "Random Oversampling"),
("smote", "SMOTE"),
("gans", "GANS"),
]
AUGMENT_CHOICES = [
("none", "None"),
("gans", "GANS"),
]
class SaveModelForm(forms.Form):
name = forms.CharField(max_length=120)
public = forms.BooleanField(required=False)
description = forms.CharField(required=False,widget=forms.Textarea(attrs={"rows": 3, "maxlength": 2000})
)
class UploadForm(forms.Form):
dataset = forms.FileField(
label="Upload CSV Dataset",
widget=forms.ClearableFileInput(attrs={"class": "form-control"})
)
scaler = forms.ChoiceField(
choices=SCALER_CHOICES,
initial="standard",
widget=forms.Select(attrs={"class": "form-select"})
)
base_models = forms.MultipleChoiceField(
choices=MODEL_CHOICES,
widget=forms.CheckboxSelectMultiple,
label="Base Models",
required=True,
)
meta_learner = forms.ChoiceField(
choices=META_CHOICES,
initial="lr",
widget=forms.Select(attrs={"class": "form-select"})
)
ensemble_type = forms.ChoiceField(
choices=ENSEMBLE_CHOICES,
initial="none",
widget=forms.Select(attrs={"class": "form-select"})
)
voting_type = forms.ChoiceField(
choices=VOTING_CHOICES,
required=False,
initial="hard",
widget=forms.Select(attrs={"class": "form-select"})
)
balancing_strategy = forms.ChoiceField(
choices=BALANCING_CHOICES,
initial="none",
widget=forms.Select(attrs={"class": "form-select"})
)
augmentation = forms.ChoiceField(
choices=AUGMENT_CHOICES,
initial="none",
widget=forms.Select(attrs={"class": "form-select"})
)
target_samples = forms.IntegerField(
required=False,
min_value=1,
widget=forms.NumberInput(attrs={"class": "form-control", "placeholder": "e.g. 500"})
)
def clean_base_models(self):
data = self.cleaned_data.get("base_models") or []
if len(data) == 0:
raise forms.ValidationError("Pick at least one base model.")
return data
class RegisterForm(UserCreationForm):
full_name = forms.CharField(
max_length=150,
label="Name",
widget=forms.TextInput(attrs={"class": "form-control"})
)
date_of_birth = forms.DateField(
label="Date of birth",
widget=forms.DateInput(attrs={"type": "date", "class": "form-control"})
)
class Meta:
model = get_user_model()
fields = ("username", "full_name", "date_of_birth", "password1", "password2")
def clean_date_of_birth(self):
dob = self.cleaned_data["date_of_birth"]
if dob >= date.today():
raise forms.ValidationError("Date of birth must be in the past.")
return dob
def save(self, commit=True):
user = super().save(commit=False)
# Best-effort split full_name into first/last for the auth user
name = self.cleaned_data.get("full_name", "").strip()
first, *rest = name.split()
user.first_name = first or ""
user.last_name = " ".join(rest) if rest else ""
if commit:
user.save()
UserProfile.objects.create(
user=user,
full_name=name,
date_of_birth=self.cleaned_data["date_of_birth"],
)
return user
|