{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * Serving Flask app '__main__'\n", " * Debug mode: off\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\u001b[31m\u001b[1mWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\u001b[0m\n", " * Running on http://127.0.0.1:5000\n", "\u001b[33mPress CTRL+C to quit\u001b[0m\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "127.0.0.1 - - [06/Jan/2025 13:19:51] \"GET / HTTP/1.1\" 200 -\n", "/home/faddy/Documents/7-sem/mlops/assignment4/.venv/lib/python3.11/site-packages/sklearn/utils/validation.py:2739: UserWarning: X does not have valid feature names, but GradientBoostingRegressor was fitted with feature names\n", " warnings.warn(\n", "127.0.0.1 - - [06/Jan/2025 13:20:05] \"POST / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [06/Jan/2025 13:20:16] \"POST / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [06/Jan/2025 13:20:28] \"POST / HTTP/1.1\" 200 -\n" ] } ], "source": [ "from flask import Flask, request, render_template\n", "import numpy as np\n", "import pickle\n", "\n", "app = Flask(__name__)\n", "\n", "# Cloud Computing Weights and Max Marks\n", "cc_weights = {\n", " 'A1': 1, 'Q1': 1.5, 'A2': 1, 'Q2': 1.5, 'A3': 1,\n", " 'A4': 4, 'Q3': 1.5, 'Mid': 35, 'AWS Labs': 3,\n", " 'Q4': 1.25, 'A5': 4, 'Q5': 1.25, 'A6': 4, 'Final': 40\n", "}\n", "cc_max_marks = {\n", " 'A1': 10, 'Q1': 21, 'A2': 10, 'Q2': 30, 'A3': 100,\n", " 'A4': 10, 'Q3': 41, 'Mid': 35, 'AWS Labs': 10,\n", " 'Q4': 40, 'A5': 100, 'Q5': 20, 'A6': 100, 'Final': 40\n", "}\n", "\n", "# ICT Weights and Max Marks\n", "ict_weights = {\n", " 'Q1': 2.625, 'Q2': 2.625, 'A1': 2, 'Q3': 2.625, 'Q4': 2.625,\n", " 'Midterm': 35, 'Q5': 2.625, 'A2': 2, 'Q6': 2.625, 'Q7': 2.625,\n", " 'Q8': 2.625, 'Final': 40\n", "}\n", "ict_max_marks = {\n", " 'Q1': 30, 'Q2': 49, 'A1': 100, 'Q3': 30, 'Q4': 15,\n", " 'Midterm': 35, 'Q5': 45, 'A2': 100, 'Q6': 32, 'Q7': 24,\n", " 'Q8': 40, 'Final': 100\n", "}\n", "\n", "def normalize_input(input_activities, weights, max_marks):\n", " \"\"\"\n", " Normalize the input activities based on weights and max marks.\n", " \"\"\"\n", " normalized_activities = []\n", " for activity, score in input_activities.items():\n", " if score > 0: # Ignore zeros\n", " normalized_score = (score / max_marks[activity]) * weights[activity]\n", " normalized_activities.append(normalized_score)\n", " return normalized_activities\n", "\n", "def predict_final_score(input_activities, weights, max_marks, model_prefix):\n", " \"\"\"\n", " Predict the final score based on input activities after normalization.\n", " \"\"\"\n", " normalized_activities = normalize_input(input_activities, weights, max_marks)\n", " n = len(normalized_activities)\n", "\n", " if n == 0:\n", " return \"No valid activities entered. Please provide scores greater than 0.\"\n", "\n", " try:\n", " with open(f\"{model_prefix}_model_{n}_activities.pkl\", \"rb\") as file:\n", " model = pickle.load(file)\n", " except FileNotFoundError:\n", " return f\"No model available for {n} activities. Train the model first.\"\n", "\n", " input_array = np.array(normalized_activities).reshape(1, -1)\n", " predicted_score = model.predict(input_array)[0]\n", " return round(predicted_score, 2)\n", "\n", "@app.route('/', methods=['GET', 'POST'])\n", "def home():\n", " cc_predicted_score = None\n", " ict_predicted_score = None\n", "\n", " if request.method == 'POST':\n", " # Identify the form (Cloud Computing or ICT)\n", " form_type = request.form.get('form_type')\n", "\n", " if form_type == 'cloud_computing':\n", " input_activities = {\n", " activity: float(request.form.get(activity, 0) or 0) # Default to 0 for empty inputs\n", " for activity in cc_weights.keys()\n", " }\n", " cc_predicted_score = predict_final_score(input_activities, cc_weights, cc_max_marks, \"cloud_computing\")\n", "\n", " elif form_type == 'ict':\n", " input_activities = {\n", " activity: float(request.form.get(activity, 0) or 0) # Default to 0 for empty inputs\n", " for activity in ict_weights.keys()\n", " }\n", " ict_predicted_score = predict_final_score(input_activities, ict_weights, ict_max_marks, \"ict\")\n", "\n", " return render_template(\n", " 'index.html',\n", " cc_max_marks=cc_max_marks,\n", " ict_max_marks=ict_max_marks,\n", " cc_predicted_score=cc_predicted_score,\n", " ict_predicted_score=ict_predicted_score\n", " )\n", "\n", "if __name__ == '__main__':\n", " app.run(debug=False, port=5000)\n" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 2 }