From 5a895850808bff9b36e6651cfcfdddffb6e81d70 Mon Sep 17 00:00:00 2001 From: MinhTu Thomas Hoang Date: Fri, 10 Sep 2021 21:00:58 +0700 Subject: [PATCH 1/4] Fix the issue of 'CoreNLP' singleton object not found --- Dockerfile | 2 +- ratsql/resources/corenlp.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0103070..2ccf008 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ RUN mkdir -p /usr/share/man/man1 && \ COPY requirements.txt setup.py /app/ WORKDIR /app RUN pip install --user -r requirements.txt --no-warn-script-location && \ - pip install --user entmax && \ + pip install --user entmax stanfordnlp && \ python -c "import nltk; nltk.download('stopwords'); nltk.download('punkt')" # Cache the pretrained BERT model diff --git a/ratsql/resources/corenlp.py b/ratsql/resources/corenlp.py index 9e81b6a..859e527 100644 --- a/ratsql/resources/corenlp.py +++ b/ratsql/resources/corenlp.py @@ -1,8 +1,8 @@ import os import sys -import corenlp import requests +from stanfordnlp.server import CoreNLPClient class CoreNLP: @@ -18,7 +18,7 @@ def __init__(self): Direct URL: http://nlp.stanford.edu/software/stanford-corenlp-full-2018-10-05.zip Landing page: https://stanfordnlp.github.io/CoreNLP/''') - self.client = corenlp.CoreNLPClient() + self.client = CoreNLPClient() def __del__(self): self.client.stop() From a79456b5504f9383e88212a71746f6f4316c5e06 Mon Sep 17 00:00:00 2001 From: MinhTu Thomas Hoang Date: Fri, 10 Sep 2021 21:02:27 +0700 Subject: [PATCH 2/4] Fix the issue of `inspect.signature` recognising parent's ctor params instead of the called ctor params --- ratsql/utils/registry.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/ratsql/utils/registry.py b/ratsql/utils/registry.py index e743706..e1d6d76 100644 --- a/ratsql/utils/registry.py +++ b/ratsql/utils/registry.py @@ -36,22 +36,30 @@ def construct(kind, config, unused_keys=(), **kwargs): **kwargs) -def instantiate(callable, config, unused_keys=(), **kwargs): +def instantiate(invocable, config, unused_keys=(), **kwargs): merged = {**config, **kwargs} - signature = inspect.signature(callable) - for name, param in signature.parameters.items(): + signature = inspect.signature(invocable) + + if hasattr(invocable, '__init__'): + # to avoid inspecting ctor of parent class (if exists) instead of the target class's ctor + params = dict(inspect.signature(invocable.__init__).parameters) + params.pop('self', None) + else: + params = dict(inspect.signature(invocable).parameters) + + for name, param in params.items(): if param.kind in (inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.VAR_POSITIONAL): raise ValueError(f'Unsupported kind for param {name}: {param.kind}') - if any(param.kind == inspect.Parameter.VAR_KEYWORD for param in signature.parameters.values()): - return callable(**merged) + if any(param.kind == inspect.Parameter.VAR_KEYWORD for param in params.values()): + return invocable(**merged) missing = {} for key in list(merged.keys()): - if key not in signature.parameters: + if key not in params: if key not in unused_keys: missing[key] = merged[key] merged.pop(key) if missing: - print(f'WARNING {callable}: superfluous {missing}', file=sys.stderr) - return callable(**merged) + print(f'WARNING {invocable}: superfluous {missing}', file=sys.stderr) + return invocable(**merged) From d32aac77a265f27cdd719211eaeff0d1b585b67f Mon Sep 17 00:00:00 2001 From: MinhTu Thomas Hoang Date: Fri, 10 Sep 2021 21:15:31 +0700 Subject: [PATCH 3/4] Remove unused local variable --- ratsql/utils/registry.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ratsql/utils/registry.py b/ratsql/utils/registry.py index e1d6d76..7308917 100644 --- a/ratsql/utils/registry.py +++ b/ratsql/utils/registry.py @@ -38,7 +38,6 @@ def construct(kind, config, unused_keys=(), **kwargs): def instantiate(invocable, config, unused_keys=(), **kwargs): merged = {**config, **kwargs} - signature = inspect.signature(invocable) if hasattr(invocable, '__init__'): # to avoid inspecting ctor of parent class (if exists) instead of the target class's ctor From 5d238b3bb4fa7201d2f51d217888b3096b3ce891 Mon Sep 17 00:00:00 2001 From: MinhTu Thomas Hoang Date: Sat, 11 Sep 2021 20:15:59 +0700 Subject: [PATCH 4/4] Introduce 'stanfordnlp' to the list of required dependencies, remove 'stanford-corenlp' --- Dockerfile | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2ccf008..0103070 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ RUN mkdir -p /usr/share/man/man1 && \ COPY requirements.txt setup.py /app/ WORKDIR /app RUN pip install --user -r requirements.txt --no-warn-script-location && \ - pip install --user entmax stanfordnlp && \ + pip install --user entmax && \ python -c "import nltk; nltk.download('stopwords'); nltk.download('punkt')" # Cache the pretrained BERT model diff --git a/setup.py b/setup.py index 0131946..edaa188 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ 'pyrsistent~=0.14.9', 'pytest~=5.3.2', 'records~=0.5.3', - 'stanford-corenlp~=3.9.2', + 'stanfordnlp~=0.2.0', 'tabulate~=0.8.6', 'torch~=1.3.1', 'torchtext~=0.3.1',