amonfortc commited on
Commit
25d12fa
·
verified ·
1 Parent(s): 44e5e86

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -115
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
- 'Pedigree', 'Age at diagnosis', 'FVC (L) at diagnosis',
22
- 'FVC (%) at diagnosis', 'DLCO (%) at diagnosis', 'RadioWorsening2y',
23
- 'Severity of telomere shortening - Transform 4', 'Progressive disease'
24
- ],
25
- "Binary diagnosis": [
26
- 'Pedigree', 'Age at diagnosis', 'Antifibrotic Drug',
27
- 'Prednisone', 'Mycophenolate', 'FVC (L) at diagnosis',
28
- 'FVC (%) at diagnosis', 'DLCO (%) at diagnosis'
29
- ],
30
- "Necessity of transplantation": [
31
- 'Pedigree','Age at diagnosis','FVC (L) at diagnosis', 'FVC (%) at diagnosis', 'DLCO (%) at diagnosis',
32
- 'FVC (L) 1 year after diagnosis','FVC (%) 1 year after diagnosis','DLCO (%) 1 year after diagnosis',
33
- 'RadioWorsening2y'
34
- ],
35
- "Progressive disease": [
36
- 'Pedigree', 'Age at diagnosis', 'FVC (L) at diagnosis','FVC (%) at diagnosis', 'DLCO (%) at diagnosis','FVC (L) 1 year after diagnosis',
37
- 'FVC (%) 1 year after diagnosis', 'DLCO (%) 1 year after diagnosis',
38
- 'RadioWorsening2y', 'Genetic mutation studied in patient'
39
- ]
40
-
41
- }
42
-
43
- FEATURE_RANGES = {
44
- 'Pedigree': (0, 67),
45
- 'Age at diagnosis': (0, 200),
46
- 'FVC (L) at diagnosis': (0.0, 5.0),
47
- 'FVC (%) at diagnosis': (0.0, 200.0),
48
- 'DLCO (%) at diagnosis': (0.0, 200.0),
49
- 'RadioWorsening2y': (0, 3),
50
- 'Severity of telomere shortening - Transform 4': (1, 6),
51
- 'Progressive disease': (0, 1),
52
- 'Antifibrotic Drug': (0, 1),
53
- 'Prednisone': (0, 1),
54
- 'Mycophenolate': (0, 1),
55
- 'FVC (L) 1 year after diagnosis': (0.0, 5.0),
56
- 'FVC (%) 1 year after diagnosis': (0.0, 200.0),
57
- 'DLCO (%) 1 year after diagnosis': (0.0, 200.0),
58
- 'Genetic mutation studied in patient': (0, 1),
59
- 'Comorbidities': (0, 1)
60
- }
61
-
62
-
63
- # Define prediction function
64
- def make_prediction(input_features, friendly_model_name):
65
- """
66
- Predict using the selected model and input features.
67
- """
68
- # Map the friendly model name to the real model name
69
- target_model = MODEL_MAPPING.get(friendly_model_name)
70
- if target_model not in models:
71
- return f"Model '{friendly_model_name}' not found. Please select a valid model."
72
-
73
- model = models[target_model]
74
- features = model_features[target_model]
75
-
76
- if len(input_features) != len(features):
77
- return f"Invalid input. Expected features: {features}"
78
-
79
- input_array = [float(x) for x in input_features]
80
- prediction = predict_with_model(model, input_array)
81
- return f"Prediction for {friendly_model_name}: {prediction}"
82
-
83
- # Define Gradio interface
84
- def gradio_interface():
85
- def create_inputs_for_features(features):
86
- inputs = []
87
- for feature in features:
88
- min_val, max_val = FEATURE_RANGES.get(feature, (None, None))
89
- inputs.append(gr.Number(label=f"{feature} (Range: {min_val} - {max_val})", minimum=min_val, maximum=max_val))
90
- return inputs
91
-
92
- # Create a separate interface for each target variable
93
- interfaces = []
94
- for target, features in FEATURES.items():
95
- inputs = create_inputs_for_features(features)
96
- interface = gr.Interface(
97
- fn=lambda *args, target=target: make_prediction(args, target),
98
- inputs=inputs,
99
- outputs=gr.Text(label="Prediction Result"),
100
- title=f"Prediction for {target}",
101
- description=f"Provide values for features relevant to {target}"
102
- )
103
- interfaces.append(interface)
104
-
105
- # Combine all interfaces into a tabbed layout
106
- tabbed_interface = gr.TabbedInterface(
107
- interface_list=interfaces,
108
- tab_names=list(FEATURES.keys())
109
- )
110
- return tabbed_interface
111
-
112
- # Launch Gradio app
113
- if __name__ == "__main__":
114
- interface = gradio_interface()
115
- interface.launch()
 
 
 
 
 
 
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()