| import os |
| import gradio as gr |
| import pandas as pd |
| import numpy as np |
| from sklearn.ensemble import RandomForestClassifier |
| from sklearn.preprocessing import StandardScaler |
|
|
| |
| def create_model(): |
| |
| data = { |
| 'age': [65, 72, 58, 81, 45], |
| 'time_in_hospital': [5, 8, 3, 12, 4], |
| 'num_lab_procedures': [45, 32, 28, 51, 38], |
| 'num_medications': [15, 22, 8, 18, 12], |
| 'readmitted': [1, 1, 0, 1, 0] |
| } |
| df = pd.DataFrame(data) |
| |
| X = df.drop('readmitted', axis=1) |
| y = df['readmitted'] |
| |
| |
| model = RandomForestClassifier(n_estimators=10, random_state=42) |
| model.fit(X, y) |
| |
| |
| preprocessor = StandardScaler() |
| preprocessor.fit(X) |
| |
| return model, preprocessor |
|
|
| |
| model, preprocessor = create_model() |
| print("Successfully created model and preprocessor in memory!") |
|
|
| |
| FEATURES = ['age', 'time_in_hospital', 'num_lab_procedures', 'num_medications'] |
|
|
| def predict_readmission(age, time_in_hospital, num_lab_procedures, num_medications): |
| input_data = np.array([[age, time_in_hospital, num_lab_procedures, num_medications]]) |
| processed = preprocessor.transform(input_data) |
| prob = model.predict_proba(processed)[0][1] |
| return {"Probability": float(prob), "Risk": "High" if prob > 0.5 else "Low"} |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# 🏥 Hospital Readmission Risk Predictor") |
| |
| with gr.Row(): |
| gr.Number(label="Age", value=65) |
| gr.Number(label="Days in Hospital", value=5) |
| with gr.Row(): |
| gr.Number(label="Lab Procedures", value=45) |
| gr.Number(label="Medications", value=15) |
| |
| inputs = [ |
| gr.Number(label="Age", value=65), |
| gr.Number(label="Days in Hospital", value=5), |
| gr.Number(label="Lab Procedures", value=45), |
| gr.Number(label="Medications", value=15) |
| ] |
| |
| submit = gr.Button("Predict Readmission Risk") |
| output = gr.JSON() |
| |
| submit.click( |
| fn=predict_readmission, |
| inputs=inputs, |
| outputs=output |
| ) |
|
|
| demo.launch() |