import gradio as gr import pandas as pd from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # Load dataset (Pima Indians Diabetes dataset) url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv" columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome'] data = pd.read_csv(url, names=columns) # Split data into features and target X = data.drop('Outcome', axis=1) y = data['Outcome'] # Train-Test Split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Create and train RandomForestClassifier model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train, y_train) # Check model accuracy y_pred = model.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print(f"Model Accuracy: {accuracy * 100:.2f}%") # Function for prediction with debugging def predict_diabetes(Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, BMI, DiabetesPedigreeFunction, Age): input_data = [[Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, BMI, DiabetesPedigreeFunction, Age]] prediction = model.predict(input_data) return "Diabetic" if prediction[0] == 1 else "Not Diabetic" # Create Gradio interface with gr.Blocks() as demo: gr.Markdown("# Diabetes Prediction App") with gr.Row(): pregnancies = gr.Number(label="Pregnancies", value=0, precision=0) glucose = gr.Number(label="Glucose", value=120) blood_pressure = gr.Number(label="Blood Pressure", value=70) skin_thickness = gr.Number(label="Skin Thickness", value=20) with gr.Row(): insulin = gr.Number(label="Insulin", value=80) bmi = gr.Number(label="BMI", value=30.0) pedigree = gr.Number(label="Diabetes Pedigree Function", value=0.5) age = gr.Number(label="Age", value=30, precision=0) predict_button = gr.Button("Predict") output = gr.Textbox(label="Prediction", placeholder="Prediction will appear here.") # Connect button to prediction function predict_button.click( fn=predict_diabetes, inputs=[pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, pedigree, age], outputs=output ) # Launch the Gradio app demo.launch(share = True)