Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,91 @@
|
|
1 |
-
import os
|
2 |
-
import warnings
|
3 |
-
import numpy as np
|
4 |
-
import pandas as pd
|
5 |
-
from tensorflow.keras.models import load_model
|
6 |
-
import pickle
|
7 |
-
from dotenv import load_dotenv
|
8 |
-
import gradio as gr
|
9 |
-
|
10 |
-
# Suppress TensorFlow and other warnings
|
11 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
12 |
-
warnings.filterwarnings("ignore")
|
13 |
-
|
14 |
-
# Load environment variables
|
15 |
-
load_dotenv()
|
16 |
-
|
17 |
-
# Load the trained model and scaler
|
18 |
-
model = None
|
19 |
-
scaler = None
|
20 |
-
|
21 |
-
def load_model_and_scaler():
|
22 |
-
global model, scaler
|
23 |
-
try:
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import warnings
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
from tensorflow.keras.models import load_model
|
6 |
+
import pickle
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
# Suppress TensorFlow and other warnings
|
11 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
12 |
+
warnings.filterwarnings("ignore")
|
13 |
+
|
14 |
+
# Load environment variables
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
# Load the trained model and scaler
|
18 |
+
model = None
|
19 |
+
scaler = None
|
20 |
+
|
21 |
+
def load_model_and_scaler():
|
22 |
+
global model, scaler
|
23 |
+
try:
|
24 |
+
model_path = 'final_marks_predictor_model.h5'
|
25 |
+
scaler_path = 'scaler.pkl'
|
26 |
+
|
27 |
+
if not os.path.exists(model_path):
|
28 |
+
print(f"Model file not found: {model_path}")
|
29 |
+
return
|
30 |
+
|
31 |
+
if not os.path.exists(scaler_path):
|
32 |
+
print(f"Scaler file not found: {scaler_path}")
|
33 |
+
return
|
34 |
+
|
35 |
+
model = load_model(model_path)
|
36 |
+
with open(scaler_path, 'rb') as f:
|
37 |
+
scaler = pickle.load(f)
|
38 |
+
|
39 |
+
print("Model and scaler loaded successfully")
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Error loading model or scaler: {e}")
|
42 |
+
|
43 |
+
# Load model and scaler when the application starts
|
44 |
+
load_model_and_scaler()
|
45 |
+
|
46 |
+
def predict_new_input(age, year1_marks, year2_marks, studytime, failures):
|
47 |
+
try:
|
48 |
+
feature_names = ['age', 'year1_marks', 'year2_marks', 'studytime', 'failures']
|
49 |
+
new_input_df = pd.DataFrame([[age, year1_marks, year2_marks, studytime, failures]], columns=feature_names)
|
50 |
+
|
51 |
+
if model is None or scaler is None:
|
52 |
+
return "Model or scaler is not loaded."
|
53 |
+
|
54 |
+
new_input_scaled = scaler.transform(new_input_df)
|
55 |
+
predicted_marks = model.predict(new_input_scaled, verbose=0)
|
56 |
+
return round(float(predicted_marks[0][0]), 2)
|
57 |
+
except Exception as e:
|
58 |
+
print(f"Error during prediction: {e}")
|
59 |
+
return "Error during prediction"
|
60 |
+
|
61 |
+
# Define Gradio Interface
|
62 |
+
def gradio_predict(age, year1_marks, year2_marks, studytime, failures):
|
63 |
+
return predict_new_input(age, year1_marks, year2_marks, studytime, failures)
|
64 |
+
|
65 |
+
if __name__ == '__main__':
|
66 |
+
# Create the Gradio interface
|
67 |
+
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
68 |
+
with gr.Column():
|
69 |
+
gr.Markdown("## Student Performance Prediction")
|
70 |
+
gr.Markdown("Please fill in all the required fields and click 'Predict' to see your final predicted marks.")
|
71 |
+
|
72 |
+
# Create form fields
|
73 |
+
with gr.Row():
|
74 |
+
age = gr.Number(label="Age", interactive=True, placeholder="Enter Age")
|
75 |
+
with gr.Row():
|
76 |
+
year1_marks = gr.Number(label="First Year Marks", interactive=True, placeholder="Enter Year 1 Marks")
|
77 |
+
with gr.Row():
|
78 |
+
year2_marks = gr.Number(label="Second Year Marks", interactive=True, placeholder="Enter Year 2 Marks")
|
79 |
+
with gr.Row():
|
80 |
+
studytime = gr.Number(label="Study Time (hours/week)", interactive=True, placeholder="Enter Study Time")
|
81 |
+
with gr.Row():
|
82 |
+
failures = gr.Number(label="Number of Failures", interactive=True, placeholder="Enter Number of Failures")
|
83 |
+
|
84 |
+
submit_button = gr.Button("Predict", variant="primary")
|
85 |
+
output = gr.Textbox(label="Predicted Final Marks", interactive=False)
|
86 |
+
|
87 |
+
# Button action
|
88 |
+
submit_button.click(gradio_predict, inputs=[age, year1_marks, year2_marks, studytime, failures], outputs=output)
|
89 |
+
|
90 |
+
interface.launch(share=False)
|
91 |
+
|