hoveyc commited on
Commit
f1597cc
1 Parent(s): 74851c1

Update example

Browse files
Files changed (2) hide show
  1. app.py +13 -34
  2. translations.yaml +9 -11
app.py CHANGED
@@ -1,42 +1,21 @@
1
- import os
2
-
3
  import gradio as gr
4
- import yaml
5
- from gradio_i18n import dump_blocks, gettext, translate_blocks
6
-
7
-
8
- def greet(name, age, gender, school):
9
- return f"hello {name}! Age: {age}"
10
-
11
-
12
- trans_file = "translations.yaml"
13
- if not os.path.exists(trans_file):
14
- lang_store = {}
15
- else:
16
- lang_store = yaml.safe_load(open(trans_file))
17
-
18
 
19
- with gr.Blocks() as block:
20
- with gr.Accordion(label=gettext("Accordion")):
21
- name = gr.Textbox(label=gettext("Submit"), value="World")
22
- greeting = gr.Textbox(placeholder=gettext("text"))
23
- age = gr.Slider(
24
- minimum=0,
25
- maximum=100,
26
- value=50,
27
- label=gettext("Age"),
28
- )
29
 
30
- gender = gr.Radio(choices=["male", "female"], label=gettext("Gender"))
31
- school = gr.Dropdown(["MIT", "Stanford", "CMU"], label=gettext("School"))
32
 
33
- btn = gr.Button(value=gettext("Submit"))
34
- btn.click(greet, [name, age, gender, school], [greeting])
35
 
36
- translate_blocks(block, lang_store)
37
 
38
- trans = dump_blocks(block, langs=["zh", "en"], include_translations=lang_store)
39
- yaml.safe_dump(trans, open(trans_file, "w"), allow_unicode=True)
 
 
 
 
 
40
 
41
- block.launch()
42
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ from gradio_i18n import Translate
4
+ from gradio_i18n import gettext as _
 
 
 
 
 
 
 
 
5
 
 
 
6
 
7
+ def greet(name, lang):
8
+ return f"Hello {name} in {lang}!"
9
 
 
10
 
11
+ with gr.Blocks() as demo:
12
+ gr.Markdown(value="> Check out [Repository](https://github.com/hoveychen/gradio-i18n) for more examples")
13
+ lang = gr.Radio(choices=[("English", "en"), ("中文", "zh")], label=_("Language"))
14
+ with Translate("translation.yaml", lang, placeholder_langs=["en", "zh"]):
15
+ name = gr.Textbox(label=_("Name"), placeholder=_("Input your name here."))
16
+ output = gr.Textbox(label=_("Greeting"))
17
+ submit_btn = gr.Button(value=_("Submit"))
18
 
19
+ submit_btn.click(greet, inputs=[name, lang], outputs=output)
20
 
21
+ demo.launch(server_name="0.0.0.0")
translations.yaml CHANGED
@@ -1,15 +1,13 @@
1
  en:
2
- Accordion: Advanced 😄
3
- Age: Age 📅
4
- Gender: Gender 🚻
5
- School: School 🏫
6
- Submit: Submit 🚀
7
- text: text
8
  zh:
9
- Accordion: 高级
10
- Age: 年龄
11
- Gender: 性别
12
- School: 学校
13
  Submit: 提交
14
- text: text
15
 
 
1
  en:
2
+ Greeting: Greeting
3
+ Input your name here.: Input your name here.
4
+ Language: Language
5
+ Name: Name
6
+ Submit: Submit
 
7
  zh:
8
+ Greeting: 打招呼
9
+ Input your name here.: 输入你的名字
10
+ Language: Language
11
+ Name: 名字
12
  Submit: 提交
 
13