From 40c677dc24638c5cb63d63a09559b45ce456cb80 Mon Sep 17 00:00:00 2001 From: James <james2.burt@live.uwe.ac.uk> Date: Wed, 7 May 2025 22:40:18 +0100 Subject: [PATCH] Final merging of ai with desd Linear Works fully with Test Cases Project 99% Complete --- .idea/DjangoDocker.iml | 2 +- .idea/misc.xml | 2 +- ActualProjectCode/DjangoProject/core/forms.py | 8 ++ .../DjangoProject/core/models.py | 2 + ActualProjectCode/DjangoProject/core/views.py | 14 +++- .../templates/modelForms/billingsView.html | 80 +++++++++++++++++++ .../templates/modelForms/mlUpload.html | 1 + .../templates/modelForms/userUpload.html | 1 + .../DjangoProject/templates/profile.html | 5 +- Working Models/Linear_trained.py | 3 + Working Models/Trained_Regression.py | 5 +- Working Models/train_symbolic_model.py | 26 +++++- 12 files changed, 137 insertions(+), 12 deletions(-) diff --git a/.idea/DjangoDocker.iml b/.idea/DjangoDocker.iml index ae11b5f3..72ca9979 100644 --- a/.idea/DjangoDocker.iml +++ b/.idea/DjangoDocker.iml @@ -17,7 +17,7 @@ <sourceFolder url="file://$MODULE_DIR$/ActualProjectCode/DjangoProject" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/mainDockerImage/backend" isTestSource="false" /> </content> - <orderEntry type="jdk" jdkName="Python 3.13" jdkType="Python SDK" /> + <orderEntry type="jdk" jdkName="C:\Users\james\AppData\Local\Programs\Python\Python311\python.exe" jdkType="Python SDK" /> <orderEntry type="sourceFolder" forTests="false" /> </component> </module> \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 1d3ce46b..52b60581 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ <component name="Black"> <option name="sdkName" value="Python 3.13" /> </component> - <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13" project-jdk-type="Python SDK" /> + <component name="ProjectRootManager" version="2" project-jdk-name="C:\Users\james\AppData\Local\Programs\Python\Python311\python.exe" project-jdk-type="Python SDK" /> </project> \ No newline at end of file diff --git a/ActualProjectCode/DjangoProject/core/forms.py b/ActualProjectCode/DjangoProject/core/forms.py index c95ed113..ec889d2c 100644 --- a/ActualProjectCode/DjangoProject/core/forms.py +++ b/ActualProjectCode/DjangoProject/core/forms.py @@ -3,6 +3,7 @@ from django.forms import ModelForm from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm from . import models +import re class CustomUserCreationForm(UserCreationForm): email = forms.EmailField(required=True) @@ -11,6 +12,7 @@ class CustomUserCreationForm(UserCreationForm): model = User fields = ['username', 'email', 'password1', 'password2'] + def save(self, commit=True): user = super().save(commit=False) user.email = self.cleaned_data['email'] @@ -18,6 +20,8 @@ class CustomUserCreationForm(UserCreationForm): user.save() return user + + class UserLoginForm(forms.Form): username = forms.CharField(max_length=100) password = forms.CharField(widget=forms.PasswordInput) @@ -40,6 +44,10 @@ class GenerateBillingsForm(forms.ModelForm): model = models.Billing fields = ['amount', 'username', 'companyName'] +class BillingFilterForm(forms.Form): + start_date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}), required=False) + end_date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}), required=False) + class UpdateUser(forms.ModelForm): class Meta: model = models.Profile diff --git a/ActualProjectCode/DjangoProject/core/models.py b/ActualProjectCode/DjangoProject/core/models.py index 98f15ba2..2258c65f 100644 --- a/ActualProjectCode/DjangoProject/core/models.py +++ b/ActualProjectCode/DjangoProject/core/models.py @@ -29,6 +29,8 @@ class Billing(models.Model): username = models.CharField(max_length=100, default="company") companyName = models.CharField(max_length=100) approvalStatus = models.CharField(max_length=10, default="unapproved") + created_at = models.DateTimeField(auto_now_add=True) + def __str__(self): return f"{self.companyName} Billing - ID: {self.billingID}" diff --git a/ActualProjectCode/DjangoProject/core/views.py b/ActualProjectCode/DjangoProject/core/views.py index 57566fb7..0784fea8 100644 --- a/ActualProjectCode/DjangoProject/core/views.py +++ b/ActualProjectCode/DjangoProject/core/views.py @@ -108,6 +108,7 @@ def genBillings(request): context = {'form': form} return render(request, 'modelForms/genBillings.html', context) + @login_required def manageUsers(request): profile = Profile.objects.all() @@ -154,10 +155,21 @@ def interactionView(request): context = {'interactions': interactions} return render(request, 'modelForms/interactionView.html', context) + @login_required def billingsView(request): + # Start with all billings billings = Billing.objects.all() - context = {} + + # Apply date filters if provided + start_date = request.GET.get('start_date') + end_date = request.GET.get('end_date') + + if start_date: + billings = billings.filter(created_at__date__gte=start_date) + if end_date: + billings = billings.filter(created_at__date__lte=end_date) + context = {'billings': billings} return render(request, 'modelForms/billingsView.html', context) diff --git a/ActualProjectCode/DjangoProject/templates/modelForms/billingsView.html b/ActualProjectCode/DjangoProject/templates/modelForms/billingsView.html index 1d44a99b..5fe03642 100644 --- a/ActualProjectCode/DjangoProject/templates/modelForms/billingsView.html +++ b/ActualProjectCode/DjangoProject/templates/modelForms/billingsView.html @@ -81,6 +81,64 @@ background: #ccc; margin: 15px 0; } + + .filter-section { + background: #f9f9f9; + padding: 15px; + margin-bottom: 20px; + border-radius: 5px; + text-align: left; + } + + .filter-section h3 { + margin-top: 0; + color: #333; + } + + .form-group { + margin-bottom: 15px; + } + + .form-group label { + display: inline-block; + width: 100px; + font-weight: bold; + } + + .form-group input { + padding: 8px; + border: 1px solid #ddd; + border-radius: 4px; + } + + button { + padding: 8px 16px; + background: #004085; + color: white; + border: none; + border-radius: 4px; + cursor: pointer; + margin-right: 10px; + } + + button:hover { + background: #002754; + } + + .btn-secondary { + background: #6c757d; + color: white; + padding: 8px 16px; + border: none; + border-radius: 4px; + text-decoration: none; + display: inline-block; + } + + .btn-secondary:hover { + background: #5a6268; + color: white; + } </style> </head> <body> @@ -88,6 +146,23 @@ <div class="container"> <p><a href="{% url 'profile' %}">Return to Profile</a></p> + <!-- Filter Form Section --> + <div class="filter-section"> + <h3>Filter Billings by Date</h3> + <form method="get"> + <div class="form-group"> + <label for="start_date">Start Date:</label> + <input type="date" name="start_date" id="start_date"> + </div> + <div class="form-group"> + <label for="end_date">End Date:</label> + <input type="date" name="end_date" id="end_date"> + </div> + <button type="submit">Apply Filter</button> + <a href="{% url 'billingsView' %}" class="btn-secondary">Clear Filters</a> + </form> + </div> + {% for record in billings %} {% if record.approvalStatus == 'approved' %} <div class="record"> @@ -95,10 +170,15 @@ <p><strong>Amount: </strong>£{{ record.amount }}</p> <p><strong>Generated By: </strong>{{ record.username }}</p> <p><strong>Company Billed: </strong>{{ record.companyName }}</p> + <p><strong>Date Created: </strong>{{ record.created_at|date:"F d, Y H:i" }}</p> <a href="" class="update-btn">Pay Here</a> <a href="{% url 'deleteBillings' record.billingID %}" class="delete-btn">Delete Billing</a> </div> {% endif %} + {% empty %} + <div class="record"> + <p>No approved billings found for the selected date range.</p> + </div> {% endfor %} </div> diff --git a/ActualProjectCode/DjangoProject/templates/modelForms/mlUpload.html b/ActualProjectCode/DjangoProject/templates/modelForms/mlUpload.html index cd5806b2..a85e26d7 100644 --- a/ActualProjectCode/DjangoProject/templates/modelForms/mlUpload.html +++ b/ActualProjectCode/DjangoProject/templates/modelForms/mlUpload.html @@ -59,6 +59,7 @@ <div class="container"> <h2>Upload a new ML model</h2> + <p><a href="{% url 'profile' %}">Return to Profile</a></p> <form action="" method="post" enctype="multipart/form-data"> {% csrf_token %} {{ form.as_p }} diff --git a/ActualProjectCode/DjangoProject/templates/modelForms/userUpload.html b/ActualProjectCode/DjangoProject/templates/modelForms/userUpload.html index 1e41304e..38b576a5 100644 --- a/ActualProjectCode/DjangoProject/templates/modelForms/userUpload.html +++ b/ActualProjectCode/DjangoProject/templates/modelForms/userUpload.html @@ -78,6 +78,7 @@ </head> <body> <div class="container"> + <p><a href="{% url 'profile' %}">Return to Profile</a></p> <form action="" method="POST" enctype="multipart/form-data"> {% for model in models %} <h2> {{ model.modelTitle }}</h2> diff --git a/ActualProjectCode/DjangoProject/templates/profile.html b/ActualProjectCode/DjangoProject/templates/profile.html index ad63c143..afd46665 100644 --- a/ActualProjectCode/DjangoProject/templates/profile.html +++ b/ActualProjectCode/DjangoProject/templates/profile.html @@ -168,8 +168,8 @@ <hr> <h1 class="sectionTitle">AI Engineer Section</h1> <!-- AI Section --> - <h2>Upload Dataset</h2> - <h2><a href="{% url 'userUpload' %}">Upload</a></h2> + <h2>Upload ML Model</h2> + <h2><a href="{% url 'mlUpload' %}">Upload</a></h2> <h2>Access ML Interactions</h2> <h2><a href="{% url 'interactionView' %}">All Interactions</a></h2> <h2>Manage Models</h2> @@ -233,6 +233,7 @@ <hr> <h1 class="sectionTitle">User Section</h1> <!-- User Section --> + <h2>Upload Files to ML</h2> <h2><a href="{% url 'userUpload' %}">Upload CSV</a></h2> <h2>View your uploads</h2> diff --git a/Working Models/Linear_trained.py b/Working Models/Linear_trained.py index 2e6f67ed..cad2a47e 100644 --- a/Working Models/Linear_trained.py +++ b/Working Models/Linear_trained.py @@ -195,6 +195,7 @@ def main(): # Save predictions to CSV csv_output_path = os.path.join(input_dir, f"{input_name}_predictions.csv") + print(csv_output_path) predictions_df.to_csv(csv_output_path, index=False) # Restore original stderr before printing @@ -207,5 +208,7 @@ def main(): null_file.close() + + if __name__ == "__main__": main() \ No newline at end of file diff --git a/Working Models/Trained_Regression.py b/Working Models/Trained_Regression.py index a3818df9..de4f1b56 100644 --- a/Working Models/Trained_Regression.py +++ b/Working Models/Trained_Regression.py @@ -46,7 +46,7 @@ predictions = model.predict(X_scaled) #Save output output_df = df.copy() output_df[f"Predicted_{target_col}"] = predictions -output_path = os.path.join(script_dir, "new_data_for_prediction_predictions.csv") +output_path = os.path.join(script_dir, "symbolic_regression_trained_data.csv") # error here with script_dir output_df.to_csv(output_path, index=False) # Optional evaluation @@ -59,5 +59,4 @@ if target_col in df.columns: rmse = np.sqrt(mean_squared_error(y_true[mask], y_pred[mask])) r2 = r2_score(y_true[mask], y_pred[mask]) - else: - end +print(output_path) \ No newline at end of file diff --git a/Working Models/train_symbolic_model.py b/Working Models/train_symbolic_model.py index 3aec09d7..9644f2c7 100644 --- a/Working Models/train_symbolic_model.py +++ b/Working Models/train_symbolic_model.py @@ -1,6 +1,4 @@ -import os -import json -import re +import os, json, re, sys, warnings import pandas as pd import numpy as np from gplearn.genetic import SymbolicRegressor @@ -8,8 +6,17 @@ from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, r2_score from sklearn.preprocessing import StandardScaler, LabelEncoder from sklearn.impute import SimpleImputer +from sklearn.exceptions import SkipTestWarning, ConvergenceWarning from joblib import dump + +warnings.filterwarnings("ignore", category=UserWarning) +warnings.filterwarnings("ignore", category=DeprecationWarning) +warnings.filterwarnings("ignore", category=FutureWarning) +warnings.filterwarnings("ignore", category=SkipTestWarning) +warnings.filterwarnings("ignore", category=ConvergenceWarning) +warnings.filterwarnings("ignore") + # Path construction script_dir = os.path.dirname(os.path.abspath(__file__)) file_path = f"{sys.argv[2]}" @@ -112,7 +119,7 @@ model = SymbolicRegressor( p_hoist_mutation=0.05, p_point_mutation=0.1, max_samples=0.9, - verbose=1, + verbose=0, parsimony_coefficient=0.01, random_state=42, function_set=('add', 'sub', 'mul', 'div', 'sqrt', 'log', 'sin', 'cos') @@ -136,3 +143,14 @@ with open(os.path.join(script_dir, "symbolic_model_meta.json"), "w") as f: "expression": str(model._program) }, f, indent=4) +input_dir = os.path.dirname(file_path) if os.path.dirname(file_path) else "." +output_path = os.path.join(input_dir, "Symbolic_Results.txt") +with open(output_path, "w") as f: + f.write(f"Train RMSE: {rmse}\n") + f.write(f"Test RMSE: {rmse}\n") + f.write(f"Train R² Score: {r2}\n") + f.write(f"Test R² Score: {r2}\n") + f.write(f"Expression {str(model._program)}\n") + +print(output_path) + -- GitLab