harbor run -t LiteCoder/cpu-bertopic-hdbscan-pipelineharbor download LiteCoder/cpu-bertopic-hdbscan-pipelineBuild a BERTopic topic modeling pipeline that uses Hugging Face sentence-transformer embeddings and HDBSCAN clustering, running entirely on CPU, to organize synthetic computer science paper abstracts into coherent topics.
bertopic, hdbscan, sentence-transformers, scikit-learn| File | Purpose |
|---|---|
/app/generate_data.py |
Generate the synthetic dataset |
/app/data/abstracts.json |
Generated dataset |
/app/train.py |
Train the BERTopic model and produce all outputs |
/app/output/results.json |
Training results and topic info |
/app/output/model/ |
Saved BERTopic model directory |
/app/predict.py |
Demo script for single-document inference |
/app/output/prediction.json |
Prediction output from demo script |
generate_data.py)Generate a synthetic dataset of computer science paper abstracts and write it to /app/data/abstracts.json.
Requirements:
[
{
"id": 0,
"title": "string",
"abstract": "string",
"domain": "string"
}
]
train.py)Read /app/data/abstracts.json, train a BERTopic model, and write outputs.
Configuration constraints:
all-MiniLM-L6-v2 or similar) configured for CPU.n_components=5.min_cluster_size=10.Output — /app/output/results.json must contain:
{
"num_topics": <int>,
"topics": [
{
"topic_id": <int>,
"count": <int>,
"top_words": ["word1", "word2", "word3", "word4", "word5"],
"representative_doc_ids": [<int>, ...]
}
],
"topic_assignments": [<int>, ...],
"outlier_count": <int>
}
Field details:
num_topics: total number of discovered topics excluding the outlier topic (-1).topics: one entry per discovered topic (including -1 for outliers). Each entry has the topic id, document count, top-5 representative words, and up to 3 representative document ids (by index in the input list).topic_assignments: list of length 200 mapping each document index to its assigned topic id.outlier_count: number of documents assigned to topic -1.Save the trained model to /app/output/model/ using BERTopic's built-in save method so it can be reloaded.
predict.py)Load the saved model from /app/output/model/ and predict the topic for the following hard-coded input text:
Deep reinforcement learning has shown remarkable success in game playing and robotic control tasks, combining neural network function approximation with temporal difference learning methods.
Write the result to /app/output/prediction.json:
{
"input_text": "<the input string>",
"predicted_topic": <int>,
"topic_words": ["word1", "word2", ...],
"all_topic_probabilities": { "<topic_id>": <float>, ... }
}
predicted_topic: the integer topic id assigned.topic_words: top-5 words for the predicted topic.all_topic_probabilities: a mapping from topic id (string key) to probability (float value). If the model does not support probabilities, set this to an empty object {}.pip install bertopic sentence-transformers hdbscan umap-learn
python /app/generate_data.py
python /app/train.py
python /app/predict.py
Each script must be independently runnable in the order above and must exit with code 0 on success.