Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
import json | |
from sklearn.compose import ColumnTransformer | |
from sklearn.preprocessing import StandardScaler, OneHotEncoder | |
# Load selected features from JSON file | |
with open("selected_features.json", "r") as file: | |
selected_features = json.load(file) | |
def preprocess_data(data): | |
# Identify numerical and categorical columns | |
numerical_cols = [col for col in data.columns if data[col].dtype in ['int64', 'float64']] | |
categorical_cols = [col for col in data.columns if col not in numerical_cols] | |
# Preprocessing pipeline | |
preprocessor = ColumnTransformer( | |
transformers=[ | |
('num', StandardScaler(), numerical_cols), | |
('cat', OneHotEncoder(sparse_output=False, drop='first'), categorical_cols) | |
] | |
) | |
# Apply preprocessing | |
processed_data = preprocessor.fit_transform(data) | |
feature_names = numerical_cols + list(preprocessor.named_transformers_['cat'].get_feature_names_out(categorical_cols)) | |
return pd.DataFrame(processed_data, columns=feature_names) | |
def process_manual_input(**inputs): | |
# Create a DataFrame for a single patient from inputs | |
input_data = pd.DataFrame([inputs]) | |
# Preprocess the data | |
processed_data = preprocess_data(input_data) | |
return processed_data.to_csv(index=False) | |
# GUI inputs for each feature | |
gui_inputs = [] | |
for feature in selected_features: | |
if feature.startswith("Had"): # Binary categorical features | |
gui_inputs.append(gr.Radio(label=feature, choices=["Yes", "No"], value="No")) | |
elif feature in ["BMI", "WeightInKilograms", "HeightInMeters"]: # Numerical features | |
gui_inputs.append(gr.Slider(label=feature, minimum=0, maximum=300, step=0.1, value=25)) | |
elif feature == "PhysicalHealthDays": # Numerical feature with a smaller range | |
gui_inputs.append(gr.Slider(label=feature, minimum=0, maximum=30, step=1, value=5)) | |
elif feature == "SleepHours": # Hours of sleep | |
gui_inputs.append(gr.Slider(label=feature, minimum=0, maximum=24, step=0.5, value=8)) | |
else: # Default for any remaining numerical features | |
gui_inputs.append(gr.Slider(label=feature, minimum=0, maximum=100, step=1, value=50)) | |
# Create the Gradio app interface | |
interface = gr.Interface( | |
fn=process_manual_input, | |
inputs=gui_inputs, | |
outputs=gr.Textbox(label="Processed Data (CSV Format)"), | |
title="Single Patient Data Preprocessor", | |
description="Input data for a single patient using sliders and radio buttons. The data will be preprocessed and displayed as CSV." | |
) | |
# Launch the app | |
interface.launch() |