|
|
import joblib |
|
|
import numpy as np |
|
|
import pandas as pd |
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
model = joblib.load('ckd_model.joblib') |
|
|
|
|
|
FEATURE_COLUMNS = joblib.load('model_features.joblib') |
|
|
except FileNotFoundError: |
|
|
raise FileNotFoundError("Model or feature files not found. Ensure 'ckd_model.joblib' and 'model_features.joblib' are uploaded.") |
|
|
|
|
|
|
|
|
def predict_ckd(*inputs): |
|
|
""" |
|
|
Takes 24 inputs (features) from the Gradio interface and returns the prediction. |
|
|
""" |
|
|
|
|
|
|
|
|
input_data = np.array(inputs).reshape(1, -1) |
|
|
|
|
|
|
|
|
prediction = model.predict(input_data)[0] |
|
|
|
|
|
|
|
|
|
|
|
probability_not_ckd = model.predict_proba(input_data)[0][1] |
|
|
|
|
|
|
|
|
if prediction == 0: |
|
|
result_text = "Positive for Chronic Kidney Disease (CKD)" |
|
|
confidence = 1 - probability_not_ckd |
|
|
color = "red" |
|
|
else: |
|
|
result_text = "Negative for Chronic Kidney Disease (CKD)" |
|
|
confidence = probability_not_ckd |
|
|
color = "green" |
|
|
|
|
|
|
|
|
return f"<h2 style='color: {color};'>Prediction: {result_text}</h2><p>Confidence: {confidence:.2f} ({confidence*100:.0f}%)</p>" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
input_components = [] |
|
|
for feature in FEATURE_COLUMNS: |
|
|
|
|
|
|
|
|
input_components.append(gr.Textbox(label=feature.upper(), placeholder=f"Enter value for {feature}")) |
|
|
|
|
|
|
|
|
|
|
|
output_component = gr.HTML(label="Prediction Result") |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=predict_ckd, |
|
|
inputs=input_components, |
|
|
outputs=output_component, |
|
|
title="CKD Prediction Model (Random Forest)", |
|
|
description="Enter the 24 clinical parameters below to predict the risk of Chronic Kidney Disease. **Note: For binary features (yes/no, present/notpresent), use the encoded numerical values (0 or 1) used during training.**" |
|
|
) |
|
|
|
|
|
|
|
|
iface.launch(share=False) |