File size: 1,965 Bytes
23dfbf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
from pycaret.classification import load_model

# Load the saved model
model = load_model('best_automl_model')

# Prediction function
def predict_anemia(sex, red_pixel, green_pixel, blue_pixel):
    # Map input values to a DataFrame
    input_data = pd.DataFrame({
        'Red Pixel': [red_pixel],
        'Green Pixel': [green_pixel],
        'Blue Pixel': [blue_pixel],
        'Sex': [1 if sex == "Male" else 0]  # Encode 'Male' as 1, 'Female' as 0
    })
    
    # Make predictions
    prediction = model.predict(input_data)[0]
    probability = model.predict_proba(input_data)[0][1]
    
    # Convert results
    prediction_label = "Yes" if prediction == 1 else "No"
    return prediction_label, f"{probability:.2%}"  # Probability as a percentage

# Define input components with better labels and layout
inputs = [
    gr.Radio(["Male", "Female"], label="Sex", interactive=True),
    gr.Slider(0, 100, step=0.1, label="Red Pixel Percentage (%)"),
    gr.Slider(0, 100, step=0.1, label="Green Pixel Percentage (%)"),
    gr.Slider(0, 100, step=0.1, label="Blue Pixel Percentage (%)"),
]

# Define output components
outputs = [
    gr.Textbox(label="Prediction", interactive=False),
    gr.Textbox(label="Probability of Anemia (%)", interactive=False),
]

# Create the Gradio Interface
interface = gr.Interface(
    fn=predict_anemia,
    inputs=inputs,
    outputs=outputs,
    title="Anemia Prediction App",
    description=(
        "This app predicts whether a person is anemic based on the percentages of red, "
        "green, and blue pixels in their image and their sex. "
        "Simply adjust the sliders and select the person's sex to get a prediction."
    ),
    theme="huggingface",  # Optional theme
    live=False,  # Turn off live predictions for better performance
    examples=[
        ["Male", 45.0, 30.0, 25.0],
        ["Female", 50.0, 28.0, 22.0],
    ],
)

# Launch the app
interface.launch()