rama0519's picture
Update app.py
f2521e8 verified
#!pip install gradio --upgrade # Upgrade to the latest version of Gradio
#!pip install huggingface_hub joblib
import gradio as gr
import pandas as pd
import joblib
from huggingface_hub import hf_hub_download
import os
# Load the Hugging Face token from the environment variable
token = os.getenv("HF_TOKEN")
if token is None:
raise ValueError("Hugging Face token not found. Please set the 'HF_TOKEN' environment variable.")
# Download the model and scaler from the Hugging Face Hub using the token
model_path = hf_hub_download(repo_id="rama0519/DiabeticLogistic123", filename="logistic_regression_model.joblib", use_auth_token=token)
scaler_path = hf_hub_download(repo_id="rama0519/DiabeticLogistic123", filename="scaler.joblib", use_auth_token=token)
# Load the model and scaler
model = joblib.load(model_path)
scaler = joblib.load(scaler_path)
# 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 interface
#interface.launch()
interface.launch(share=True)