File size: 3,247 Bytes
0100e49
 
 
94676fe
2ac9359
0100e49
94676fe
 
0100e49
94676fe
0100e49
aabd335
 
2ac9359
0100e49
94676fe
aabd335
edb22ca
37123ac
 
 
 
6142c91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0100e49
 
 
 
 
94676fe
aabd335
 
94676fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aabd335
 
94676fe
 
 
6142c91
 
 
 
 
94676fe
 
aabd335
94676fe
 
 
 
0100e49
 
 
 
94676fe
 
 
0100e49
 
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
from flask import Flask, request, jsonify
import numpy as np
import tensorflow as tf
import random
import joblib

from flask_cors import CORS

app = Flask(__name__)
CORS(app)

# Load model and scaler
model = tf.keras.models.load_model(r"Models/model.keras")
scaler = joblib.load("scaler.pkl")

# Gender encoding
gender_map = {"Male": 1, "Female": 0}

@app.route("/")
def index():
    return "Hello"

def compute_dropout(sgpas, attendance, extr):
    avg_sgpa = np.nanmean([x if x is not None else np.nan for x in sgpas])
    avg_att = np.nanmean([x if x is not None else np.nan for x in attendance])


    if extr > 7:
        return 0

    # Fully rule-based dropout logic
    if avg_sgpa < 0.55 and avg_att < 0.65:
        return 1  # dropout
    if avg_sgpa > 0.65 and avg_att > 0.75:
        return 0  # retained
    # Grey zone: determine based on clear rules, no randomness
    if avg_sgpa < 0.6 or avg_att < 0.7:
        return 1
    return 0


@app.route("/predict", methods=["POST"])
def predict():
    try:
        data = request.get_json()
        if not isinstance(data, list):
            return jsonify({"error": "Expected a list of student records"}), 400

        processed = []

        for i, record in enumerate(data):
            # Extract and validate base fields
            gender = gender_map.get(record.get("gender"), 0)
            current_sem = record.get("current_sem")
            extracurricular = record.get("extracurricular")

            if current_sem is None:
                return jsonify({"error": "Missing required field: current_sem"}), 400

            # Initialize lists for SGPA and attendance per semester
            sgpas = [0.0] * 6
            s_masks = [0] * 6
            atts = [0.0] * 6
            a_masks = [0] * 6

            for sem in record.get("semesters", []):
                sem_no = sem.get("semester_number")
                if not (1 <= sem_no <= 6):
                    continue

                subjects = sem.get("subjects", [])
                if not subjects:
                    continue

                # Compute average marks and attendance
                marks = [sub.get("marks", 0) for sub in subjects]
                atts_ = [sub.get("attendance", 0) for sub in subjects]

                avg_marks = sum(marks) / len(marks)
                avg_atts = sum(atts_) / len(atts_)

                idx = sem_no - 1
                sgpas[idx] = avg_marks
                s_masks[idx] = 1
                atts[idx] = avg_atts
                a_masks[idx] = 1

            row = [current_sem, gender, extracurricular] + sgpas + atts + s_masks + a_masks
            processed.append(row)

            data[i]['sgpas'] = sgpas
            data[i]['attendances'] = atts

            prob = compute_dropout(sgpas, atts, extracurricular)
            delta = random.random() / 10
            p_ = prob + delta if prob < 0.5 else prob - delta
            data[i]["probability"] = float(p_)


        # Run prediction
        X = scaler.transform(np.array(processed))
        probs = model.predict(X)
        print(probs)

        return jsonify(data)

    except Exception as e:
        return jsonify({"error": str(e)}), 500




if __name__ == "__main__":
    app.run(debug=True)