Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import pickle
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
def load_model():
|
7 |
+
"""Load the trained sklearn model from disk"""
|
8 |
+
with open('model.pkl', 'rb') as f:
|
9 |
+
model = pickle.load(f)
|
10 |
+
return model
|
11 |
+
|
12 |
+
def process_input(df):
|
13 |
+
"""Process input dataframe to match model requirements"""
|
14 |
+
# Add any necessary preprocessing steps here
|
15 |
+
# For example: handling missing values, scaling, encoding
|
16 |
+
return df
|
17 |
+
|
18 |
+
def predict_mortality(csv_file):
|
19 |
+
try:
|
20 |
+
# Read the CSV file
|
21 |
+
df = pd.read_csv(csv_file.name)
|
22 |
+
|
23 |
+
# Load the model
|
24 |
+
model = load_model()
|
25 |
+
|
26 |
+
# Process the input data
|
27 |
+
processed_df = process_input(df)
|
28 |
+
|
29 |
+
# Make predictions
|
30 |
+
predictions = model.predict(processed_df)
|
31 |
+
probabilities = model.predict_proba(processed_df)
|
32 |
+
|
33 |
+
# Add predictions to the original dataframe
|
34 |
+
df['Mortality_Risk'] = predictions
|
35 |
+
df['Death_Probability'] = probabilities[:, 1] # Assuming 1 is the positive class
|
36 |
+
|
37 |
+
# Format the results
|
38 |
+
results_df = df.copy()
|
39 |
+
results_df['Mortality_Risk'] = results_df['Mortality_Risk'].map({1: 'High Risk', 0: 'Low Risk'})
|
40 |
+
results_df['Death_Probability'] = results_df['Death_Probability'].round(3)
|
41 |
+
|
42 |
+
return results_df
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
return f"Error processing file: {str(e)}"
|
46 |
+
|
47 |
+
# Create the Gradio interface
|
48 |
+
iface = gr.Interface(
|
49 |
+
fn=predict_mortality,
|
50 |
+
inputs=gr.File(label="Upload Patient Data (CSV)"),
|
51 |
+
outputs=gr.Dataframe(label="Prediction Results"),
|
52 |
+
title="Patient Mortality Risk Prediction",
|
53 |
+
description="""
|
54 |
+
Upload a CSV file containing patient data to predict mortality risk.
|
55 |
+
The model will return the original data with two additional columns:
|
56 |
+
- Mortality_Risk: High Risk or Low Risk classification
|
57 |
+
- Death_Probability: Probability of death (0-1)
|
58 |
+
""",
|
59 |
+
examples=[["sample_data.csv"]], # Add example files if available
|
60 |
+
cache_examples=True
|
61 |
+
)
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
iface.launch(share=True)
|