Spaces:
Sleeping
Sleeping
File size: 1,988 Bytes
f76e985 27f09ba f76e985 |
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 62 |
import gradio as gr
import pandas as pd
import pickle
import numpy as np
def load_model():
"""Load the trained sklearn model from disk"""
with open('model.pkl', 'rb') as f:
model = pickle.load(f)
return model
def process_input(df):
"""Process input dataframe to match model requirements"""
# Add any necessary preprocessing steps here
# For example: handling missing values, scaling, encoding
return df
def predict_mortality(csv_file):
try:
# Read the CSV file
df = pd.read_csv(csv_file.name)
# Load the model
model = load_model()
# Process the input data
processed_df = process_input(df)
# Make predictions
predictions = model.predict(processed_df)
probabilities = model.predict_proba(processed_df)
# Add predictions to the original dataframe
df['Mortality_Risk'] = predictions
df['Death_Probability'] = probabilities[:, 1] # Assuming 1 is the positive class
# Format the results
results_df = df.copy()
results_df['Mortality_Risk'] = results_df['Mortality_Risk'].map({1: 'High Risk', 0: 'Low Risk'})
results_df['Death_Probability'] = results_df['Death_Probability'].round(3)
return results_df
except Exception as e:
return f"Error processing file: {str(e)}"
# Create the Gradio interface
iface = gr.Interface(
fn=predict_mortality,
inputs=gr.File(label="Upload Patient Data (CSV)"),
outputs=gr.Dataframe(label="Prediction Results"),
title="Patient Mortality Risk Prediction",
description="""
Upload a CSV file containing patient data to predict mortality risk.
The model will return the original data with two additional columns:
- Mortality_Risk: High Risk or Low Risk classification
- Death_Probability: Probability of death (0-1)
"""
)
if __name__ == "__main__":
iface.launch(share=True) |