CoolFace
Apppublic

zeynepbkn01/Smoker_Detection_Based_On_Body_Signals

sourceHugging Facemitupdated 1y agoView on Hugging Face
1likes
App README

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

python
# 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 pipeline

2. Data Processing Pipeline

When user submits health data:

  1. 1.Input Collection: Gradio form collects 24 health parameters
  2. 2.Data Preprocessing: preprocessor.pkl transforms raw inputs (scaling + encoding)
  3. 3.Prediction: optimized_random_forest_model.pkl generates smoking probability
  4. 4.Explanation: LIME analyzes which features influenced the prediction

3. Where PKL Files Are Used

PKL FileUsed ForCalled When
preprocessor.pklData transformation (StandardScaler + OneHotEncoder)Every prediction
optimized_random_forest_model.pklSmoking status predictionEvery prediction

๐Ÿ–ฅ๏ธ 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

python
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