From 5ba21d010f30fa379fe8ff534ebba959e1f197cf Mon Sep 17 00:00:00 2001 From: Roberto Lapuente Date: Fri, 15 Jan 2021 11:15:06 +1300 Subject: [PATCH 1/4] #467 Update setup.utils.py Add convenience function for building the config dict --- tabpy/models/utils/setup_utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tabpy/models/utils/setup_utils.py b/tabpy/models/utils/setup_utils.py index 10801fb7..01a9fc44 100644 --- a/tabpy/models/utils/setup_utils.py +++ b/tabpy/models/utils/setup_utils.py @@ -13,10 +13,14 @@ def get_default_config_file_path(): return config_file_path -def parse_config(config_file_path): +def get_config(config_file_path): config = configparser.ConfigParser() config.read(config_file_path) - tabpy_config = config["TabPy"] + return config + + +def parse_config(config_file_path): + tabpy_config = get_config(config_file_path)["TabPy"] port = 9004 if "TABPY_PORT" in tabpy_config: From d6f337a17a7ab72db594a9d61a0780a650a2909c Mon Sep 17 00:00:00 2001 From: Roberto Lapuente Date: Fri, 15 Jan 2021 11:17:41 +1300 Subject: [PATCH 2/4] #467 Update SentimentAnalysis.py Allow to modify the NLTK download path and proxy settings via the tabpy config file. --- tabpy/models/scripts/SentimentAnalysis.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tabpy/models/scripts/SentimentAnalysis.py b/tabpy/models/scripts/SentimentAnalysis.py index ed4e0c7e..abb7df06 100644 --- a/tabpy/models/scripts/SentimentAnalysis.py +++ b/tabpy/models/scripts/SentimentAnalysis.py @@ -1,3 +1,4 @@ +import sys from textblob import TextBlob import nltk from nltk.sentiment.vader import SentimentIntensityAnalyzer @@ -10,8 +11,18 @@ ssl._create_default_https_context = _ctx -nltk.download("vader_lexicon") -nltk.download("punkt") +def setup(): + file_path = sys.argv[1] if len(sys.argv) > 1 else setup_utils.get_default_config_file_path() + config = setup_utils.get_config(file_path) + download_dir = None + if "nltk" in config: + nltk_config = config["nltk"] + download_dir = nltk_config.get("NLTK_DOWNLOAD_PATH") + if "NLTK_PROXY" in nltk_config: + nltk.set_proxy(nltk_config["NLTK_PROXY"]) + + nltk.download("vader_lexicon", download_dir=download_dir) + nltk.download("punkt", download_dir=download_dir) def SentimentAnalysis(_arg1, library="nltk"): @@ -45,6 +56,7 @@ def SentimentAnalysis(_arg1, library="nltk"): if __name__ == "__main__": + setup() setup_utils.deploy_model( "Sentiment Analysis", SentimentAnalysis, From 643fa6c575ccc3613ae1868ab302a2e0901b42b7 Mon Sep 17 00:00:00 2001 From: Roberto Lapuente Date: Fri, 15 Jan 2021 11:25:16 +1300 Subject: [PATCH 3/4] #467 Update server-config.md to include nltk config options --- docs/server-config.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/server-config.md b/docs/server-config.md index 28fa0deb..f798ba0e 100755 --- a/docs/server-config.md +++ b/docs/server-config.md @@ -91,6 +91,12 @@ at [`logging.config` documentation page](https://docs.python.org/3.6/library/log value - `30`. This timeout does not apply when evaluating models either through the `/query` method, or using the `tabpy.query(...)` syntax with the `/evaluate` method. + +`[nltk]` parameters: + +- `NLTK_DOWNLOAD_PATH` - Set the NLTK download path. Defaul '~/nltk_data'. +- `NLTK_PROXY` - Set the proxy server used for for NLTK `nltk.download()`. + ### Configuration File Example @@ -130,6 +136,14 @@ settings._ # The value should be a float representing the timeout time in seconds. # TABPY_EVALUATE_TIMEOUT = 30 +[nltk] +# Set the download directory for nltk downloads. +# NLTK_DOWNLOAD_PATH = ~/nltk_data + +# If TabPy is behind a proxy and it needs to connect to the internet for +# some tasks, you can define a proxy here. +# NLTK_PROXY = http://proxy:3128 + [loggers] keys=root From cd0acc3ac9d9d7b67ea6df33cfee5744fc3e38fe Mon Sep 17 00:00:00 2001 From: Roberto Lapuente Date: Fri, 15 Jan 2021 13:34:18 +1300 Subject: [PATCH 4/4] Remove extra spaces on server-config.md --- docs/server-config.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/server-config.md b/docs/server-config.md index f798ba0e..0f80f60a 100755 --- a/docs/server-config.md +++ b/docs/server-config.md @@ -97,7 +97,6 @@ at [`logging.config` documentation page](https://docs.python.org/3.6/library/log - `NLTK_DOWNLOAD_PATH` - Set the NLTK download path. Defaul '~/nltk_data'. - `NLTK_PROXY` - Set the proxy server used for for NLTK `nltk.download()`. - ### Configuration File Example **Note:** _Always use absolute paths for the configuration paths