shahinurislamkowser's picture
Update app.py
bba8866 verified
import gradio as gr
import numpy as np
import tensorflow as tf
import joblib
# 1. Load the model
model = tf.keras.models.load_model('consolidated_fault_model.keras')
# Load the scaler and label encoder
scaler = joblib.load('scaler.joblib')
label_encoder = joblib.load('label_encoder.joblib')
def predict_smart_grid(Ia, Ib, Ic, Va, Vb, Vc):
# Prepare the input features
features = np.array([[Ia, Ib, Ic, Va, Vb, Vc]])
# Scale the input using the loaded scaler
features_scaled = scaler.transform(features)
# Make prediction
det_pred, cl_pred = model.predict(features_scaled, verbose=0)
# Process Detection Output
fault_prob = float(det_pred[0][0])
det_status = "FAULT DETECTED" if fault_prob > 0.5 else "NO FAULT"
det_conf = fault_prob if fault_prob > 0.5 else 1 - fault_prob
detection_str = f"{det_status} ({det_conf:.2%})"
# Process Classification Output
class_idx = np.argmax(cl_pred[0])
class_label = label_encoder.classes_[class_idx] # Use the loaded label_encoder
class_conf = float(cl_pred[0][class_idx])
classification_str = f"{class_label} ({class_conf:.2%})"
return detection_str, classification_str
iface = gr.Interface(
fn=predict_smart_grid,
inputs=[
gr.Number(label="Ia (Line current of phase A)"),
gr.Number(label="Ib (Line current of phase B)"),
gr.Number(label="Ic (Line current of phase C)"),
gr.Number(label="Va (Line voltage of phase A)"),
gr.Number(label="Vb (Line voltage of phase B)"),
gr.Number(label="Vc (Line voltage of phase C)")
],
outputs=[
gr.Textbox(label="Fault Detection Analysis"),
gr.Textbox(label="Fault Type Classification")
],
title="AI-Driven Smart Grid Transmission Line Fault Detection and Classification System",
description="Input the three-phase current and voltage parameters (Ia, Ib, Ic, Va, Vb, and Vc) to intelligently analyze transmission line conditions and accurately detect and classify smart grid faults in real time.",
theme="soft"
)
iface.launch()