Skip to content

Commit

Permalink
add loop for timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaimran08 committed Nov 29, 2023
1 parent bbb4a03 commit 5701477
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
22 changes: 12 additions & 10 deletions serve-python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ RUN useradd -m -u 1000 $USER
# Set working directory (this is where the code should go)
WORKDIR $HOME

RUN /bin/bash -c "apt update"
RUN /bin/bash -c "apt install python3.7-dev -y"
RUN /bin/bash -c "apt install curl -y"
RUN /bin/bash -c "apt-get install python3-distutils -y"
RUN /bin/bash -c "curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py"
RUN /bin/bash -c "python3.7 get-pip.py"
RUN /bin/bash -c "apt install gcc -y"
RUN /bin/bash -c "update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1"
RUN /bin/bash -c "pip3 install --upgrade pip"
RUN apt update \

Check failure on line 15 in serve-python/Dockerfile

View workflow job for this annotation

GitHub Actions / lint

DL3015 info: Avoid additional packages by specifying `--no-install-recommends`

Check failure on line 15 in serve-python/Dockerfile

View workflow job for this annotation

GitHub Actions / lint

DL3027 warning: Do not use apt as it is meant to be a end-user tool, use apt-get or apt-cache instead

Check failure on line 15 in serve-python/Dockerfile

View workflow job for this annotation

GitHub Actions / lint

DL3042 warning: Avoid use of cache directory with pip. Use `pip install --no-cache-dir <package>`
&& apt install python3.7-dev -y \
&& apt install curl -y \
&& apt-get install python3-distutils -y \
&& curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py \
&& python3.7 get-pip.py \
&& apt install gcc -y \
&& update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1 \
&& pip3 install --upgrade pip \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# RUN /bin/bash -c "apt update"
# RUN /bin/bash -c "curl https://dl.min.io/client/mc/release/linux-amd64/mc --output mc && chmod +x mc"
COPY requirements.txt $HOME/requirements.txt
Expand All @@ -36,4 +38,4 @@ RUN chmod +x start-script.sh \
ENV STACKN_MODEL_PATH=$HOME/models
ENV PYTHONPATH=$HOME/models

CMD ./deploy.sh
CMD ["./deploy.sh"]
63 changes: 44 additions & 19 deletions serve-python/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,54 @@ def test_container_ports():

def test_container_access():
"""Test of basic communication with the container returns status 200 (OK)."""
try:
url = _get_api_url(container) + "/health"
response = requests.get(url, timeout=TIMEOUT_CALL)
if response.status_code == 200:
assert True
except ConnectionError:
assert False
max_attempts = 100
for attempt in range(1, max_attempts + 1):
try:
url = _get_api_url(container) + "/health"
response = requests.get(url, timeout=TIMEOUT_CALL)
if response.status_code == 200:
assert True
except:
pass
if attempt == max_attempts:
RuntimeError(
f"Python Deployment did not become ready after {max_attempts} attempts"
)
assert False

print(
f"Attempt {attempt} failed, waiting for 10 seconds before trying again..."
)
time.sleep(10)


def test_prediction():
"""Verify that the model can be used for predictions."""
# Set input string
example = "Jag är ett barn, och det här är mitt hem. Alltså är det ett barnhem!"
# msk_ind takes an index in order to mask (or hide) one of the word in the example sentence, which should then be predicted by the BERT trained model
msk_ind = 4
url = _get_api_url(container) + "/predict/"
res = requests.post(url, json={"pred": example, "msk_ind": msk_ind})
text_encoded = res.json().encode("latin1")
text_decoded = text_encoded.decode("unicode-escape")
print(json.loads(text_decoded))
assert json.loads(text_decoded) == {
"result": ["barn", "hem", "hus", "spädbarn", "##hem"]
}
max_attempts = 100
for attempt in range(1, max_attempts + 1):
# Set input string
example = "Jag är ett barn, och det här är mitt hem. Alltså är det ett barnhem!"
# msk_ind takes an index in order to mask (or hide) one of the word in the example sentence, which should then be predicted by the BERT trained model
msk_ind = 4
url = _get_api_url(container) + "/predict/"
res = requests.post(url, json={"pred": example, "msk_ind": msk_ind})
text_encoded = res.json().encode("latin1")
text_decoded = text_encoded.decode("unicode-escape")
print(json.loads(text_decoded))
if res.status_code == 200:
assert json.loads(text_decoded) == {
"result": ["barn", "hem", "hus", "spädbarn", "##hem"]
}
if attempt == max_attempts:
RuntimeError(
f"Python Deployment did not become ready after {max_attempts} attempts"
)
assert False

print(
f"Attempt {attempt} failed, waiting for 10 seconds before trying again..."
)
time.sleep(10)


def test_shutdown():
Expand Down

0 comments on commit 5701477

Please sign in to comment.