Skip to main content

Library for address model predictions

Project description

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 layers ClassNamesLayer and BertLayer.

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 the ClassNamesLayer.
  • 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_address_lib-0.4.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

ai_address_lib-0.4-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file ai_address_lib-0.4.tar.gz.

File metadata

  • Download URL: ai_address_lib-0.4.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for ai_address_lib-0.4.tar.gz
Algorithm Hash digest
SHA256 6b533a97f606c1a9852927cf0e644f87fdbb0c9cece97e3c1c1d7d10f1f7fbf9
MD5 0b78acac600f8ddaaf1308c42f031af5
BLAKE2b-256 9f468683f3db73d0f73c3cf5e3fff437a4b07940ee47c39d58fd9f9f3feb96a3

See more details on using hashes here.

File details

Details for the file ai_address_lib-0.4-py3-none-any.whl.

File metadata

  • Download URL: ai_address_lib-0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for ai_address_lib-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f4d6e61b3d2d7f91572c04f2055038b24409f2d28b16852faa130e6e9180392c
MD5 3312f6d526320e8cf8b727374c4f7f57
BLAKE2b-256 a118032b5f8ade1c442cb5abb0f521ffabee53c9ce75302cee24f4ac11cab51d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page