TejAndrewsACC commited on
Commit
bc41b18
·
verified ·
1 Parent(s): 6afa44a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -56
app.py CHANGED
@@ -1,13 +1,7 @@
1
  import numpy as np
2
  from datascience import *
3
- import matplotlib.pyplot as plt
4
  import gradio as gr
5
 
6
- plt.style.use('fivethirtyeight')
7
-
8
- # ----------------------------
9
- # Load and prepare data
10
- # ----------------------------
11
  SPD = Table.read_table('Student_Performance_Data.csv')
12
 
13
  def study_effectiveness(study_time, absences, parental_support, tutoring):
@@ -32,13 +26,10 @@ def pass_or_fail(grade):
32
  passes = SPD.apply(pass_or_fail, 'GradeClass')
33
  SPD = SPD.with_column('Pass', passes)
34
 
35
- # ----------------------------
36
- # Build feature matrix
37
- # ----------------------------
38
  X = []
39
 
40
  for i in np.arange(SPD.num_rows):
41
- row = [
42
  SPD.column('StudyTimeWeekly').item(i) / 20,
43
  SPD.column('Absences').item(i) / 30,
44
  SPD.column('Tutoring').item(i),
@@ -48,15 +39,11 @@ for i in np.arange(SPD.num_rows):
48
  SPD.column('Music').item(i),
49
  SPD.column('Volunteering').item(i),
50
  SPD.column('StudyEffectiveness').item(i) / 40
51
- ]
52
- X.append(row)
53
 
54
  X = np.array(X)
55
  y = np.array(SPD.column('Pass'))
56
 
57
- # ----------------------------
58
- # Neural network setup
59
- # ----------------------------
60
  np.random.seed(1)
61
  W1 = np.random.normal(0, 1, (9, 16))
62
  W2 = np.random.normal(0, 1, (16, 1))
@@ -64,36 +51,22 @@ W2 = np.random.normal(0, 1, (16, 1))
64
  def sigmoid(x):
65
  return 1 / (1 + np.exp(-x))
66
 
67
- # ----------------------------
68
- # Train model
69
- # ----------------------------
70
  for i in range(20000):
71
  hidden = sigmoid(np.dot(X, W1))
72
  output = sigmoid(np.dot(hidden, W2))
73
-
74
  error = y.reshape(len(y), 1) - output
75
-
76
  output_change = error * output * (1 - output)
77
  hidden_error = np.dot(output_change, W2.T)
78
  hidden_change = hidden_error * hidden * (1 - hidden)
79
-
80
  W2 += 0.001 * np.dot(hidden.T, output_change)
81
  W1 += 0.001 * np.dot(X.T, hidden_change)
82
 
83
- # Training accuracy
84
  hidden = sigmoid(np.dot(X, W1))
85
  output = sigmoid(np.dot(hidden, W2))
86
  predictions = output > 0.5
87
- accuracynn = np.mean(predictions.flatten() == y)
88
-
89
- print("Training accuracy:", accuracynn)
90
-
91
- # ----------------------------
92
- # Prediction function for UI
93
- # ----------------------------
94
- def predict(student_time, absences, tutoring, parental_support,
95
- extracurricular, sports, music, volunteering):
96
 
 
97
  study_effect = (student_time * 1.5) + (parental_support * 2) + (tutoring * 3) - (absences * 0.7)
98
 
