Registry / ai-ml / fasttext-predict

fasttext-predict

JSON →
library0.9.2.4pypypi✓ verified 24d ago

fasttext-predict is a Python package that provides a lightweight, standalone implementation of fastText's prediction functionality. It is specifically designed to include only the `predict` method, making it compact (<1MB) and free of external dependencies, including NumPy. This library aims to provide pre-built wheels for various architectures, ensuring easy installation for deployment scenarios where a full fastText installation is not desired. It is actively maintained with frequent minor updates, offering a stable solution for inference.

pip install fasttext-predict
INSTALL
IMPORT
SIG · FASTTEXT-PREDICT
F
fasttext-predict
ai-mlpythonv0.9.2.4
Install
1.5s avg
Import
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.2.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 22MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.000s · 19MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fasttext
import fasttext
from fasttext_predict import fasttext
The package name is `fasttext-predict`, but the primary module to import is `fasttext`. Importing directly from `fasttext_predict` is incorrect for typical usage.

This quickstart demonstrates how to load a pre-trained fastText model (e.g., for language identification) and use its `predict` method to classify a given text. It also shows how to retrieve multiple top-k predictions and their associated probabilities. A fastText model file must be downloaded and accessible for this example to run correctly.

# A fastText model file (e.g., for language identification) needs to be downloaded separately. # Example model: lid.176.ftz from https://fasttext.cc/docs/en/language-identification.html import fasttext import os # Ensure the model file is accessible, e.g., placed in the current directory model_path = os.environ.get('FASTTEXT_MODEL_PATH', 'lid.176.ftz') try: model = fasttext.load_model(model_path) text_to_predict = 'Fondant au chocolat et tarte aux myrtilles' predictions = model.predict(text_to_predict) print(f"Text: '{text_to_predict}'") print(f"Predicted label(s): {predictions[0]}") print(f"Probabilities: {predictions[1]}") # To get top-k predictions with probabilities top_k_predictions = model.predict(text_to_predict, k=2) print(f"\nTop 2 Predicted label(s): {top_k_predictions[0]}") print(f"Top 2 Probabilities: {top_k_predictions[1]}") except ValueError as e: print(f"Error loading model or making prediction: {e}") print("Please ensure the model file is downloaded and the path is correct.") except FileNotFoundError: print(f"Error: Model file not found at '{model_path}'.") print("Please download 'lid.176.ftz' (or your target model) and place it correctly, or set FASTTEXT_MODEL_PATH environment variable.")
Debug
Known issues
gotchaThis library provides *only* the prediction functionality (`predict` method) of fastText. Training, model quantization, word/sentence vector generation, or other utilities available in the full `fasttext` library are explicitly *not* included. Attempting to call non-prediction methods will result in an `AttributeError`.
fix
Use the original `fasttext` library (e.g., `pip install fasttext`) for training or other advanced features. This `fasttext-predict` library is specifically for lightweight inference.
affects: All versions
gotchaThe Python package name for installation is `fasttext-predict`, but the correct Python import statement is `import fasttext`. Users accustomed to other `pip install X` -> `import X` patterns should be aware of this difference to avoid `ModuleNotFoundError`.
fix
Always use `import fasttext` after installing `fasttext-predict`.
affects: All versions
breakingModels used with `fasttext-predict` are typically trained with the original `fastText` library (facebookresearch/fastText), which was set to a read-only archive on March 19, 2024. While `fasttext-predict` is actively maintained for prediction, users should be aware of the upstream project's archived status and consider potential long-term implications for model format compatibility or training new models with actively developed forks of `fastText`.
fix
Monitor actively maintained forks of the original `fastText` library for training new models if long-term support for model generation is critical. Ensure model versions are compatible with `fasttext-predict`.
affects: Models trained with fastText versions potentially impacted by upstream archive status.
gotchaThe `predict` method expects clean, single-line text input. Newline characters (`\n`) or other special formatting within input strings can lead to incorrect predictions or errors, especially when processing text from sources like dataframes. Each prediction input should ideally be a single, pre-processed string.
fix
Preprocess input text to remove or replace newline characters (e.g., `text.replace('\n', ' ')`) and ensure each element passed for prediction is a properly formatted string.
affects: All versions
gotchaBy default, `model.predict()` returns only the single top predicted label and its corresponding probability. To retrieve multiple labels (e.g., the top-k most likely labels) and their probabilities for a given text, the `k` parameter must be explicitly passed to the `predict` method.
fix
To get top-k predictions, call `model.predict(text, k=N)` where `N` is the desired number of predictions.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'fasttext_predict'
The `fasttext-predict` package is not installed or the Python environment where it's installed is not active.
fix
Ensure the package is installed using `pip install fasttext-predict` in your active Python environment.
AttributeError: 'Model' object has no attribute 'train_supervised'
The `fasttext-predict` library is a lightweight version of fastText that only includes the prediction functionality. It does not support model training or other utility methods like `train_supervised`.
fix
Use the full `fasttext` library (`pip install fasttext`) if you need training capabilities. If you only need prediction, ensure you are calling the `predict` method on the loaded model and not attempting to train or use other methods.
AttributeError: 'Model' object has no attribute 'get_word_vector'
The `fasttext-predict` library is specifically designed for prediction only and does not include methods for retrieving word vectors (`get_word_vector`), sentence vectors, or other model introspection capabilities of the full `fasttext` library.
fix
If you require access to word vectors or other model components, you need to install and use the complete `fasttext` library (`pip install fasttext`).
fasttext-predict error: predict processes one line at a time (remove '\n')
The `predict` method in fastText (and by extension `fasttext-predict`) expects clean text inputs, typically without newline characters or other extraneous formatting that might interfere with tokenization or prediction, especially when processing text line by line from a file-like input or iterable. While `fasttext-predict`'s `predict` can accept a list of strings, individual strings in the list should be preprocessed.
fix
Preprocess your input strings to remove unwanted characters like newlines before passing them to the `predict` method. For example: `cleaned_text = text.replace('\n', ' ').strip()`.
AttributeError: module 'fasttext_predict' has no attribute 'fasttext'
Users attempting to use `fasttext_predict.fasttext.load_model` (or similar) when the `fasttext-predict` library directly exposes `load_model` at the top level, or confusing the import structure with the full `fasttext` library.
fix
Import `load_model` directly from the `fasttext_predict` package: `from fasttext_predict import load_model`, then use `model = load_model('path/to/model.bin')`.
Upgrade
Version history
0.9.2.4latest on PyPI · released Nov 23, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
26 hits · last 30 days
node
24
OpenAI (training)
1
Resources