virtualaidressing / HF_Deployment_Report.md
ammar101's picture
Deploy application code and models
0bb49b0

Hugging Face Deployment Report

1. Project Analysis & What Was Checked

I have comprehensively analyzed the project specifically targeting a deployment to Hugging Face Spaces (Docker Plan). The free tier of Hugging Face Spaces provides up to 16GB of RAM, which is excellent to handle the YOLOv8 ML models yolov8n-pose.pt and pose_landmarker.task without crashing.

I reviewed the files you had already created:

  • Dockerfile: The existing Dockerfile correctly used the python:3.11-slim base image and properly installed the OpenCV system dependencies (libgl1, libglib2.0-0), which was perfect!
  • .dockerignore: Found that it correctly ignores virtual environments, caches, .env file, etc.
  • config/settings.py: The core of the Django settings.
  • requirements.txt: The python dependencies.

2. What I Updated

To make the deployment strictly compliant with Hugging Face's Docker Spaces architecture, I updated the following files:

Dockerfile

  • Added useradd -m -u 1000 user: Hugging Face runs their containers natively as a non-root user (id 1000). To avoid "Permission Denied" errors when running python manage.py collectstatic, an exact user needed to be defined.
  • Added --chown=user: Ensured that the newly created user specifically owns the copied files and installed directories.

requirements.txt

  • Added gunicorn==21.2.0: Your production server framework.
  • Added whitenoise==6.6.0: Required so your Django app can seamlessly serve static files without requiring an external Nginx server.

config/settings.py

  • Security & Hosts: Replaced ALLOWED_HOSTS = [] with ALLOWED_HOSTS = ['*'] so Hugging Face infrastructure can correctly host your domain.
  • Form Submissions: Added CSRF_TRUSTED_ORIGINS = ['https://*.hf.space'] so POST requests (like uploading images inside the HF IFrame) are not rejected with 403 Forbidden errors.
  • Static Files: Confirmed the integration of whitenoise.middleware.WhiteNoiseMiddleware.

3. Hugging Face Deployment Steps

You are now fully ready to deploy. Follow these precise steps:

  1. Create the Space: Go to Hugging Face Spaces and click Create new Space.
  2. Space Settings:
    • Name: virtual-dressing (or similar).
    • License: Optional (e.g., MIT).
    • Select the Space SDK: Choose Docker, then choose the Blank template.
    • Space Hardware: Select standard Free tier (16GB RAM, 2 vCPU).
  3. Upload Files: Connect your local repository to the Space using git remote add huggingface <YOUR_HF_CLONE_URL>, or manually upload all the project files into the Space's Files tab.
  4. Environment Secrets: Once the files are uploaded, go to the Space's Settings tab. Scroll down to the Variables and secrets section and create New Secrets:
    • SECRET_KEY: Enter a random hard-to-guess string.
    • DEBUG: False
    • GEMINI_API_KEY: Your Google AI API key.
  5. View Logs: Wait for the container to say "Building". You can click on the 'Logs' button on the top right to watch Gunicorn boot up on port 7860.
  6. Once it says "Running", your Virtual Dressing app is deployed and live!

Note regarding ephemeral storage: Any photos users upload or models saved to the SQLite DB will wipe when the Space goes to sleep (usually after 48h of user inactivity). To make the database persist, you'll want to configure Hugging Face Persistent Storage via the settings later.

4. Current Core Project Structure

Your project is tightly structured for Docker deployment:

virtual_dressing/
β”œβ”€β”€ Dockerfile                  <-- Orchestrates the production setup
β”œβ”€β”€ .dockerignore               <-- Keeps Docker image small and secure
β”œβ”€β”€ requirements.txt            <-- Specifies needed dependencies
β”œβ”€β”€ manage.py                   
β”œβ”€β”€ db.sqlite3                  <-- Database
β”œβ”€β”€ yolov8n-pose.pt             <-- Heavy YOLO Weights
β”œβ”€β”€ pose_landmarker.task        <-- MediaPipe Task weights
β”œβ”€β”€ appDataDir/                  <-- Optional AI context & configuration
β”œβ”€β”€ config/                     <-- Core Django Settings
β”‚   └── settings.py             <-- Configured for HF / Whitenoise
β”œβ”€β”€ fitting_system/             <-- Main App Logic
β”‚   β”œβ”€β”€ ai_modules/             <-- Integration with Gemini and YOLO
β”‚   β”œβ”€β”€ templates/              <-- HTML views (scan.html, store.html)
β”‚   β”œβ”€β”€ static/                 <-- CSS / JS styles & scripts
β”‚   β”œβ”€β”€ models.py
β”‚   └── views.py                
└── locale/                     <-- Translations (Arabic/English)