99
  x = np.array([
@@ -114,29 +87,38 @@ def predict(student_time, absences, tutoring, parental_support,
114
  result = "PASS" if output[0] > 0.5 else "FAIL"
115
  confidence = float(output[0])
116
 
117
- return result, confidence
118
-
119
- # ----------------------------
120
- # Gradio Interface
121
- # ----------------------------
122
- interface = gr.Interface(
123
- fn=predict,
124
- inputs=[
125
- gr.Number(label="Study Time Weekly"),
126
- gr.Number(label="Absences"),
127
- gr.Number(label="Tutoring (0 or 1)"),
128
- gr.Number(label="Parental Support (0–4)"),
129
- gr.Number(label="Extracurricular (0 or 1)"),
130
- gr.Number(label="Sports (0 or 1)"),
131
- gr.Number(label="Music (0 or 1)"),
132
- gr.Number(label="Volunteering (0 or 1)")
133
- ],
134
- outputs=[
135
- gr.Textbox(label="Prediction"),
136
- gr.Number(label="Confidence")
137
- ],
138
- title="Student Performance Predictor",
139
- description="Predict whether a student will pass or fail based on academic and activity inputs."
140
- )
 
 
 
 
 
 
 
 
 
141
 
142
- interface.launch()
 
1
  import numpy as np
2
  from datascience import *
 
3
  import gradio as gr
4
 
 
 
 
 
 
5
  SPD = Table.read_table('Student_Performance_Data.csv')
6
 
7
  def study_effectiveness(study_time, absences, parental_support, tutoring):
 
26
  passes = SPD.apply(pass_or_fail, 'GradeClass')
27
  SPD = SPD.with_column('Pass', passes)
28
 
 
 
 
29
  X = []
30
 
31
  for i in np.arange(SPD.num_rows):
32
+ X.append([
33
  SPD.column('StudyTimeWeekly').item(i) / 20,
34
  SPD.column('Absences').item(i) / 30,
35
  SPD.column('Tutoring').item(i),
 
39
  SPD.column('Music').item(i),
40
  SPD.column('Volunteering').item(i),
41
  SPD.column('StudyEffectiveness').item(i) / 40
42
+ ])
 
43
 
44
  X = np.array(X)
45
  y = np.array(SPD.column('Pass'))
46
 
 
 
 
47
  np.random.seed(1)
48
  W1 = np.random.normal(0, 1, (9, 16))
49
  W2 = np.random.normal(0, 1, (16, 1))
 
51
  def sigmoid(x):
52
  return 1 / (1 + np.exp(-x))
53
 
 
 
 
54
  for i in range(20000):
55
  hidden = sigmoid(np.dot(X, W1))
56
  output = sigmoid(np.dot(hidden, W2))
 
57
  error = y.reshape(len(y), 1) - output
 
58
  output_change = error * output * (1 - output)
59
  hidden_error = np.dot(output_change, W2.T)
60
  hidden_change = hidden_error * hidden * (1 - hidden)
 
61
  W2 += 0.001 * np.dot(hidden.T, output_change)
62
  W1 += 0.001 * np.dot(X.T, hidden_change)
63
 
 
64
  hidden = sigmoid(np.dot(X, W1))
65
  output = sigmoid(np.dot(hidden, W2))
66
  predictions = output > 0.5
67
+ accuracy_nn = np.mean(predictions.flatten() == y)
 
 
 
 
 
 
 
 
68
 
69
+ def predict(student_time, absences, tutoring, parental_support, extracurricular, sports, music, volunteering):
70
  study_effect = (student_time * 1.5) + (parental_support * 2) + (tutoring * 3) - (absences * 0.7)
71
 
72
  x = np.array([
 
87
  result = "PASS" if output[0] > 0.5 else "FAIL"
88
  confidence = float(output[0])
89
 
90
+ return result, round(confidence, 4)
91
+
92
+ theme = gr.themes.Soft()
93
+
94
+ with gr.Blocks(theme=theme) as app:
95
+ gr.Markdown("# Student Performance Predictor")
96
+ gr.Markdown("Predict whether a student will pass or fail based on academic behavior and activities.")
97
+
98
+ with gr.Row():
99
+ with gr.Column():
100
+ student_time = gr.Slider(0, 20, value=10, label="Study Time Weekly")
101
+ absences = gr.Slider(0, 30, value=5, label="Absences")
102
+ parental_support = gr.Slider(0, 4, value=2, step=1, label="Parental Support")
103
+
104
+ tutoring = gr.Checkbox(label="Tutoring")
105
+ extracurricular = gr.Checkbox(label="Extracurricular Activities")
106
+ sports = gr.Checkbox(label="Sports")
107
+ music = gr.Checkbox(label="Music")
108
+ volunteering = gr.Checkbox(label="Volunteering")
109
+
110
+ btn = gr.Button("Predict", variant="primary")
111
+
112
+ with gr.Column():
113
+ result = gr.Textbox(label="Prediction")
114
+ confidence = gr.Number(label="Confidence Score")
115
+
116
+ btn.click(
117
+ predict,
118
+ inputs=[student_time, absences, tutoring, parental_support, extracurricular, sports, music, volunteering],
119
+ outputs=[result, confidence]
120
+ )
121
+
122
+ gr.Markdown(f"Model training accuracy: {accuracy_nn:.3f}")
123
 
124
+ app.launch()