-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (57 loc) · 1.9 KB
/
app.py
File metadata and controls
73 lines (57 loc) · 1.9 KB
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
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
import pickle
import io
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
app = Flask(__name__)
CORS(app)
# ---------------- LOAD TRAINED ML MODEL ----------------
model = pickle.load(open("model.pkl", "rb"))
# ---------------- HOME ROUTE ----------------
@app.route("/")
def home():
return "Student Performance Prediction Backend Running"
# ---------------- ML PREDICTION ROUTE ----------------
@app.route("/predict", methods=["POST"])
def predict():
data = request.get_json()
attendance = int(data["attendance"])
internal = int(data["internal"])
assignment = int(data["assignment"])
# ML prediction (probability of PASS)
probability = model.predict_proba(
[[attendance, internal, assignment]]
)[0][1]
return jsonify({
"attendance": attendance,
"internal": internal,
"assignment": assignment,
"probability": int(probability * 100)
})
# ---------------- PDF GENERATION ROUTE ----------------
@app.route("/download-pdf", methods=["POST"])
def download_pdf():
data = request.get_json()
buffer = io.BytesIO()
pdf = canvas.Canvas(buffer, pagesize=A4)
width, height = A4
pdf.setFont("Helvetica-Bold", 18)
pdf.drawCentredString(width / 2, height - 60, "Student Performance Report")
pdf.setFont("Helvetica", 12)
y = height - 120
for key, value in data.items():
pdf.drawString(80, y, f"{key}: {value}")
y -= 28
pdf.showPage()
pdf.save()
buffer.seek(0)
return send_file(
buffer,
as_attachment=True,
download_name="Student_Performance_Report.pdf",
mimetype="application/pdf"
)
# ---------------- RUN SERVER ----------------
if __name__ == "__main__":
app.run(debug=True)