Spaces:
Running
Running
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| import pickle | |
| import json | |
| import tensorflow as tf | |
| from tensorflow.keras.models import load_model | |
| import plotly.graph_objects as go | |
| import plotly.express as px | |
| from plotly.subplots import make_subplots | |
| import os | |
| # Load model artifacts | |
| def load_model_artifacts(): | |
| try: | |
| # Load the trained model | |
| model = load_model('final_model.h5') | |
| # Load the scaler | |
| with open('scaler.pkl', 'rb') as f: | |
| scaler = pickle.load(f) | |
| # Load metadata | |
| with open('metadata.json', 'r') as f: | |
| metadata = json.load(f) | |
| return model, scaler, metadata | |
| except Exception as e: | |
| raise Exception(f"Error loading model artifacts: {str(e)}") | |
| # Initialize model components | |
| model, scaler, metadata = load_model_artifacts() | |
| feature_names = metadata['feature_names'] | |
| def predict_student_eligibility(*args): | |
| """ | |
| Predict student eligibility based on input features | |
| """ | |
| try: | |
| # Create input dictionary from gradio inputs | |
| input_data = {feature_names[i]: args[i] for i in range(len(feature_names))} | |
| # Convert to DataFrame | |
| input_df = pd.DataFrame([input_data]) | |
| # Scale the input | |
| input_scaled = scaler.transform(input_df) | |
| # Reshape for CNN | |
| input_reshaped = input_scaled.reshape(input_scaled.shape[0], input_scaled.shape[1], 1) | |
| # Make prediction | |
| probability = model.predict(input_reshaped)[0][0] | |
| prediction = "Eligible" if probability > 0.5 else "Not Eligible" | |
| confidence = abs(probability - 0.5) * 2 # Convert to confidence score | |
| # Create prediction visualization | |
| fig = create_prediction_viz(probability, prediction, input_data) | |
| return prediction, f"{probability:.4f}", f"{confidence:.4f}", fig | |
| except Exception as e: | |
| return f"Error: {str(e)}", "N/A", "N/A", None | |
| def create_prediction_viz(probability, prediction, input_data): | |
| """ | |
| Create visualization for prediction results | |
| """ | |
| # Create subplots | |
| fig = make_subplots( | |
| rows=2, cols=2, | |
| subplot_titles=('Prediction Probability', 'Confidence Meter', 'Input Features', 'Feature Distribution'), | |
| specs=[[{"type": "indicator"}, {"type": "indicator"}], | |
| [{"type": "bar"}, {"type": "histogram"}]] | |
| ) | |
| # Prediction probability gauge | |
| fig.add_trace( | |
| go.Indicator( | |
| mode="gauge+number+delta", | |
| value=probability, | |
| domain={'x': [0, 1], 'y': [0, 1]}, | |
| title={'text': "Eligibility Probability"}, | |
| gauge={ | |
| 'axis': {'range': [None, 1]}, | |
| 'bar': {'color': "darkblue"}, | |
| 'steps': [ | |
| {'range': [0, 0.5], 'color': "lightgray"}, | |
| {'range': [0.5, 1], 'color': "lightgreen"} | |
| ], | |
| 'threshold': { | |
| 'line': {'color': "red", 'width': 4}, | |
| 'thickness': 0.75, | |
| 'value': 0.5 | |
| } | |
| } | |
| ), | |
| row=1, col=1 | |
| ) | |
| # Confidence meter | |
| confidence = abs(probability - 0.5) * 2 | |
| fig.add_trace( | |
| go.Indicator( | |
| mode="gauge+number", | |
| value=confidence, | |
| domain={'x': [0, 1], 'y': [0, 1]}, | |
| title={'text': "Prediction Confidence"}, | |
| gauge={ | |
| 'axis': {'range': [None, 1]}, | |
| 'bar': {'color': "orange"}, | |
| 'steps': [ | |
| {'range': [0, 0.3], 'color': "lightcoral"}, | |
| {'range': [0.3, 0.7], 'color': "lightyellow"}, | |
| {'range': [0.7, 1], 'color': "lightgreen"} | |
| ] | |
| } | |
| ), | |
| row=1, col=2 | |
| ) | |
| # Input features bar chart | |
| features = list(input_data.keys()) | |
| values = list(input_data.values()) | |
| fig.add_trace( | |
| go.Bar(x=features, y=values, name="Input Values", marker_color="skyblue"), | |
| row=2, col=1 | |
| ) | |
| # Feature distribution (example data) | |
| fig.add_trace( | |
| go.Histogram(x=values, nbinsx=10, name="Distribution", marker_color="lightcoral"), | |
| row=2, col=2 | |
| ) | |
| fig.update_layout( | |
| height=800, | |
| showlegend=False, | |
| title_text="Student Eligibility Prediction Dashboard", | |
| title_x=0.5 | |
| ) | |
| return fig | |
| def create_model_info(): | |
| """ | |
| Create model information display | |
| """ | |
| info_html = f""" | |
| <div style="padding: 20px; background-color: #f0f2f6; border-radius: 10px; margin: 10px 0;"> | |
| <h3>๐ค Model Information</h3> | |
| <ul> | |
| <li><strong>Model Type:</strong> {metadata.get('model_type', 'CNN')}</li> | |
| <li><strong>Test Accuracy:</strong> {metadata['performance_metrics']['test_accuracy']:.4f}</li> | |
| <li><strong>AUC Score:</strong> {metadata['performance_metrics']['auc_score']:.4f}</li> | |
| <li><strong>Creation Date:</strong> {metadata.get('creation_date', 'N/A')}</li> | |
| <li><strong>Features:</strong> {len(feature_names)} input features</li> | |
| </ul> | |
| </div> | |
| """ | |
| return info_html | |
| def batch_predict(file): | |
| """ | |
| Batch prediction from uploaded CSV file | |
| """ | |
| try: | |
| # Read the uploaded file | |
| df = pd.read_csv(file.name) | |
| # Check if all required features are present | |
| missing_features = set(feature_names) - set(df.columns) | |
| if missing_features: | |
| return f"Missing features: {missing_features}", None | |
| # Select only the required features | |
| df_features = df[feature_names] | |
| # Scale the features | |
| df_scaled = scaler.transform(df_features) | |
| # Reshape for CNN | |
| df_reshaped = df_scaled.reshape(df_scaled.shape[0], df_scaled.shape[1], 1) | |
| # Make predictions | |
| probabilities = model.predict(df_reshaped).flatten() | |
| predictions = ["Eligible" if p > 0.5 else "Not Eligible" for p in probabilities] | |
| # Create results dataframe | |
| results_df = df_features.copy() | |
| results_df['Probability'] = probabilities | |
| results_df['Prediction'] = predictions | |
| results_df['Confidence'] = np.abs(probabilities - 0.5) * 2 | |
| # Save results | |
| output_file = "batch_predictions.csv" | |
| results_df.to_csv(output_file, index=False) | |
| # Create summary statistics | |
| summary = f""" | |
| Batch Prediction Summary: | |
| - Total predictions: {len(results_df)} | |
| - Eligible: {sum(1 for p in predictions if p == 'Eligible')} | |
| - Not Eligible: {sum(1 for p in predictions if p == 'Not Eligible')} | |
| - Average Probability: {np.mean(probabilities):.4f} | |
| - Average Confidence: {np.mean(np.abs(probabilities - 0.5) * 2):.4f} | |
| """ | |
| return summary, output_file | |
| except Exception as e: | |
| return f"Error processing file: {str(e)}", None | |
| # Create Gradio interface | |
| with gr.Blocks( | |
| theme=gr.themes.Soft(), | |
| title="Student Eligibility Prediction", | |
| css=""" | |
| .gradio-container { | |
| max-width: 1200px !important; | |
| } | |
| .main-header { | |
| text-align: center; | |
| padding: 20px; | |
| background: linear-gradient(45deg, #667eea 0%, #764ba2 100%); | |
| color: white; | |
| border-radius: 10px; | |
| margin-bottom: 20px; | |
| } | |
| """ | |
| ) as demo: | |
| # Header | |
| gr.HTML(""" | |
| <div class="main-header"> | |
| <h1>๐ Student Eligibility Prediction System</h1> | |
| <p>AI-powered CNN model for predicting student eligibility with advanced analytics</p> | |
| </div> | |
| """) | |
| with gr.Tabs(): | |
| # Single Prediction Tab | |
| with gr.TabItem("Single Prediction"): | |
| gr.Markdown("### Enter student information to predict eligibility") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| # Create input components dynamically based on features | |
| inputs = [] | |
| for feature in feature_names: | |
| inputs.append( | |
| gr.Number( | |
| label=f"{feature}", | |
| value=85, # Default value | |
| minimum=0, | |
| maximum=100, | |
| step=1 | |
| ) | |
| ) | |
| predict_btn = gr.Button("๐ฎ Predict Eligibility", variant="primary", size="lg") | |
| with gr.Column(scale=2): | |
| with gr.Row(): | |
| prediction_output = gr.Textbox(label="Prediction", scale=1) | |
| probability_output = gr.Textbox(label="Probability", scale=1) | |
| confidence_output = gr.Textbox(label="Confidence", scale=1) | |
| prediction_plot = gr.Plot(label="Prediction Visualization") | |
| # Model information | |
| gr.HTML(create_model_info()) | |
| # Batch Prediction Tab | |
| with gr.TabItem("Batch Prediction"): | |
| gr.Markdown("### Upload a CSV file for batch predictions") | |
| gr.Markdown(f"**Required columns:** {', '.join(feature_names)}") | |
| with gr.Row(): | |
| with gr.Column(): | |
| file_input = gr.File( | |
| label="Upload CSV File", | |
| file_types=[".csv"], | |
| type="file" | |
| ) | |
| batch_predict_btn = gr.Button("๐ Process Batch", variant="primary") | |
| with gr.Column(): | |
| batch_output = gr.Textbox(label="Batch Results Summary", lines=10) | |
| download_file = gr.File(label="Download Results") | |
| # Model Analytics Tab | |
| with gr.TabItem("Model Analytics"): | |
| gr.Markdown("### Model Performance Metrics") | |
| # Performance metrics | |
| metrics_df = pd.DataFrame([metadata['performance_metrics']]) | |
| gr.Dataframe(metrics_df, label="Performance Metrics") | |
| # Feature importance (placeholder - you'd need to calculate this) | |
| gr.Markdown("### Feature Names") | |
| gr.Textbox(value=", ".join(feature_names), label="Model Features", lines=3) | |
| # Event handlers | |
| predict_btn.click( | |
| fn=predict_student_eligibility, | |
| inputs=inputs, | |
| outputs=[prediction_output, probability_output, confidence_output, prediction_plot] | |
| ) | |
| batch_predict_btn.click( | |
| fn=batch_predict, | |
| inputs=[file_input], | |
| outputs=[batch_output, download_file] | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| demo.launch(share=True) |