Upload app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,120 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from model_utils import load_all_models, predict_with_model
|
3 |
-
|
4 |
-
# Load all models
|
5 |
-
models, model_features = load_all_models()
|
6 |
-
|
7 |
-
# Mapeo de nombres amigables a nombres reales
|
8 |
-
MODEL_MAPPING = {
|
9 |
-
"Death": "Death_random_forest_model",
|
10 |
-
"Binary diagnosis": "Binary diagnosis_random_forest_model",
|
11 |
-
"Necessity of transplantation": "Necessity of transplantation_random_forest_model",
|
12 |
-
"Progressive disease": "Progressive disease_random_forest_model"
|
13 |
-
}
|
14 |
-
|
15 |
-
# Invertir el mapeo (opcional para facilidad)
|
16 |
-
INVERSE_MODEL_MAPPING = {v: k for k, v in MODEL_MAPPING.items()}
|
17 |
-
|
18 |
-
# Feature sets for each target variable
|
19 |
-
FEATURES = {
|
20 |
-
"Death": [
|
21 |
-
'
|
22 |
-
'FVC (
|
23 |
-
'Severity of telomere shortening - Transform 4'
|
24 |
-
],
|
25 |
-
"Binary diagnosis": [
|
26 |
-
'Pedigree', '
|
27 |
-
'
|
28 |
-
'
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
'
|
33 |
-
'
|
34 |
-
|
35 |
-
|
36 |
-
'
|
37 |
-
'FVC (
|
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 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
model
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
inputs
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from model_utils import load_all_models, predict_with_model
|
3 |
+
|
4 |
+
# Load all models
|
5 |
+
models, model_features = load_all_models()
|
6 |
+
|
7 |
+
# Mapeo de nombres amigables a nombres reales
|
8 |
+
MODEL_MAPPING = {
|
9 |
+
"Death": "Death_random_forest_model",
|
10 |
+
"Binary diagnosis": "Binary diagnosis_random_forest_model",
|
11 |
+
"Necessity of transplantation": "Necessity of transplantation_random_forest_model",
|
12 |
+
"Progressive disease": "Progressive disease_random_forest_model"
|
13 |
+
}
|
14 |
+
|
15 |
+
# Invertir el mapeo (opcional para facilidad)
|
16 |
+
INVERSE_MODEL_MAPPING = {v: k for k, v in MODEL_MAPPING.items()}
|
17 |
+
|
18 |
+
# Feature sets for each target variable
|
19 |
+
FEATURES = {
|
20 |
+
"Death": [
|
21 |
+
'ProgressiveDisease', 'DLCO (%) at diagnosis', 'FVC (%) at diagnosis',
|
22 |
+
'FVC (L) at diagnosis', 'RadioWorsening2y', 'Age at diagnosis',
|
23 |
+
'Pedigree', 'Severity of telomere shortening - Transform 4'
|
24 |
+
],
|
25 |
+
"Binary diagnosis": [
|
26 |
+
'Age at diagnosis', 'Pedigree', 'DLCO (%) at diagnosis', 'TOBACCO',
|
27 |
+
'FVC (%) at diagnosis', 'FVC (L) at diagnosis',
|
28 |
+
'Multidisciplinary committee', 'Severity of telomere shortening - Transform 4',
|
29 |
+
'Severity of telomere shortening', 'Biopsy'
|
30 |
+
],
|
31 |
+
"Necessity of transplantation": [
|
32 |
+
'RadioWorsening2y', 'FVC (%) 1 year after diagnosis',
|
33 |
+
'Genetic mutation studied in patient', 'Comorbidities',
|
34 |
+
'DLCO (%) 1 year after diagnosis', 'FVC (L) at diagnosis',
|
35 |
+
'DLCO (%) at diagnosis', 'Biopsy',
|
36 |
+
'Severity of telomere shortening - Transform 4', 'FVC (%) at diagnosis',
|
37 |
+
'FVC (L) 1 year after diagnosis'
|
38 |
+
],
|
39 |
+
"Progressive disease": [
|
40 |
+
'DLCO (%) at diagnosis', 'Age at diagnosis', 'Pedigree',
|
41 |
+
'1st degree relative', 'FVC (L) at diagnosis', 'FVC (%) at diagnosis',
|
42 |
+
'Biopsy', 'Genetic mutation studied in patient',
|
43 |
+
'Severity of telomere shortening - Transform 4', 'Severity of telomere shortening'
|
44 |
+
]
|
45 |
+
}
|
46 |
+
|
47 |
+
FEATURE_RANGES = {
|
48 |
+
'Pedigree': (0, 67),
|
49 |
+
'Age at diagnosis': (0, 200),
|
50 |
+
'FVC (L) at diagnosis': (0.0, 5.0),
|
51 |
+
'FVC (%) at diagnosis': (0.0, 200.0),
|
52 |
+
'DLCO (%) at diagnosis': (0.0, 200.0),
|
53 |
+
'RadioWorsening2y': (0, 3),
|
54 |
+
'Severity of telomere shortening - Transform 4': (1, 6),
|
55 |
+
'ProgressiveDisease': (0, 1),
|
56 |
+
'TOBACCO': (0, 1),
|
57 |
+
'Multidisciplinary committee': (0, 1),
|
58 |
+
'Biopsy': (0, 1),
|
59 |
+
'Genetic mutation studied in patient': (0, 1),
|
60 |
+
'Comorbidities': (0, 1),
|
61 |
+
'FVC (L) 1 year after diagnosis': (0.0, 5.0),
|
62 |
+
'FVC (%) 1 year after diagnosis': (0.0, 200.0),
|
63 |
+
'DLCO (%) 1 year after diagnosis': (0.0, 200.0),
|
64 |
+
'1st degree relative': (0, 1),
|
65 |
+
'Severity of telomere shortening': (1, 6)
|
66 |
+
}
|
67 |
+
|
68 |
+
# Define prediction function
|
69 |
+
def make_prediction(input_features, friendly_model_name):
|
70 |
+
"""
|
71 |
+
Predict using the selected model and input features.
|
72 |
+
"""
|
73 |
+
# Map the friendly model name to the real model name
|
74 |
+
target_model = MODEL_MAPPING.get(friendly_model_name)
|
75 |
+
if target_model not in models:
|
76 |
+
return f"Model '{friendly_model_name}' not found. Please select a valid model."
|
77 |
+
|
78 |
+
model = models[target_model]
|
79 |
+
features = model_features[target_model]
|
80 |
+
|
81 |
+
if len(input_features) != len(features):
|
82 |
+
return f"Invalid input. Expected features: {features}"
|
83 |
+
|
84 |
+
input_array = [float(x) for x in input_features]
|
85 |
+
prediction = predict_with_model(model, input_array)
|
86 |
+
return f"Prediction for {friendly_model_name}: {prediction}"
|
87 |
+
|
88 |
+
# Define Gradio interface
|
89 |
+
def gradio_interface():
|
90 |
+
def create_inputs_for_features(features):
|
91 |
+
inputs = []
|
92 |
+
for feature in features:
|
93 |
+
min_val, max_val = FEATURE_RANGES.get(feature, (None, None))
|
94 |
+
inputs.append(gr.Number(label=f"{feature} (Range: {min_val} - {max_val})", minimum=min_val, maximum=max_val))
|
95 |
+
return inputs
|
96 |
+
|
97 |
+
# Create a separate interface for each target variable
|
98 |
+
interfaces = []
|
99 |
+
for target, features in FEATURES.items():
|
100 |
+
inputs = create_inputs_for_features(features)
|
101 |
+
interface = gr.Interface(
|
102 |
+
fn=lambda *args, target=target: make_prediction(args, target),
|
103 |
+
inputs=inputs,
|
104 |
+
outputs=gr.Text(label="Prediction Result"),
|
105 |
+
title=f"Prediction for {target}",
|
106 |
+
description=f"Provide values for features relevant to {target}"
|
107 |
+
)
|
108 |
+
interfaces.append(interface)
|
109 |
+
|
110 |
+
# Combine all interfaces into a tabbed layout
|
111 |
+
tabbed_interface = gr.TabbedInterface(
|
112 |
+
interface_list=interfaces,
|
113 |
+
tab_names=list(FEATURES.keys())
|
114 |
+
)
|
115 |
+
return tabbed_interface
|
116 |
+
|
117 |
+
# Launch Gradio app
|
118 |
+
if __name__ == "__main__":
|
119 |
+
interface = gradio_interface()
|
120 |
+
interface.launch()
|