-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code styling typos and error behavior
- Loading branch information
Showing
12 changed files
with
169 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
start_container.sh | ||
start_container.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,3 @@ HEALTHCHECK CMD ./healthcheck.sh | |
|
||
ENV TEMP=/usr/src/app/tmp | ||
ENTRYPOINT ["./docker-entrypoint.sh"] | ||
CMD ["serve"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.DEFAULT_GOAL := help | ||
|
||
target_dirs := punctuation http_server celery_app | ||
|
||
help: | ||
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' | ||
|
||
style: ## update code style. | ||
black -l 100 ${target_dirs} | ||
isort ${target_dirs} | ||
|
||
lint: ## run pylint linter. | ||
pylint ${target_dirs} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# 1.0.1 | ||
- Changes behavior on prediction error from failed to ignore. | ||
- Adds makefile for code styling (PEP 8) | ||
- Fixes typos. | ||
- Changes code style (PEP 8) | ||
|
||
# 1.0.0 | ||
- Punctuation service. | ||
- HTTP or Celery serving. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
import os | ||
|
||
from celery import Celery | ||
|
||
from punctuation import logger | ||
|
||
celery = Celery(__name__, include=['celery_app.tasks']) | ||
celery = Celery(__name__, include=["celery_app.tasks"]) | ||
service_name = os.environ.get("SERVICE_NAME") | ||
broker_url = os.environ.get("SERVICES_BROKER") | ||
if os.environ.get("BROKER_PASS", False): | ||
components = broker_url.split('//') | ||
components = broker_url.split("//") | ||
broker_url = f'{components[0]}//:{os.environ.get("BROKER_PASS")}@{components[1]}' | ||
celery.conf.broker_url = "{}/0".format(broker_url) | ||
celery.conf.result_backend = "{}/1".format(broker_url) | ||
celery.conf.update( | ||
result_expires=3600, | ||
task_acks_late=True, | ||
task_track_started = True) | ||
celery.conf.broker_url = f"{broker_url}/0" | ||
celery.conf.result_backend = f"{broker_url}/1" | ||
celery.conf.update(result_expires=3600, task_acks_late=True, task_track_started=True) | ||
|
||
# Queues | ||
language = os.environ.get("LANGUAGE") | ||
celery.conf.update( | ||
{'task_routes': { | ||
'punctuation_task' : {'queue': f"punctuation_{language}"},} | ||
{ | ||
"task_routes": { | ||
"punctuation_task": {"queue": f"punctuation_{language}"}, | ||
} | ||
} | ||
) | ||
logger.info("Celery configured for broker located at {} with service name {}".format(broker_url, service_name)) | ||
logger.info( | ||
f"Celery configured for broker located at {broker_url} with service name {service_name}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,47 @@ | ||
import os | ||
import requests | ||
import json | ||
from celery_app.celeryapp import celery | ||
from typing import Union | ||
|
||
import requests | ||
|
||
from celery_app.celeryapp import celery | ||
|
||
|
||
@celery.task(name="punctuation_task", bind=True) | ||
def punctuation_task(self, text: Union[str, list]): | ||
""" punctuation_task do a synchronous call to the punctuation serving API """ | ||
"""punctuation_task do a synchronous call to the punctuation serving API""" | ||
self.update_state(state="STARTED") | ||
# Fetch model name | ||
try: | ||
result = requests.get("http://localhost:8081/models", | ||
headers={"accept": "application/json",},) | ||
result = requests.get( | ||
"http://localhost:8081/models", | ||
headers={ | ||
"accept": "application/json", | ||
}, | ||
) | ||
models = json.loads(result.text) | ||
model_name = models["models"][0]["modelName"] | ||
except: | ||
raise Exception("Failed to fetch model name") | ||
except Exception as error: | ||
raise Exception("Failed to fetch model name") from error | ||
|
||
if isinstance(text, str): | ||
sentences = [text] | ||
else: | ||
sentences = text | ||
punctuated_sentences = [] | ||
for i, sentence in enumerate(sentences): | ||
self.update_state(state="STARTED", meta={"current": i, "total": len(sentences)}) | ||
|
||
result = requests.post("http://localhost:8080/predictions/{}".format(model_name), | ||
headers={'content-type': 'application/octet-stream'}, | ||
data=sentence.strip().encode('utf-8')) | ||
|
||
result = requests.post( | ||
f"http://localhost:8080/predictions/{model_name}", | ||
headers={"content-type": "application/octet-stream"}, | ||
data=sentence.strip().encode("utf-8"), | ||
) | ||
if result.status_code == 200: | ||
punctuated_sentence = result.text | ||
punctuated_sentence = punctuated_sentence[0].upper() + punctuated_sentence[1:] | ||
punctuated_sentences.append(punctuated_sentence) | ||
else: | ||
raise Exception(result.text) | ||
return punctuated_sentences[0] if len(punctuated_sentences) == 1 else punctuated_sentences | ||
print("Failed to predict punctuation on sentence: >{sentence}<") | ||
punctuated_sentence = sentence | ||
punctuated_sentence = punctuated_sentence[0].upper() + punctuated_sentence[1:] | ||
punctuated_sentences.append(punctuated_sentence) | ||
|
||
return punctuated_sentences[0] if len(punctuated_sentences) == 1 else punctuated_sentences |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,45 @@ | ||
import os | ||
import argparse | ||
import os | ||
|
||
__all__ = ["createParser"] | ||
|
||
|
||
def createParser() -> argparse.ArgumentParser: | ||
parser = argparse.ArgumentParser() | ||
|
||
# SERVICE | ||
parser.add_argument( | ||
'--service_name', | ||
"--service_name", | ||
type=str, | ||
help='Service Name', | ||
default=os.environ.get('SERVICE_NAME', 'punctuation')) | ||
|
||
#GUNICORN | ||
parser.add_argument( | ||
'--service_port', | ||
type=int, | ||
help='Service port', | ||
default=80) | ||
help="Service Name", | ||
default=os.environ.get("SERVICE_NAME", "punctuation"), | ||
) | ||
|
||
# GUNICORN | ||
parser.add_argument("--service_port", type=int, help="Service port", default=80) | ||
parser.add_argument( | ||
'--workers', | ||
"--workers", | ||
type=int, | ||
help="Number of Gunicorn workers (default=CONCURRENCY + 1)", | ||
default=int(os.environ.get('CONCURRENCY', 1)) + 1) | ||
|
||
#SWAGGER | ||
parser.add_argument( | ||
'--swagger_url', | ||
type=str, | ||
help='Swagger interface url', | ||
default='/docs') | ||
default=int(os.environ.get("CONCURRENCY", 1)) + 1, | ||
) | ||
|
||
# SWAGGER | ||
parser.add_argument("--swagger_url", type=str, help="Swagger interface url", default="/docs") | ||
parser.add_argument( | ||
'--swagger_prefix', | ||
"--swagger_prefix", | ||
type=str, | ||
help='Swagger prefix', | ||
default=os.environ.get('SWAGGER_PREFIX', '')) | ||
help="Swagger prefix", | ||
default=os.environ.get("SWAGGER_PREFIX", ""), | ||
) | ||
parser.add_argument( | ||
'--swagger_path', | ||
"--swagger_path", | ||
type=str, | ||
help='Swagger file path', | ||
default=os.environ.get('SWAGGER_PATH', '/usr/src/app/document/swagger.yml')) | ||
|
||
#MISC | ||
parser.add_argument( | ||
'--debug', | ||
action='store_true', | ||
help='Display debug logs') | ||
help="Swagger file path", | ||
default=os.environ.get("SWAGGER_PATH", "/usr/src/app/document/swagger.yml"), | ||
) | ||
|
||
# MISC | ||
parser.add_argument("--debug", action="store_true", help="Display debug logs") | ||
|
||
return parser | ||
return parser |
Oops, something went wrong.