poetic.util module¶
Module for package utility.
This module includes the necessary functionalities to load and download assets for other modules in the package and provide basic information for the current build and version as needed.
Examples
To get the build and version information of the package:
import poetic
poetic.util.Info.version()
poetic.util.Info.build_status()
Under normal circumstances, the methods in the Initializer class is not needed as part of the prediction workflow. One of the most common usage of a first-time user is to download the assets:
import poetic
poetic.util.Initializer.download_assets()
The tensorflow model and gensim models can also be loaded and returned if they theselves are useful (the Predictor class loads the model automatically):
import poetic
poetic.util.Initializer.load_dict()
poetic.util.Initializer.load_model()
Both download_assets() and load_model() methods have the force_download parameter which controls whether to download the models without taking commandline inputs when the model is missing. It is default to False so that it does not take up bandwidth unintendedly, but it can also be set to True in cases necessary.
-
class
poetic.util.
Info
¶ Bases:
object
Info class provides the basic information of the package.
-
static
build_status
() → str¶ Get the build status of the current version.
- Returns
The build status of the current version.
- Return type
str
-
static
version
() → str¶ A single method to return the version of the package.
- Returns
The current version of the package.
- Return type
str
-
static
-
class
poetic.util.
Initializer
¶ Bases:
object
Initializes core components of the package.
The Initializer is core part of Poetic that loads and downloads models and other necessary assets. It also facilitates the command line mode by interacting with the _Arguments class.
-
classmethod
check_assets
() → Dict[str, bool]¶ Method to check whether assets requirements are met.
This method checks both the model and its weights in the corresponding directory. It reports back their existence as part of the package requirement.
- Returns
the status of the assets as a dictionary.
- Return type
dict
-
classmethod
download_assets
(assets_status: Optional[Dict[str, bool]] = None, force_download: Optional[bool] = False, *, _test: Optional[bool] = False, _test_input: Optional[str] = None) → None¶ Method to download models.
This method downloads models from the poetic-models github repository. Under usual circumstances, other functions will download the models automatically if needed. If all the models already exist, this function will not download them again for package efficiency and bandwidth saving.
If you would like to redownload the assets anyway, a manual download from https://github.com/kevin931/poetic-models/releases is necessary.
- Parameters
assets_status (dict, optional) – A dictionary generated by
check_assets()
method. It has keys “all_exist”, “model”, and “weights” with all values being boolean.force_download (bool, optional) – A boolean indicating whether assets should be downloaded regardless of their existence and user inputs.
-
classmethod
initialize
(*, _test: Optional[bool] = False, _test_args: Optional[Union[List[str], str]] = None)¶ Initializes the package.
This methods checks for any command line arguments, and then loads both the gensim dictionary and the Keras model with its weights.
- Returns
Tuple with the following elements
- dict:
A dictionary of commandline arguments.
- tensorflow.keras.Model:
A pre-trained Keras model with its weights loaded.
- gensim.corpora.dictionary.Dictionary:
A gensim dictionary.
- Return type
tuple
-
classmethod
load_dict
() → gensim.corpora.dictionary.Dictionary¶ Loads gensim dictionary.
- Returns
A gensim dictionary.
- Return type
gensim.corpora.dictionary.Dictionary
-
classmethod
load_model
(force_download: Optional[bool] = False, *, _test: Optional[bool] = False) → tensorflow.python.keras.engine.training.Model¶ Load Keras models.
This method uses Keras interface to load the previously trained models, which are necessary for the Predictor and the GUI. If the model does not exist, the download_assets() method is automatically called for the option to obtain the necessary assets.
- Parameters
force_download (bool, optional) – A boolean value on whether to download the models without asking if the models do not exist.
- Returns
Pretrained Keras model
- Return type
tensorflow.keras.Model
-
classmethod