From 32e185201c0a5ebb910ba35e1835ac6639c4f34a Mon Sep 17 00:00:00 2001 From: Sneha Singhania Date: Tue, 5 Nov 2024 13:29:03 +0000 Subject: [PATCH] commit changes --- model_evaluation/ models/model.py | 7 ++++ model_evaluation/app.py | 40 ++++++++++++++++++++ model_evaluation/datasets/dataset_manager.py | 19 ++++++++++ model_evaluation/evaluations/evaluator.py | 40 ++++++++++++++++++++ model_evaluation/requirements.txt | 4 ++ model_evaluation/static/script.js | 37 ++++++++++++++++++ model_evaluation/static/style.css | 24 ++++++++++++ model_evaluation/templates/index.html | 40 ++++++++++++++++++++ model_evaluation/templates/results.html | 17 +++++++++ 9 files changed, 228 insertions(+) create mode 100644 model_evaluation/ models/model.py create mode 100644 model_evaluation/app.py create mode 100644 model_evaluation/datasets/dataset_manager.py create mode 100644 model_evaluation/evaluations/evaluator.py create mode 100644 model_evaluation/requirements.txt create mode 100644 model_evaluation/static/script.js create mode 100644 model_evaluation/static/style.css create mode 100644 model_evaluation/templates/index.html create mode 100644 model_evaluation/templates/results.html diff --git a/model_evaluation/ models/model.py b/model_evaluation/ models/model.py new file mode 100644 index 00000000..ff100bb4 --- /dev/null +++ b/model_evaluation/ models/model.py @@ -0,0 +1,7 @@ +import joblib + +def load_model(model_name): + return joblib.load(f'models/{model_name}.pkl') + +def save_model(model, model_name): + joblib.dump(model, f'models/{model_name}.pkl') diff --git a/model_evaluation/app.py b/model_evaluation/app.py new file mode 100644 index 00000000..bcb46dbf --- /dev/null +++ b/model_evaluation/app.py @@ -0,0 +1,40 @@ +from flask import Flask, render_template, request, redirect, url_for, flash +from datasets.dataset_manager import DatasetManager +from evaluations.evaluator import ModelEvaluator +import os + +app = Flask(__name__) +app.secret_key = 'your_secret_key' # Set a secret key for session management + +dataset_manager = DatasetManager() +evaluator = ModelEvaluator() + +@app.route('/') +def index(): + datasets = dataset_manager.get_standard_datasets() # Get available datasets + return render_template('index.html', datasets=datasets) + +@app.route('/upload', methods=['POST']) +def upload_dataset(): + if request.method == 'POST': + file = request.files['dataset'] + if file and dataset_manager.allowed_file(file.filename): + dataset_name = dataset_manager.save_uploaded_dataset(file) + flash(f'Dataset {dataset_name} uploaded successfully!', 'success') + else: + flash('Invalid file type. Please upload a valid dataset.', 'danger') + return redirect(url_for('index')) + +@app.route('/evaluate', methods=['POST']) +def evaluate_model(): + model_name = request.form['model_name'] + dataset_name = request.form['dataset_name'] + results = evaluator.evaluate(model_name, dataset_name) + if results: + return render_template('results.html', results=results) + else: + flash('Evaluation failed. Please check the model and dataset.', 'danger') + return redirect(url_for('index')) + +if __name__ == '__main__': + app.run(debug=True) diff --git a/model_evaluation/datasets/dataset_manager.py b/model_evaluation/datasets/dataset_manager.py new file mode 100644 index 00000000..d53d56ab --- /dev/null +++ b/model_evaluation/datasets/dataset_manager.py @@ -0,0 +1,19 @@ +import os +import pandas as pd + +class DatasetManager: + ALLOWED_EXTENSIONS = {'csv', 'xlsx', 'json'} # Define allowed file extensions + + def __init__(self): + self.standard_datasets = ['MNIST', 'CIFAR-10', 'IMDB'] # Placeholder for actual paths + + def allowed_file(self, filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in self.ALLOWED_EXTENSIONS + + def get_standard_datasets(self): + return self.standard_datasets + + def save_uploaded_dataset(self, file): + dataset_path = os.path.join('datasets', file.filename) + file.save(dataset_path) + return file.filename diff --git a/model_evaluation/evaluations/evaluator.py b/model_evaluation/evaluations/evaluator.py new file mode 100644 index 00000000..a25e74da --- /dev/null +++ b/model_evaluation/evaluations/evaluator.py @@ -0,0 +1,40 @@ +from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score +import joblib +import pandas as pd +import os + +class ModelEvaluator: + def evaluate(self, model_name, dataset_name): + model_path = f'models/{model_name}.pkl' + + # Check if model file exists + if not os.path.exists(model_path): + print(f"Model {model_name} not found.") + return None + + model = joblib.load(model_path) # Load model + X, y = self.load_data(dataset_name) # Load data + + if X is None or y is None: + return None + + y_pred = model.predict(X) + + metrics = { + 'accuracy': accuracy_score(y, y_pred), + 'precision': precision_score(y, y_pred, average='weighted'), + 'recall': recall_score(y, y_pred, average='weighted'), + 'f1_score': f1_score(y, y_pred, average='weighted') + } + return metrics + + def load_data(self, dataset_name): + # Implement data loading based on the dataset type + dataset_path = os.path.join('datasets', dataset_name) + if dataset_name.endswith('.csv'): + data = pd.read_csv(dataset_path) + X = data.iloc[:, :-1].values # Features (all but last column) + y = data.iloc[:, -1].values # Labels (last column) + return X, y + # Add more conditions to load other dataset types if necessary + return None, None diff --git a/model_evaluation/requirements.txt b/model_evaluation/requirements.txt new file mode 100644 index 00000000..6569f1df --- /dev/null +++ b/model_evaluation/requirements.txt @@ -0,0 +1,4 @@ +Flask +scikit-learn +joblib +pandas diff --git a/model_evaluation/static/script.js b/model_evaluation/static/script.js new file mode 100644 index 00000000..ffa38120 --- /dev/null +++ b/model_evaluation/static/script.js @@ -0,0 +1,37 @@ +// Function to automatically hide flash messages after a few seconds +document.addEventListener("DOMContentLoaded", () => { + const flashMessages = document.querySelectorAll(".flash-message"); + flashMessages.forEach(msg => { + setTimeout(() => { + msg.style.display = "none"; + }, 5000); // Adjust the timeout as needed (5000ms = 5 seconds) + }); +}); + +// Form validation for dataset upload and model evaluation +document.addEventListener("DOMContentLoaded", () => { + const uploadForm = document.querySelector("form[action='/upload']"); + const evaluateForm = document.querySelector("form[action='/evaluate']"); + + if (uploadForm) { + uploadForm.addEventListener("submit", (e) => { + const fileInput = uploadForm.querySelector("input[type='file']"); + if (!fileInput.value) { + alert("Please select a dataset file to upload."); + e.preventDefault(); + } + }); + } + + if (evaluateForm) { + evaluateForm.addEventListener("submit", (e) => { + const modelSelect = evaluateForm.querySelector("select[name='model_name']"); + const datasetSelect = evaluateForm.querySelector("select[name='dataset_name']"); + + if (!modelSelect.value || !datasetSelect.value) { + alert("Please select both a model and a dataset to evaluate."); + e.preventDefault(); + } + }); + } +}); diff --git a/model_evaluation/static/style.css b/model_evaluation/static/style.css new file mode 100644 index 00000000..d2b692d4 --- /dev/null +++ b/model_evaluation/static/style.css @@ -0,0 +1,24 @@ +body { + font-family: Arial, sans-serif; +} + +h1 { + color: #333; +} + +form { + margin: 20px 0; +} + +input, select, button { + padding: 10px; + margin: 5px 0; +} + +.success { + color: green; +} + +.danger { + color: red; +} diff --git a/model_evaluation/templates/index.html b/model_evaluation/templates/index.html new file mode 100644 index 00000000..a3f44884 --- /dev/null +++ b/model_evaluation/templates/index.html @@ -0,0 +1,40 @@ + + + + + Model Evaluation + + + +

Model Evaluation and Benchmarking

+ + {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + + {% endif %} + {% endwith %} + +
+ + +
+ +
+ + + +
+ + diff --git a/model_evaluation/templates/results.html b/model_evaluation/templates/results.html new file mode 100644 index 00000000..941581ae --- /dev/null +++ b/model_evaluation/templates/results.html @@ -0,0 +1,17 @@ + + + + + Evaluation Results + + +

Evaluation Results

+ + Back + +