ntam0001's picture
Update app.py
bffed60 verified
raw
history blame
4.48 kB
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
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize model components
model = None
scaler = None
metadata = {}
feature_names = []
model_loaded = False
def load_model():
global model, scaler, metadata, feature_names, model_loaded
try:
# Verify all required files exist
required_files = ['model_architecture.json', 'final_model.h5', 'scaler.pkl', 'metadata.json']
for file in required_files:
if not os.path.exists(file):
raise FileNotFoundError(f"Missing required file: {file}")
logger.info("Loading model architecture...")
with open('model_architecture.json', 'r') as json_file:
model_json = json_file.read()
model = model_from_json(model_json)
logger.info("Loading model weights...")
model.load_weights('final_model.h5')
logger.info("Loading scaler...")
with open('scaler.pkl', 'rb') as f:
scaler = pickle.load(f)
logger.info("Loading metadata...")
with open('metadata.json', 'r') as f:
metadata = json.load(f)
feature_names = metadata.get('feature_names', [])
model_loaded = True
logger.info("βœ… Model loaded successfully!")
logger.info(f"Features: {feature_names}")
except Exception as e:
logger.error(f"❌ Model loading failed: {str(e)}")
model_loaded = False
# Load model at startup
load_model()
def predict(*args):
try:
if not model_loaded:
raise RuntimeError("Model failed to load. Check server logs for details.")
if len(args) != len(feature_names):
raise ValueError(f"Expected {len(feature_names)} features, got {len(args)}")
# Create input dictionary
input_data = {}
for i, val in enumerate(args):
try:
input_data[feature_names[i]] = float(val)
except ValueError:
raise ValueError(f"Invalid value for {feature_names[i]}: {val}")
# Create DataFrame
input_df = pd.DataFrame([input_data], columns=feature_names)
# Scale features
scaled_input = scaler.transform(input_df)
# Predict
probability = float(model.predict(scaled_input, verbose=0)[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:
logger.error(f"Prediction error: {str(e)}")
return {"Error": str(e)}
# Create Gradio interface
with gr.Blocks(title="Student Eligibility Predictor") as demo:
gr.Markdown("# πŸŽ“ Student Eligibility Predictor")
gr.Markdown("Predict student eligibility based on academic performance metrics")
with gr.Row():
with gr.Column():
input_components = [gr.Number(label=name) for name in feature_names]
predict_btn = gr.Button("Predict", variant="primary")
with gr.Column():
prediction_output = gr.Textbox(label="Prediction")
probability_output = gr.Textbox(label="Probability")
confidence_output = gr.Textbox(label="Confidence")
# Add examples if features exist
if len(feature_names) > 0:
examples = []
if len(feature_names) >= 3:
examples.append([75, 80, 85] + [0]*(len(feature_names)-3))
elif len(feature_names) == 2:
examples.append([75, 80])
else:
examples.append([75])
gr.Examples(
examples=examples,
inputs=input_components,
outputs=[prediction_output, probability_output, confidence_output],
fn=predict,
cache_examples=False
)
predict_btn.click(
fn=predict,
inputs=input_components,
outputs=[prediction_output, probability_output, confidence_output]
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)