hayas commited on
Commit
375fa8a
1 Parent(s): b3c0b17
Files changed (10) hide show
  1. .gitignore +162 -0
  2. .pre-commit-config.yaml +60 -0
  3. .python-version +1 -0
  4. .vscode/settings.json +30 -0
  5. README.md +4 -3
  6. app.py +84 -0
  7. pyproject.toml +9 -0
  8. requirements.txt +146 -0
  9. style.css +11 -0
  10. uv.lock +0 -0
.gitignore ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .gradio/
2
+
3
+ # Byte-compiled / optimized / DLL files
4
+ __pycache__/
5
+ *.py[cod]
6
+ *$py.class
7
+
8
+ # C extensions
9
+ *.so
10
+
11
+ # Distribution / packaging
12
+ .Python
13
+ build/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ lib/
20
+ lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ wheels/
25
+ share/python-wheels/
26
+ *.egg-info/
27
+ .installed.cfg
28
+ *.egg
29
+ MANIFEST
30
+
31
+ # PyInstaller
32
+ # Usually these files are written by a python script from a template
33
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
34
+ *.manifest
35
+ *.spec
36
+
37
+ # Installer logs
38
+ pip-log.txt
39
+ pip-delete-this-directory.txt
40
+
41
+ # Unit test / coverage reports
42
+ htmlcov/
43
+ .tox/
44
+ .nox/
45
+ .coverage
46
+ .coverage.*
47
+ .cache
48
+ nosetests.xml
49
+ coverage.xml
50
+ *.cover
51
+ *.py,cover
52
+ .hypothesis/
53
+ .pytest_cache/
54
+ cover/
55
+
56
+ # Translations
57
+ *.mo
58
+ *.pot
59
+
60
+ # Django stuff:
61
+ *.log
62
+ local_settings.py
63
+ db.sqlite3
64
+ db.sqlite3-journal
65
+
66
+ # Flask stuff:
67
+ instance/
68
+ .webassets-cache
69
+
70
+ # Scrapy stuff:
71
+ .scrapy
72
+
73
+ # Sphinx documentation
74
+ docs/_build/
75
+
76
+ # PyBuilder
77
+ .pybuilder/
78
+ target/
79
+
80
+ # Jupyter Notebook
81
+ .ipynb_checkpoints
82
+
83
+ # IPython
84
+ profile_default/
85
+ ipython_config.py
86
+
87
+ # pyenv
88
+ # For a library or package, you might want to ignore these files since the code is
89
+ # intended to run in multiple environments; otherwise, check them in:
90
+ # .python-version
91
+
92
+ # pipenv
93
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
94
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
95
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
96
+ # install all needed dependencies.
97
+ #Pipfile.lock
98
+
99
+ # poetry
100
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
101
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
102
+ # commonly ignored for libraries.
103
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
104
+ #poetry.lock
105
+
106
+ # pdm
107
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
108
+ #pdm.lock
109
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
110
+ # in version control.
111
+ # https://pdm.fming.dev/#use-with-ide
112
+ .pdm.toml
113
+
114
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115
+ __pypackages__/
116
+
117
+ # Celery stuff
118
+ celerybeat-schedule
119
+ celerybeat.pid
120
+
121
+ # SageMath parsed files
122
+ *.sage.py
123
+
124
+ # Environments
125
+ .env
126
+ .venv
127
+ env/
128
+ venv/
129
+ ENV/
130
+ env.bak/
131
+ venv.bak/
132
+
133
+ # Spyder project settings
134
+ .spyderproject
135
+ .spyproject
136
+
137
+ # Rope project settings
138
+ .ropeproject
139
+
140
+ # mkdocs documentation
141
+ /site
142
+
143
+ # mypy
144
+ .mypy_cache/
145
+ .dmypy.json
146
+ dmypy.json
147
+
148
+ # Pyre type checker
149
+ .pyre/
150
+
151
+ # pytype static type analyzer
152
+ .pytype/
153
+
154
+ # Cython debug symbols
155
+ cython_debug/
156
+
157
+ # PyCharm
158
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
161
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
+ #.idea/
.pre-commit-config.yaml ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.6.0
4
+ hooks:
5
+ - id: check-executables-have-shebangs
6
+ - id: check-json
7
+ - id: check-merge-conflict
8
+ - id: check-shebang-scripts-are-executable
9
+ - id: check-toml
10
+ - id: check-yaml
11
+ - id: end-of-file-fixer
12
+ - id: mixed-line-ending
13
+ args: ["--fix=lf"]
14
+ - id: requirements-txt-fixer
15
+ - id: trailing-whitespace
16
+ - repo: https://github.com/myint/docformatter
17
+ rev: v1.7.5
18
+ hooks:
19
+ - id: docformatter
20
+ args: ["--in-place"]
21
+ - repo: https://github.com/pycqa/isort
22
+ rev: 5.13.2
23
+ hooks:
24
+ - id: isort
25
+ args: ["--profile", "black"]
26
+ - repo: https://github.com/pre-commit/mirrors-mypy
27
+ rev: v1.11.2
28
+ hooks:
29
+ - id: mypy
30
+ args: ["--ignore-missing-imports"]
31
+ additional_dependencies:
32
+ [
33
+ "types-python-slugify",
34
+ "types-requests",
35
+ "types-PyYAML",
36
+ "types-pytz",
37
+ ]
38
+ - repo: https://github.com/psf/black
39
+ rev: 24.8.0
40
+ hooks:
41
+ - id: black
42
+ language_version: python3.10
43
+ args: ["--line-length", "119"]
44
+ - repo: https://github.com/kynan/nbstripout
45
+ rev: 0.7.1
46
+ hooks:
47
+ - id: nbstripout
48
+ args:
49
+ [
50
+ "--extra-keys",
51
+ "metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
52
+ ]
53
+ - repo: https://github.com/nbQA-dev/nbQA
54
+ rev: 1.8.7
55
+ hooks:
56
+ - id: nbqa-black
57
+ - id: nbqa-pyupgrade
58
+ args: ["--py37-plus"]
59
+ - id: nbqa-isort
60
+ args: ["--float-to-top"]
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.10
.vscode/settings.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "editor.formatOnSave": true,
3
+ "files.insertFinalNewline": false,
4
+ "[python]": {
5
+ "editor.defaultFormatter": "ms-python.black-formatter",
6
+ "editor.formatOnType": true,
7
+ "editor.codeActionsOnSave": {
8
+ "source.organizeImports": "explicit"
9
+ }
10
+ },
11
+ "[jupyter]": {
12
+ "files.insertFinalNewline": false
13
+ },
14
+ "black-formatter.args": [
15
+ "--line-length=119"
16
+ ],
17
+ "isort.args": ["--profile", "black"],
18
+ "flake8.args": [
19
+ "--max-line-length=119"
20
+ ],
21
+ "ruff.lint.args": [
22
+ "--line-length=119"
23
+ ],
24
+ "notebook.output.scrolling": true,
25
+ "notebook.formatOnCellExecution": true,
26
+ "notebook.formatOnSave.enabled": true,
27
+ "notebook.codeActionsOnSave": {
28
+ "source.organizeImports": "explicit"
29
+ }
30
+ }
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
  title: Gradio Language Switch Demo
