Skip to content

feat: i18n / translations #124

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
33 changes: 18 additions & 15 deletions gliner_api/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
from stamina import retry_context

from gliner_api.config import Config, get_config
from gliner_api.translations import Translations

config: Config = get_config()
client: AsyncClient = AsyncClient(
base_url=f"http://localhost:{config.port}",
headers={"Authorization": f"Bearer {config.api_key}"} if config.api_key is not None else None,
)

languages: dict[str, dict[str, str]] = Translations().model_dump()["languages"]
i18n: gr.I18n = gr.I18n(**languages)


async def call_invoke(
text: str,
Expand Down Expand Up @@ -92,45 +96,45 @@ async def call_invoke(
fn=call_invoke,
inputs=[
gr.Textbox(
label="Input Text",
placeholder="Enter text...\nYou can also paste longer texts here.",
label=i18n("input_label"),
placeholder=i18n("input_placeholder"),
lines=3,
max_lines=15,
value="Steve Jobs founded Apple Inc. in Cupertino, CA on April 1, 1976.",
info="Text to analyze for named entities.",
info=i18n("input_info"),
),
gr.Slider(
label="Threshold",
label=i18n("slider_label"),
minimum=0.0,
maximum=1.0,
step=0.05,
value=config.default_threshold,
info="Minimum confidence score for entities to be included in the response.",
info=i18n("slider_info"),
),
gr.Dropdown(
label="Entity Types",
label=i18n("dropdown_label"),
choices=config.default_entities,
multiselect=True,
value=config.default_entities,
allow_custom_value=True,
info="Select entity types to detect. Add custom entity types as needed.",
info=i18n("dropdown_info"),
),
gr.CheckboxGroup(
label="Additional Options (Advanced)",
label=i18n("options_label"),
choices=[
("Enable deep NER mode", "deep_ner"),
("Enable multi-label classification", "multi_label"),
(i18n("options_deep_ner"), "deep_ner"),
(i18n("options_multi_label"), "multi_label"),
],
value=[],
info="Deep NER: hierarchical entity detection for nested entities. Multi-label: entities can belong to multiple types.",
info=i18n("options_info"),
),
],
outputs=[
gr.HighlightedText(
label="Detected Entities",
label=i18n("outputs_highlighted_text"),
),
gr.Label(label="Inference Time"),
gr.JSON(label="Raw Response Body"),
gr.Label(label=i18n("outputs_label")),
gr.JSON(label=i18n("outputs_json")),
],
title="GLiNER API - Frontend",
description=description,
Expand Down Expand Up @@ -176,5 +180,4 @@ async def call_invoke(
api_name=False,
flagging_mode="never",
theme=gr_themes.Base(primary_hue="teal"),
submit_btn="Detect Entities",
)
58 changes: 58 additions & 0 deletions gliner_api/translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from pydantic import BaseModel, Field
from pydantic_settings import (
BaseSettings,
JsonConfigSettingsSource,
PydanticBaseSettingsSource,
SettingsConfigDict,
)


class Translations(BaseSettings):
languages: dict[str, "Language"] = Field(
default_factory=dict,
)

model_config = SettingsConfigDict(
json_file="translations.json",
json_file_encoding="utf-8",
)

@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (
init_settings,
JsonConfigSettingsSource(settings_cls),
)


class Language(BaseModel):
# TextInput properties
input_label: str
input_info: str
input_placeholder: str

# ThresholdSlider properties
slider_label: str
slider_info: str

# EntityDropdown properties
dropdown_label: str
dropdown_info: str

# OptionsCheckboxGroup properties
options_label: str
options_info: str
options_deep_ner: str
options_multi_label: str

# OutputsLabels properties
outputs_highlighted_text: str
outputs_label: str
outputs_json: str
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ async def close_metrics_server():
from fastapi.staticfiles import StaticFiles
from gradio import mount_gradio_app

from gliner_api.frontend import client, interface
from gliner_api.frontend import client, i18n, interface

app.mount("/static", StaticFiles(directory="static"), name="static")
mount_gradio_app(
app,
interface,
path="",
show_api=False,
i18n=i18n,
)

@app.on_event("shutdown")
Expand Down
36 changes: 36 additions & 0 deletions translations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"languages": {
"de": {
"dropdown_info": "Wählen Sie zu erkennende Entitäts-Klassen aus. Fügen Sie bei Bedarf benutzerdefinierte Klassen hinzu.",
"dropdown_label": "Entitäts-Klassen",
"input_info": "Text, der auf Entitäten analysiert werden soll.",
"input_label": "Eingabe",
"input_placeholder": "Text eingeben...\nSie können auch längere Texte hier einfügen.",
"options_deep_ner": "Deep NER aktivieren",
"options_info": "Deep NER: Hierarchische Entitätserkennung für verschachtelte Entitäten.\nMulti-Label: Entitäten können mehreren Klassen angehören.",
"options_label": "Zusätzliche Optionen (erweitert)",
"options_multi_label": "Multi-Label-Klassifikation aktivieren",
"outputs_highlighted_text": "Erkannte Entitäten",
"outputs_json": "Ursprüngliche Ausgabe",
"outputs_label": "Inferenzzeit",
"slider_info": "Mindest-Konfidenzwert für die Einbeziehung erkannter Entitäten in die Ausgabe.",
"slider_label": "Konfidenzgrenze"
},
"en": {
"dropdown_info": "Select entity types to detect. Add custom entity types as needed.",
"dropdown_label": "Entity Types",
"input_info": "Text to analyze for named entities.",
"input_label": "Input Text",
"input_placeholder": "Enter text...\nYou can also paste longer texts here.",
"options_deep_ner": "Enable Deep NER mode",
"options_info": "Deep NER: hierarchical entity detection for nested entities.\nMulti-label: entities can belong to multiple types.",
"options_label": "Additional Options (Advanced)",
"options_multi_label": "Enable multi-label classification",
"outputs_highlighted_text": "Detected Entities",
"outputs_json": "Raw Response Body",
"outputs_label": "Inference Time",
"slider_info": "Minimum confidence score for entities to be included in the response.",
"slider_label": "Confidence Threshold"
}
}
}