File size: 2,684 Bytes
4be7b9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import gradio as gr
import numpy as np
import urllib.request

# Downloading the image and saving it in the local folder
# urllib.request.urlretrieve('https://cdn.discordapp.com/attachments/682736875483430985/1089711182094553108/Dopeshott_young_black_femal_doctor_19e26be2-6801-4aed-b481-793b1e77a984.png', 'doctor.png')

# Dummy data
data = np.array([
    [65, 1, 1, 1, 0.5],
    [45, 0, 0, 0, 0.2],
    [70, 1, 0, 1, 0.8],
    [55, 0, 1, 0, 0.6],
    [50, 1, 1, 1, 0.7]
])

features = ['Age', 'Sex', 'Smoking', 'Treatment', 'Chemotherapy']
sex_options = ['Male', 'Female']
smoking_options = ['No', 'Yes']
treatment_options = ['No', 'Yes']
chemo_options = ['No', 'Yes']

# Function to predict cancer
def predict_cancer(age, sex, smoking, treatment, chemotherapy):
    # Format input
    sex = 0 if sex == 'Male' else 1
    smoking = 0 if smoking == 'No' else 1
    treatment = 0 if treatment == 'No' else 1
    chemotherapy = 0 if chemotherapy == 'No' else 1
    input_data = np.array([[age, sex, smoking, treatment, chemotherapy]])
    # Dummy prediction
    prob_alive = np.random.choice([0.8, 0.7, 0.65, 0.548, 0.78])
    prediction = 'Alive' if prob_alive >= 0.5 else 'Dead'
    return f'{prediction} with a survival probability of {prob_alive:.3f}'

# Interface
age_slider = gr.inputs.Slider(minimum=20, maximum=90, step=1, label='Age')
sex_dropdown = gr.inputs.Dropdown(choices=sex_options, label='Sex')
smoking_dropdown = gr.inputs.Dropdown(choices=smoking_options, label='Smoking')
treatment_dropdown = gr.inputs.Dropdown(choices=treatment_options, label='Treatment')
chemo_dropdown = gr.inputs.Dropdown(choices=chemo_options, label='Chemotherapy')
inputs = [age_slider, sex_dropdown, smoking_dropdown, treatment_dropdown, chemo_dropdown]
outputs = gr.outputs.Textbox(label='Prediction')

css = """
.sidebar-content {
    padding: 20px;
    background-color: #f5f5f5;
    box-shadow: 1px 1px 10px #ccc;
    margin-bottom: 20px;
    border-radius: 10px;
}
.doctor-img-container {
    display: flex;
    align-items: center;
    justify-content: center;
}
.doctor-img {
    height: auto;
    width: auto;
    max-width: 100%;
    max-height: 100%;
}
"""

sidebar = [
    # Adding image of a doctor to the sidebar
    '<div class="doctor-img-container"><img class="doctor-img" src="doctor.png" alt="A medical doctor"></div>',
    'This is a cancer prediction app',
]

iface = gr.Interface(
    fn=predict_cancer,
    inputs=inputs,
    outputs=outputs,
    title='Cancer Predictor',
    description='Predicts the risk of survival in cancer patients.\nChoose the input values and the prediction will be displayed automatically.',
    sidebar=sidebar,
    css=css
)

iface.launch()