File size: 2,104 Bytes
e56940d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import joblib

# Load the model
rf_model = joblib.load("rf_model.pkl")

features = {
    "CD4": [0, 500, 1],
    "AST/ALT": [0, 100, 1],
    "ALT": [0, 2000, 1],
    "Hb": [1, 150, 1],
    "CRP": [1, 500, 1],
    "ALB": [10, 50, 1],
    "POAL": [0, 1, 1],
    "ALC": [0, 5, 1],
    "Age (years)": [12, 100, 1],
    "WBC": [0, 20, 1],
    "PLT": [1, 800, 1],
    "AST": [0, 2000, 1],
}


# Define the inference function
def predict(*args):
    try:
        # Convert input values to numpy array
        input_values = [float(arg) for arg in args]

        # Reshape to (1, n_features)
        input_array = np.array(input_values).reshape(1, -1)

        # Use the model for inference
        prediction_proba = rf_model.predict_proba(input_array)
        prediction = rf_model.predict(input_array)

        # Get the confidence of the prediction being class 1 (probability)
        confidence = prediction_proba[0][1]

        # Return the result
        if prediction[0] == 1:
            return f"Prediction: 1\nConfidence: {confidence:.2f}"
        else:
            return f"Prediction: 0\nConfidence: {confidence:.2f}"
    except Exception as e:
        return f"Inference error: {str(e)}"


# Create Gradio interface
inputs = [
    gr.Number(value=v[0], label=k, minimum=v[0], maximum=v[1], step=v[2])
    for k, v in features.items()
]  # Dynamically generate input components
outputs = gr.Textbox(label="Inference Result")  # Output component

# create queue
interface = gr.Interface(
    fn=predict,  # Inference function
    inputs=inputs,  # Dynamically generated input components
    outputs=outputs,  # Output component
    title="Random Forest Model Inference",  # Interface title
    description="Input feature values to get the model's inference result.",  # Interface description
    live=False,  # Whether to update in real-time (set to False, requires button click)
    flagging_mode="never",
)

# Enable queue
interface.queue()

# Launch the app
interface.launch(share=True)