diff --git a/gliner_api/frontend.py b/gliner_api/frontend.py index f211a9e..56b5d7c 100644 --- a/gliner_api/frontend.py +++ b/gliner_api/frontend.py @@ -7,6 +7,7 @@ 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( @@ -14,6 +15,9 @@ 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, @@ -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, @@ -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", ) diff --git a/gliner_api/translations.py b/gliner_api/translations.py new file mode 100644 index 0000000..506eb3b --- /dev/null +++ b/gliner_api/translations.py @@ -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 diff --git a/main.py b/main.py index 6ec3553..ea8ae44 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,7 @@ 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( @@ -37,6 +37,7 @@ async def close_metrics_server(): interface, path="", show_api=False, + i18n=i18n, ) @app.on_event("shutdown") diff --git a/translations.json b/translations.json new file mode 100644 index 0000000..201cd2d --- /dev/null +++ b/translations.json @@ -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" + } + } +}