students / predict.py
d-e-e-k-11's picture
Upload folder using huggingface_hub
5575a8a verified
import tensorflow as tf
import numpy as np
import joblib
import os
def predict_marks(num_courses, time_study):
# Load model and scalers
if not (os.path.exists('student_marks_rnn_model.h5') and
os.path.exists('scaler_X.pkl') and
os.path.exists('scaler_y.pkl')):
return "Error: Model or scalers not found. Please run train_rnn.py first."
model = tf.keras.models.load_model('student_marks_rnn_model.h5')
scaler_X = joblib.load('scaler_X.pkl')
scaler_y = joblib.load('scaler_y.pkl')
# Preprocess input
input_data = np.array([[num_courses, time_study]])
input_scaled = scaler_X.transform(input_data)
# Reshape for RNN (Samples, TimeSteps, Features)
input_reshaped = input_scaled.reshape((1, 1, 2))
# Predict
prediction_scaled = model.predict(input_reshaped)
prediction = scaler_y.inverse_transform(prediction_scaled)
return prediction[0][0]
if __name__ == "__main__":
print("--- Student Marks Prediction RNN ---")
try:
nc = float(input("Enter number of courses: "))
ts = float(input("Enter time spent studying (hours): "))
result = predict_marks(nc, ts)
if isinstance(result, str):
print(result)
else:
print(f"\nPredicted Marks: {result:.2f}")
except ValueError:
print("Invalid input. Please enter numeric values.")
except Exception as e:
print(f"Error: {e}")