import gradio as gr import pandas as pd import numpy as np from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.models import load_model import os # Suppress TensorFlow warnings os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' # Step 1: Load the HDF5 model model_path = "lstm_student_performance_model.h5" # Adjust this if your model file name differs try: model = load_model(model_path) print("Model loaded successfully!") except Exception as e: print(f"Error loading model: {e}") raise # Step 2: Define the prediction function def predict_performance(file): try: # Step 2.1: Load the Excel file data = pd.read_excel(file) # Step 2.2: Preprocess the data if "Time" in data.columns: data = data.drop(columns=["Time"]) # Drop 'Time' column if it exists features = data.drop(columns=["Student_ID", "Performance"], errors="ignore") scaler = MinMaxScaler() features_scaled = scaler.fit_transform(features) # Step 2.3: Ensure the shape is correct (time-series data reshaped per student) num_time_steps = 440 # Expected number of time steps if features_scaled.shape[0] != num_time_steps: return "Error: Input data does not have the required time steps." # Reshape the data for prediction X_test = np.expand_dims(features_scaled, axis=0) # Step 2.4: Make predictions predictions = model.predict(X_test) predicted_label = np.argmax(predictions, axis=1)[0] # Step 2.5: Interpret the predictions label_mapping = {0: "Low", 1: "Medium", 2: "High"} interpretation_messages = { 0: "The simulation exercise needs major improvement.", 1: "The simulation exercise is well done but can be improved further.", 2: "The simulation exercise was excellently executed." } performance = label_mapping[predicted_label] interpretation = interpretation_messages[predicted_label] return f"Predicted Performance: {performance}\nInterpretation: {interpretation}" except Exception as e: return f"Error during prediction: {e}" # Step 3: Create the Gradio interface interface = gr.Interface( fn=predict_performance, inputs=gr.File(label="Upload Excel File"), outputs=gr.Textbox(label="Prediction"), title="LSTM Model Performance Prediction", description="Upload an Excel file containing student simulation data to predict performance." ) # Step 4: Launch the app if __name__ == "__main__": interface.launch()