fendy07's picture
Add model, gradio app, and documentation files
177e42b

A newer version of the Gradio SDK is available: 6.14.0

Upgrade
metadata
title: Prediksi Karyawan Resign
emoji: πŸƒ
colorFrom: red
colorTo: blue
sdk: gradio
sdk_version: 6.2.0
app_file: gradio_app.py
pinned: false
short_description: This space about employee resignation prediction using ML

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference

πŸš€ Deployment Guide - Gradio App

Overview

Panduan lengkap untuk deploy HR Analytics Resignation Prediction Model menggunakan Gradio Web Interface.

πŸ“¦ File Structure

Setelah training model, Anda harus memiliki:

deployment/
β”œβ”€β”€ gradio_app.py                    # ← Main app (Full features)
β”œβ”€β”€ gradio_app_simple.py             # ← Simple version
β”œβ”€β”€ best_model_RF_SMOTE.pkl          # ← Trained model
β”œβ”€β”€ scaler.pkl                       # ← Feature scaler
β”œβ”€β”€ label_encoders.pkl               # ← Categorical encoders
β”œβ”€β”€ target_encoder.pkl               # ← Target encoder (for full app)
β”œβ”€β”€ requirements_gradio.txt          # ← Dependencies
└── README_DEPLOYMENT.md             # ← This file

🎯 Two Deployment Options

Option 1: Simple App (Recommended for Quick Start)

  • βœ“ Single employee prediction only
  • βœ“ Simple, clean interface
  • βœ“ Easy to understand
  • βœ“ Perfect for demos

File: gradio_app_simple.py

Option 2: Full App (Recommended for Production)

  • βœ“ Single employee prediction
  • βœ“ Batch prediction (CSV upload)
  • βœ“ Advanced visualizations
  • βœ“ Model information tab
  • βœ“ User guide tab
  • βœ“ Downloadable results

File: gradio_app.py


πŸ“ Step-by-Step Deployment

Step 1: Train Your Model

Jalankan notebook terlebih dahulu untuk generate model files:

jupyter notebook HR_Analytics_Dataset_HR_FINAL.ipynb

Required outputs:

  • best_model_RF_SMOTETomek.pkl
  • scaler.pkl
  • label_encoders.pkl
  • target_encoder.pkl (optional untuk simple app)

Step 2: Install Gradio Dependencies

pip install gradio plotly

Atau install semua:

pip install -r requirements.txt

Step 3: Verify Files

Pastikan semua file ada di folder yang sama:

ls -la
# Output harus menunjukkan:
# - gradio_app.py atau gradio_app_simple.py
# - best_model_RF_SMOTE.pkl
# - scaler.pkl
# - label_encoders.pkl

Step 4: Run Gradio App

A. Simple Version:

python gradio_simple.py

B. Full Version:

python gradio_app.py

Step 5: Access the App

Setelah running, Gradio akan menampilkan:

Running on local URL:  http://127.0.0.1:7860
Running on public URL: https://xxxxx.gradio.live

To create a permanent link, use `share=True`

Local Access:

  • Buka browser
  • Go to http://127.0.0.1:7860

Public Access:

  • Share link https://xxxxx.gradio.live ke team
  • Link valid 72 jam
  • Anyone dengan link bisa akses

🌐 Deployment Options

Option A: Local Development (Quick Testing)

app.launch()  # Default: local only

Pros:

  • Instant deployment
  • No setup needed
  • Perfect for testing

Cons:

  • Only accessible from your computer
  • Stops when you close terminal

Option B: Temporary Public Link (Share with Team)

app.launch(share=True)  # Creates public link

Pros:

  • Anyone can access with link
  • Great for demos/presentations
  • No infrastructure needed

Cons:

  • Link expires in 72 hours
  • Not suitable for production
  • Limited to Gradio's free tier

Option C: Gradio Spaces (Free Hosting) ⭐ RECOMMENDED

Hugging Face Spaces provides free hosting for Gradio apps!

