-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
406 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
# Button | ||
# Application | ||
|
||
See [Ant Design](https://ant.design/components/button/) for more information. | ||
应用的根组件,该组件包含了所有`modelscope_studio`的组件依赖,需要确保所有从`modelscope_studio`导出的组件都被其包裹,否则页面将会无法成功预览。 | ||
|
||
该组件还可以监听用户页面的生命周期,并获取当前用户的环境信息,您可以 | ||
|
||
- 获取当前用户的语言、页面主题、user agent 和屏幕状态。 | ||
- 监听页面行为并触发相应事件(页面加载、尺寸变化、页面关闭等)。 | ||
|
||
另外,该组件还提供了`custom`事件,您可以通过在任意 Javascript 函数中调用`window.ms_globals.dispatch`主动向 Python 端发送事件,在 Python 端可以通过`ms.Application.custom`事件接收。 | ||
|
||
## Examples | ||
|
||
<demo name="basic"></demo> | ||
|
||
<demo name="language_adaptation" title="自动适配用户语言环境"></demo> | ||
|
||
<demo name="theme_adaptation" title="根据用户界面主题返回不同权重内容"></demo> | ||
|
||
<demo name="custom_event" title="发送自定义事件"></demo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
# Application | ||
|
||
A Provider for all components of modelscope_studio. | ||
The root component of the application, this component contains all the component dependencies of `modelscope_studio`. It is necessary to ensure that all components exported from `modelscope_studio` are wrapped by it, otherwise the page will not be successfully previewed. | ||
|
||
In addition, this component can also listen to the lifecycle of the user's page and obtain the current user's environment information, you can: | ||
|
||
- Obtain the current user's language, page theme, user agent, and screen status. | ||
- Listen to page behaviors and trigger corresponding events (page loading, size changes, page closing, etc.). | ||
|
||
In addition, this component provides the `custom` event, you can send events to the Python side by calling `window.ms_globals.dispatch` in any Javascript function, and receive the events on the Python side through the `ms.Application.custom` event. | ||
|
||
## Examples | ||
|
||
<demo name="basic"></demo> | ||
|
||
<demo name="language_adaptation" title="Automatically adapt to user language environment"></demo> | ||
|
||
<demo name="theme_adaptation" title="Return different weighted content based on user interface theme"></demo> | ||
|
||
<demo name="custom_event" title="Send custom events"></demo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import gradio as gr | ||
|
||
import modelscope_studio.components.antd as antd | ||
import modelscope_studio.components.base as ms | ||
|
||
|
||
def on_custom(e: gr.EventData): | ||
print(e) | ||
|
||
|
||
with gr.Blocks() as demo: | ||
with ms.Application() as app: | ||
with antd.ConfigProvider(): | ||
gr.HTML( | ||
"""<button onclick="window.ms_globals.dispatch({type: 'custom', data: 'test'})">Click me</button>""" | ||
) | ||
|
||
app.custom(on_custom) | ||
if __name__ == "__main__": | ||
demo.queue().launch() |
39 changes: 39 additions & 0 deletions
39
docs/components/base/application/demos/language_adaptation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import time | ||
|
||
import gradio as gr | ||
|
||
import modelscope_studio.components.base as ms | ||
|
||
messages = { | ||
'en': { | ||
"hello": "Hello" | ||
}, | ||
'en-US': { | ||
"hello": "Hello" | ||
}, | ||
'zh-CN': { | ||
"hello": "你好" | ||
} | ||
} | ||
|
||
default_lang = "en" | ||
|
||
|
||
def mount(e: gr.EventData, _state): | ||
lang = e._data["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: | ||
with ms.Application() as app: | ||
state = gr.State({"current_lang": default_lang}) | ||
markdown = gr.Markdown(value=messages[default_lang]["hello"]) | ||
|
||
app.mount(fn=mount, inputs=[state], outputs=[markdown, state]) | ||
|
||
if __name__ == "__main__": | ||
demo.queue().launch() |
34 changes: 34 additions & 0 deletions
34
docs/components/base/application/demos/theme_adaptation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import gradio as gr | ||
|
||
import modelscope_studio.components.antd as antd | ||
import modelscope_studio.components.base as ms | ||
|
||
|
||
def mount(e: gr.EventData, _state): | ||
_state["theme"] = e._data["theme"] | ||
yield _state | ||
|
||
|
||
def fn(_state): | ||
theme = _state["theme"] | ||
color = '000/fff' if theme == 'dark' else 'fff/000' | ||
yield gr.update( | ||
value=f"https://dummyimage.com/200x100/{color}.png&text={theme}") | ||
|
||
|
||
with gr.Blocks() as demo: | ||
state = gr.State({"theme": "light"}) | ||
with ms.Application() as app: | ||
with antd.ConfigProvider(): | ||
btn = antd.Button( | ||
"Run", | ||
type="primary", | ||
block=True, | ||
) | ||
image = antd.Image() | ||
|
||
app.mount(fn=mount, inputs=[state], outputs=[state]) | ||
btn.click(fn=fn, inputs=[state], outputs=[image]) | ||
|
||
if __name__ == "__main__": | ||
demo.queue().launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,54 @@ | ||
# Button | ||
# ModelScope Studio | ||
|
||
See [Ant Design](https://ant.design/components/button/) for more information. | ||
<p align="center"> | ||
<img src="https://modelscope.oss-cn-beijing.aliyuncs.com/modelscope.gif" height="60" style="vertical-align: middle;"/> | ||
<span style="font-size: 30px; vertical-align: middle;"> | ||
✖️ | ||
</span> | ||
<img src="https://github.com/gradio-app/gradio/raw/main/readme_files/gradio.svg" height="60" style="vertical-align: middle;"> | ||
<p> | ||
|
||
## Examples | ||
<p align="center"> | ||
<a href="https://github.com/modelscope/modelscope-studio">GitHub</a> | 🤖 <a href="https://modelscope.cn/studios/modelscope/modelscope-studio/summary">ModelScope Studio</a> | 🤗 <a href="https://huggingface.co/spaces/modelscope/modelscope-studio">Hugging Face Space</a> | ||
|
||
<demo name="basic"></demo> | ||
`modelscope_studio`是一个基于 Gradio 的三方组件库,在原有 Gradio 组件的基础上延伸了更多的组件和使用形式。 | ||
|
||
目前支持的 UI 库: | ||
|
||
- [Ant Design](https://ant.design/) | ||
|
||
## 何时使用 | ||
|
||
比起 Gradio 自身的组件,`modelscope_studio`更加注重页面布局和组件的灵活性,如果您想要构建更漂亮的用户界面,我们非常推荐您使用`modelscope_studio`。然而,当您的应用需要 Gradio 在 Python 端更多地处理内置数据时,`modelscope_studio`可能不是最好的选择,但仍然可以使用`modelscope_studio`的布局和展示组件来帮助您构建页面。 | ||
|
||
## 依赖 | ||
|
||
- Gradio >= 4.0 | ||
|
||
## 安装 | ||
|
||
> 目前`modelscope_studio` 1.0 版本仍在开发中,您可以通过安装`beta`版本提前使用。 | ||
```sh | ||
pip install modelscope_studio~=1.0.0b | ||
``` | ||
|
||
## 示例 | ||
|
||
<demo name="example"></demo> | ||
|
||
## 迁移到 1.0 | ||
|
||
如果您在之前使用了`modelscope_studio`的组件,并且想要在新版本中继续使用。不需要对原有组件做任何修改,只需要在外层引入`ms.Application`即可。 | ||
|
||
```python | ||
import gradio as gr | ||
import modelscope_studio.components.base as ms | ||
import modelscope_studio.components.legacy as mgr | ||
|
||
with gr.Blocks() as demo: | ||
with ms.Application(): | ||
mgr.Chatbot() | ||
|
||
demo.launch() | ||
``` |
Oops, something went wrong.