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)