| | import gradio as gr
|
| | import numpy as np
|
| | import joblib
|
| |
|
| |
|
| | 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],
|
| | }
|
| |
|
| |
|
| |
|
| | def predict(*args):
|
| | try:
|
| |
|
| | input_values = [float(arg) for arg in args]
|
| |
|
| |
|
| | input_array = np.array(input_values).reshape(1, -1)
|
| |
|
| |
|
| | prediction_proba = rf_model.predict_proba(input_array)
|
| | prediction = rf_model.predict(input_array)
|
| |
|
| |
|
| | confidence = prediction_proba[0][1]
|
| |
|
| |
|
| | 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)}"
|
| |
|
| |
|
| |
|
| | inputs = [
|
| | gr.Number(value=v[0], label=k, minimum=v[0], maximum=v[1], step=v[2])
|
| | for k, v in features.items()
|
| | ]
|
| | outputs = gr.Textbox(label="Inference Result")
|
| |
|
| |
|
| | interface = gr.Interface(
|
| | fn=predict,
|
| | inputs=inputs,
|
| | outputs=outputs,
|
| | title="Random Forest Model Inference",
|
| | description="Input feature values to get the model's inference result.",
|
| | live=False,
|
| | flagging_mode="never",
|
| | )
|
| |
|
| |
|
| | interface.queue()
|
| |
|
| |
|
| | interface.launch(share=True)
|
| |
|