3
- emoji: 📚
4
- colorFrom: green
5
- colorTo: blue
6
  sdk: gradio
7
  sdk_version: 5.1.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Gradio Language Switch Demo
3
+ emoji:
4
+ colorFrom: red
5
+ colorTo: purple
6
  sdk: gradio
7
  sdk_version: 5.1.0
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ import gradio as gr
4
+
5
+ DESCRIPTION_JA = """\
6
+ # 言語切り替えデモ
7
+
8
+ これは Gradio での言語切り替えのデモです。
9
+
10
+ このアプリではブラウザの言語設定に応じて、このテキスト自身と、テキストボックスのラベルとプレースホルダーを切り替えます。\
11
+ また、ページ下部のボタンでも言語を切り替えることができます。
12
+
13
+ このデモでは簡単のため、`gr.Markdown` と `gr.Textbox` のみを切り替えますが、\
14
+ 同様にして、他のコンポーネントの表示言語も切り替えることができます。
15
+ """
16
+ DESCRIPTION_EN = """\
17
+ # Language Switch Demo
18
+
19
+ This is a demo of language switching in Gradio.
20
+
21
+ This app switches this text itself, as well as the textbox label and placeholder, according to the browser's language settings.
22
+ You can also switch languages using the button at the bottom of the page.
23
+
24
+ For simplicity, this demo only switches the `gr.Markdown` and `gr.Textbox`, but
25
+ you can switch the display language of other components in the same way.
26
+ """
27
+ TEXTBOX_LABEL_JA = "入力テキスト"
28
+ TEXTBOX_LABEL_EN = "Input Text"
29
+ TEXTBOX_PLACEHOLDER_JA = "ここにテキストを入力してください。"
30
+ TEXTBOX_PLACEHOLDER_EN = "Please input text here."
31
+
32
+
33
+ def set_default_language(request: gr.Request) -> gr.Radio:
34
+ if request.headers["Accept-Language"].split(",")[0].lower().startswith("ja"):
35
+ return gr.Radio(value="🇯🇵 JA")
36
+ else:
37
+ return gr.Radio(value="🇺🇸 EN")
38
+
39
+
40
+ def switch_ui_language(language: str) -> tuple[gr.Markdown, gr.Textbox]:
41
+ if language == "🇯🇵 JA":
42
+ return (
43
+ gr.Markdown(value=DESCRIPTION_JA),
44
+ gr.Textbox(label=TEXTBOX_LABEL_JA, placeholder=TEXTBOX_PLACEHOLDER_JA),
45
+ )
46
+ else:
47
+ return (
48
+ gr.Markdown(value=DESCRIPTION_EN),
49
+ gr.Textbox(label=TEXTBOX_LABEL_EN, placeholder=TEXTBOX_PLACEHOLDER_EN),
50
+ )
51
+
52
+
53
+ with gr.Blocks(css_paths="style.css") as demo:
54
+ description = gr.Markdown(DESCRIPTION_EN)
55
+
56
+ text = gr.Textbox(label=TEXTBOX_LABEL_EN, placeholder=TEXTBOX_PLACEHOLDER_EN)
57
+
58
+ language_selector = gr.Radio(
59
+ choices=["🇯🇵 JA", "🇺🇸 EN"],
60
+ value="🇯🇵 JA",
61
+ elem_classes="language-selector",
62
+ show_label=False,
63
+ container=False,
64
+ )
65
+
66
+ demo.load(
67
+ fn=set_default_language,
68
+ outputs=language_selector,
69
+ api_name=False,
70
+ queue=False,
71
+ )
72
+ language_selector.change(
73
+ fn=switch_ui_language,
74
+ inputs=language_selector,
75
+ outputs=[
76
+ description,
77
+ text,
78
+ ],
79
+ api_name=False,
80
+ queue=False,
81
+ )
82
+
83
+ if __name__ == "__main__":
84
+ demo.launch()
pyproject.toml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "gradio-language-switch-demo"
3
+ version = "0.1.0"
4
+ description = ""
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ dependencies = [
8
+ "gradio>=5.1.0",
9
+ ]
requirements.txt ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file was autogenerated by uv via the following command:
2
+ # uv pip compile pyproject.toml -o requirements.txt
3
+ aiofiles==23.2.1
4
+ # via gradio
5
+ annotated-types==0.7.0
6
+ # via pydantic
7
+ anyio==4.6.2.post1
8
+ # via
9
+ # gradio
10
+ # httpx
11
+ # starlette
12
+ certifi==2024.8.30
13
+ # via
14
+ # httpcore
15
+ # httpx
16
+ # requests
17
+ charset-normalizer==3.4.0
18
+ # via requests
19
+ click==8.1.7
20
+ # via
21
+ # typer
22
+ # uvicorn
23
+ exceptiongroup==1.2.2
24
+ # via anyio
25
+ fastapi==0.115.2
26
+ # via gradio
27
+ ffmpy==0.4.0
28
+ # via gradio
29
+ filelock==3.16.1
30
+ # via huggingface-hub
31
+ fsspec==2024.9.0
32
+ # via
33
+ # gradio-client
34
+ # huggingface-hub
35
+ gradio==5.1.0
36
+ # via gradio-language-switch-demo (pyproject.toml)
37
+ gradio-client==1.4.0
38
+ # via gradio
39
+ h11==0.14.0
40
+ # via
41
+ # httpcore
42
+ # uvicorn
43
+ httpcore==1.0.6
44
+ # via httpx
45
+ httpx==0.27.2
46
+ # via
47
+ # gradio
48
+ # gradio-client
49
+ huggingface-hub==0.26.0
50
+ # via
51
+ # gradio
52
+ # gradio-client
53
+ idna==3.10
54
+ # via
55
+ # anyio
56
+ # httpx
57
+ # requests
58
+ jinja2==3.1.4
59
+ # via gradio
60
+ markdown-it-py==3.0.0
61
+ # via rich
62
+ markupsafe==2.1.5
63
+ # via
64
+ # gradio
65
+ # jinja2
66
+ mdurl==0.1.2
67
+ # via markdown-it-py
68
+ numpy==2.1.2
69
+ # via
70
+ # gradio
71
+ # pandas
72
+ orjson==3.10.9
73
+ # via gradio
74
+ packaging==24.1
75
+ # via
76
+ # gradio
77
+ # gradio-client
78
+ # huggingface-hub
79
+ pandas==2.2.3
80
+ # via gradio
81
+ pillow==10.4.0
82
+ # via gradio
83
+ pydantic==2.9.2
84
+ # via
85
+ # fastapi
86
+ # gradio
87
+ pydantic-core==2.23.4
88
+ # via pydantic
89
+ pydub==0.25.1
90
+ # via gradio
91
+ pygments==2.18.0
92
+ # via rich
93
+ python-dateutil==2.9.0.post0
94
+ # via pandas
95
+ python-multipart==0.0.12
96
+ # via gradio
97
+ pytz==2024.2
98
+ # via pandas
99
+ pyyaml==6.0.2
100
+ # via
101
+ # gradio
102
+ # huggingface-hub
103
+ requests==2.32.3
104
+ # via huggingface-hub
105
+ rich==13.9.2
106
+ # via typer
107
+ ruff==0.7.0
108
+ # via gradio
109
+ semantic-version==2.10.0
110
+ # via gradio
111
+ shellingham==1.5.4
112
+ # via typer
113
+ six==1.16.0
114
+ # via python-dateutil
115
+ sniffio==1.3.1
116
+ # via
117
+ # anyio
118
+ # httpx
119
+ starlette==0.40.0
120
+ # via fastapi
121
+ tomlkit==0.12.0
122
+ # via gradio
123
+ tqdm==4.66.5
124
+ # via huggingface-hub
125
+ typer==0.12.5
126
+ # via gradio
127
+ typing-extensions==4.12.2
128
+ # via
129
+ # anyio
130
+ # fastapi
131
+ # gradio
132
+ # gradio-client
133
+ # huggingface-hub
134
+ # pydantic
135
+ # pydantic-core
136
+ # rich
137
+ # typer
138
+ # uvicorn
139
+ tzdata==2024.2
140
+ # via pandas
141
+ urllib3==2.2.3
142
+ # via requests
143
+ uvicorn==0.32.0
144
+ # via gradio
145
+ websockets==12.0
146
+ # via gradio-client
style.css ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ display: block;
4
+ }
5
+
6
+ .language-selector {
7
+ width: auto;
8
+ display: flex;
9
+ justify-content: center;
10
+ margin: 20px 0;
11
+ }
uv.lock ADDED
The diff for this file is too large to render. See raw diff