{"id":5219,"library":"fasttext-predict","title":"fastText Predict","description":"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.","status":"active","version":"0.9.2.4","language":"en","source_language":"en","source_url":"https://github.com/searxng/fasttext-predict/","tags":["fasttext","nlp","text-classification","language-detection","embedding","prediction","inference","lightweight"],"install":[{"cmd":"pip install fasttext-predict","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The package name is `fasttext-predict`, but the primary module to import is `fasttext`. Importing directly from `fasttext_predict` is incorrect for typical usage.","wrong":"from fasttext_predict import fasttext","symbol":"fasttext","correct":"import fasttext"}],"quickstart":{"code":"# A fastText model file (e.g., for language identification) needs to be downloaded separately.\n# Example model: lid.176.ftz from https://fasttext.cc/docs/en/language-identification.html\n\nimport fasttext\nimport os\n\n# Ensure the model file is accessible, e.g., placed in the current directory\nmodel_path = os.environ.get('FASTTEXT_MODEL_PATH', 'lid.176.ftz')\n\ntry:\n    model = fasttext.load_model(model_path)\n    text_to_predict = 'Fondant au chocolat et tarte aux myrtilles'\n    predictions = model.predict(text_to_predict)\n    \n    print(f\"Text: '{text_to_predict}'\")\n    print(f\"Predicted label(s): {predictions[0]}\")\n    print(f\"Probabilities: {predictions[1]}\")\n\n    # To get top-k predictions with probabilities\n    top_k_predictions = model.predict(text_to_predict, k=2)\n    print(f\"\\nTop 2 Predicted label(s): {top_k_predictions[0]}\")\n    print(f\"Top 2 Probabilities: {top_k_predictions[1]}\")\n\nexcept ValueError as e:\n    print(f\"Error loading model or making prediction: {e}\")\n    print(\"Please ensure the model file is downloaded and the path is correct.\")\nexcept FileNotFoundError:\n    print(f\"Error: Model file not found at '{model_path}'.\")\n    print(\"Please download 'lid.176.ftz' (or your target model) and place it correctly, or set FASTTEXT_MODEL_PATH environment variable.\")","lang":"python","description":"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."},"warnings":[{"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.","message":"This 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`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use `import fasttext` after installing `fasttext-predict`.","message":"The 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`.","severity":"gotcha","affected_versions":"All versions"},{"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`.","message":"Models 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`.","severity":"breaking","affected_versions":"Models trained with fastText versions potentially impacted by upstream archive status."},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To get top-k predictions, call `model.predict(text, k=N)` where `N` is the desired number of predictions.","message":"By 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}