Spaces:
Sleeping
Sleeping
| 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 model_from_json | |
| import os | |
| # Initialize model components | |
| model = None | |
| scaler = None | |
| metadata = {} | |
| feature_names = [] | |
| def load_model(): | |
| global model, scaler, metadata, feature_names | |
| try: | |
| # Load model architecture | |
| with open('model_architecture.json', 'r') as json_file: | |
| model_json = json_file.read() | |
| model = model_from_json(model_json) | |
| # Load weights | |
| model.load_weights('final_model.h5') | |
| # Load 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) | |
| feature_names = metadata['feature_names'] | |
| print("β Model loaded successfully!") | |
| print(f"Using features: {feature_names}") | |
| except Exception as e: | |
| print(f"β Error loading model: {str(e)}") | |
| # Load model at startup | |
| load_model() | |
| def predict(*args): | |
| try: | |
| if model is None or scaler is None: | |
| raise Exception("Model not loaded. Please check the model files.") | |
| # Create input dictionary | |
| input_data = {feature_names[i]: float(args[i]) for i in range(len(feature_names))} | |
| input_df = pd.DataFrame([input_data]) | |
| # Scale features | |
| scaled_input = scaler.transform(input_df) | |
| # Predict | |
| probability = float(model.predict(scaled_input)[0][0]) | |
| prediction = "Eligible" if probability > 0.5 else "Not Eligible" | |
| confidence = abs(probability - 0.5) * 2 | |
| return { | |
| "Prediction": prediction, | |
| "Probability": f"{probability:.4f}", | |
| "Confidence": f"{confidence:.4f}" | |
| } | |
| except Exception as e: | |
| return {"Error": str(e)} | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=predict, | |
| inputs=[gr.Number(label=name) for name in feature_names], | |
| outputs=[ | |
| gr.Textbox(label="Prediction"), | |
| gr.Textbox(label="Probability"), | |
| gr.Textbox(label="Confidence") | |
| ], | |
| title="π Student Eligibility Predictor", | |
| description="Predict student eligibility based on academic performance metrics", | |
| examples=[[75, 80, 85] if len(feature_names) >= 3 else [75, 80]] # Example inputs | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() |