freddyaboulton HF Staff commited on
Commit
616b201
·
verified ·
1 Parent(s): 71ef6df

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +7 -7
  2. requirements.txt +2 -0
  3. run.ipynb +1 -0
  4. run.py +39 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: I18n Main
3
- emoji: 📚
4
- colorFrom: gray
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 5.29.1
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
  ---
3
+ title: i18n_main
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
  sdk_version: 5.29.1
9
+ app_file: run.py
10
  pinned: false
11
+ hf_oauth: true
12
  ---
 
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio-client @ git+https://github.com/gradio-app/gradio@6d4b8a7f10daefc9c79aa224635da23fbaeebb76#subdirectory=client/python
2
+ https://gradio-pypi-previews.s3.amazonaws.com/6d4b8a7f10daefc9c79aa224635da23fbaeebb76/gradio-5.29.1-py3-none-any.whl
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: i18n"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "# create an i18n instance with translations for different languages\n", "i18n = gr.I18n(\n", " en={\"name_label\": \"Your Name\", \"submit_button\": \"Greet\", \"john_doe\": \"John English\", \"result_label\": \"Result\"},\n", " es={\"name_label\": \"Tu Nombre\", \"submit_button\": \"Saludar\", \"john_doe\": \"John Spanish\", \"result_label\": \"Resultado\"},\n", " fr={\"name_label\": \"Votre Nom\", \"submit_button\": \"Saluer\", \"john_doe\": \"John French\", \"result_label\": \"R\u00e9sultat\"},\n", " de={\"name_label\": \"Dein Name\", \"submit_button\": \"Gr\u00fc\u00dfen\", \"john_doe\": \"John German\", \"result_label\": \"Ergebnis\"},\n", ")\n", "\n", "def add_hello_world(name):\n", " return \"hello \" + name\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " # use i18n() for any string that should be translated\n", " name_input = gr.Textbox(label=i18n(\"name_label\"), value=i18n(\"john_doe\"))\n", "\n", " with gr.Row():\n", " output_text = gr.Textbox(label=i18n(\"result_label\"))\n", "\n", " with gr.Row():\n", " greet_btn = gr.Button(value=i18n(\"submit_button\"))\n", "\n", " with gr.Row():\n", " reset_btn = gr.Button(\"Reset Name\")\n", "\n", " greet_btn.click(fn=add_hello_world, inputs=name_input, outputs=output_text)\n", " reset_btn.click(fn=lambda: i18n(\"john_doe\"), inputs=None, outputs=name_input)\n", "\n", " gr.Markdown(\"\"\"\n", " This demo shows Gradio's internationalization (i18n) functionality. \n", " The interface automatically displays text in the user's browser language \n", " (if available in our translations), or falls back to English.\n", " \"\"\")\n", "\n", "if __name__ == \"__main__\":\n", " # pass i18n to the launch function\n", " demo.launch(i18n=i18n)\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # create an i18n instance with translations for different languages
4
+ i18n = gr.I18n(
5
+ en={"name_label": "Your Name", "submit_button": "Greet", "john_doe": "John English", "result_label": "Result"},
6
+ es={"name_label": "Tu Nombre", "submit_button": "Saludar", "john_doe": "John Spanish", "result_label": "Resultado"},
7
+ fr={"name_label": "Votre Nom", "submit_button": "Saluer", "john_doe": "John French", "result_label": "Résultat"},
8
+ de={"name_label": "Dein Name", "submit_button": "Grüßen", "john_doe": "John German", "result_label": "Ergebnis"},
9
+ )
10
+
11
+ def add_hello_world(name):
12
+ return "hello " + name
13
+
14
+ with gr.Blocks() as demo:
15
+ with gr.Row():
16
+ # use i18n() for any string that should be translated
17
+ name_input = gr.Textbox(label=i18n("name_label"), value=i18n("john_doe"))
18
+
19
+ with gr.Row():
20
+ output_text = gr.Textbox(label=i18n("result_label"))
21
+
22
+ with gr.Row():
23
+ greet_btn = gr.Button(value=i18n("submit_button"))
24
+
25
+ with gr.Row():
26
+ reset_btn = gr.Button("Reset Name")
27
+
28
+ greet_btn.click(fn=add_hello_world, inputs=name_input, outputs=output_text)
29
+ reset_btn.click(fn=lambda: i18n("john_doe"), inputs=None, outputs=name_input)
30
+
31
+ gr.Markdown("""
32
+ This demo shows Gradio's internationalization (i18n) functionality.
33
+ The interface automatically displays text in the user's browser language
34
+ (if available in our translations), or falls back to English.
35
+ """)
36
+
37
+ if __name__ == "__main__":
38
+ # pass i18n to the launch function
39
+ demo.launch(i18n=i18n)