Spaces:
Sleeping
Sleeping
Download webapp/forms.py from PeaceAbolisher/Model_Lab_Deployment: direct link, hf CLI and curl.
- Browser
- Download file 4.34 kB
-
https://huggingface.co/spaces/PeaceAbolisher/Model_Lab_Deployment/resolve/main/webapp/forms.py
- Command line
-
hf download hf://spaces/PeaceAbolisher/Model_Lab_Deployment/webapp/forms.py
-
curl -L -o forms.py https://huggingface.co/spaces/PeaceAbolisher/Model_Lab_Deployment/resolve/main/webapp/forms.py
4.34 kB
| 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 | |