anshharora commited on
Commit
483af95
·
verified ·
1 Parent(s): 8e80310

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -79
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
- model = load_model('final_marks_predictor_model.h5')
25
- with open('scaler.pkl', 'rb') as f:
26
- scaler = pickle.load(f)
27
- except Exception as e:
28
- print(f"Error loading model or scaler: {e}")
29
-
30
- # Load model and scaler when the application starts
31
- load_model_and_scaler()
32
-
33
- def predict_new_input(age, year1_marks, year2_marks, studytime, failures):
34
- try:
35
- feature_names = ['age', 'year1_marks', 'year2_marks', 'studytime', 'failures']
36
- new_input_df = pd.DataFrame([[age, year1_marks, year2_marks, studytime, failures]], columns=feature_names)
37
-
38
- if model is None or scaler is None:
39
- return "Model or scaler is not loaded."
40
-
41
- new_input_scaled = scaler.transform(new_input_df)
42
- predicted_marks = model.predict(new_input_scaled, verbose=0)
43
- return round(float(predicted_marks[0][0]), 2)
44
- except Exception as e:
45
- print(f"Error during prediction: {e}")
46
- return "Error during prediction"
47
-
48
- # Define Gradio Interface
49
- def gradio_predict(age, year1_marks, year2_marks, studytime, failures):
50
- return predict_new_input(age, year1_marks, year2_marks, studytime, failures)
51
-
52
- if __name__ == '__main__':
53
- # Create the Gradio interface
54
- 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:
55
- with gr.Column():
56
- gr.Markdown("## Student Performance Prediction")
57
- gr.Markdown("Please fill in all the required fields and click 'Predict' to see your final predicted marks.")
58
-
59
- # Create form fields
60
- with gr.Row():
61
- age = gr.Number(label="Age", interactive=True, elem_id="age-input")
62
- with gr.Row():
63
- year1_marks = gr.Number(label="First Year Marks", interactive=True, elem_id="year1-input")
64
- with gr.Row():
65
- year2_marks = gr.Number(label="Second Year Marks", interactive=True, elem_id="year2-input")
66
- with gr.Row():
67
- studytime = gr.Number(label="Study Time (hours/week)", interactive=True, elem_id="studytime-input")
68
- with gr.Row():
69
- failures = gr.Number(label="Number of Failures", interactive=True, elem_id="failures-input")
70
-
71
- submit_button = gr.Button("Predict", elem_id="predict-button", variant="primary")
72
-
73
- # Create output display
74
- output = gr.Textbox(label="Predicted Final Marks", interactive=False, elem_id="output-box")
75
-
76
- # Button action
77
- submit_button.click(gradio_predict, inputs=[age, year1_marks, year2_marks, studytime, failures], outputs=output)
78
-
79
- interface.launch(share=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+