netrosec commited on
Commit
4fdb56d
·
1 Parent(s): 349864f

updated feature inputs and chart..

Browse files
Files changed (1) hide show
  1. app.py +84 -73
app.py CHANGED
@@ -1,82 +1,93 @@
1
  import gradio as gr
2
- import numpy as np
3
- import urllib.request
4
 
5
- # Downloading the image and saving it in the local folder
6
- # urllib.request.urlretrieve('https://cdn.discordapp.com/attachments/682736875483430985/1089711182094553108/Dopeshott_young_black_femal_doctor_19e26be2-6801-4aed-b481-793b1e77a984.png', 'doctor.png')
7
-
8
- # Dummy data
9
- data = np.array([
10
- [65, 1, 1, 1, 0.5],
11
- [45, 0, 0, 0, 0.2],
12
- [70, 1, 0, 1, 0.8],
13
- [55, 0, 1, 0, 0.6],
14
- [50, 1, 1, 1, 0.7]
15
- ])
16
 
17
- features = ['Age', 'Sex', 'Smoking', 'Treatment', 'Chemotherapy']
18
- sex_options = ['Male', 'Female']
19
- smoking_options = ['No', 'Yes']
20
- treatment_options = ['No', 'Yes']
21
- chemo_options = ['No', 'Yes']
22
 
23
- # Function to predict cancer
24
- def predict_cancer(age, sex, smoking, treatment, chemotherapy):
25
- # Format input
26
- sex = 0 if sex == 'Male' else 1
27
- smoking = 0 if smoking == 'No' else 1
28
- treatment = 0 if treatment == 'No' else 1
29
- chemotherapy = 0 if chemotherapy == 'No' else 1
30
- input_data = np.array([[age, sex, smoking, treatment, chemotherapy]])
31
- # Dummy prediction
32
- prob_alive = np.random.choice([0.8, 0.7, 0.65, 0.548, 0.78])
33
- prediction = 'Alive' if prob_alive >= 0.5 else 'Dead'
34
- return f'{prediction} with a survival probability of {prob_alive:.3f}'
 
 
 
 
 
 
 
 
 
 
35
 
36
- # Interface
37
- age_slider = gr.inputs.Slider(minimum=20, maximum=90, step=1, label='Age')
38
- sex_dropdown = gr.inputs.Dropdown(choices=sex_options, label='Sex')
39
- smoking_dropdown = gr.inputs.Dropdown(choices=smoking_options, label='Smoking')
40
- treatment_dropdown = gr.inputs.Dropdown(choices=treatment_options, label='Treatment')
41
- chemo_dropdown = gr.inputs.Dropdown(choices=chemo_options, label='Chemotherapy')
42
- inputs = [age_slider, sex_dropdown, smoking_dropdown, treatment_dropdown, chemo_dropdown]
43
- outputs = gr.outputs.Textbox(label='Prediction')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- css = """
46
- .sidebar-content {
47
- padding: 20px;
48
- background-color: #f5f5f5;
49
- box-shadow: 1px 1px 10px #ccc;
50
- margin-bottom: 20px;
51
- border-radius: 10px;
52
- }
53
- .doctor-img-container {
54
- display: flex;
55
- align-items: center;
56
- justify-content: center;
57
- }
58
- .doctor-img {
59
- height: auto;
60
- width: auto;
61
- max-width: 100%;
62
- max-height: 100%;
63
- }
64
- """
65
 
66
- sidebar = [
67
- # Adding image of a doctor to the sidebar
68
- '<div class="doctor-img-container"><img class="doctor-img" src="doctor.png" alt="A medical doctor"></div>',
69
- 'This is a cancer prediction app',
 
 
 
 
 
 
 
 
 
 
 
 
70
  ]
71
 
72
- iface = gr.Interface(
73
- fn=predict_cancer,
74
- inputs=inputs,
75
- outputs=outputs,
76
- title='Cancer Predictor',
77
- description='Predicts the risk of survival in cancer patients.\nChoose the input values and the prediction will be displayed automatically.',
78
- sidebar=sidebar,
79
- css=css
80
- )
81
-
82
- iface.launch()
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ import plotly.graph_objects as go
4
 
