|
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 |
|
|
|
|
|
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' |
|
warnings.filterwarnings("ignore") |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
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() |
|
|
|
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" |
|
|
|
|
|
def gradio_predict(age, year1_marks, year2_marks, studytime, failures): |
|
return predict_new_input(age, year1_marks, year2_marks, studytime, failures) |
|
|
|
if __name__ == '__main__': |
|
|
|
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.") |
|
|
|
|
|
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) |
|
|
|
|
|
submit_button.click(gradio_predict, inputs=[age, year1_marks, year2_marks, studytime, failures], outputs=output) |
|
|
|
interface.launch(share=False) |
|
|
|
|