Steps:

  1. Create account di huggingface.co

  2. Create new Space:

    • Go to huggingface.co/spaces
    • Click "Create new Space"
    • Name: "hr-analytics-resign-prediction"
    • SDK: Gradio
    • Make it Public or Private
  3. Upload files:

    Space repository/
    β”œβ”€β”€ app.py                        # Rename gradio_app.py to app.py
    β”œβ”€β”€ requirements.txt              # Gradio dependencies
    β”œβ”€β”€ best_model_RF_SMOTE.pkl
    β”œβ”€β”€ scaler.pkl
    β”œβ”€β”€ label_encoders.pkl
    └── target_encoder.pkl
    
  4. Configure requirements.txt:

    gradio
    plotly
    pandas
    numpy
    scikit-learn
    
  5. Push to Space:

    git clone https://huggingface.co/spaces/YOUR_USERNAME/hr-analytics-resign-prediction
    cd hr-analytics-resign-prediction
    cp gradio_app.py app.py
    cp best_model_RF_SMOTE.pkl .
    cp scaler.pkl .
    cp label_encoders.pkl .
    cp target_encoder.pkl .
    git add .
    git commit -m "Initial deployment"
    git push
    
  6. Access your app:

    • URL: https://huggingface.co/spaces/YOUR_USERNAME/hr-analytics-resign-prediction
    • Permanent link!
    • Free hosting!

Pros:

  • βœ… Free hosting
  • βœ… Permanent link
  • βœ… SSL certificate
  • βœ… Easy updates via git
  • βœ… Community support

Cons:

  • Public by default (use Private if needed)
  • Storage limits (5GB free)

Option D: Cloud Deployment (Production)

D1. AWS EC2

# 1. Launch EC2 instance (Ubuntu)
# 2. SSH into instance
ssh -i your-key.pem ubuntu@your-ec2-ip

# 3. Install dependencies
sudo apt update
sudo apt install python3-pip
pip3 install gradio plotly pandas numpy scikit-learn

# 4. Upload files
scp -i your-key.pem *.pkl ubuntu@your-ec2-ip:~/
scp -i your-key.pem gradio_app.py ubuntu@your-ec2-ip:~/

# 5. Run app
python3 gradio_app.py

# 6. Access via EC2 public IP
# http://your-ec2-ip:7860

Cost: ~$10-30/month (t2.micro - t2.medium)


D2. Google Cloud Run (Containerized)

  1. Create Dockerfile:
FROM python:3.9-slim

WORKDIR /app

COPY requirements_gradio.txt .
RUN pip install -r requirements_gradio.txt

COPY . .

CMD ["python", "gradio_app.py"]
  1. Deploy:
gcloud run deploy hr-analytics \
  --source . \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated

Cost: Pay per use (~$5-20/month)


D3. Heroku (Simple PaaS)

  1. Create Procfile:
web: python gradio_app.py
  1. Deploy:
heroku login
heroku create hr-analytics-app
git push heroku main

Cost: ~$7/month (Hobby tier)


D4. DigitalOcean App Platform

  1. Go to DigitalOcean App Platform
  2. Connect GitHub repo
  3. Select Python
  4. Add buildpack
  5. Deploy!

Cost: $5-12/month


πŸ”’ Security Considerations

1. Authentication (Recommended for Production)

Add Gradio authentication:

app.launch(
    auth=("admin", "your_secure_password"),
    share=False
)

Or use environment variables:

import os

username = os.getenv("GRADIO_USERNAME")
password = os.getenv("GRADIO_PASSWORD")

app.launch(
    auth=(username, password),
    share=False
)

2. HTTPS/SSL

For production, always use HTTPS:

  • Hugging Face Spaces: βœ… Built-in SSL
  • Cloud providers: Configure SSL certificate
  • Local: Use nginx reverse proxy

3. Data Privacy

# Don't log sensitive data
# Don't store user inputs permanently
# Clear outputs after session

4. Rate Limiting

Implement rate limiting to prevent abuse:

