File size: 5,908 Bytes
d0c7e75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{
 "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
}