File size: 909 Bytes
ba4a662
 
 
afb453a
1969c22
ba4a662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afb453a
ba4a662
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
import time

import gradio as gr
import modelscope_studio.components.base as ms
import modelscope_studio.components.legacy as mgr

messages = {
    'en': {
        "hello": "Hello"
    },
    'en-US': {
        "hello": "Hello"
    },
    'zh-CN': {
        "hello": "你好"
    }
}

default_lang = "en"


def mount(_lifecycle, _state):
    lang = _lifecycle.language
    if (lang in messages):
        _state["current_lang"] = lang
    yield 'Switch Language...', _state
    time.sleep(2)
    yield messages[lang]["hello"], _state


with gr.Blocks() as demo, ms.Application():
    lifecycle = mgr.Lifecycle()
    state = gr.State({"current_lang": default_lang})
    markdown = gr.Markdown(value=messages[default_lang]["hello"])

    lifecycle.mount(fn=mount,
                    inputs=[lifecycle, state],
                    outputs=[markdown, state])

if __name__ == "__main__":
    demo.queue().launch()