Mirror of github.com/Abhisingh18/IISC-Assignment-1
Browse files- Procfile +1 -0
- README.md +1 -0
- app.py +111 -0
- best_model.npy +3 -0
- requirements.txt +4 -0
- static/script.js +97 -0
- static/style.css +74 -0
- templates/index.html +54 -0
Procfile
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
web: gunicorn app:app
|
README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Deployed link: https://iisc-assignment-1-1.onrender.com/
|
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
# -------------------------------
|
| 7 |
+
# Load Trained Model Weights
|
| 8 |
+
# -------------------------------
|
| 9 |
+
best = np.load("best_model.npy") # make sure this file is present
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# -------------------------------
|
| 13 |
+
# HARDCODE YOUR MSE VALUES HERE
|
| 14 |
+
# -------------------------------
|
| 15 |
+
train_mse = 0.005707
|
| 16 |
+
test_mse = 0.008744
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# -------------------------------
|
| 20 |
+
# FCNN Model Functions
|
| 21 |
+
# -------------------------------
|
| 22 |
+
input_dim = 1
|
| 23 |
+
h1 = 32
|
| 24 |
+
h2 = 16
|
| 25 |
+
output_dim = 1
|
| 26 |
+
|
| 27 |
+
D = input_dim*h1 + h1 + h1*h2 + h2 + h2*output_dim + output_dim
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def decode_theta(theta):
|
| 31 |
+
idx = 0
|
| 32 |
+
W1 = theta[idx:idx + input_dim*h1].reshape(input_dim, h1)
|
| 33 |
+
idx += input_dim*h1
|
| 34 |
+
b1 = theta[idx:idx + h1].reshape(1, h1)
|
| 35 |
+
idx += h1
|
| 36 |
+
|
| 37 |
+
W2 = theta[idx:idx + h1*h2].reshape(h1, h2)
|
| 38 |
+
idx += h1*h2
|
| 39 |
+
b2 = theta[idx:idx + h2].reshape(1, h2)
|
| 40 |
+
idx += h2
|
| 41 |
+
|
| 42 |
+
W3 = theta[idx:idx + h2*output_dim].reshape(h2, output_dim)
|
| 43 |
+
idx += h2*output_dim
|
| 44 |
+
b3 = theta[idx:idx + output_dim].reshape(1, output_dim)
|
| 45 |
+
|
| 46 |
+
return W1, b1, W2, b2, W3, b3
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def fcnn_forward(X_batch, theta):
|
| 50 |
+
W1, b1, W2, b2, W3, b3 = decode_theta(theta)
|
| 51 |
+
|
| 52 |
+
z1 = X_batch @ W1 + b1
|
| 53 |
+
a1 = np.maximum(z1, 0)
|
| 54 |
+
|
| 55 |
+
z2 = a1 @ W2 + b2
|
| 56 |
+
a2 = np.maximum(z2, 0)
|
| 57 |
+
|
| 58 |
+
out = a2 @ W3 + b3
|
| 59 |
+
return out
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def predict_next(theta, x):
|
| 63 |
+
y = fcnn_forward(np.array([[x]]), theta)
|
| 64 |
+
return float(y[0][0])
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def forecast_future(theta, x_start, steps=20):
|
| 68 |
+
preds = []
|
| 69 |
+
x = x_start
|
| 70 |
+
for _ in range(steps):
|
| 71 |
+
y = float(fcnn_forward(np.array([[x]]), theta)[0][0])
|
| 72 |
+
preds.append(y)
|
| 73 |
+
x = y
|
| 74 |
+
return preds
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# -------------------------------
|
| 78 |
+
# Routes
|
| 79 |
+
# -------------------------------
|
| 80 |
+
|
| 81 |
+
@app.route("/")
|
| 82 |
+
def index():
|
| 83 |
+
return render_template("index.html")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
@app.route("/predict", methods=["POST"])
|
| 87 |
+
def predict():
|
| 88 |
+
data = request.json.get("data", None)
|
| 89 |
+
if data is None or len(data) == 0:
|
| 90 |
+
return jsonify({"error": "No data provided"}), 400
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
series = np.array(data, dtype=float)
|
| 94 |
+
except:
|
| 95 |
+
return jsonify({"error": "Invalid numeric data"}), 400
|
| 96 |
+
|
| 97 |
+
last_val = float(series[-1])
|
| 98 |
+
next_val = predict_next(best, last_val)
|
| 99 |
+
future_vals = forecast_future(best, last_val, steps=20)
|
| 100 |
+
|
| 101 |
+
return jsonify({
|
| 102 |
+
"last": last_val,
|
| 103 |
+
"next": next_val,
|
| 104 |
+
"future": future_vals,
|
| 105 |
+
"train_mse": train_mse,
|
| 106 |
+
"test_mse": test_mse
|
| 107 |
+
})
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
app.run(debug=True)
|
best_model.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:843aaf8165013086da69bd9a8f5d018a9f7f41f9893d4f90b4fe58ff34926331
|
| 3 |
+
size 5000
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask
|
| 2 |
+
numpy
|
| 3 |
+
gunicorn
|
| 4 |
+
flask-cors
|
static/script.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
let chartInstance = null;
|
| 2 |
+
|
| 3 |
+
document.getElementById("fileInput").addEventListener("change", function () {
|
| 4 |
+
let file = this.files[0];
|
| 5 |
+
if (!file) return;
|
| 6 |
+
|
| 7 |
+
let reader = new FileReader();
|
| 8 |
+
|
| 9 |
+
reader.onload = function (e) {
|
| 10 |
+
let text = e.target.result.trim();
|
| 11 |
+
let lines = text.split(/\r?\n/);
|
| 12 |
+
|
| 13 |
+
// Parse to float, ignore non-numeric
|
| 14 |
+
let values = [];
|
| 15 |
+
for (let line of lines) {
|
| 16 |
+
let v = parseFloat(line.trim());
|
| 17 |
+
if (!isNaN(v)) values.push(v);
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
if (values.length === 0) {
|
| 21 |
+
alert("No numeric data found in CSV");
|
| 22 |
+
return;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
// Show first 10 values in left panel
|
| 26 |
+
document.getElementById("dataPreview").textContent =
|
| 27 |
+
values.slice(0, 10).join("\n");
|
| 28 |
+
|
| 29 |
+
// Call backend for prediction
|
| 30 |
+
fetch("/predict", {
|
| 31 |
+
method: "POST",
|
| 32 |
+
headers: {
|
| 33 |
+
"Content-Type": "application/json"
|
| 34 |
+
},
|
| 35 |
+
body: JSON.stringify({ data: values })
|
| 36 |
+
})
|
| 37 |
+
.then(res => res.json())
|
| 38 |
+
.then(res => {
|
| 39 |
+
if (res.error) {
|
| 40 |
+
alert("Error from server: " + res.error);
|
| 41 |
+
return;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
// RIGHT PANEL predictions
|
| 45 |
+
document.getElementById("lastValue").textContent = res.last.toFixed(6);
|
| 46 |
+
document.getElementById("nextPred").textContent = res.next.toFixed(6);
|
| 47 |
+
|
| 48 |
+
let future = res.future;
|
| 49 |
+
document.getElementById("futurePreds").textContent =
|
| 50 |
+
future.map(v => v.toFixed(6)).join("\n");
|
| 51 |
+
|
| 52 |
+
// LEFT PANEL MSE — ADD HERE
|
| 53 |
+
document.getElementById("trainMSE").textContent = res.train_mse.toFixed(6);
|
| 54 |
+
document.getElementById("testMSE").textContent = res.test_mse.toFixed(6);
|
| 55 |
+
|
| 56 |
+
// GRAPH
|
| 57 |
+
drawGraph(future);
|
| 58 |
+
})
|
| 59 |
+
|
| 60 |
+
.catch(err => {
|
| 61 |
+
console.error(err);
|
| 62 |
+
alert("Error calling backend");
|
| 63 |
+
});
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
reader.readAsText(file);
|
| 67 |
+
});
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
function drawGraph(data) {
|
| 71 |
+
const ctx = document.getElementById("forecastChart").getContext("2d");
|
| 72 |
+
|
| 73 |
+
if (chartInstance) {
|
| 74 |
+
chartInstance.destroy();
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
chartInstance = new Chart(ctx, {
|
| 78 |
+
type: "line",
|
| 79 |
+
data: {
|
| 80 |
+
labels: data.map((_, i) => i + 1),
|
| 81 |
+
datasets: [{
|
| 82 |
+
label: "Predicted Workload",
|
| 83 |
+
data: data,
|
| 84 |
+
borderColor: "#1e88e5",
|
| 85 |
+
fill: false,
|
| 86 |
+
tension: 0.3
|
| 87 |
+
}]
|
| 88 |
+
},
|
| 89 |
+
options: {
|
| 90 |
+
responsive: true,
|
| 91 |
+
scales: {
|
| 92 |
+
x: { title: { display: true, text: "Steps Ahead" } },
|
| 93 |
+
y: { title: { display: true, text: "Workload" } }
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
});
|
| 97 |
+
}
|
static/style.css
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
.panel p span {
|
| 3 |
+
font-weight: bold;
|
| 4 |
+
color: #1e88e5;
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
body {
|
| 9 |
+
font-family: Arial, sans-serif;
|
| 10 |
+
background: #f4f6f9;
|
| 11 |
+
margin: 0;
|
| 12 |
+
padding: 20px;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
.title {
|
| 16 |
+
text-align: center;
|
| 17 |
+
font-size: 32px;
|
| 18 |
+
font-weight: 800;
|
| 19 |
+
color: #1e88e5;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
.upload-box {
|
| 23 |
+
margin: 20px auto;
|
| 24 |
+
padding: 20px;
|
| 25 |
+
width: 50%;
|
| 26 |
+
text-align: center;
|
| 27 |
+
border: 2px dashed #1e88e5;
|
| 28 |
+
background: #e3f2fd;
|
| 29 |
+
border-radius: 10px;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
.row {
|
| 33 |
+
display: flex;
|
| 34 |
+
gap: 20px;
|
| 35 |
+
margin-top: 20px;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
.panel {
|
| 39 |
+
background: white;
|
| 40 |
+
padding: 20px;
|
| 41 |
+
border-radius: 12px;
|
| 42 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
| 43 |
+
flex: 1;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
.left-panel {
|
| 47 |
+
border-left: 6px solid #1e88e5;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
.right-panel {
|
| 51 |
+
border-left: 6px solid #43a047;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
.graph-section {
|
| 55 |
+
margin-top: 40px;
|
| 56 |
+
padding: 20px;
|
| 57 |
+
background: white;
|
| 58 |
+
border-radius: 12px;
|
| 59 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
pre {
|
| 63 |
+
background: #f0f0f0;
|
| 64 |
+
padding: 10px;
|
| 65 |
+
border-radius: 6px;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
.prediction p {
|
| 69 |
+
font-size: 16px;
|
| 70 |
+
margin: 4px 0;
|
| 71 |
+
}
|
| 72 |
+
.prediction span {
|
| 73 |
+
font-weight: bold;
|
| 74 |
+
}
|
templates/index.html
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>Cloud Workload Forecasting</title>
|
| 7 |
+
|
| 8 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" />
|
| 9 |
+
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
| 10 |
+
|
| 11 |
+
</head>
|
| 12 |
+
<body>
|
| 13 |
+
|
| 14 |
+
<h1 class="title">⚡ Cloud Workload Forecasting Dashboard</h1>
|
| 15 |
+
|
| 16 |
+
<div class="upload-box">
|
| 17 |
+
<input type="file" id="fileInput" accept=".csv" />
|
| 18 |
+
</div>
|
| 19 |
+
|
| 20 |
+
<div class="row">
|
| 21 |
+
<!-- LEFT PANEL -->
|
| 22 |
+
<div class="panel left-panel">
|
| 23 |
+
<h2>📊 First 10 Values</h2>
|
| 24 |
+
<pre id="dataPreview">Upload CSV...</pre>
|
| 25 |
+
|
| 26 |
+
<h3 style="margin-top:25px;">📉 Model Performance (MSE)</h3>
|
| 27 |
+
<p>Train MSE: <span id="trainMSE">--</span></p>
|
| 28 |
+
<p>Test MSE : <span id="testMSE">--</span></p>
|
| 29 |
+
</div>
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
<!-- RIGHT PANEL -->
|
| 33 |
+
<div class="panel right-panel">
|
| 34 |
+
<h2>🧠 Predictions</h2>
|
| 35 |
+
|
| 36 |
+
<div class="prediction">
|
| 37 |
+
<p>Last Value in Given Dataset: <span id="lastValue">--</span></p>
|
| 38 |
+
<p>Next Prediction: <span id="nextPred">--</span></p>
|
| 39 |
+
</div>
|
| 40 |
+
|
| 41 |
+
<h3>⏳ Future 20 Predictions</h3>
|
| 42 |
+
<pre id="futurePreds">--</pre>
|
| 43 |
+
</div>
|
| 44 |
+
</div>
|
| 45 |
+
|
| 46 |
+
<!-- GRAPH SECTION -->
|
| 47 |
+
<div class="graph-section">
|
| 48 |
+
<h2>📈 Forecast Graph</h2>
|
| 49 |
+
<canvas id="forecastChart"></canvas>
|
| 50 |
+
</div>
|
| 51 |
+
|
| 52 |
+
<script src="{{ url_for('static', filename='script.js') }}"></script>
|
| 53 |
+
</body>
|
| 54 |
+
</html>
|