Skip to main content

Document Organizer using Hugging Face

This Python script helps you organize your documents into categorized folders using a pre-trained Hugging Face zero-shot classification model. It can classify documents based on their filenames and move them into relevant categories, or flag them for manual review if confidence is low.

Features

  • Zero-Shot Classification: Categorizes documents without needing prior training on your specific data, leveraging the facebook/bart-large-mnli model.

  • Customizable Categories: Easily define your own list of categories relevant to your document collection.

  • Filename-based Classification: Extracts a clean title from the filename for classification, removing noise like file extensions and ISBNs.

  • Confidence Thresholding: Documents with low classification confidence are moved to a special "Needs Manual Review" folder.

  • Dry Run Mode: Preview the organization structure and proposed actions before any files are moved or folders are created.

  • Optional Filename Filtering: Use regular expressions to process only specific types of files (e.g., only PDF documents).

  • GPU Acceleration (Optional): Automatically attempts to use a CUDA-enabled GPU for faster classification if PyTorch and a compatible GPU are detected.

Getting Started

Prerequisites

Before running the script, ensure you have Python 3.8+ installed. You'll also need to install the necessary Python libraries.

  1. Create a Virtual Environment (Recommended):

    python -m venv venv
    source venv/bin/activate  # On Windows: .\venv\Scripts\activate
    
  2. Install Dependencies:

    pip install -r requirements.txt
    

    requirements.txt content:

    transformers
    torch # Or tensorflow, depending on your preference and GPU setup
    
    • For GPU support: If you have an NVIDIA GPU, ensure you install PyTorch with CUDA support. Visit PyTorch's official website to get the exact pip command for your system and CUDA version (e.g., pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118).

Usage

  1. Place your documents: Put all the documents you want to organize into a single folder.

  2. Run the script: Navigate to the src directory (or the directory containing main_organizer.py) in your terminal and run the script.

    python main_organizer.py <path_to_your_documents_folder> [OPTIONS]
    

    Example (Dry Run):

    python main_organizer.py /path/to/my/documents --dry-run
    

    This will show you a tree-like structure of how your files would be organized without making any changes.

    Example (Live Run with PDF filter):

    python main_organizer.py /path/to/my/documents --filter-regex ".*\.pdf$"
    

    This will classify and move only .pdf files.

Command-Line Arguments

  • <target_folder> (positional argument): The full path to the folder containing your documents. This is a mandatory argument.

  • --dry-run: (Optional flag) If present, the script will only print the proposed organization structure and not move any files or create any folders. This is highly recommended for a first run.

  • --filter-regex <pattern>: (Optional argument) Provide a regular expression pattern to filter the filenames. Only files matching this regex will be processed. For example, ".*\\.txt$" to process only text files.

Configuration

You can customize the behavior of the organizer by modifying the following constants at the top of main_organizer.py:

  • CANDIDATE_LABELS: A list of strings representing the categories you want your documents to be organized into. Be as specific or general as needed for your collection.

  • MODEL_NAME: The name of the Hugging Face model used for zero-shot classification. facebook/bart-large-mnli is a good general-purpose model.

  • MIN_CONFIDENCE_THRESHOLD: A float between 0.0 and 1.0. If the model's confidence for the top predicted category is below this threshold, the document will be moved to the _Needs Manual Review_ folder.

  • NEEDS_REVIEW_FOLDER_NAME: The name of the folder where documents with low confidence or processing errors will be placed.

  • HYPOTHESIS_TEMPLATE: The template string used to frame the classification query to the model. Changing this (e.g., to "This book is about {}." or "The subject of this paper is {}." ) might influence classification accuracy.

How it Works

  1. Initialization: Loads the specified Hugging Face zero-shot classification model. It attempts to detect and utilize a CUDA-enabled GPU for faster processing.

  2. Planning Phase (plan_book_organization):

    • Scans the target_folder for files (optionally filtered by a regex).

    • For each file, it extracts a cleaned title (removing extensions, ISBNs, etc.).

    • It then uses the zero-shot classification model to determine the most likely category from your CANDIDATE_LABELS based on the cleaned title.

    • If the confidence score for the top category is below MIN_CONFIDENCE_THRESHOLD, the file is assigned to the _Needs Manual Review_ category.

    • This phase builds an in-memory dictionary representing the proposed final organization ({category: [filenames]}).

  3. Execution Phase (_execute_dry_run_organization or _execute_live_run_organization):

    • Dry Run: Prints the planned_organization dictionary in a clear, tree-like format, showing where each file would go. No file system changes are made.

    • Live Run: Creates the necessary category folders (if they don't exist) and then moves each file to its designated category folder. It includes checks to prevent overwriting existing files.

Contributing

Feel free to fork this repository, open issues, or submit pull requests to improve the script.

Metadata

Release files for ai-document-organizer 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ai-document-organizer 0.0.1
File Size Uploaded Provenance
ai_document_organizer-0.0.1.tar.gz 10.7 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ai-document-organizer 0.0.1
File Interpreter ABI Platform
ai_document_organizer-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.0 kB

Release files / ai_document_organizer-0.0.1.tar.gz

Download URL ai_document_organizer-0.0.1.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
85a420529a54f514919c29b397a443bef3f5cc1db36887ff30248bf456166e7d
BLAKE2b-256 checksum
How to use checksums
194b0619e7812e504a537a46e99b8a375686c95b82ccf7e9c15f516dd5a08d31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 25, 2025.

Transparency log

Release files / ai_document_organizer-0.0.1-py3-none-any.whl

Download URL ai_document_organizer-0.0.1-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f45ec71976c2c3bd234756b5294f914147de4ecb9a18b49c7bd75d5353156c4d
BLAKE2b-256 checksum
How to use checksums
d42a82bb2b94912fa2058bedb4ca61db94d36b756442efe887e1135c35a2b662
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 25, 2025.

Transparency log

Security for ai-document-organizer 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ai_document_organizer-0.0.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 25, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page