File size: 2,628 Bytes
55f664e
6dfb9dc
55f664e
6dfb9dc
bae8052
6dfb9dc
 
 
 
 
bae8052
6dfb9dc
bae8052
 
6dfb9dc
 
 
 
 
 
 
 
 
 
bae8052
 
6dfb9dc
 
136e0e8
 
 
6dfb9dc
bae8052
6dfb9dc
bae8052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dfb9dc
bae8052
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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(**kwargs):
    # Create a DataFrame for a single patient from keyword arguments
    input_data = pd.DataFrame([kwargs])
    
    # 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()