-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlanguage.py
More file actions
37 lines (30 loc) · 1.13 KB
/
language.py
File metadata and controls
37 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import json
from pathlib import Path
import streamlit as st
LANGUAGES = {"en": "🇬🇧 English", "fr": "🇫🇷 Français"}
@st.cache_data
def load_translations():
translations = {}
translation_path = Path(__file__).parent / "translations"
for lang_code, lang_file in [("en", "en.json"), ("fr", "fr.json")]:
try:
with open(translation_path / lang_file, "r", encoding="utf-8") as f:
translations[lang_code] = json.load(f)
except FileNotFoundError:
pass
if "en" not in translations:
translations["en"] = {
"app_title": "LLM Inference GPU Calculator",
"app_description": "Estimate the number of GPUs required to serve a Large Language Model (LLM)",
}
return translations
def get_text(key: str, **kwargs) -> str:
lang = st.session_state.get("language", "en")
translations = st.session_state.get("translations", {})
text = translations.get(lang, {}).get(key, translations.get("en", {}).get(key, key))
if kwargs:
try:
text = text.format(**kwargs)
except:
pass
return text