File size: 3,299 Bytes
483af95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe3aabc
483af95
fe3aabc
483af95
fe3aabc
483af95
fe3aabc
483af95
fe3aabc
483af95
 
 
 
 
 
 
 
 
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
import os
import warnings
import numpy as np
import pandas as pd
from tensorflow.keras.models import load_model
import pickle
from dotenv import load_dotenv
import gradio as gr

# Suppress TensorFlow and other warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
warnings.filterwarnings("ignore")

# Load environment variables
load_dotenv()

# Load the trained model and scaler
model = None
scaler = None

def load_model_and_scaler():
    global model, scaler
    try:
        model_path = 'final_marks_predictor_model.h5'
        scaler_path = 'scaler.pkl'
        
        if not os.path.exists(model_path):
            print(f"Model file not found: {model_path}")
            return
        
        if not os.path.exists(scaler_path):
            print(f"Scaler file not found: {scaler_path}")
            return
        
        model = load_model(model_path)
        with open(scaler_path, 'rb') as f:
            scaler = pickle.load(f)
        
        print("Model and scaler loaded successfully")
    except Exception as e:
        print(f"Error loading model or scaler: {e}")

# Load model and scaler when the application starts
load_model_and_scaler()

def predict_new_input(age, year1_marks, year2_marks, studytime, failures):
    try:
        feature_names = ['age', 'year1_marks', 'year2_marks', 'studytime', 'failures']
        new_input_df = pd.DataFrame([[age, year1_marks, year2_marks, studytime, failures]], columns=feature_names)

        if model is None or scaler is None:
            return "Model or scaler is not loaded."
        
        new_input_scaled = scaler.transform(new_input_df)
        predicted_marks = model.predict(new_input_scaled, verbose=0)
        return round(float(predicted_marks[0][0]), 2)
    except Exception as e:
        print(f"Error during prediction: {e}")
        return "Error during prediction"

# Define Gradio Interface
def gradio_predict(age, year1_marks, year2_marks, studytime, failures):
    return predict_new_input(age, year1_marks, year2_marks, studytime, failures)

if __name__ == '__main__':
    # Create the Gradio interface
    with gr.Blocks(theme=gr.themes.Soft()) as interface:
        with gr.Column():
            gr.Markdown("## Student Performance Prediction")
            gr.Markdown("Please fill in all the required fields and click 'Predict' to see your final predicted marks.")
            
            # Create form fields
            with gr.Row():
                age = gr.Number(label="Age", interactive=True)
            with gr.Row():
                year1_marks = gr.Number(label="First Year Marks", interactive=True)
            with gr.Row():
                year2_marks = gr.Number(label="Second Year Marks", interactive=True)
            with gr.Row():
                studytime = gr.Number(label="Study Time (hours/week)", interactive=True)
            with gr.Row():
                failures = gr.Number(label="Number of Failures", interactive=True)
            
            submit_button = gr.Button("Predict", variant="primary")
            output = gr.Textbox(label="Predicted Final Marks", interactive=False)
        
        # Button action
        submit_button.click(gradio_predict, inputs=[age, year1_marks, year2_marks, studytime, failures], outputs=output)

        interface.launch(share=False)