hari6677 commited on
Commit
bd46d4d
·
verified ·
1 Parent(s): 48e6459

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import numpy as np
3
+ import pandas as pd
4
+ import gradio as gr
5
+
6
+ # --- 1. Load Model and Features ---
7
+ # Ensure these two files are uploaded to your Hugging Face Space folder!
8
+ try:
9
+ model = joblib.load('ckd_model.joblib')
10
+ # This list ensures input fields are created and data is fed in the correct order (24 features)
11
+ FEATURE_COLUMNS = joblib.load('model_features.joblib')
12
+ except FileNotFoundError:
13
+ raise FileNotFoundError("Model or feature files not found. Ensure 'ckd_model.joblib' and 'model_features.joblib' are uploaded.")
14
+
15
+ # --- 2. Define the Prediction Function ---
16
+ def predict_ckd(*inputs):
17
+ """
18
+ Takes 24 inputs (features) from the Gradio interface and returns the prediction.
19
+ """
20
+ # Convert inputs (which come as a tuple from Gradio) into a NumPy array
21
+ # and reshape it to match the model's required input format (1 sample, 24 features)
22
+ input_data = np.array(inputs).reshape(1, -1)
23
+
24
+ # 1. Make the raw prediction (0 or 1)
25
+ prediction = model.predict(input_data)[0]
26
+
27
+ # 2. Get the probability for the "Not CKD" class (class 1)
28
+ # This is often more informative than just a 0/1 result
29
+ probability_not_ckd = model.predict_proba(input_data)[0][1]
30
+
31
+ # Determine the final output text and confidence
32
+ if prediction == 0:
33
+ result_text = "Positive for Chronic Kidney Disease (CKD)"
34
+ confidence = 1 - probability_not_ckd # Confidence in CKD
35
+ color = "red"
36
+ else: # prediction == 1
37
+ result_text = "Negative for Chronic Kidney Disease (CKD)"
38
+ confidence = probability_not_ckd # Confidence in Not CKD
39
+ color = "green"
40
+
41
+ # Return prediction with formatted confidence
42
+ return f"<h2 style='color: {color};'>Prediction: {result_text}</h2><p>Confidence: {confidence:.2f} ({confidence*100:.0f}%)</p>"
43
+
44
+
45
+ # --- 3. Create the Gradio Interface Components ---
46
+
47
+ # Dynamically generate the 24 input components based on the feature list
48
+ input_components = []
49
+ for feature in FEATURE_COLUMNS:
50
+ # Use a generic Textbox for input, as Gradio will automatically handle numeric types
51
+ # You might customize these later (e.g., using gr.Slider for age, gr.Radio for binary inputs)
52
+ input_components.append(gr.Textbox(label=feature.upper(), placeholder=f"Enter value for {feature}"))
53
+
54
+ # --- 4. Launch the Gradio Interface ---
55
+ # The output component is set to HTML to allow for colored text output
56
+ output_component = gr.HTML(label="Prediction Result")
57
+
58
+ # Combine the function, inputs, and outputs into a Gradio interface
59
+ iface = gr.Interface(
60
+ fn=predict_ckd,
61
+ inputs=input_components,
62
+ outputs=output_component,
63
+ title="CKD Prediction Model (Random Forest)",
64
+ description="Enter the 24 clinical parameters below to predict the risk of Chronic Kidney Disease. **Note: For binary features (yes/no, present/notpresent), use the encoded numerical values (0 or 1) used during training.**"
65
+ )
66
+
67
+ # Launch the app (Hugging Face Spaces runs this automatically)
68
+ iface.launch(share=False)