Setting up the development environment
Now that you have a basic understanding of features offered by the framework, as well as an understanding of the basic NLP concepts, you are now ready to move to the next step of setting up your development environment for Flair.
To be able to follow the instructions in this section, first make sure you have Python 3.6+ installed on your device as described in the Technical requirements section.
Creating the virtual environment
In Python, it's generally good practice to install packages in virtual environments so that the project dependencies you are currently working on will not affect your global Python dependencies or other projects you may work on in the future.
We will use the venv
tool that is part of the Python Standard Library and requires no installation. To create a virtual environment, simply create a new directory, move into it, then run the following command:
$ python3 -m venv learning-flair
Then, to activate the virtual environment on Linux or macOS, run the following:
$ source learning-flair/bin/activate
If you are running Windows, run the following:
$ learning-flair\Scripts\activate.bat
Your command line should become prefixed with (learning-flair) $
and your virtual environment is now active.
Installing a published version of Flair in a virtual environment
You should now be ready to install Flair version 0.11 with this single command:
(learning-flair) $ pip install flair==0.11
The installation should now commence and finish within a minute or so depending on the speed of your internet connection.
You can verify the installation by running the following command, which will display a list of package properties including its version:
(learning-flair) $ pip show flair
Name: flair
Version: 0.11
Summary: A very simple framework for state-of-the-art NLP
Home-page: https://github.com/flairNLP/flair
…
A command output like the preceding indicates the package has been successfully installed in your virtual environment.
Installing directly from the GitHub repository (optional)
In some cases, the features we aim to make use of in Flair may already be implemented in a branch on GitHub, but those changes may not yet be released as part of a Python package published on PyPI. We can install Flair with those features directly from the Git repository branch.
For example, here is how you can install Flair from the master
branch:
(learning-flair) $ git clone https://github.com/flairNLP/flair.git (learning-flair) $ cd flair (learning-flair) $ git checkout master (learning-flair) $ pip install .
Important note
Installing code from non-reviewed branches can introduce unreliable or unsafe code. When installing Flair from development branches, make sure the code you are installing comes from a trusted source. Also note that the future versions of Flair (versions larger than 0.11) may not be compatible with the code snippets found in this book.
Replace the term master
with any other branch name to install the package from a branch of your choice.
Running code that uses Flair
Running code that makes use of the Flair Python package is no different from running any other type of Python code.
The recommended way for you to run the code snippets in this book is to execute them as code cells in a Jupyter notebook, which you can install and run as follows:
(learning-flair) $ pip install notebook (learning-flair) $ jupyter notebook
You can then create a new Python 3 notebook and run your first Flair script to verify the package is imported successfully:
import flair print(flair.__version__)
After executing, the preceding code should print out the version of Flair you are currently using, indicating that the Flair package has been imported successfully and you are ready to start.