from gradio_client import Client

# Limit requests per user

🎨 Customization

Change Theme

with gr.Blocks(theme=gr.themes.Soft()) as app:
    # Your interface

Available themes:

  • gr.themes.Soft()
  • gr.themes.Base()
  • gr.themes.Glass()
  • gr.themes.Monochrome()

Custom CSS

css = """
.gradio-container {
    font-family: 'Arial', sans-serif;
}
.button {
    background-color: #4CAF50;
}
"""

with gr.Blocks(css=css) as app:
    # Your interface

Add Logo

gr.Image("company_logo.png", height=100, width=200)

πŸ“Š Monitoring & Analytics

Option 1: Built-in Analytics

Gradio provides basic analytics:

  • Page views
  • User interactions
  • Error rates

Access via Spaces dashboard.

Option 2: Custom Logging

import logging

logging.basicConfig(
    filename='app.log',
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

def predict_employee(...):
    logging.info(f"Prediction requested: {divisi}, {gaji}")
    # Your code
    logging.info(f"Result: {resign_prob}%")

Option 3: Google Analytics

Add GA tracking code to custom HTML.


πŸ› Troubleshooting

Problem: "Model file not found"

Solution:

# Check current directory
pwd

# List files
ls -la

# Verify .pkl files exist
ls *.pkl

Problem: "Module 'gradio' not found"

Solution:

pip install gradio plotly

Problem: "Port 7860 already in use"

Solution:

app.launch(server_port=7861)  # Change port

Problem: App is slow

Solutions:

  1. Use smaller model (reduce n_estimators)
  2. Implement caching
  3. Use faster instance type
  4. Optimize preprocessing

Problem: Public link expired

Solutions:

  1. Deploy to Hugging Face Spaces (permanent)
  2. Use cloud hosting
  3. Set up your own server

πŸ“ˆ Performance Optimization

1. Model Optimization

# Reduce model size
import joblib
joblib.dump(model, 'model.pkl', compress=3)

2. Caching

from functools import lru_cache

@lru_cache(maxsize=100)
def predict_cached(...):
    # Prediction logic

3. Async Processing

For batch predictions:

import asyncio

async def predict_batch_async(file):
    # Async processing

πŸ”„ Updates & Maintenance

Update Model

  1. Retrain model with new data
  2. Generate new .pkl files
  3. Replace old files
  4. Restart app
# If on Spaces
git add *.pkl
git commit -m "Update model"
git push

Update UI

  1. Edit gradio_app.py
  2. Test locally
  3. Deploy changes

Monitor Performance

  • Track prediction accuracy over time
  • Collect user feedback
  • A/B test different models
  • Update based on business needs

πŸ“ž Support & Resources

Official Documentation

Community

Troubleshooting

  • Check GitHub issues
  • Stack Overflow
  • Gradio Slack community

βœ… Deployment Checklist

Before deploying to production:

  • Model trained and tested (F1 > 0.90)
  • All .pkl files generated
  • Gradio app tested locally
  • Authentication configured
  • Error handling implemented
  • Logging configured
  • Documentation updated
  • User guide included
  • Security reviewed
  • Performance tested
  • Backup plan in place
  • Monitoring setup
  • Team trained on usage
  • Stakeholders notified

πŸŽ“ Next Steps

  1. Deploy to Hugging Face Spaces (Easiest, FREE)
  2. Add authentication for security
  3. Set up monitoring to track usage
  4. Collect feedback from users
  5. Iterate and improve based on data

🌟 Best Practices

  1. Keep it simple - Start with simple version, add features as needed
  2. Test thoroughly - Test with edge cases before deploying
  3. Document everything - Help users understand how to use it
  4. Monitor actively - Track errors and usage patterns
  5. Update regularly - Retrain model with new data quarterly
  6. Secure properly - Always use authentication in production
  7. Backup frequently - Keep copies of model files

Happy Deploying! πŸš€

Need help? Check the troubleshooting section or reach out to the community!