File size: 3,363 Bytes
b503408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = load_model('final_marks_predictor_model.h5')
        with open('scaler.pkl', 'rb') as f:
            scaler = pickle.load(f)
    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(), css=".gradio-container {max-width: 600px; margin: auto; padding: 20px; border: 1px solid #e0e0e0; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);}") 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, elem_id="age-input")
            with gr.Row():
                year1_marks = gr.Number(label="First Year Marks", interactive=True,  elem_id="year1-input")
            with gr.Row():
                year2_marks = gr.Number(label="Second Year Marks", interactive=True,  elem_id="year2-input")
            with gr.Row():
                studytime = gr.Number(label="Study Time (hours/week)", interactive=True, elem_id="studytime-input")
            with gr.Row():
                failures = gr.Number(label="Number of Failures", interactive=True,  elem_id="failures-input")
            
            submit_button = gr.Button("Predict", elem_id="predict-button", variant="primary")
            
            # Create output display
            output = gr.Textbox(label="Predicted Final Marks", interactive=False, elem_id="output-box")
        
        # Button action
        submit_button.click(gradio_predict, inputs=[age, year1_marks, year2_marks, studytime, failures], outputs=output)

        interface.launch(share=False)