Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Make language-tool a separate service #170

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@ COPY . .
RUN mkdir -p src/ctk_functions/data/signatures
COPY --from=unzipper /files/*.png /home/site/wwwroot/src/ctk_functions/data/signatures

RUN apt-get clean; apt-get -y update && \
mkdir -p /usr/share/man/man1/ && \
apt-get install -y openjdk-17-jdk && \
apt-get install -y openjdk-17-jre && \
update-alternatives --config java && \
update-alternatives --config javac && \
uv sync --frozen --no-cache && \
uv run python -c 'import spacy; spacy.load("en_core_web_sm")' && \
uv run python -c \
'import language_tool_python; language_tool_python.LanguageTool ("en-US")'
RUN uv sync --frozen --no-cache && \
uv run python -c 'import spacy; spacy.load("en_core_web_sm")'

CMD ["uv", "run", "fastapi", "run", "src/ctk_functions/app.py", "--port", "8000", "--host", "0.0.0.0"]
2 changes: 2 additions & 0 deletions src/ctk_functions/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Settings(pydantic_settings.BaseSettings):
AZURE_OPENAI_LLM_DEPLOYMENT: pydantic.SecretStr
AZURE_OPENAI_ENDPOINT: pydantic.SecretStr

LANGUAGE_TOOL_URL: str | None = None

LOGGER_VERBOSITY: int = logging.INFO
LOGGER_PHI_LOGGING_LEVEL: int = pydantic.Field(1, lt=logging.DEBUG)
LOG_PHI: bool = pydantic.Field(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from docx import document
from docx.text import paragraph

from ctk_functions.core import config
from ctk_functions.text import corrections

settings = config.get_settings()
NLP = spacy.load("en_core_web_sm", enable=["parser"])

# c.f. https://community.languagetool.org/rule/list?lang=en for a list of rules.
Expand Down Expand Up @@ -46,6 +48,7 @@ def __init__(
self.document = doc
self.correcter = corrections.LanguageCorrecter(
enabled_rules or DEFAULT_LANGUAGE_RULES,
settings.LANGUAGE_TOOL_URL,
)

def correct(self) -> None:
Expand Down
5 changes: 4 additions & 1 deletion src/ctk_functions/routers/language_tool/controller.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Functions for converting files between different formats."""

from ctk_functions.core import config
from ctk_functions.routers.language_tool import schemas
from ctk_functions.text import corrections

settings = config.get_settings()


def language_tool(body: schemas.PostLanguageToolRequest) -> str:
"""Corrects the grammar of the input text.
Expand All @@ -13,5 +16,5 @@ def language_tool(body: schemas.PostLanguageToolRequest) -> str:
Returns:
The corrected text.
"""
correcter = corrections.LanguageCorrecter(body.rules)
correcter = corrections.LanguageCorrecter(body.rules, settings.LANGUAGE_TOOL_URL)
return correcter.run(body.text)
5 changes: 4 additions & 1 deletion src/ctk_functions/text/corrections.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ class LanguageCorrecter:
def __init__(
self,
enabled_rules: Iterable[str],
remote_server: str | None = None,
) -> None:
"""Initializes the language tool.

Args:
enabled_rules: The rules to enable for the correction.
remote_server: The remote server to connect to.

"""
self.language_tool = language_tool_python.LanguageTool(
"en-US",
language="en-US",
remote_server=remote_server,
)
self.language_tool.enabled_rules = set(enabled_rules)
self.language_tool.enabled_rules_only = True
Expand Down
Loading