Custom ALBERT Text Classification with Keras
This project provides a custom implementation of text classification using ALBERT, LSTM, and custom Keras layers. It includes model loading, tokenization, predictions, and custom layers for handling class names and ALBERT embeddings.
Files Overview
model_loader.py
This module is responsible for loading a pre-trained Keras model with custom objects.
Functions:
load_custom_model(model_path): Loads the model with custom layersClassNamesLayerandBertLayer.
tokenizer_utils.py
This module provides utilities for loading a pre-trained ALBERT tokenizer and tokenizing input texts.
Functions:
load_tokenizer(): Loads the ALBERT tokenizer (albert-base-v2).tokenize_texts(texts, tokenizer, max_len): Tokenizes input texts and returns token IDs and attention masks.
predictions.py
This module handles model predictions and formatting the output. It provides utilities to make predictions and process them for output.
Functions:
get_classes_from_model(model): Retrieves class names from theClassNamesLayer.make_predictions(model, tokenizer, texts, max_len): Tokenizes the input texts and generates predictions from the model.process_predictions(text, predictions, tokenizer, classes, write_func): Processes the predictions and writes the formatted output using the specified write function.write_current_word(classes, write_func, pred_values, idx, current_word): Writes the predicted class for the current word to the output.write_predictions_to_file(file, text, predictions, tokenizer, classes): Writes predictions to a file.print_predictions(text, predictions, tokenizer, classes): Prints predictions to the console.
class_names_layer.py
This module defines the custom ClassNamesLayer used to store class names in the Keras model.
Class:
ClassNamesLayer: A custom Keras layer to handle class names. It stores the class names and can be serialized and restored.
bert_layer.py
This module defines the custom BertLayer used to integrate the pre-trained ALBERT model into a Keras model.
Class:
BertLayer: A custom Keras layer that loads and uses the ALBERT model (albert-base-v2) for embeddings.
Example Usage
from transformers import AlbertTokenizer
from model_loader import load_custom_model
from predictions import get_classes_from_model, make_predictions, print_predictions
from tokenizer_utils import load_tokenizer
# Load the pre-trained model
model = load_custom_model('path_to_model.keras')
# Load the ALBERT tokenizer
tokenizer = load_tokenizer()
# Example texts to classify
texts = ["This is a test sentence for classification"]
# Get class names from the model
classes = get_classes_from_model(model)
# Make predictions
predictions = make_predictions(model, tokenizer, texts, max_len=128)
# Print predictions to console
print_predictions(texts[0], predictions, tokenizer, classes)
Installation
To install the necessary dependencies, use the following command:
pip install ai_address_lib
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for ai-address-lib 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ai_address_lib-0.4.1.tar.gz | 6.5 kB | Without provenance | Details for ai_address_lib-0.4.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ai_address_lib-0.4.1-py3-none-any.whl | Python 3 | none | any | Details for ai_address_lib-0.4.1-py3-none-any.whl |
Total release size: 17.4 kB
Release files / ai_address_lib-0.4.1.tar.gz
| Download URL | ai_address_lib-0.4.1.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d3f959863957e273a8dffe61e4ebf6724cdef8a10e8965dc7c4560b5afe5d0f4
|
|
BLAKE2b-256 checksum How to use checksums |
faafba04964703ba67788f6fb0b68b2d85f216d9719f589bf183586f9cfc6784
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.9
|
Release files / ai_address_lib-0.4.1-py3-none-any.whl
| Download URL | ai_address_lib-0.4.1-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9dc1b701873b3cd8a4e26c5141058f9b2d47327986ed104fba1efdcdbb6654d9
|
|
BLAKE2b-256 checksum How to use checksums |
ed5a8860510781b959d685cf9f1774e994f6886742e0fafc78cbaf04fe3e79fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.9
|
Security for ai-address-lib 0.4.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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.