from django.contrib.auth.decorators import login_required from django.contrib import messages from django.shortcuts import render, get_object_or_404 from django.http import HttpResponse from .models import RegressionModel, ClassificationModel, NLPModel, UnsupervisedModel import joblib # Used for loading the trained models import numpy as np @login_required def index(request): # Ensure that the user is logged in before accessing the index page messages.info(request, "You need to log in to access this page.") return render(request, 'machinelearning/index.html') # Regression Views def regression_list(request): models = RegressionModel.objects.filter(is_active=True) return render(request, 'machinelearning/regression_list.html', {'models': models}) def regression_detail(request, pk): model = get_object_or_404(RegressionModel, pk=pk) return render(request, 'machinelearning/regression_detail.html', {'model': model}) # Classification Views def classification_list(request): models = ClassificationModel.objects.filter(is_active=True) return render(request, 'machinelearning/classification_list.html', {'models': models}) def classification_detail(request, pk): model = get_object_or_404(ClassificationModel, pk=pk) return render(request, 'machinelearning/classification_detail.html', {'model': model}) # NLP Views def nlp_list(request): models = NLPModel.objects.filter(is_active=True) return render(request, 'machinelearning/nlp_list.html', {'models': models}) def nlp_detail(request, pk): model = get_object_or_404(NLPModel, pk=pk) return render(request, 'machinelearning/nlp_detail.html', {'model': model}) # Unsupervised Learning Views def unsupervised_list(request): models = UnsupervisedModel.objects.filter(is_active=True) return render(request, 'machinelearning/unsupervised_list.html', {'models': models}) def unsupervised_detail(request, pk): model = get_object_or_404(UnsupervisedModel, pk=pk) return render(request, 'machinelearning/unsupervised_detail.html', {'model': model}) # Make Prediction View def make_prediction(request, model_type, pk): # Map the model_type to the corresponding model class model_class = { 'regression': RegressionModel, 'classification': ClassificationModel, 'nlp': NLPModel, 'unsupervised': UnsupervisedModel, }.get(model_type) # Get the model instance model = get_object_or_404(model_class, pk=pk, is_active=True) prediction = None if request.method == 'POST': # Extract input data from the POST request input_data = request.POST.get('input_data') # Load the model file try: model_file = model.model_file.path loaded_model = joblib.load(model_file) except Exception as e: return HttpResponse(f"Error loading model: {e}") # Prepare and process the input data for prediction try: if model_type in ['regression', 'classification', 'unsupervised']: # Handle numeric data input for these model types data = np.array([float(i) for i in input_data.split(',')]).reshape(1, -1) prediction = loaded_model.predict(data) elif model_type == 'nlp': # Handle text data input for NLP models prediction = loaded_model.predict([input_data]) except Exception as e: return HttpResponse(f"Error processing input data: {e}") # Render the prediction result return render(request, 'machinelearning/make_prediction.html', {'model': model, 'prediction': prediction})