zeynepbkn01/Smoker_Detection_Based_On_Body_Signals
1
app.py - Gradio Interface for Smoking Prediction
This document explains how the `app.py` file works for Hugging Face Spaces deployment, its user interface, and model integration.
๐ Required Files for app.py
โโโ app.py # Main Gradio application
โโโ optimized_random_forest_model.pkl # Trained model (246MB)
โโโ preprocessor.pkl # Data preprocessing pipeline (2.7KB)
โโโ requirements.txt # Dependencies๐ง How app.py Works
1. Model Loading Process
# On startup, app.py loads both pkl files:
model = joblib.load('optimized_random_forest_model.pkl') # Random Forest classifier
preprocessor = joblib.load('preprocessor.pkl') # Data preprocessing pipeline2. Data Processing Pipeline
When user submits health data:
- Input Collection: Gradio form collects 24 health parameters
- Data Preprocessing:
preprocessor.pkltransforms raw inputs (scaling + encoding) - Prediction:
optimized_random_forest_model.pklgenerates smoking probability - Explanation: LIME analyzes which features influenced the prediction
3. Where PKL Files Are Used
๐ฅ๏ธ User Interface Overview
Input Form (24 Health Parameters)
The Gradio interface provides:
Demographic Inputs:
- Age (20-85), Gender (M/F), Height (130-190cm), Weight (30-135kg), Waist (51-129cm)
Health Measurements:
- Blood Pressure: Systolic (71-240), Diastolic (40-146)
- Blood Chemistry: Cholesterol (55-445), Triglycerides (8-999), HDL (4-618), LDL (1-1860)
- Liver Function: AST (6-1311), ALT (1-2914), GTP (1-999)
- Other: Hemoglobin (4.9-21.1), Blood Sugar (46-505), Creatinine (0.1-11.6)
- Sensory: Vision L/R (0.1-9.9), Hearing L/R (1.0-2.0)
- Dental: Tartar (Y/N), Caries (0/1), Urine Protein (1-6)
Output Display
Prediction Results:
- Primary prediction: "Smoker" or "Non-Smoker"
- Confidence percentages for both classes
- Color-coded risk assessment
LIME Explanation:
- Top 5 most influential features for this specific prediction
- Shows which health indicators contributed to the decision
๐ Prediction Workflow
def make_prediction(age, gender, height, weight, ...):
# 1. Create a DataFrame from user inputs
input_data = pd.DataFrame({...})
# 2. Transform data using preprocessor.pkl
processed_data = preprocessor.transform(input_data)
# 3. Generate prediction using model.pkl
prediction_proba = model.predict_proba(processed_data)
prediction = model.predict(processed_data)
# 4. Create LIME explanation
lime_explanation = get_instance_feature_importance_lime(processed_data)
# 5. Return formatted results
return prediction_result, confidence_score, lime_features๐ก Key Features of app.py
Error Handling
- Model Loading: Graceful failure if pkl files are missing
- Input Validation: Checks for valid ranges and data types
- LIME Fallback: Uses global feature importance if LIME fails
LIME Integration
- Dynamic Explainer: Initializes LIME explainer on first prediction
- Feature Importance: Shows top 5 features affecting each prediction
- Personalized Insights: Different explanations for different health profiles
Cloud Optimization
- Logging: Comprehensive error logging for Hugging Face Spaces
- Memory Efficient: Loads models once, reuses for all predictions
- Performance Tracking: Built-in timing for prediction performance
๐ Interface Layout
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ INPUT SECTION โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Age: [slider] Gender: [dropdown] โ
โ Height: [slider] Weight: [slider] โ
โ Blood Pressure: [slider] [slider] โ
โ Cholesterol: [slider] HDL: [slider] โ
โ ... (24 total parameters) โ
โ โ
โ [Predict Smoking Status] โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ OUTPUT SECTION โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Result: SMOKER (78.5% confidence) โ
โ Non-Smoker: 21.5% | Smoker: 78.5% โ
โ โ
โ Most Influential Features: โ
โ 1. hemoglobin (15.2) โ
โ 2. gender_M (1.0) โ
โ 3. triglyceride (245) โ
โ 4. height(cm) (175) โ
โ 5. Gtp (45) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโก Performance
- Model Loading: ~5-10 seconds on first load
- Prediction Time: ~1-2 seconds per prediction
- Memory Usage: ~300MB (including both pkl files)
- LIME Explanation: ~2-3 seconds additional processing
๐ง Technical Notes
Model Dependencies
Both pkl files must be present in the same directory as app.py for successful operation.
Data Preprocessing
The preprocessor.pkl ensures that user inputs are transformed exactly as they were during model training, maintaining prediction accuracy.
LIME Explanations
Local explanations help users understand which specific health factors led to their smoking prediction, making the AI decision transparent.
License
MIT License
