LiXiang12 commited on
Commit
e56940d
·
verified ·
1 Parent(s): e6bfbf1
Files changed (3) hide show
  1. app.py +71 -0
  2. requirements.txt +3 -0
  3. rf_model.pkl +3 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import joblib
4
+
5
+ # Load the model
6
+ rf_model = joblib.load("rf_model.pkl")
7
+
8
+ features = {
9
+ "CD4": [0, 500, 1],
10
+ "AST/ALT": [0, 100, 1],
11
+ "ALT": [0, 2000, 1],
12
+ "Hb": [1, 150, 1],
13
+ "CRP": [1, 500, 1],
14
+ "ALB": [10, 50, 1],
15
+ "POAL": [0, 1, 1],
16
+ "ALC": [0, 5, 1],
17
+ "Age (years)": [12, 100, 1],
18
+ "WBC": [0, 20, 1],
19
+ "PLT": [1, 800, 1],
20
+ "AST": [0, 2000, 1],
21
+ }
22
+
23
+
24
+ # Define the inference function
25
+ def predict(*args):
26
+ try:
27
+ # Convert input values to numpy array
28
+ input_values = [float(arg) for arg in args]
29
+
30
+ # Reshape to (1, n_features)
31
+ input_array = np.array(input_values).reshape(1, -1)
32
+
33
+ # Use the model for inference
34
+ prediction_proba = rf_model.predict_proba(input_array)
35
+ prediction = rf_model.predict(input_array)
36
+
37
+ # Get the confidence of the prediction being class 1 (probability)
38
+ confidence = prediction_proba[0][1]
39
+
40
+ # Return the result
41
+ if prediction[0] == 1:
42
+ return f"Prediction: 1\nConfidence: {confidence:.2f}"
43
+ else:
44
+ return f"Prediction: 0\nConfidence: {confidence:.2f}"
45
+ except Exception as e:
46
+ return f"Inference error: {str(e)}"
47
+
48
+
49
+ # Create Gradio interface
50
+ inputs = [
51
+ gr.Number(value=v[0], label=k, minimum=v[0], maximum=v[1], step=v[2])
52
+ for k, v in features.items()
53
+ ] # Dynamically generate input components
54
+ outputs = gr.Textbox(label="Inference Result") # Output component
55
+
56
+ # create queue
57
+ interface = gr.Interface(
58
+ fn=predict, # Inference function
59
+ inputs=inputs, # Dynamically generated input components
60
+ outputs=outputs, # Output component
61
+ title="Random Forest Model Inference", # Interface title
62
+ description="Input feature values to get the model's inference result.", # Interface description
63
+ live=False, # Whether to update in real-time (set to False, requires button click)
64
+ flagging_mode="never",
65
+ )
66
+
67
+ # Enable queue
68
+ interface.queue()
69
+
70
+ # Launch the app
71
+ interface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ joblib
2
+ gradio
3
+ numpy
rf_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b84b76ac8818fc3243df79eb164e4e878a5704de54699574d89f6ed5b524a7cb
3
+ size 2015641