5
+ data = {
6
+ 'years_after_diagnosis': [5, 8, 7, 7, 8, 7, 5, 6, 7, 5, 6, 4, 3, 4, 5, 8, 5],
7
+ 'age_at_diagnosis': [54, 41, 65, 85, 60, 66, 61, 50, 77, 64, 55, 52, 60, 51, 45, 54, 48],
8
+ 'stage level': [6, 6, 5, 5, 10, 8, 6, 7, 8, 8, 3, 6, 3, 8, 6, 8, 6],
9
+ 'status': [1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0]
10
+ }
 
 
 
 
 
11
 
12
+ df = pd.DataFrame(data)
 
 
 
 
13
 
14
+ def plot_probability_chart(years_after_diagnosis, age_at_diagnosis, stage_level, chemotherapy, brachtherapy,
15
+ chemoradiation, radiotherapy, radiation, menopause, MENO_post, HISTOLOGY, CM_1, CM_2, CM_3, Status):
16
+ match = df.loc[
17
+ (df['years_after_diagnosis'] == years_after_diagnosis) &
18
+ (df['age_at_diagnosis'] == age_at_diagnosis) &
19
+ (df['stage level'] == stage_level)
20
+ ]
21
+
22
+ if len(match) == 0:
23
+ probability_alive = 0.5
24
+ probability_dead = 0.5
25
+ label = ["No matching record", "No matching record"]
26
+ else:
27
+ status = match.iloc[0]['status']
28
+ label = ["Alive", "Dead"]
29
+
30
+ if status == 0:
31
+ probability_alive = 0.2
32
+ probability_dead = 0.8
33
+ else:
34
+ probability_alive = 0.7
35
+ probability_dead = 0.3
36
 
37
+ fig = go.Figure(
38
+ go.Bar(
39
+ x=label,
40
+ y=[probability_alive, probability_dead]
41
+ )
42
+ )
43
+ fig.update_layout(
44
+ title="Survival Probability Chart",
45
+ xaxis_title="Status",
46
+ yaxis_title="Probability",
47
+ )
48
+ return fig
49
+
50
+ if len(match) == 0:
51
+ return "No matching patient record found"
52
+
53
+ status = match.iloc[0]['status']
54
+ label = ["Alive", "Dead"]
55
+ if status == 0:
56
+ probability_alive = 0.2
57
+ probability_dead = 0.8
58
+ else:
59
+ probability_alive = 0.7
60
+ probability_dead = 0.3
61
 
62
+ fig = go.Figure(
63
+ go.Bar(
64
+ x=label,
65
+ y=[probability_alive, probability_dead]
66
+ )
67
+ )
68
+ fig.update_layout(
69
+ title="Survival Probability Chart",
70
+ xaxis_title="Status",
71
+ yaxis_title="Probability",
72
+ )
73
+ return fig
 
 
 
 
 
 
 
 
74
 
75
+ inputs = [
76
+ gr.inputs.Slider(minimum=0, maximum=10, step=1, default=5, label='years_after_diagnosis'),
77
+ gr.inputs.Slider(minimum=0, maximum=100, step=1, default=54, label='age_at_diagnosis'),
78
+ gr.inputs.Slider(minimum=0, maximum=10, step=1, default=6, label='stage level'),
79
+ gr.inputs.Checkbox(label='chemotherapy'),
80
+ gr.inputs.Checkbox(label='brachtherapy'),
81
+ gr.inputs.Checkbox(label='chemoradiation'),
82
+ gr.inputs.Checkbox(label='radiotherapy'),
83
+ gr.inputs.Checkbox(label='radiation'),
84
+ gr.inputs.Slider(minimum=0, maximum=30, step=1, default=0, label='menopause'),
85
+ gr.inputs.Slider(minimum=0, maximum=30, step=1, default=0, label='MENO_post'),
86
+ gr.inputs.Slider(minimum=0, maximum=2, step=1, default=2, label='HISTOLOGY'),
87
+ gr.inputs.Checkbox(label='CM_1'),
88
+ gr.inputs.Checkbox(label='CM_2'),
89
+ gr.inputs.Checkbox(label='CM_3'),
90
+ gr.inputs.Radio(["Alive", "Dead"], label="Status")
91
  ]
92
 
93
+ gr.Interface(fn=plot_probability_chart, inputs=inputs, outputs="plot").launch()