Spaces:
Runtime error
Runtime error
# Import necessary libraries | |
import gradio as gr | |
import pandas as pd | |
import joblib | |
from huggingface_hub import hf_hub_download | |
# Function to download model and scaler from Hugging Face Hub | |
def download_model(): | |
# Download the model and scaler | |
model_path = hf_hub_download(repo_id="rama0519/DiabeticLogistic123", filename="logistic_regression_model.joblib") | |
scaler_path = hf_hub_download(repo_id="rama0519/DiabeticLogistic123", filename="scaler.joblib") | |
# Load the model and scaler | |
model = joblib.load(model_path) | |
scaler = joblib.load(scaler_path) | |
return model, scaler | |
# Load model and scaler | |
model, scaler = download_model() | |
# Define reasonable ranges for each input parameter | |
ranges = { | |
'Pregnancies': (0, 20), | |
'Glucose': (50, 250), | |
'BloodPressure': (40, 140), | |
'SkinThickness': (0, 100), | |
'Insulin': (0, 900), | |
'BMI': (10, 60), | |
'DiabetesPedigreeFunction': (0.0, 2.5), | |
'Age': (18, 100) | |
} | |
# Define the prediction function | |
def predict_diabetes(pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, diabetes_pedigree_function, age): | |
data = pd.DataFrame({ | |
'Pregnancies': [pregnancies], | |
'Glucose': [glucose], | |
'BloodPressure': [blood_pressure], | |
'SkinThickness': [skin_thickness], | |
'Insulin': [insulin], | |
'BMI': [bmi], | |
'DiabetesPedigreeFunction': [diabetes_pedigree_function], | |
'Age': [age] | |
}) | |
data_scaled = scaler.transform(data) | |
prediction = model.predict(data_scaled) | |
# Convert prediction to "Diabetic" (1) or "Not Diabetic" (0) | |
if prediction[0] == 1: | |
prediction_text = "Diabetic" | |
else: | |
prediction_text = "Not Diabetic" | |
return prediction_text | |
# Create the Gradio interface | |
interface = gr.Interface( | |
fn=predict_diabetes, | |
inputs=[ | |
gr.Slider(label="Pregnancies", minimum=ranges['Pregnancies'][0], maximum=ranges['Pregnancies'][1]), | |
gr.Slider(label="Glucose", minimum=ranges['Glucose'][0], maximum=ranges['Glucose'][1]), | |
gr.Slider(label="BloodPressure", minimum=ranges['BloodPressure'][0], maximum=ranges['BloodPressure'][1]), | |
gr.Slider(label="SkinThickness", minimum=ranges['SkinThickness'][0], maximum=ranges['SkinThickness'][1]), | |
gr.Slider(label="Insulin", minimum=ranges['Insulin'][0], maximum=ranges['Insulin'][1]), | |
gr.Slider(label="BMI", minimum=ranges['BMI'][0], maximum=ranges['BMI'][1]), | |
gr.Slider(label="DiabetesPedigreeFunction", minimum=ranges['DiabetesPedigreeFunction'][0], maximum=ranges['DiabetesPedigreeFunction'][1]), | |
gr.Slider(label="Age", minimum=ranges['Age'][0], maximum=ranges['Age'][1]) | |
], | |
outputs=gr.Textbox(label="Prediction"), | |
title="Diabetes Prediction", | |
description="Enter the medical details to predict if the patient is diabetic or not." | |
) | |
# Launch the Gradio interface | |
if __name__ == "__main__": | |
interface.launch() | |