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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import pickle
|
5 |
+
import json
|
6 |
+
import tensorflow as tf
|
7 |
+
from tensorflow.keras.models import load_model
|
8 |
+
import plotly.graph_objects as go
|
9 |
+
import plotly.express as px
|
10 |
+
from plotly.subplots import make_subplots
|
11 |
+
import os
|
12 |
+
|
13 |
+
# Load model artifacts
|
14 |
+
@st.cache_resource
|
15 |
+
def load_model_artifacts():
|
16 |
+
try:
|
17 |
+
# Load the trained model
|
18 |
+
model = load_model('final_model.h5')
|
19 |
+
|
20 |
+
# Load the scaler
|
21 |
+
with open('scaler.pkl', 'rb') as f:
|
22 |
+
scaler = pickle.load(f)
|
23 |
+
|
24 |
+
# Load metadata
|
25 |
+
with open('metadata.json', 'r') as f:
|
26 |
+
metadata = json.load(f)
|
27 |
+
|
28 |
+
return model, scaler, metadata
|
29 |
+
except Exception as e:
|
30 |
+
raise Exception(f"Error loading model artifacts: {str(e)}")
|
31 |
+
|
32 |
+
# Initialize model components
|
33 |
+
model, scaler, metadata = load_model_artifacts()
|
34 |
+
feature_names = metadata['feature_names']
|
35 |
+
|
36 |
+
def predict_student_eligibility(*args):
|
37 |
+
"""
|
38 |
+
Predict student eligibility based on input features
|
39 |
+
"""
|
40 |
+
try:
|
41 |
+
# Create input dictionary from gradio inputs
|
42 |
+
input_data = {feature_names[i]: args[i] for i in range(len(feature_names))}
|
43 |
+
|
44 |
+
# Convert to DataFrame
|
45 |
+
input_df = pd.DataFrame([input_data])
|
46 |
+
|
47 |
+
# Scale the input
|
48 |
+
input_scaled = scaler.transform(input_df)
|
49 |
+
|
50 |
+
# Reshape for CNN
|
51 |
+
input_reshaped = input_scaled.reshape(input_scaled.shape[0], input_scaled.shape[1], 1)
|
52 |
+
|
53 |
+
# Make prediction
|
54 |
+
probability = model.predict(input_reshaped)[0][0]
|
55 |
+
prediction = "Eligible" if probability > 0.5 else "Not Eligible"
|
56 |
+
confidence = abs(probability - 0.5) * 2 # Convert to confidence score
|
57 |
+
|
58 |
+
# Create prediction visualization
|
59 |
+
fig = create_prediction_viz(probability, prediction, input_data)
|
60 |
+
|
61 |
+
return prediction, f"{probability:.4f}", f"{confidence:.4f}", fig
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
return f"Error: {str(e)}", "N/A", "N/A", None
|
65 |
+
|
66 |
+
def create_prediction_viz(probability, prediction, input_data):
|
67 |
+
"""
|
68 |
+
Create visualization for prediction results
|
69 |
+
"""
|
70 |
+
# Create subplots
|
71 |
+
fig = make_subplots(
|
72 |
+
rows=2, cols=2,
|
73 |
+
subplot_titles=('Prediction Probability', 'Confidence Meter', 'Input Features', 'Feature Distribution'),
|
74 |
+
specs=[[{"type": "indicator"}, {"type": "indicator"}],
|
75 |
+
[{"type": "bar"}, {"type": "histogram"}]]
|
76 |
+
)
|
77 |
+
|
78 |
+
# Prediction probability gauge
|
79 |
+
fig.add_trace(
|
80 |
+
go.Indicator(
|
81 |
+
mode="gauge+number+delta",
|
82 |
+
value=probability,
|
83 |
+
domain={'x': [0, 1], 'y': [0, 1]},
|
84 |
+
title={'text': "Eligibility Probability"},
|
85 |
+
gauge={
|
86 |
+
'axis': {'range': [None, 1]},
|
87 |
+
'bar': {'color': "darkblue"},
|
88 |
+
'steps': [
|
89 |
+
{'range': [0, 0.5], 'color': "lightgray"},
|
90 |
+
{'range': [0.5, 1], 'color': "lightgreen"}
|
91 |
+
],
|
92 |
+
'threshold': {
|
93 |
+
'line': {'color': "red", 'width': 4},
|
94 |
+
'thickness': 0.75,
|
95 |
+
'value': 0.5
|
96 |
+
}
|
97 |
+
}
|
98 |
+
),
|
99 |
+
row=1, col=1
|
100 |
+
)
|
101 |
+
|
102 |
+
# Confidence meter
|
103 |
+
confidence = abs(probability - 0.5) * 2
|
104 |
+
fig.add_trace(
|
105 |
+
go.Indicator(
|
106 |
+
mode="gauge+number",
|
107 |
+
value=confidence,
|
108 |
+
domain={'x': [0, 1], 'y': [0, 1]},
|
109 |
+
title={'text': "Prediction Confidence"},
|
110 |
+
gauge={
|
111 |
+
'axis': {'range': [None, 1]},
|
112 |
+
'bar': {'color': "orange"},
|
113 |
+
'steps': [
|
114 |
+
{'range': [0, 0.3], 'color': "lightcoral"},
|
115 |
+
{'range': [0.3, 0.7], 'color': "lightyellow"},
|
116 |
+
{'range': [0.7, 1], 'color': "lightgreen"}
|
117 |
+
]
|
118 |
+
}
|
119 |
+
),
|
120 |
+
row=1, col=2
|
121 |
+
)
|
122 |
+
|
123 |
+
# Input features bar chart
|
124 |
+
features = list(input_data.keys())
|
125 |
+
values = list(input_data.values())
|
126 |
+
|
127 |
+
fig.add_trace(
|
128 |
+
go.Bar(x=features, y=values, name="Input Values", marker_color="skyblue"),
|
129 |
+
row=2, col=1
|
130 |
+
)
|
131 |
+
|
132 |
+
# Feature distribution (example data)
|
133 |
+
fig.add_trace(
|
134 |
+
go.Histogram(x=values, nbinsx=10, name="Distribution", marker_color="lightcoral"),
|
135 |
+
row=2, col=2
|
136 |
+
)
|
137 |
+
|
138 |
+
fig.update_layout(
|
139 |
+
height=800,
|
140 |
+
showlegend=False,
|
141 |
+
title_text="Student Eligibility Prediction Dashboard",
|
142 |
+
title_x=0.5
|
143 |
+
)
|
144 |
+
|
145 |
+
return fig
|
146 |
+
|
147 |
+
def create_model_info():
|
148 |
+
"""
|
149 |
+
Create model information display
|
150 |
+
"""
|
151 |
+
info_html = f"""
|
152 |
+
<div style="padding: 20px; background-color: #f0f2f6; border-radius: 10px; margin: 10px 0;">
|
153 |
+
<h3>🤖 Model Information</h3>
|
154 |
+
<ul>
|
155 |
+
<li><strong>Model Type:</strong> {metadata.get('model_type', 'CNN')}</li>
|
156 |
+
<li><strong>Test Accuracy:</strong> {metadata['performance_metrics']['test_accuracy']:.4f}</li>
|
157 |
+
<li><strong>AUC Score:</strong> {metadata['performance_metrics']['auc_score']:.4f}</li>
|
158 |
+
<li><strong>Creation Date:</strong> {metadata.get('creation_date', 'N/A')}</li>
|
159 |
+
<li><strong>Features:</strong> {len(feature_names)} input features</li>
|
160 |
+
</ul>
|
161 |
+
</div>
|
162 |
+
"""
|
163 |
+
return info_html
|
164 |
+
|
165 |
+
def batch_predict(file):
|
166 |
+
"""
|
167 |
+
Batch prediction from uploaded CSV file
|
168 |
+
"""
|
169 |
+
try:
|
170 |
+
# Read the uploaded file
|
171 |
+
df = pd.read_csv(file.name)
|
172 |
+
|
173 |
+
# Check if all required features are present
|
174 |
+
missing_features = set(feature_names) - set(df.columns)
|
175 |
+
if missing_features:
|
176 |
+
return f"Missing features: {missing_features}", None
|
177 |
+
|
178 |
+
# Select only the required features
|
179 |
+
df_features = df[feature_names]
|
180 |
+
|
181 |
+
# Scale the features
|
182 |
+
df_scaled = scaler.transform(df_features)
|
183 |
+
|
184 |
+
# Reshape for CNN
|
185 |
+
df_reshaped = df_scaled.reshape(df_scaled.shape[0], df_scaled.shape[1], 1)
|
186 |
+
|
187 |
+
# Make predictions
|
188 |
+
probabilities = model.predict(df_reshaped).flatten()
|
189 |
+
predictions = ["Eligible" if p > 0.5 else "Not Eligible" for p in probabilities]
|
190 |
+
|
191 |
+
# Create results dataframe
|
192 |
+
results_df = df_features.copy()
|
193 |
+
results_df['Probability'] = probabilities
|
194 |
+
results_df['Prediction'] = predictions
|
195 |
+
results_df['Confidence'] = np.abs(probabilities - 0.5) * 2
|
196 |
+
|
197 |
+
# Save results
|
198 |
+
output_file = "batch_predictions.csv"
|
199 |
+
results_df.to_csv(output_file, index=False)
|
200 |
+
|
201 |
+
# Create summary statistics
|
202 |
+
summary = f"""
|
203 |
+
Batch Prediction Summary:
|
204 |
+
- Total predictions: {len(results_df)}
|
205 |
+
- Eligible: {sum(1 for p in predictions if p == 'Eligible')}
|
206 |
+
- Not Eligible: {sum(1 for p in predictions if p == 'Not Eligible')}
|
207 |
+
- Average Probability: {np.mean(probabilities):.4f}
|
208 |
+
- Average Confidence: {np.mean(np.abs(probabilities - 0.5) * 2):.4f}
|
209 |
+
"""
|
210 |
+
|
211 |
+
return summary, output_file
|
212 |
+
|
213 |
+
except Exception as e:
|
214 |
+
return f"Error processing file: {str(e)}", None
|
215 |
+
|
216 |
+
# Create Gradio interface
|
217 |
+
with gr.Blocks(
|
218 |
+
theme=gr.themes.Soft(),
|
219 |
+
title="Student Eligibility Prediction",
|
220 |
+
css="""
|
221 |
+
.gradio-container {
|
222 |
+
max-width: 1200px !important;
|
223 |
+
}
|
224 |
+
.main-header {
|
225 |
+
text-align: center;
|
226 |
+
padding: 20px;
|
227 |
+
background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
|
228 |
+
color: white;
|
229 |
+
border-radius: 10px;
|
230 |
+
margin-bottom: 20px;
|
231 |
+
}
|
232 |
+
"""
|
233 |
+
) as demo:
|
234 |
+
|
235 |
+
# Header
|
236 |
+
gr.HTML("""
|
237 |
+
<div class="main-header">
|
238 |
+
<h1>🎓 Student Eligibility Prediction System</h1>
|
239 |
+
<p>AI-powered CNN model for predicting student eligibility with advanced analytics</p>
|
240 |
+
</div>
|
241 |
+
""")
|
242 |
+
|
243 |
+
with gr.Tabs():
|
244 |
+
# Single Prediction Tab
|
245 |
+
with gr.TabItem("Single Prediction"):
|
246 |
+
gr.Markdown("### Enter student information to predict eligibility")
|
247 |
+
|
248 |
+
with gr.Row():
|
249 |
+
with gr.Column(scale=1):
|
250 |
+
# Create input components dynamically based on features
|
251 |
+
inputs = []
|
252 |
+
for feature in feature_names:
|
253 |
+
inputs.append(
|
254 |
+
gr.Number(
|
255 |
+
label=f"{feature}",
|
256 |
+
value=85, # Default value
|
257 |
+
minimum=0,
|
258 |
+
maximum=100,
|
259 |
+
step=1
|
260 |
+
)
|
261 |
+
)
|
262 |
+
|
263 |
+
predict_btn = gr.Button("🔮 Predict Eligibility", variant="primary", size="lg")
|
264 |
+
|
265 |
+
with gr.Column(scale=2):
|
266 |
+
with gr.Row():
|
267 |
+
prediction_output = gr.Textbox(label="Prediction", scale=1)
|
268 |
+
probability_output = gr.Textbox(label="Probability", scale=1)
|
269 |
+
confidence_output = gr.Textbox(label="Confidence", scale=1)
|
270 |
+
|
271 |
+
prediction_plot = gr.Plot(label="Prediction Visualization")
|
272 |
+
|
273 |
+
# Model information
|
274 |
+
gr.HTML(create_model_info())
|
275 |
+
|
276 |
+
# Batch Prediction Tab
|
277 |
+
with gr.TabItem("Batch Prediction"):
|
278 |
+
gr.Markdown("### Upload a CSV file for batch predictions")
|
279 |
+
gr.Markdown(f"**Required columns:** {', '.join(feature_names)}")
|
280 |
+
|
281 |
+
with gr.Row():
|
282 |
+
with gr.Column():
|
283 |
+
file_input = gr.File(
|
284 |
+
label="Upload CSV File",
|
285 |
+
file_types=[".csv"],
|
286 |
+
type="file"
|
287 |
+
)
|
288 |
+
batch_predict_btn = gr.Button("📊 Process Batch", variant="primary")
|
289 |
+
|
290 |
+
with gr.Column():
|
291 |
+
batch_output = gr.Textbox(label="Batch Results Summary", lines=10)
|
292 |
+
download_file = gr.File(label="Download Results")
|
293 |
+
|
294 |
+
# Model Analytics Tab
|
295 |
+
with gr.TabItem("Model Analytics"):
|
296 |
+
gr.Markdown("### Model Performance Metrics")
|
297 |
+
|
298 |
+
# Performance metrics
|
299 |
+
metrics_df = pd.DataFrame([metadata['performance_metrics']])
|
300 |
+
gr.Dataframe(metrics_df, label="Performance Metrics")
|
301 |
+
|
302 |
+
# Feature importance (placeholder - you'd need to calculate this)
|
303 |
+
gr.Markdown("### Feature Names")
|
304 |
+
gr.Textbox(value=", ".join(feature_names), label="Model Features", lines=3)
|
305 |
+
|
306 |
+
# Event handlers
|
307 |
+
predict_btn.click(
|
308 |
+
fn=predict_student_eligibility,
|
309 |
+
inputs=inputs,
|
310 |
+
outputs=[prediction_output, probability_output, confidence_output, prediction_plot]
|
311 |
+
)
|
312 |
+
|
313 |
+
batch_predict_btn.click(
|
314 |
+
fn=batch_predict,
|
315 |
+
inputs=[file_input],
|
316 |
+
outputs=[batch_output, download_file]
|
317 |
+
)
|
318 |
+
|
319 |
+
# Launch the app
|
320 |
+
if __name__ == "__main__":
|
321 |
+
demo.launch(share=True)
|