Skip to main content

pyHercules

PyPI version Python Version License: MIT

pyHercules is a flexible Python framework for hierarchical clustering of text, numeric, or image data. The core algorithm, Hercules, uses hierarchical clustering algorithms (like recursive k-means or agglomerative clustering) and leverages Large Language Models (LLMs) for efficient and meaningful summarization of clusters at each level of the hierarchy. The project includes the core library (pyhercules), a set of "batteries-included" model functions, and a powerful Dash web application for interactive exploration.

Key Features

  • Hierarchical Clustering: Automatically builds a tree of clusters from your data.
  • Choice of Clustering Algorithm: Select between 'kmeans' (default) for iterative partitioning or 'agglomerative' clustering for a bottom-up hierarchical approach.
  • Multi-Modal: Natively handles text, numeric (NumPy, Pandas), and image data (file paths, URLs, PIL Images). (One modality at a time.)
  • LLM-Powered Summarization: Uses Large Language Models (LLMs) to generate human-readable titles and descriptions for each cluster.
  • Flexible Representation: For k-means, choose between direct mode (using original data embeddings) or description mode (using LLM-generated summary embeddings) for clustering at higher levels.
  • Interactive Web App: An included Dash application (pyhercules_app.py) allows for easy data upload, parameter configuration, and visualization of clustering results.
  • Extensible: The core library is dependency-light. Bring your own model functions or use the provided ones in pyhercules_functions.py.

Project Structure

  • pyhercules.py: The core clustering library. Contains the Hercules and Cluster classes.
  • pyhercules_functions.py: A collection of ready-to-use functions for embedding, captioning, and LLM calls (using Hugging Face, Google Gemini, etc.).
  • pyhercules_app.py: A comprehensive Dash web application for interactive clustering and visualization.
  • examples.ipynb: A Jupyter Notebook demonstrating various use cases of the library.
  • requirements-*.txt: Dependency files for different use cases (for reference).
  • setup.py: The packaging configuration script.

Installation

You can install pyhercules directly from PyPI. Several installation options are available depending on your needs.

1. Core Library Only

For using the Hercules class with your own model client functions. This is a minimal, lightweight installation.

pip install pyhercules

2. Library with Model Functions

To use the pre-built functions in pyhercules_functions.py (e.g., for running the examples.ipynb notebook).

pip install "pyhercules[models]"

3. Full Web Application

To run the interactive Dash application, which includes all dependencies.

pip install "pyhercules[app]"

Configuration: API Keys

To use models from Google or gated models from Hugging Face (like Gemma), you must configure your API keys. The recommended way is to create a .env file in your project's working directory:

# .env
GOOGLE_API_KEY="your-google-api-key-here"
HUGGINGFACE_HUB_TOKEN="your-hugging-face-token-for-gated-models"

The library will automatically load these variables. Alternatively, you can set them as system environment variables.

Usage

1. Running the Dash Web Application (Recommended)

The easiest way to get started is with the interactive app.

  1. Install dependencies:
    pip install "pyhercules[app]"
    
  2. Set API keys: Create a .env file as described in the Configuration section.
  3. Run the app:
    pyhercules-app
    

Then, open your web browser to http://127.0.0.1:8050.

2. Using the Core Library in Python

You can use the Hercules class directly in your scripts. See examples.ipynb for more detailed use cases.

from pyhercules import Hercules
from pyhercules_functions import local_minilm_l6_v2_embedding, local_gemma_3_4b_it_llm

# 1. Sample data
sample_texts = [
    "Introduction to machine learning concepts.",
    "Advanced techniques in deep neural networks.",
    "A guide to Python programming for beginners.",
    "Web development using Flask and Jinja.",
    "Understanding gradient descent and backpropagation.",
]

# 2. Instantiate Hercules with your chosen model clients
# Ensure you have set up your HUGGINGFACE_HUB_TOKEN in a .env file for Gemma
hercules = Hercules(
    level_cluster_counts=[3, 2],  # Desired hierarchy: 3 top-level, then subdivide
    representation_mode="direct",
    text_embedding_client=local_minilm_l6_v2_embedding,
    llm_client=local_gemma_3_4b_it_llm,
    verbose=1
)

# 3. Run clustering
top_clusters = hercules.cluster(sample_texts, topic_seed="computer science topics")

# 4. Print results
if top_clusters:
    for cluster in top_clusters:
        cluster.print_hierarchy(indent_increment=2, print_level_0=False)

Citation

If you find pyhercules useful in your research, please consider citing our paper.

@article{petnehazi2026hercules,
  title   = {HERCULES: Hierarchical Embedding-based Recursive Clustering Using LLMs for Efficient Summarization},
  author  = {Petnehazi, Gabor and Aradi, Bernadett},
  journal = {International Journal of Cognitive Computing in Engineering},
  volume  = {7},
  pages   = {445--456},
  year    = {2026},
  doi     = {10.1016/j.ijcce.2026.04.002},
  url     = {https://www.sciencedirect.com/science/article/pii/S2666307426000057}
}

License

This project is licensed under the MIT License. See the LICENSE file for details.

Metadata

Release files for pyhercules 1.1.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 pyhercules 1.1.1
File Size Uploaded Provenance
pyhercules-1.1.1.tar.gz 90.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pyhercules 1.1.1
File Interpreter ABI Platform
pyhercules-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 181.3 kB

Release files / pyhercules-1.1.1.tar.gz

Download URL pyhercules-1.1.1.tar.gz
Size 90.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fca341df9954a9b04f7822796b27504a443cf7bf7add1e2af040c2f0d96069e6
BLAKE2b-256 checksum
How to use checksums
0163bbe7c30d7d62a79f19ff878afe00d41891643c4f78907d1cdab1b5b59744
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / pyhercules-1.1.1-py3-none-any.whl

Download URL pyhercules-1.1.1-py3-none-any.whl
Size 91.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a423a6d8d3c148349800c8188f160e678afd136d14e74eea58f97fbc2172fc58
BLAKE2b-256 checksum
How to use checksums
04bf2aa3414e0979f13f0afaaef0158348690e83a6eb2b43ac7b6e06ccc77480
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Security for pyhercules 1.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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