Spaces:
Runtime error
Runtime error
Update chest_utils.py
Browse files- chest_utils.py +22 -19
chest_utils.py
CHANGED
|
@@ -1,47 +1,50 @@
|
|
| 1 |
import joblib
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
chest_model = joblib.load("chest_model.joblib")
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
condition_questions = {
|
| 9 |
-
"Asthma": "Do you have shortness of breath or chest tightness?",
|
| 10 |
-
"Bronchiectasis": "Have you been experiencing chronic cough with sputum?",
|
| 11 |
-
"COPD": "Are you experiencing difficulty breathing and chronic cough?",
|
| 12 |
-
"Lung Cancer": "Have you noticed unexplained weight loss or coughing blood?",
|
| 13 |
-
"Pneumonia": "Do you have a fever, chills, and chest pain?",
|
| 14 |
-
"Tuberculosis": "Have you had a persistent cough for more than 3 weeks?"
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
-
# Helper encoding functions
|
| 18 |
def encode_gender(gender):
|
| 19 |
return 0 if gender == "Male" else 1
|
| 20 |
|
| 21 |
def encode_view_position(position):
|
| 22 |
return 0 if position == "PA" else 1
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
def predict_chest(
|
| 26 |
"""
|
| 27 |
Parameters:
|
| 28 |
-
- image: uploaded X-ray (optional for future model use)
|
| 29 |
- age: int
|
| 30 |
- gender: 'Male' or 'Female'
|
| 31 |
- view_position: 'PA' or 'AP'
|
| 32 |
- conditions: list of 10 binary values (0 or 1)
|
| 33 |
|
| 34 |
Returns:
|
| 35 |
-
-
|
| 36 |
"""
|
| 37 |
gender_encoded = encode_gender(gender)
|
| 38 |
position_encoded = encode_view_position(view_position)
|
| 39 |
-
|
| 40 |
if len(conditions) != 10:
|
| 41 |
raise ValueError("Expected 10 binary values for conditions.")
|
| 42 |
|
| 43 |
-
# Combine all features
|
| 44 |
features = np.array([[age, gender_encoded, position_encoded] + conditions])
|
| 45 |
-
|
| 46 |
prediction = chest_model.predict(features)[0]
|
|
|
|
| 47 |
return "Chest Disease Detected" if prediction == 1 else "No Chest Disease Detected"
|
|
|
|
| 1 |
import joblib
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
+
# 10 condition-based questions
|
| 5 |
+
condition_questions = [
|
| 6 |
+
"Do you have a cough?",
|
| 7 |
+
"Do you feel shortness of breath?",
|
| 8 |
+
"Are you experiencing chest pain?",
|
| 9 |
+
"Do you smoke?",
|
| 10 |
+
"Do you have a fever?",
|
| 11 |
+
"Do you have fatigue?",
|
| 12 |
+
"Have you had recent respiratory infection?",
|
| 13 |
+
"Do you have a family history of lung issues?",
|
| 14 |
+
"Do you feel wheezing or noisy breathing?",
|
| 15 |
+
"Have you been exposed to pollution or chemicals recently?"
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
# Load the trained model
|
| 19 |
chest_model = joblib.load("chest_model.joblib")
|
| 20 |
|
| 21 |
+
# Encoding helpers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def encode_gender(gender):
|
| 23 |
return 0 if gender == "Male" else 1
|
| 24 |
|
| 25 |
def encode_view_position(position):
|
| 26 |
return 0 if position == "PA" else 1
|
| 27 |
|
| 28 |
+
# Main prediction function
|
| 29 |
+
def predict_chest(age, gender, view_position, conditions):
|
| 30 |
"""
|
| 31 |
Parameters:
|
|
|
|
| 32 |
- age: int
|
| 33 |
- gender: 'Male' or 'Female'
|
| 34 |
- view_position: 'PA' or 'AP'
|
| 35 |
- conditions: list of 10 binary values (0 or 1)
|
| 36 |
|
| 37 |
Returns:
|
| 38 |
+
- prediction string
|
| 39 |
"""
|
| 40 |
gender_encoded = encode_gender(gender)
|
| 41 |
position_encoded = encode_view_position(view_position)
|
| 42 |
+
|
| 43 |
if len(conditions) != 10:
|
| 44 |
raise ValueError("Expected 10 binary values for conditions.")
|
| 45 |
|
| 46 |
+
# Combine all features
|
| 47 |
features = np.array([[age, gender_encoded, position_encoded] + conditions])
|
|
|
|
| 48 |
prediction = chest_model.predict(features)[0]
|
| 49 |
+
|
| 50 |
return "Chest Disease Detected" if prediction == 1 else "No Chest Disease Detected"
|