Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| import zipfile | |
| import os | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, roc_curve, auc | |
| # Extract ZIP File | |
| zip_path = "DIABETES_PREDICTION.zip" | |
| extract_path = "diabetes_data" | |
| with zipfile.ZipFile(zip_path, 'r') as zip_ref: | |
| zip_ref.extractall(extract_path) | |
| # Load Dataset | |
| csv_file_path = os.path.join(extract_path, "diabetes_prediction_dataset.csv") | |
| df = pd.read_csv(csv_file_path) | |
| # Data Preprocessing | |
| df['smoking_history'] = df['smoking_history'].map({'never':0, 'No Info':1, 'current':2, 'former':3, 'ever':4, 'not current':5}) | |
| df['gender'] = df['gender'].map({'Male': 0, 'Female': 1}) | |
| # Feature Selection | |
| target = 'diabetes' | |
| X = df.drop(columns=[target]) | |
| y = df[target] | |
| # Normalize Features | |
| scaler = StandardScaler() | |
| X_scaled = scaler.fit_transform(X) | |
| # Train-Test Split | |
| X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42) | |
| # Train Model | |
| model = RandomForestClassifier(n_estimators=100, random_state=42) | |
| model.fit(X_train, y_train) | |
| # Define Prediction Function | |
| def predict_diabetes(gender, age, hypertension, heart_disease, smoking_history, bmi, HbA1c, blood_glucose): | |
| # Convert categorical inputs to numeric | |
| gender_numeric = 1 if gender == "Male" else 0 # Encode 'Male' as 1, 'Female' as 0 | |
| # Prepare the input as a NumPy array | |
| input_data = np.array([[gender_numeric, age, hypertension, heart_disease, smoking_history, bmi, HbA1c, blood_glucose]]) | |
| # Scale the input data | |
| input_scaled = scaler.transform(input_data) # Ensure all values are numeric | |
| # Make prediction | |
| prediction = model.predict(input_scaled) | |
| return "Diabetes Positive" if prediction[0] == 1 else "Diabetes Negative" | |
| # Define UI with Gradio | |
| iface = gr.Interface( | |
| fn=predict_diabetes, | |
| inputs=[ | |
| gr.Radio(["Male", "Female"], label="Gender"), | |
| gr.Number(label="Age"), | |
| gr.Radio([0, 1], label="Hypertension (0: No, 1: Yes)"), | |
| gr.Radio([0, 1], label="Heart Disease (0: No, 1: Yes)"), | |
| gr.Dropdown([0, 1, 2, 3, 4, 5], label="Smoking History (Encoded)"), | |
| gr.Number(label="BMI"), | |
| gr.Number(label="HbA1c Level"), | |
| gr.Number(label="Blood Glucose Level") | |
| ], | |
| outputs=gr.Textbox(label="Diabetes Prediction"), | |
| title="Diabetes Risk Prediction", | |
| description="Enter the patient's details to predict diabetes risk." | |
| ) | |
| # Launch App | |
| if __name__ == "__main__": | |
| iface.launch() | |