Spaces:
Running
Running
feat: init
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +3 -1
- .gitignore +60 -0
- Dockerfile +15 -0
- README-zh_CN.md +26 -0
- README.md +40 -23
- app.py +22 -0
- components/Chatbot/README-zh_CN.md +137 -0
- components/Chatbot/README.md +136 -0
- components/Chatbot/app.py +6 -0
- components/Chatbot/demos/accordion.py +49 -0
- components/Chatbot/demos/basic.py +55 -0
- components/Chatbot/demos/message_config.py +35 -0
- components/Chatbot/demos/multi_bots.py +87 -0
- components/Chatbot/demos/multimodal.py +43 -0
- components/Chatbot/demos/select-box.py +48 -0
- components/Chatbot/resources/audio.wav +0 -0
- components/Chatbot/resources/bot.jpeg +0 -0
- components/Chatbot/resources/custom_components/custom_select.js +26 -0
- components/Chatbot/resources/dog.mp4 +3 -0
- components/Chatbot/resources/image-bot.jpeg +0 -0
- components/Chatbot/resources/music-bot.jpeg +0 -0
- components/Chatbot/resources/screen.jpeg +0 -0
- components/Chatbot/resources/user.jpeg +0 -0
- components/Docs.py +162 -0
- components/Markdown/README-zh_CN.md +89 -0
- components/Markdown/README.md +85 -0
- components/Markdown/app.py +20 -0
- components/Markdown/custom_tags/accordion-zh_CN.md +23 -0
- components/Markdown/custom_tags/accordion.md +22 -0
- components/Markdown/custom_tags/select-box-zh_CN.md +45 -0
- components/Markdown/custom_tags/select-box.md +45 -0
- components/Markdown/demos/accordion.py +28 -0
- components/Markdown/demos/basic.py +11 -0
- components/Markdown/demos/custom-tag.py +21 -0
- components/Markdown/demos/custom-tag2.py +38 -0
- components/Markdown/demos/custom-tag3.py +31 -0
- components/Markdown/demos/custom-tag4.py +38 -0
- components/Markdown/demos/custom_tags/accordion/accordion-title.py +19 -0
- components/Markdown/demos/custom_tags/accordion/basic.py +17 -0
- components/Markdown/demos/custom_tags/select-box/basic.py +21 -0
- components/Markdown/demos/custom_tags/select-box/card_shape.py +32 -0
- components/Markdown/demos/custom_tags/select-box/card_shape_width_auto.py +26 -0
- components/Markdown/demos/custom_tags/select-box/python_events.py +24 -0
- components/Markdown/demos/multimodal.py +31 -0
- components/Markdown/demos/select-box.py +32 -0
- components/Markdown/resources/audio.wav +0 -0
- components/Markdown/resources/bot.jpeg +0 -0
- components/Markdown/resources/custom_components/custom_select.js +26 -0
- components/Markdown/resources/dog.mp4 +3 -0
- components/Markdown/resources/screen.jpeg +0 -0
.gitattributes
CHANGED
@@ -31,4 +31,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
31 |
**/*ckpt*.meta filter=lfs diff=lfs merge=lfs -text
|
32 |
**/*ckpt*.index filter=lfs diff=lfs merge=lfs -text
|
33 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
34 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
31 |
**/*ckpt*.meta filter=lfs diff=lfs merge=lfs -text
|
32 |
**/*ckpt*.index filter=lfs diff=lfs merge=lfs -text
|
33 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*.whl filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.eggs/
|
2 |
+
dist/
|
3 |
+
*.pyc
|
4 |
+
__pycache__/
|
5 |
+
*.py[cod]
|
6 |
+
*$py.class
|
7 |
+
__tmp/*
|
8 |
+
*.pyi
|
9 |
+
templates
|
10 |
+
|
11 |
+
# common
|
12 |
+
|
13 |
+
!.*ignore
|
14 |
+
!.*rc
|
15 |
+
!.gitattributes
|
16 |
+
!.aoneci.yml
|
17 |
+
!.editorconfig
|
18 |
+
|
19 |
+
# Logs
|
20 |
+
logs
|
21 |
+
*.log*
|
22 |
+
|
23 |
+
# Runtime data
|
24 |
+
pids
|
25 |
+
*.pid
|
26 |
+
*.seed
|
27 |
+
*.pid.lock
|
28 |
+
|
29 |
+
# Directory for instrumented libs generated by jscoverage/JSCover
|
30 |
+
lib-cov
|
31 |
+
|
32 |
+
# Coverage directory used by tools like istanbul
|
33 |
+
coverage
|
34 |
+
|
35 |
+
# Dependency directories
|
36 |
+
bower_components
|
37 |
+
node_modules/
|
38 |
+
jspm_packages/
|
39 |
+
|
40 |
+
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
41 |
+
build/Release
|
42 |
+
lib
|
43 |
+
dist
|
44 |
+
|
45 |
+
# TypeScript v1 declaration files
|
46 |
+
typings/
|
47 |
+
|
48 |
+
# Output of 'npm pack'
|
49 |
+
*.tgz
|
50 |
+
|
51 |
+
# xconsole
|
52 |
+
src/.xconsole
|
53 |
+
build
|
54 |
+
.faas_debug_tmp
|
55 |
+
.yarn
|
56 |
+
.yalc
|
57 |
+
yalc.lock
|
58 |
+
.eslintcache
|
59 |
+
.stylelintcache
|
60 |
+
.DS_Store
|
Dockerfile
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY --link --chown=1000 . .
|
6 |
+
|
7 |
+
RUN mkdir -p /tmp/cache/
|
8 |
+
RUN chmod a+rwx -R /tmp/cache/
|
9 |
+
ENV TRANSFORMERS_CACHE=/tmp/cache/
|
10 |
+
|
11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
|
13 |
+
ENV PYTHONUNBUFFERED=1 GRADIO_ALLOW_FLAGGING=never GRADIO_NUM_PORTS=1 GRADIO_SERVER_NAME=0.0.0.0 GRADIO_SERVER_PORT=7860 SYSTEM=spaces
|
14 |
+
|
15 |
+
CMD ["python", "app.py"]
|
README-zh_CN.md
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<h1>Modelscope Studio</h1>
|
2 |
+
|
3 |
+
<p align="center">
|
4 |
+
<img src="https://modelscope.oss-cn-beijing.aliyuncs.com/modelscope.gif" height="60" style="vertical-align: middle;"/>
|
5 |
+
<span style="font-size: 30px; vertical-align: middle;">
|
6 |
+
✖️
|
7 |
+
</span>
|
8 |
+
<img src="https://www.gradio.app/_app/immutable/assets/gradio.8a5e8876.svg" height="60" style="vertical-align: middle;">
|
9 |
+
<p>
|
10 |
+
|
11 |
+
<p align="center">
|
12 |
+
<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">HuggingFace Space</a>
|
13 |
+
|
14 |
+
`modelscope_studio` 是一套基于 gradio 4.x 的扩展组件库,致力于服务于 ModelScope 创空间中对于 gradio 应用的各类扩展需求,目前主要聚集在对话场景增强、多模态场景以及一些其他垂直场景支持。
|
15 |
+
|
16 |
+
## Install
|
17 |
+
|
18 |
+
```sh
|
19 |
+
pip install modelscope_studio
|
20 |
+
```
|
21 |
+
|
22 |
+
## Components
|
23 |
+
|
24 |
+
- <tab-link component-tab="Chatbot">Chatbot</tab-link>
|
25 |
+
- <tab-link component-tab="MultimodalInput">MultimodalInput</tab-link>
|
26 |
+
- <tab-link component-tab="Markdown">Markdown</tab-link>
|
README.md
CHANGED
@@ -1,25 +1,42 @@
|
|
1 |
---
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
#- iic/ofa_ocr-recognition_general_base_zh
|
16 |
-
|
17 |
-
## 启动文件(若SDK为Gradio/Streamlit,默认为app.py, 若为Static HTML, 默认为index.html)
|
18 |
-
# deployspec:
|
19 |
-
# entry_file: app.py
|
20 |
-
license: Apache License 2.0
|
21 |
---
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
tags:
|
3 |
+
- gradio-custom-component
|
4 |
+
- Chatbot
|
5 |
+
- MutilmodalInput
|
6 |
+
- Markdown
|
7 |
+
- gradio-template-Chatbot
|
8 |
+
- gradio-template-Markdown
|
9 |
+
title: modelscope-studio
|
10 |
+
colorFrom: blue
|
11 |
+
colorTo: gray
|
12 |
+
sdk: docker
|
13 |
+
pinned: false
|
14 |
+
license: apache-2.0
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
---
|
16 |
+
|
17 |
+
<h1>Modelscope Studio</h1>
|
18 |
+
|
19 |
+
<p align="center">
|
20 |
+
<img src="https://modelscope.oss-cn-beijing.aliyuncs.com/modelscope.gif" height="60" style="vertical-align: middle;"/>
|
21 |
+
<span style="font-size: 30px; vertical-align: middle;">
|
22 |
+
✖️
|
23 |
+
</span>
|
24 |
+
<img src="https://www.gradio.app/_app/immutable/assets/gradio.8a5e8876.svg" height="60" style="vertical-align: middle;">
|
25 |
+
<p>
|
26 |
+
|
27 |
+
<p align="center">
|
28 |
+
<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">HuggingFace Space</a>
|
29 |
+
|
30 |
+
`modelscope_studio` is a set of extension component libraries based on gradio 4.x, dedicated to serving the various extension needs of gradio applications within the ModelScope Studio. It mainly focuses on enhancing conversational scenarios, supporting multimodal contexts, and providing assistance for various other specialized scenarios.
|
31 |
+
|
32 |
+
## Install
|
33 |
+
|
34 |
+
```sh
|
35 |
+
pip install modelscope_studio
|
36 |
+
```
|
37 |
+
|
38 |
+
## Components
|
39 |
+
|
40 |
+
- <tab-link component-tab="Chatbot">Chatbot</tab-link>
|
41 |
+
- <tab-link component-tab="MultimodalInput">MultimodalInput</tab-link>
|
42 |
+
- <tab-link component-tab="Markdown">Markdown</tab-link>
|
app.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from components.Chatbot.app import docs as chatbot_docs
|
3 |
+
from components.Docs import Docs
|
4 |
+
from components.Markdown.app import docs as markdown_docs
|
5 |
+
from components.MultimodalInput.app import docs as multimodel_input_docs
|
6 |
+
|
7 |
+
readme_docs = Docs(__file__)
|
8 |
+
|
9 |
+
docs = [
|
10 |
+
["Quick Start", readme_docs],
|
11 |
+
["Chatbot", chatbot_docs],
|
12 |
+
["Markdown", markdown_docs],
|
13 |
+
["MultimodalInput", multimodel_input_docs],
|
14 |
+
]
|
15 |
+
|
16 |
+
with gr.Blocks() as demo:
|
17 |
+
with gr.Tabs() as components_tabs:
|
18 |
+
for doc in docs:
|
19 |
+
with gr.TabItem(doc[0], id=doc[0]):
|
20 |
+
doc[1].render(components_tabs)
|
21 |
+
|
22 |
+
demo.queue().launch()
|
components/Chatbot/README-zh_CN.md
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Chatbot
|
2 |
+
|
3 |
+
升级版的 gradio Chatbot。
|
4 |
+
|
5 |
+
- 支持前端匀速流式输出 message
|
6 |
+
- 支持输出多模态内容(音频、视频、语音、文件、文本)
|
7 |
+
- 支持多 agent 场景
|
8 |
+
- 支持自定义渲染组件,并与 Python 侧事件交互
|
9 |
+
|
10 |
+
## 如何使用
|
11 |
+
|
12 |
+
### 基本使用
|
13 |
+
|
14 |
+
<demo name="basic"></demo>
|
15 |
+
|
16 |
+
### 多模态 & 支持本地文件的展示
|
17 |
+
|
18 |
+
<demo name="multimodal"></demo>
|
19 |
+
|
20 |
+
### 控制打字机单句 message 开关
|
21 |
+
|
22 |
+
<demo name="message_config"></demo>
|
23 |
+
|
24 |
+
### 支持手风琴内容展示
|
25 |
+
|
26 |
+
在返回的内容中加入 `accordion` 标签,可以在内容中加入手风琴,更多用法详见 <tab-link component-tab="Markdown">Markdown 内置自定义标签</tab-link>
|
27 |
+
|
28 |
+
同时为了适配大模型的工具调用链路,额外对某些大模型的格式做了预设配置,支持下述格式的预设处理(会将下面的格式转换成上方`accordion`标签包裹形式)
|
29 |
+
|
30 |
+
```python
|
31 |
+
import modelscope_studio as mgr
|
32 |
+
from modelscope_studio.components.Chatbot.llm_thinking_presets import qwen
|
33 |
+
|
34 |
+
# 添加 qwen 解析预设
|
35 |
+
mgr.Chatbot(llm_thinking_presets=[qwen()])
|
36 |
+
```
|
37 |
+
|
38 |
+
```text
|
39 |
+
Action: image_gen
|
40 |
+
Action Input: {"text": "glorious weather", "resolution": "1024*1024"}
|
41 |
+
Observation: <result>![IMAGEGEN](https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/1d/a2/20231213/723609ee/1926736d-7c6e-4d2f-b438-b7746b3d89f5-1.png?Expires=1702537773&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=H%2B0rIn6BMfE%2BOr1uPb7%2Br9G3%2B5w%3D)</result> 根据您的描述"glorious weather",我生成了一张图片。![](https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/1d/a2/20231213/723609ee/1926736d-7c6e-4d2f-b438-b7746b3d89f5-1.png?Expires=1702537773&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=H%2B0rIn6BMfE%2BOr1uPb7%2Br9G3%2B5w%3D)
|
42 |
+
|
43 |
+
Action: 「任意文本表示,将展示为思考链调用的名称」
|
44 |
+
Action Input: 「任意json or md 内容,将展示到调用过程的下拉框」
|
45 |
+
Observation: <result>「任意 md 内容,将作为完成调用的展示的下拉框内」</result>
|
46 |
+
```
|
47 |
+
|
48 |
+
<demo name="accordion"></demo>
|
49 |
+
|
50 |
+
### 支持用户选择交互
|
51 |
+
|
52 |
+
在返回的内容中加入 `select-box` 标签,更多用法详见 <tab-link component-tab="Markdown">Markdown 内置自定义标签</tab-link>
|
53 |
+
|
54 |
+
<demo name="select-box"></demo>
|
55 |
+
|
56 |
+
### 多 bot 场景
|
57 |
+
|
58 |
+
<demo name="multi_bots"></demo>
|
59 |
+
|
60 |
+
### 自定义标签(高阶用法,需要了解前端知识)
|
61 |
+
|
62 |
+
详见 <tab-link component-tab="Markdown">Markdown</tab-link> 组件
|
63 |
+
|
64 |
+
## API 及参数列表
|
65 |
+
|
66 |
+
以下 API 均为在原有 gradio Chatbot 外的额外拓展参数。
|
67 |
+
|
68 |
+
### value
|
69 |
+
|
70 |
+
接口定义:
|
71 |
+
|
72 |
+
```python
|
73 |
+
|
74 |
+
class FileMessage(GradioModel):
|
75 |
+
file: FileData
|
76 |
+
alt_text: Optional[str] = None
|
77 |
+
|
78 |
+
|
79 |
+
class MultimodalMessage(GradioModel):
|
80 |
+
# 默认以 index 为作为 id,id 改变会导致 message 重新渲染
|
81 |
+
id: Optional[str] = None
|
82 |
+
# message 容器的 elem id
|
83 |
+
elem_id: Optional[str] = None
|
84 |
+
# message 容器的 elem classes
|
85 |
+
elem_classes: Optional[list[str] | str] = None
|
86 |
+
name: Optional[str] = None
|
87 |
+
text: Optional[str] = None
|
88 |
+
flushing: Optional[bool] = None
|
89 |
+
avatar: Optional[Union[str, FileData]] = ''
|
90 |
+
files: Optional[List[Union[FileMessage, dict, FileData, str]]] = None
|
91 |
+
|
92 |
+
# 支持多 bot 场景
|
93 |
+
MultimodalMessageItem = Optional[Union[MultimodalMessage, MultimodalInputData,
|
94 |
+
dict, str]]
|
95 |
+
|
96 |
+
|
97 |
+
class ChatbotData(GradioRootModel):
|
98 |
+
root: List[Tuple[Union[MultimodalMessageItem, List[MultimodalMessageItem]],
|
99 |
+
Union[MultimodalMessageItem,
|
100 |
+
List[MultimodalMessageItem]]]]
|
101 |
+
```
|
102 |
+
|
103 |
+
### props
|
104 |
+
|
105 |
+
| 属性 | 类型 | 默认值 | 描述 |
|
106 |
+
| ----------------------------- | -------------------------------------------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
107 |
+
| flushing | bool | True | 是否开启打字机效果。默认只有 bot 的 message 会开启,可以通过单独修改 message 的 flushing 属性精确控制每一条 message 的显示效果 |
|
108 |
+
| enable_base64 | bool | False | 是否支持渲染的内容为 base64,因为直接渲染 base64 会带来安全问题,默认为 False。 |
|
109 |
+
| enable_latex | bool | True | 是否支持 Latex 公式渲染 |
|
110 |
+
| latex_single_dollar_delimiter | bool | True | 是否支持单`$`符号在 Latex 公式中渲染 |
|
111 |
+
| preview | bool | True | 是否开启图片预览功能 |
|
112 |
+
| avatar_images | tuple\[str \| Path \| None \| dict \| list, str \| Path \| None \| dict\| list\] | None | 拓展gr.Chatbot的参数值,除了接收 url 外还可以接收 dict 和 list,dict 可以传入avatar和name字段,name字段在渲染时会显示在头像下方。 <br/> - 当传入 dict 时,必须包含有avatar字段。<br/> - 当传入 list 时,一般对应多 bot 模式,每一项可以接收前面所有的值,每个 bot 的头像与 message 中 bot 的位置一一对应 |
|
113 |
+
| avatar_image_align | Literal['top', 'middle', 'bottom'] | 'bottom' | 控制头像与 message 的对齐方式,默认为下对齐 |
|
114 |
+
| avatar_image_width | int | 45 | 头像与名称的宽度 |
|
115 |
+
| flushing_speed | int | 3 | 打字机速度,值为 1 - 10,值越大速度越快 |
|
116 |
+
| llm_thinking_presets | list\[dict\] | \[\] | llm 思考链路解析预设,可以将 llm 调用工具的输出格式转为固定的前端展示格式,需要从modelscope_studio.Chatbot.llm_thinking_presets引入,目前支持:qwen |
|
117 |
+
| custom_components | dict\[str, CustomComponentDict\] CustomComponentDict 定义见下方 | None | 支持用户定义自定义标签,并通过 js 控制标签渲染样式与触发 python 事件。 |
|
118 |
+
|
119 |
+
**CustomComponent 定义如下**
|
120 |
+
|
121 |
+
```python
|
122 |
+
class CustomComponentDict(TypedDict):
|
123 |
+
props: Optional[List[str]]
|
124 |
+
template: Optional[str]
|
125 |
+
js: Optional[str]
|
126 |
+
```
|
127 |
+
|
128 |
+
### 内置的自定义标签
|
129 |
+
|
130 |
+
见 <tab-link component-tab="Markdown">Markdown 内置自定义标签</tab-link>
|
131 |
+
|
132 |
+
### event listeners
|
133 |
+
|
134 |
+
| 事件 | 描述 |
|
135 |
+
| ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
136 |
+
| `mgr.Chatbot.flushed(fn, ···)` | 当打字机效果结束时触发。EventData 为:<br/> - index:当前 message 的 index tuple。<br/> - value:当前 message value。 |
|
137 |
+
| `mgr.Chatbot.custom(fn, ···)` | 自定义标签触发事件时触发,EventData 为:<br/> - index:前 message 的 index tuple。<br/> - tag:当前触发的标签。<br/> - tag_index:当前触发标签的 index,此 index 在 mesage 的 index tuple 基础上重新计算。<br/> - value:自定义传入的值。 |
|
components/Chatbot/README.md
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Chatbot
|
2 |
+
|
3 |
+
Upgraded gradio Chatbot.
|
4 |
+
|
5 |
+
- Supports uniform frontend streaming output of messages
|
6 |
+
- Supports output of multimodal content (audio, video, voice, files, text)
|
7 |
+
- Supports multi-agent scenarios
|
8 |
+
- Supports custom rendering components and interaction with events on the Python side
|
9 |
+
|
10 |
+
## How to Use
|
11 |
+
|
12 |
+
### Basic Usage
|
13 |
+
|
14 |
+
<demo name="basic"></demo>
|
15 |
+
|
16 |
+
### Multimodal & Support for Local File Display
|
17 |
+
|
18 |
+
<demo name="multimodal"></demo>
|
19 |
+
|
20 |
+
### Control for Typewriter Single Sentence Message
|
21 |
+
|
22 |
+
<demo name="message_config"></demo>
|
23 |
+
|
24 |
+
### Support for Accordion Content Display
|
25 |
+
|
26 |
+
Include the `accordion` tag in the returned content to add an accordion within the content. For more usage details, see <tab-link component-tab="Markdown">Markdown Built-in Custom Tags</tab-link>.
|
27 |
+
Additionally, to adapt to the toolchain usage of large models, some preset configurations for certain large models have been made. Support for the following preset formats (which will be converted into the form wrapped by the above `accordion` tag).
|
28 |
+
|
29 |
+
```python
|
30 |
+
import modelscope_studio as mgr
|
31 |
+
from modelscope_studio.components.Chatbot.llm_thinking_presets import qwen
|
32 |
+
|
33 |
+
# Add qwen preset
|
34 |
+
mgr.Chatbot(llm_thinking_presets=[qwen()])
|
35 |
+
```
|
36 |
+
|
37 |
+
```text
|
38 |
+
Action: image_gen
|
39 |
+
Action Input: {"text": "glorious weather", "resolution": "1024*1024"}
|
40 |
+
Observation: <result>![IMAGEGEN](https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/1d/a2/20231213/723609ee/1926736d-7c6e-4d2f-b438-b7746b3d89f5-1.png?Expires=1702537773&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=H%2B0rIn6BMfE%2BOr1uPb7%2Br9G3%2B5w%3D)</result> Based on your description: glorious weather,I generated a picture.[](https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/1d/a2/20231213/723609ee/1926736d-7c6e-4d2f-b438-b7746b3d89f5-1.png?Expires=1702537773&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=H%2B0rIn6BMfE%2BOr1uPb7%2Br9G3%2B5w%3D)
|
41 |
+
|
42 |
+
Action: 「An arbitrary text representation that will be displayed as the name of the thought chain call」
|
43 |
+
Action Input: 「Any json or md content will be displayed in the drop-down box of the calling process」
|
44 |
+
Observation: <result>「Any md content will be displayed in the drop-down box when the call is completed」</result>
|
45 |
+
```
|
46 |
+
|
47 |
+
<demo name="accordion"></demo>
|
48 |
+
|
49 |
+
### Support for User Selection Interaction
|
50 |
+
|
51 |
+
Include the `select-box` tag in the returned content for more usage details, see <tab-link component-tab="Markdown">Markdown Built-in Custom Tags <tab-link component-tab="Markdown">.
|
52 |
+
|
53 |
+
<demo name="select-box"></demo>
|
54 |
+
|
55 |
+
### Multi-bot Scenarios
|
56 |
+
|
57 |
+
<demo name="multi_bots"></demo>
|
58 |
+
|
59 |
+
### Custom Tags (Advanced Usage, Requires Frontend Knowledge)
|
60 |
+
|
61 |
+
See the <tab-link component-tab="Markdown">Markdown component</tab-link> for details.
|
62 |
+
|
63 |
+
## API and Parameter List
|
64 |
+
|
65 |
+
The following APIs are additional extended parameters beyond the original gradio Chatbot.
|
66 |
+
|
67 |
+
### value
|
68 |
+
|
69 |
+
Interface definition:
|
70 |
+
|
71 |
+
```python
|
72 |
+
|
73 |
+
class FileMessage(GradioModel):
|
74 |
+
file: FileData
|
75 |
+
alt_text: Optional[str] = None
|
76 |
+
|
77 |
+
|
78 |
+
class MultimodalMessage(GradioModel):
|
79 |
+
# By default, message index is used as id. it will cause the message to be re-rendered when id changed.
|
80 |
+
id: Optional[str] = None
|
81 |
+
# elem id of message container
|
82 |
+
elem_id: Optional[str] = None
|
83 |
+
# elem classes of message container
|
84 |
+
elem_classes: Optional[list[str] | str] = None
|
85 |
+
name: Optional[str] = None
|
86 |
+
text: Optional[str] = None
|
87 |
+
flushing: Optional[bool] = None
|
88 |
+
avatar: Optional[Union[str, FileData]] = ''
|
89 |
+
files: Optional[List[Union[FileMessage, dict, FileData, str]]] = None
|
90 |
+
|
91 |
+
# Support multi-bot scenarios
|
92 |
+
MultimodalMessageItem = Optional[Union[MultimodalMessage, MultimodalInputData,
|
93 |
+
dict, str]]
|
94 |
+
|
95 |
+
|
96 |
+
class ChatbotData(GradioRootModel):
|
97 |
+
root: List[Tuple[Union[MultimodalMessageItem, List[MultimodalMessageItem]],
|
98 |
+
Union[MultimodalMessageItem,
|
99 |
+
List[MultimodalMessageItem]]]]
|
100 |
+
```
|
101 |
+
|
102 |
+
### props
|
103 |
+
|
104 |
+
| Attribute | Type | Default Value | Description |
|
105 |
+
| ----------------------------- | -------------------------------------------------------------------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
106 |
+
| flushing | bool | True | Whether to enable the typewriter effect. By default, only the bot's messages will have this effect, but you can control the display effect of each message precisely by modifying the flushing attribute of a message individually. |
|
107 |
+
| enable_base64 | bool | False | Whether to support rendering content as base64, since rendering base64 is unsafe, the default is False. |
|
108 |
+
| enable_latex | bool | True | Whether to enable LaTeX rendering. |
|
109 |
+
| latex_single_dollar_delimiter | bool | True | Whether to enable single dollar delimiter `$` for LaTeX rendering. |
|
110 |
+
| preview | bool | True | Whether to enable image preview functionality. |
|
111 |
+
| avatar_images | tuple\[str \| Path \| None \| dict \| list, str \| Path \| None \| dict\| list\] | None | An extended parameter value for gr.Chatbot, in addition to accepting a URL, it can also accept a dict and list. The dict can include the fields avatar and name, where the name field will be displayed under the avatar when rendered. <br/> - When passing a dict, it must include an avatar field.<br/> - When passing a list, it generally corresponds to the multi-bot mode, where each item can receive all the aforementioned values, and each bot’s avatar matches with the position of the bot in the messages. |
|
112 |
+
| avatar_image_align | Literal['top', 'middle', 'bottom'] | 'bottom' | Controls the alignment of the avatar with the messages, default is bottom-aligned. |
|
113 |
+
| avatar_image_width | int | 45 | The width of the avatar and name. |
|
114 |
+
| flushing_speed | int | 3 | Typewriter speed, values range from 1 - 10, with larger values indicating faster speeds. |
|
115 |
+
| llm_thinking_presets | list\[dict\] | \[\] | llm thinking link presets, which can convert the output format of llm calling tools into a fixed front-end display format. It needs to be imported from modelscope_studio.Chatbot.llm_thinking_presets, and currently supports: qwen. |
|
116 |
+
| custom_components | dict\[str, CustomComponentDict\] CustomComponentDict is defined below | None | Allows users to define custom tags and control tag rendering styles and trigger Python events through JS. |
|
117 |
+
|
118 |
+
**Definition of CustomComponent is as follows:**
|
119 |
+
|
120 |
+
```python
|
121 |
+
class CustomComponentDict(TypedDict):
|
122 |
+
props: Optional[List[str]]
|
123 |
+
template: Optional[str]
|
124 |
+
js: Optional[str]
|
125 |
+
```
|
126 |
+
|
127 |
+
### Built-in Custom Tags
|
128 |
+
|
129 |
+
See <tab-link component-tab="Markdown">Markdown Built-in Custom Tags</tab-link>
|
130 |
+
|
131 |
+
### event listeners
|
132 |
+
|
133 |
+
| Event | Description |
|
134 |
+
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
135 |
+
| `mgr.Chatbot.flushed(fn, ···)` | Triggered when the typewriter effect ends. EventData is: <br/> - index: The index tuple of the current message.<br/> - value: The current message value. |
|
136 |
+
| `mgr.Chatbot.custom(fn, ···)` | Triggered when a custom tag event occurs. EventData is: <br/> - index: The index tuple of the previous message.<br/> - tag: The current tag that triggered the event.<br/> - tag_index: The index of the current triggered tag, re-calculated based on the index tuple of the message.<br/> - value: The custom value passed in. |
|
components/Chatbot/app.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from components.Docs import Docs
|
2 |
+
|
3 |
+
docs = Docs(__file__)
|
4 |
+
|
5 |
+
if __name__ == "__main__":
|
6 |
+
docs.render().queue().launch()
|
components/Chatbot/demos/accordion.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
import modelscope_studio as mgr
|
6 |
+
from modelscope_studio.components.Chatbot.llm_thinking_presets import qwen
|
7 |
+
|
8 |
+
|
9 |
+
def resolve_assets(relative_path):
|
10 |
+
return os.path.join(os.path.dirname(__file__), "../resources",
|
11 |
+
relative_path)
|
12 |
+
|
13 |
+
|
14 |
+
conversation = [
|
15 |
+
[
|
16 |
+
None, {
|
17 |
+
"text": f"""
|
18 |
+
Use accordion tag:
|
19 |
+
<accordion title="Using tool">
|
20 |
+
|
21 |
+
```json
|
22 |
+
{{"text": "glorious weather", "resolution": "1024*1024"}}
|
23 |
+
```
|
24 |
+
|
25 |
+
</accordion>
|
26 |
+
|
27 |
+
Qwen preset:
|
28 |
+
Action: image_gen
|
29 |
+
Action Input: {{"text": "glorious weather", "resolution": "1024*1024"}}
|
30 |
+
Observation: <result>![IMAGEGEN]({resolve_assets("screen.jpeg")})</result> Based on your description"glorious weather",I generated a picture.![]({resolve_assets("screen.jpeg")})
|
31 |
+
|
32 |
+
Action: 「An arbitrary text representation that will be displayed as the name of the thought chain call」
|
33 |
+
Action Input: 「Any json or md content will be displayed in the drop-down box of the calling process」
|
34 |
+
Observation: <result>「Any md content will be displayed in the drop-down box when the call is completed」</result>
|
35 |
+
""",
|
36 |
+
"flushing": False
|
37 |
+
}
|
38 |
+
],
|
39 |
+
]
|
40 |
+
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
mgr.Chatbot(
|
43 |
+
value=conversation,
|
44 |
+
llm_thinking_presets=[qwen()],
|
45 |
+
height=600,
|
46 |
+
)
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
demo.queue().launch()
|
components/Chatbot/demos/basic.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
import modelscope_studio as mgr
|
7 |
+
|
8 |
+
conversation = [
|
9 |
+
[
|
10 |
+
None,
|
11 |
+
{
|
12 |
+
# The first message of bot closes the typewriter.
|
13 |
+
"text": "Hello I'm a chatbot",
|
14 |
+
"flushing": False
|
15 |
+
}
|
16 |
+
],
|
17 |
+
]
|
18 |
+
|
19 |
+
|
20 |
+
def submit(_input, _chatbot):
|
21 |
+
_chatbot.append([_input, None])
|
22 |
+
yield gr.update(interactive=False, value=None), _chatbot
|
23 |
+
time.sleep(2)
|
24 |
+
_chatbot[-1][1] = {"text": _input.text + '!'}
|
25 |
+
yield {
|
26 |
+
chatbot: _chatbot,
|
27 |
+
}
|
28 |
+
|
29 |
+
|
30 |
+
def flushed():
|
31 |
+
return gr.update(interactive=True)
|
32 |
+
|
33 |
+
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
chatbot = mgr.Chatbot(
|
36 |
+
value=conversation,
|
37 |
+
avatar_images=[
|
38 |
+
os.path.join(os.path.dirname(__file__), "../resources/user.jpeg"),
|
39 |
+
{
|
40 |
+
"name":
|
41 |
+
"bot",
|
42 |
+
"avatar":
|
43 |
+
os.path.join(os.path.dirname(__file__),
|
44 |
+
"../resources/bot.jpeg")
|
45 |
+
}
|
46 |
+
],
|
47 |
+
height=600,
|
48 |
+
)
|
49 |
+
|
50 |
+
input = mgr.MultimodalInput()
|
51 |
+
input.submit(fn=submit, inputs=[input, chatbot], outputs=[input, chatbot])
|
52 |
+
chatbot.flushed(fn=flushed, outputs=[input])
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
demo.queue().launch()
|
components/Chatbot/demos/message_config.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
import modelscope_studio as mgr
|
6 |
+
|
7 |
+
|
8 |
+
def submit(_chatbot):
|
9 |
+
_chatbot.append(["test user",
|
10 |
+
"test bot"]) # bot starts the typewriter by default
|
11 |
+
yield _chatbot
|
12 |
+
time.sleep(2)
|
13 |
+
_chatbot.append(["test user", {
|
14 |
+
"text": "test bot",
|
15 |
+
"flushing": False
|
16 |
+
}]) # both start the typewriter
|
17 |
+
yield _chatbot
|
18 |
+
time.sleep(2)
|
19 |
+
_chatbot.append([{
|
20 |
+
"text": "test user",
|
21 |
+
"flushing": True
|
22 |
+
}, {
|
23 |
+
"text": "test bot",
|
24 |
+
"flushing": False
|
25 |
+
}]) # user starts the typewriter
|
26 |
+
yield _chatbot
|
27 |
+
|
28 |
+
|
29 |
+
with gr.Blocks() as demo:
|
30 |
+
chatbot = mgr.Chatbot(height=600, )
|
31 |
+
button = gr.Button("Submit")
|
32 |
+
button.click(fn=submit, inputs=[chatbot], outputs=[chatbot])
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
demo.queue().launch()
|
components/Chatbot/demos/multi_bots.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
import modelscope_studio as mgr
|
7 |
+
|
8 |
+
|
9 |
+
def resolve_assets(relative_path):
|
10 |
+
return os.path.join(os.path.dirname(__file__), "../resources",
|
11 |
+
relative_path)
|
12 |
+
|
13 |
+
|
14 |
+
conversation = [
|
15 |
+
[None, {
|
16 |
+
"text": "Hello I'm a chatbot",
|
17 |
+
"flushing": False
|
18 |
+
}],
|
19 |
+
]
|
20 |
+
|
21 |
+
|
22 |
+
def get_last_bot_message(chatbot):
|
23 |
+
return chatbot[-1][1]
|
24 |
+
|
25 |
+
|
26 |
+
def create_music_bot_message(text: str):
|
27 |
+
return {
|
28 |
+
"text": text,
|
29 |
+
}
|
30 |
+
|
31 |
+
|
32 |
+
def create_image_bot_message(text: str):
|
33 |
+
return {
|
34 |
+
"text": text,
|
35 |
+
}
|
36 |
+
|
37 |
+
|
38 |
+
def submit(_input, _chatbot):
|
39 |
+
_chatbot.append([_input, None])
|
40 |
+
yield gr.update(interactive=False, value=None), _chatbot
|
41 |
+
_chatbot[-1][1] = [
|
42 |
+
"Hello",
|
43 |
+
create_image_bot_message("Hello"),
|
44 |
+
create_music_bot_message("Hello")
|
45 |
+
]
|
46 |
+
|
47 |
+
time.sleep(2)
|
48 |
+
get_last_bot_message(_chatbot)[1][
|
49 |
+
"text"] = f"""Hello, I\'m a image bot\n![image]({resolve_assets("user.jpeg")})"""
|
50 |
+
get_last_bot_message(_chatbot)[2][
|
51 |
+
"text"] = f"""Hello, I\'m a music bot <audio src="{resolve_assets("audio.wav")}"></audio>"""
|
52 |
+
yield {
|
53 |
+
chatbot: _chatbot,
|
54 |
+
}
|
55 |
+
|
56 |
+
|
57 |
+
def flushed():
|
58 |
+
return gr.update(interactive=True)
|
59 |
+
|
60 |
+
|
61 |
+
with gr.Blocks() as demo:
|
62 |
+
chatbot = mgr.Chatbot(
|
63 |
+
value=conversation,
|
64 |
+
avatar_image_width=40,
|
65 |
+
avatar_images=[
|
66 |
+
resolve_assets('user.jpeg'),
|
67 |
+
# default bot avatar and name
|
68 |
+
[{
|
69 |
+
"name": "bot",
|
70 |
+
"avatar": resolve_assets('bot.jpeg')
|
71 |
+
}, {
|
72 |
+
"name": "image bot",
|
73 |
+
"avatar": resolve_assets('image-bot.jpeg')
|
74 |
+
}, {
|
75 |
+
"name": "music bot",
|
76 |
+
"avatar": resolve_assets('music-bot.jpeg')
|
77 |
+
}]
|
78 |
+
],
|
79 |
+
height=600,
|
80 |
+
)
|
81 |
+
|
82 |
+
input = mgr.MultimodalInput()
|
83 |
+
input.submit(fn=submit, inputs=[input, chatbot], outputs=[input, chatbot])
|
84 |
+
chatbot.flushed(fn=flushed, outputs=[input])
|
85 |
+
|
86 |
+
if __name__ == "__main__":
|
87 |
+
demo.queue().launch()
|
components/Chatbot/demos/multimodal.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
import modelscope_studio as mgr
|
6 |
+
|
7 |
+
|
8 |
+
def resolve_assets(relative_path):
|
9 |
+
return os.path.join(os.path.dirname(__file__), "../resources",
|
10 |
+
relative_path)
|
11 |
+
|
12 |
+
|
13 |
+
conversation = [
|
14 |
+
[
|
15 |
+
None, {
|
16 |
+
"text": f"""
|
17 |
+
Image
|
18 |
+
|
19 |
+
![image]({resolve_assets("bot.jpeg")})
|
20 |
+
|
21 |
+
<img src="{resolve_assets("user.jpeg")}" />
|
22 |
+
|
23 |
+
Video
|
24 |
+
|
25 |
+
<video src="{resolve_assets("dog.mp4")}"></video>
|
26 |
+
|
27 |
+
Audio
|
28 |
+
|
29 |
+
<audio src="{resolve_assets("audio.wav")}"></audio>
|
30 |
+
""",
|
31 |
+
"flushing": False
|
32 |
+
}
|
33 |
+
],
|
34 |
+
]
|
35 |
+
|
36 |
+
with gr.Blocks() as demo:
|
37 |
+
mgr.Chatbot(
|
38 |
+
value=conversation,
|
39 |
+
height=600,
|
40 |
+
)
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
demo.queue().launch()
|
components/Chatbot/demos/select-box.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
import modelscope_studio as mgr
|
6 |
+
|
7 |
+
# `label` will display on the page, and `value` is the actual selected value.
|
8 |
+
options = [{"label": "A", "value": "a"}, "b", "c"]
|
9 |
+
|
10 |
+
conversation = [[
|
11 |
+
None, f"""
|
12 |
+
Single Select: <select-box options='{json.dumps(options)}' select-once></select-box>
|
13 |
+
|
14 |
+
Multiple Select:<select-box type="checkbox" options='{json.dumps(options)}' select-once submit-text="Submit"></select-box>
|
15 |
+
|
16 |
+
Vertical Direction:
|
17 |
+
|
18 |
+
<select-box direction="vertical" type="checkbox" options='{json.dumps(options)}' select-once submit-text="Submit"></select-box>
|
19 |
+
|
20 |
+
Card Shape:
|
21 |
+
|
22 |
+
<select-box shape="card" options='{json.dumps(options)}' select-once equal-height></select-box>
|
23 |
+
|
24 |
+
|
25 |
+
<select-box shape="card" columns="2" options='{json.dumps(options)}' select-once equal-height></select-box>
|
26 |
+
|
27 |
+
|
28 |
+
<select-box shape="card" direction="vertical" options='{json.dumps(options)}' select-once equal-height></select-box>
|
29 |
+
"""
|
30 |
+
]]
|
31 |
+
|
32 |
+
|
33 |
+
# The custom data must be marked by `gr.EventData`
|
34 |
+
def fn(data: gr.EventData):
|
35 |
+
print(data._data)
|
36 |
+
|
37 |
+
|
38 |
+
with gr.Blocks() as demo:
|
39 |
+
chatbot = mgr.Chatbot(
|
40 |
+
value=conversation,
|
41 |
+
flushing=False,
|
42 |
+
height=600,
|
43 |
+
)
|
44 |
+
# All custom tags will trigger the custom event
|
45 |
+
chatbot.custom(fn=fn)
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
demo.queue().launch()
|
components/Chatbot/resources/audio.wav
ADDED
Binary file (44.8 kB). View file
|
|
components/Chatbot/resources/bot.jpeg
ADDED
components/Chatbot/resources/custom_components/custom_select.js
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
(props, cc, { el, onMount }) => {
|
2 |
+
const options = JSON.parse(props.options);
|
3 |
+
el.innerHTML = `
|
4 |
+
${options
|
5 |
+
.map((option) => {
|
6 |
+
return `<div>
|
7 |
+
<label>${option} <input type="radio"/></label>
|
8 |
+
<div>`;
|
9 |
+
})
|
10 |
+
.join('')}
|
11 |
+
`;
|
12 |
+
onMount(() => {
|
13 |
+
const inputs = Array.from(el.getElementsByTagName('input'));
|
14 |
+
Array.from(el.getElementsByTagName('label')).forEach((label, i) => {
|
15 |
+
label.addEventListener('click', () => {
|
16 |
+
inputs.forEach((input) => {
|
17 |
+
input.checked = false;
|
18 |
+
});
|
19 |
+
const input = label.getElementsByTagName('input')[0];
|
20 |
+
input.checked = true;
|
21 |
+
// Use cc.dispatch to trigger events.
|
22 |
+
cc.dispatch(options[i]);
|
23 |
+
});
|
24 |
+
});
|
25 |
+
});
|
26 |
+
};
|
components/Chatbot/resources/dog.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:39d086ce29e48cf76e5042d2f3f0611ee46575f70fa3dc0c40dd4cfffde3d933
|
3 |
+
size 8626383
|
components/Chatbot/resources/image-bot.jpeg
ADDED
components/Chatbot/resources/music-bot.jpeg
ADDED
components/Chatbot/resources/screen.jpeg
ADDED
components/Chatbot/resources/user.jpeg
ADDED
components/Docs.py
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
from typing import Callable
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
import modelscope_studio as mgr
|
8 |
+
|
9 |
+
from .parse_markdown import parse_markdown
|
10 |
+
|
11 |
+
with open(os.path.join(os.path.dirname(__file__), "tab-link.js")) as f:
|
12 |
+
tab_link_js = f.read()
|
13 |
+
|
14 |
+
custom_components = {
|
15 |
+
"tab-link": {
|
16 |
+
"props": ["tab", "component-tab"],
|
17 |
+
"js": tab_link_js
|
18 |
+
}
|
19 |
+
}
|
20 |
+
|
21 |
+
|
22 |
+
def remove_formatter(markdown_text):
|
23 |
+
pattern = r"^ *---[\s\S]*?---"
|
24 |
+
|
25 |
+
replaced_text = re.sub(pattern, "", markdown_text)
|
26 |
+
|
27 |
+
return replaced_text
|
28 |
+
|
29 |
+
|
30 |
+
def list_demos(dir_path: str, prefix=''):
|
31 |
+
result = []
|
32 |
+
if (not os.path.isdir(dir_path)):
|
33 |
+
return result
|
34 |
+
for name in os.listdir(dir_path):
|
35 |
+
path = os.path.join(dir_path, name)
|
36 |
+
|
37 |
+
if os.path.isfile(path):
|
38 |
+
result.append(prefix + name)
|
39 |
+
elif os.path.isdir(path):
|
40 |
+
sub_prefix = prefix + name + '/'
|
41 |
+
result.extend(list_demos(path, sub_prefix))
|
42 |
+
|
43 |
+
return result
|
44 |
+
|
45 |
+
|
46 |
+
def get_demo_modules(file_path: str):
|
47 |
+
import importlib.util
|
48 |
+
|
49 |
+
demos = [
|
50 |
+
demo for demo in list_demos(
|
51 |
+
os.path.join(os.path.dirname(file_path), "demos"))
|
52 |
+
if demo.endswith(".py")
|
53 |
+
]
|
54 |
+
demo_modules = {}
|
55 |
+
for demo in demos:
|
56 |
+
demo_name = demo.split(".")[0]
|
57 |
+
spec = importlib.util.spec_from_file_location(
|
58 |
+
"demo", os.path.join(os.path.dirname(file_path), "demos", demo))
|
59 |
+
module = importlib.util.module_from_spec(spec)
|
60 |
+
spec.loader.exec_module(module)
|
61 |
+
demo_modules[demo_name] = module
|
62 |
+
return demo_modules
|
63 |
+
|
64 |
+
|
65 |
+
is_modelscope_studio = os.getenv('MODELSCOPE_ENVIRONMENT') == 'studio'
|
66 |
+
|
67 |
+
|
68 |
+
class Docs:
|
69 |
+
|
70 |
+
def __init__(self, file_path: str, markdown_files: list = None):
|
71 |
+
self.file_path = file_path
|
72 |
+
self.demo_modules = get_demo_modules(file_path)
|
73 |
+
# default current directory
|
74 |
+
self.markdown_files = markdown_files if markdown_files else [
|
75 |
+
filename for filename in os.listdir(os.path.dirname(file_path))
|
76 |
+
if filename.endswith(".md")
|
77 |
+
]
|
78 |
+
if is_modelscope_studio:
|
79 |
+
self.markdown_files = list(
|
80 |
+
filter(
|
81 |
+
lambda x: x.endswith("-zh_CN.md") or
|
82 |
+
(f"{'.'.join(x.split('.')[:-1])}-zh_CN.md" not in self.
|
83 |
+
markdown_files), self.markdown_files))
|
84 |
+
else:
|
85 |
+
self.markdown_files = list(
|
86 |
+
filter(lambda x: not x.endswith("-zh_CN.md"),
|
87 |
+
self.markdown_files))
|
88 |
+
|
89 |
+
self.tabs = None
|
90 |
+
|
91 |
+
def read_file(self, relative_path: str):
|
92 |
+
with open(os.path.join(os.path.dirname(self.file_path), relative_path),
|
93 |
+
"r") as f:
|
94 |
+
return f.read()
|
95 |
+
|
96 |
+
def render_demo(self, demo_name, prefix='', suffix=''):
|
97 |
+
content = self.read_file(f"./demos/{demo_name}.py")
|
98 |
+
module = self.demo_modules[demo_name]
|
99 |
+
with gr.Accordion("Show Demo", open=False):
|
100 |
+
with gr.Row():
|
101 |
+
with gr.Column():
|
102 |
+
mgr.Markdown(f"""
|
103 |
+
{prefix}
|
104 |
+
````python
|
105 |
+
{content}
|
106 |
+
````
|
107 |
+
{suffix}
|
108 |
+
""",
|
109 |
+
header_links=True,
|
110 |
+
custom_components=custom_components)
|
111 |
+
with gr.Column():
|
112 |
+
module.demo.render()
|
113 |
+
|
114 |
+
def render_markdown(self,
|
115 |
+
markdown_file,
|
116 |
+
on_tab_link_click: Callable = None,
|
117 |
+
components_tabs=None):
|
118 |
+
items = parse_markdown(remove_formatter(self.read_file(markdown_file)),
|
119 |
+
read_file=self.read_file)
|
120 |
+
for item in items:
|
121 |
+
if item["type"] == "text":
|
122 |
+
md = mgr.Markdown(item["value"],
|
123 |
+
header_links=True,
|
124 |
+
custom_components=custom_components,
|
125 |
+
preview=False)
|
126 |
+
deps = [dep for dep in [components_tabs, self.tabs] if dep]
|
127 |
+
if len(deps) > 0:
|
128 |
+
md.custom(fn=on_tab_link_click, outputs=deps)
|
129 |
+
elif item["type"] == "demo":
|
130 |
+
self.render_demo(item["name"],
|
131 |
+
prefix=item["prefix"],
|
132 |
+
suffix=item["suffix"])
|
133 |
+
|
134 |
+
def render(self, components_tabs=None):
|
135 |
+
|
136 |
+
def tab_link_click(data: gr.EventData):
|
137 |
+
tab: str = data._data["value"].get("tab", '')
|
138 |
+
component_tab: str = data._data["value"].get("component_tab", '')
|
139 |
+
if tab and tabs:
|
140 |
+
return {tabs: gr.update(selected=tab)}
|
141 |
+
elif components_tabs and component_tab:
|
142 |
+
return {components_tabs: gr.update(selected=component_tab)}
|
143 |
+
|
144 |
+
with gr.Blocks() as demo:
|
145 |
+
|
146 |
+
if len(self.markdown_files) > 1:
|
147 |
+
with gr.Tabs() as tabs:
|
148 |
+
self.tabs = tabs
|
149 |
+
|
150 |
+
for markdown_file in self.markdown_files:
|
151 |
+
tab_name = ".".join(markdown_file.split(".")[:-1])
|
152 |
+
tab_name = tab_name.split("-zh_CN")[0]
|
153 |
+
with gr.TabItem(tab_name, id=tab_name):
|
154 |
+
self.render_markdown(
|
155 |
+
markdown_file,
|
156 |
+
on_tab_link_click=tab_link_click,
|
157 |
+
components_tabs=components_tabs)
|
158 |
+
elif (len(self.markdown_files) == 1):
|
159 |
+
self.render_markdown(self.markdown_files[0],
|
160 |
+
on_tab_link_click=tab_link_click,
|
161 |
+
components_tabs=components_tabs)
|
162 |
+
return demo
|
components/Markdown/README-zh_CN.md
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Markdown
|
2 |
+
|
3 |
+
升级版的 gradio Markdown。
|
4 |
+
|
5 |
+
- 支持输出多模态内容(音频、视频、语音、文件、文本)
|
6 |
+
- 支持自定义渲染组件,并与 Python 侧事件交互
|
7 |
+
|
8 |
+
## 如何使用
|
9 |
+
|
10 |
+
### 基本使用
|
11 |
+
|
12 |
+
<demo name="basic"></demo>
|
13 |
+
|
14 |
+
### 多模态 & 支持本地文件的展示
|
15 |
+
|
16 |
+
<demo name="multimodal"></demo>
|
17 |
+
|
18 |
+
### 支持手风琴内容展示
|
19 |
+
|
20 |
+
在返回的内容中加入 `accordion` 标签,更多用法详见 <tab-link tab="custom_tags/accordion">accordion</tab-link>
|
21 |
+
|
22 |
+
<demo name="accordion"></demo>
|
23 |
+
|
24 |
+
### 支持用户选择交互
|
25 |
+
|
26 |
+
在返回的内容中加入 `select-box` 标签,更多用法详见 <tab-link tab="custom_tags/select-box">select-box</tab-link>
|
27 |
+
|
28 |
+
<demo name="select-box"></demo>
|
29 |
+
|
30 |
+
### 自定义标签(高阶用法,需要了解前端知识)
|
31 |
+
|
32 |
+
<demo name="custom-tag"></demo>
|
33 |
+
|
34 |
+
#### 引入 js
|
35 |
+
|
36 |
+
<demo name="custom-tag2"></demo>
|
37 |
+
|
38 |
+
template只能做简单的变量替换,如果想要引入更多自定义的行为,如条件判断、循环渲染等,请使用 js 控制 el 自行处理,下面是简单的示例:
|
39 |
+
|
40 |
+
<demo name="custom-tag3">
|
41 |
+
<demo-suffix>
|
42 |
+
custom_select.js
|
43 |
+
|
44 |
+
```js
|
45 |
+
<file src="./resources/custom_components/custom_select.js"></file>
|
46 |
+
```
|
47 |
+
|
48 |
+
</demo-suffix>
|
49 |
+
</demo>
|
50 |
+
|
51 |
+
#### 与 Python 侧交互
|
52 |
+
|
53 |
+
在 js 中可以使用`cc.dispatch`触发 Python 侧监听的`custom`事件,以前面的custom_select.js为例,我们在前端调用了`cc.dispatch(options[i])`,则会向 Python 侧同时发送通知。
|
54 |
+
|
55 |
+
<demo name="custom-tag4"></demo>
|
56 |
+
|
57 |
+
## API 及参数列表
|
58 |
+
|
59 |
+
以下 API 均为在原有 gradio Markdown 外的额外拓展参数。
|
60 |
+
|
61 |
+
### props
|
62 |
+
|
63 |
+
| 属性 | 类型 | 默认值 | 描述 |
|
64 |
+
| ----------------------------- | --------------------------------------------------------------- | ------ | --------------------------------------------------------------------------- |
|
65 |
+
| enable_base64 | bool | False | 是否支持渲染的内容为 base64,因为直接渲染 base64 有安全问题,默认为 False。 |
|
66 |
+
| enable_latex | bool | True | 是否支持 Latex 公式渲染 |
|
67 |
+
| latex_single_dollar_delimiter | bool | True | 是否支持单`$`符号在 Latex 公式中渲染 |
|
68 |
+
| preview | bool | True | 是否开启图片预览功能 |
|
69 |
+
| custom_components | dict\[str, CustomComponentDict\] CustomComponentDict 定义见下方 | None | 支持用户定义自定义标签,并通过 js 控制标签渲染样式与触发 python 事件。 |
|
70 |
+
|
71 |
+
**CustomComponent 定义如下**
|
72 |
+
|
73 |
+
```python
|
74 |
+
class CustomComponentDict(TypedDict):
|
75 |
+
props: Optional[List[str]]
|
76 |
+
template: Optional[str]
|
77 |
+
js: Optional[str]
|
78 |
+
```
|
79 |
+
|
80 |
+
### 内置的自定义标签
|
81 |
+
|
82 |
+
- <tab-link tab="custom_tags/select-box">select-box</tab-link>
|
83 |
+
- <tab-link tab="custom_tags/accordion">accordion</tab-link>
|
84 |
+
|
85 |
+
### event listeners
|
86 |
+
|
87 |
+
| 事件 | 描述 |
|
88 |
+
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
89 |
+
| `mgr.Markdown.custom(fn, ···)` | 自定义标签触发事件时触发,EventData 为:<br/> - index:当前 message 的 index tuple ([message index, user group(index 0) or bot group(index 1), user/bot group index])。<br/> - tag:当前触发的标签。<br/> - tag_index:当前触发标签的 index,此 index 在 mesage 的 index tuple 基础上重新计算。<br/> - value:自定义传入的值。 |
|
components/Markdown/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Markdown
|
2 |
+
|
3 |
+
Upgraded gradio Markdown.
|
4 |
+
|
5 |
+
- Supports output of multimodal content (audio, video, voice, files, text)
|
6 |
+
- Supports custom rendering components and interaction with Python-side events
|
7 |
+
|
8 |
+
## How to Use
|
9 |
+
|
10 |
+
### Basic Usage
|
11 |
+
|
12 |
+
<demo name="basic"></demo>
|
13 |
+
|
14 |
+
### Multimodal & Support for Local File Display
|
15 |
+
|
16 |
+
<demo name="multimodal"></demo>
|
17 |
+
|
18 |
+
### Support for Accordion Content Display
|
19 |
+
|
20 |
+
Include the `accordion` tag in the returned content for more usage details, see <tab-link tab="custom_tags/accordion">accordion</tab-link>
|
21 |
+
<demo name="accordion"></demo>
|
22 |
+
|
23 |
+
### Support for User Selection Interaction
|
24 |
+
|
25 |
+
Include the `select-box` tag in the returned content for more usage details, see <tab-link tab="custom_tags/select-box">select-box</tab-link>
|
26 |
+
<demo name="select-box"></demo>
|
27 |
+
|
28 |
+
### Custom Tags (Advanced Usage, Requires Frontend Knowledge)
|
29 |
+
|
30 |
+
<demo name="custom-tag"></demo>
|
31 |
+
|
32 |
+
#### Importing js
|
33 |
+
|
34 |
+
<demo name="custom-tag2"></demo>
|
35 |
+
The template can only perform simple variable replacements. If you want to introduce more custom behaviors, such as conditional judgments, loop rendering, etc., please use js to control the element for processing. Here is a simple example:
|
36 |
+
<demo name="custom-tag3">
|
37 |
+
<demo-suffix>
|
38 |
+
custom_select.js
|
39 |
+
|
40 |
+
```js
|
41 |
+
<file src="./resources/custom_components/custom_select.js"></file>
|
42 |
+
```
|
43 |
+
|
44 |
+
</demo-suffix>
|
45 |
+
</demo>
|
46 |
+
|
47 |
+
#### Interaction with Python Side
|
48 |
+
|
49 |
+
In js, you can use `cc.dispatch` to trigger the `custom` event listened to on the Python side. Taking the previous custom_select.js as an example, when we call `cc.dispatch(options[i])` on the frontend, a notification will be sent to the Python side simultaneously.
|
50 |
+
<demo name="custom-tag4"></demo>
|
51 |
+
|
52 |
+
## API and Parameter List
|
53 |
+
|
54 |
+
The following APIs are additional extended parameters beyond the original gradio Markdown.
|
55 |
+
|
56 |
+
### props
|
57 |
+
|
58 |
+
| Attribute | Type | Default Value | Description |
|
59 |
+
| ----------------------------- | ------------------------------------------------------------------- | ------------- | ---------------------------------------------------------------------------------------------------------- |
|
60 |
+
| enable_base64 | bool | False | Whether to support rendering content as base64, since rendering base64 is unsafe, the default is False. |
|
61 |
+
| preview | bool | True | Whether to enable image preview functionality. |
|
62 |
+
| enable_latex | bool | True | Whether to enable LaTeX rendering. |
|
63 |
+
| latex_single_dollar_delimiter | bool | True | Whether to enable single dollar delimiter `$` for LaTeX rendering. |
|
64 |
+
| custom_components | dict[str, CustomComponentDict] CustomComponentDict definition below | None | Supports user-defined custom tags and controls tag rendering styles and triggers Python events through js. |
|
65 |
+
| |
|
66 |
+
|
67 |
+
**CustomComponent definition is as follows:**
|
68 |
+
|
69 |
+
```python
|
70 |
+
class CustomComponentDict(TypedDict):
|
71 |
+
props: Optional[List[str]]
|
72 |
+
template: Optional[str]
|
73 |
+
js: Optional[str]
|
74 |
+
```
|
75 |
+
|
76 |
+
### Built-in Custom Tags
|
77 |
+
|
78 |
+
- <tab-link tab="custom_tags/select-box">select-box</tab-link>
|
79 |
+
- <tab-link tab="custom_tags/accordion">accordion</tab-link>
|
80 |
+
|
81 |
+
### Event Listeners
|
82 |
+
|
83 |
+
| Event | Description |
|
84 |
+
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
85 |
+
| `mgr.Markdown.custom(fn, ···)` | Triggered when a custom tag event occurs. EventData is: <br/> - index: The index tuple of the current message ([message index, user group(index 0) or bot group(index 1), user/bot group index]).<br/> - tag: The current tag that triggered the event.<br/> - tag_index: The index of the current triggered tag, re-calculated based on the message’s index tuple.<br/> - value: The custom value passed in. |
|
components/Markdown/app.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from components.Docs import Docs
|
4 |
+
|
5 |
+
|
6 |
+
def resolve(relative_path: str):
|
7 |
+
return os.path.join(os.path.dirname(__file__), relative_path)
|
8 |
+
|
9 |
+
|
10 |
+
docs = Docs(
|
11 |
+
__file__,
|
12 |
+
markdown_files=(["README.md", "README-zh_CN.md"] + [
|
13 |
+
f"custom_tags/{filename}"
|
14 |
+
for filename in os.listdir(resolve('custom_tags'))
|
15 |
+
if filename.endswith(".md")
|
16 |
+
]),
|
17 |
+
)
|
18 |
+
|
19 |
+
if __name__ == "__main__":
|
20 |
+
docs.render().queue().launch()
|
components/Markdown/custom_tags/accordion-zh_CN.md
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# accordion
|
2 |
+
|
3 |
+
在 markdown 文本中添加手风琴效果。
|
4 |
+
|
5 |
+
## 如何使用
|
6 |
+
|
7 |
+
### 基本使用
|
8 |
+
|
9 |
+
<demo name="custom_tags/accordion/basic"></demo>
|
10 |
+
|
11 |
+
### 使用 accordion-title 标记
|
12 |
+
|
13 |
+
使用`::accordion-title[content]`的形式可以在标题输入 markdown 文本。
|
14 |
+
|
15 |
+
<demo name="custom_tags/accordion/accordion-title"></demo>
|
16 |
+
|
17 |
+
## API 及参数列表
|
18 |
+
|
19 |
+
### props
|
20 |
+
|
21 |
+
| 属性 | 类型 | 默认值 | 描述 |
|
22 |
+
| ----- | ------ | ------ | ------------ |
|
23 |
+
| title | string | | 手风琴的标题 |
|
components/Markdown/custom_tags/accordion.md
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# accordion
|
2 |
+
|
3 |
+
Add an accordion effect to markdown text.
|
4 |
+
|
5 |
+
## How to Use
|
6 |
+
|
7 |
+
### Basic Usage
|
8 |
+
|
9 |
+
<demo name="custom_tags/accordion/basic"></demo>
|
10 |
+
|
11 |
+
### Using the accordion-title Marker
|
12 |
+
|
13 |
+
Using the form `::accordion-title[content]`, you can input markdown text into the title.
|
14 |
+
<demo name="custom_tags/accordion/accordion-title"></demo>
|
15 |
+
|
16 |
+
## API and Parameter List
|
17 |
+
|
18 |
+
### props
|
19 |
+
|
20 |
+
| Attribute | Type | Default Value | Description |
|
21 |
+
| --------- | ------ | ------------- | -------------------------- |
|
22 |
+
| title | string | | The title of the accordion |
|
components/Markdown/custom_tags/select-box-zh_CN.md
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# select-box
|
2 |
+
|
3 |
+
在 markdown 文本中添加选择交互框。
|
4 |
+
|
5 |
+
## 如何使用
|
6 |
+
|
7 |
+
### 基本使用
|
8 |
+
|
9 |
+
<demo name="custom_tags/select-box/basic"></demo>
|
10 |
+
|
11 |
+
### Card 样式
|
12 |
+
|
13 |
+
<demo name="custom_tags/select-box/card_shape"></demo>
|
14 |
+
|
15 |
+
### Card 自适应内部元素宽度
|
16 |
+
|
17 |
+
<demo name="custom_tags/select-box/card_shape_width_auto"></demo>
|
18 |
+
|
19 |
+
### 监听 Python 事件
|
20 |
+
|
21 |
+
<demo name="custom_tags/select-box/python_events"></demo>
|
22 |
+
|
23 |
+
## API 及参数列表
|
24 |
+
|
25 |
+
### value
|
26 |
+
|
27 |
+
custom 事件中 custom_data value 对应值, 返回值为用户 options 传入的对应 value ,如果type="checkbox",则返回一个 list。
|
28 |
+
|
29 |
+
### props
|
30 |
+
|
31 |
+
| 属性 | 类型 | 默认值 | 描述 |
|
32 |
+
| ------------ | ------------------------------------------------------------------------------------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
33 |
+
| type | 'checkbox' \| 'radio' | 'radio' | 选择框类型,'radio' 为单选框、'checkbox'为多选框。 |
|
34 |
+
| disabled | boolean | | 禁用选择,通常在需要读取历史信息二次渲染时会用到。 |
|
35 |
+
| value | string | | 默认选中值,通常适用于`type="checkbox"`时提前为用户选择部分选项和设置`disabled`后的默认值渲染。 |
|
36 |
+
| direction | 'horizontal' \| 'vertical' | 'horizontal' | 横向或竖向排列选择框 |
|
37 |
+
| shape | 'card' \| 'default' | 'default' | 选择框样式 |
|
38 |
+
| options | (string\| { label?: string, value?: string, imgSrc?: string})\[\] | | 为用户提供的选项值,每一项可以为 string 或 object。 当值为 object 时可以接收更多自定义值,其中imgSrc只有当shape="card"时才生效。 |
|
39 |
+
| select-once | boolean | false | 是否只允许用户选择一次 |
|
40 |
+
| submit-text | string | | 提交按钮的展示值,当该属性有值时,会展示提交按钮,此时用户只有点击提交按钮后才会触发选择事件。 |
|
41 |
+
| columns | number \| { xs?: number, sm?: number, md?: number, lg?: number, xl?: number, xxl?: number } | { xs: 1, sm: 2, md: 2, lg: 4} | 当shape="card"时才生效。每一行选项占用列数,值的范围为1 - 24,建议此项取值可以被 24 整除,否则可能列数会不符合预期。 当此项传入值为对象时,可以响应式控制每一行渲染列数,响应阈值如下:<br/> - xs:屏幕 < 576px <br/> - sm:屏幕 ≥ 576px <br/> - md:屏幕 ≥ 768px <br/> - lg:屏幕 ≥ 992px <br/> - xl:屏幕 ≥ 1200px <br/> - xxl:屏幕 ≥ 1600px 当direction为vertical时此配置不生效。 |
|
42 |
+
| item-width | string | | 当shape="card"时才生效。每个选项的宽度,如:'auto'、'100px',默认使用 columns 自动分配的宽度。 |
|
43 |
+
| item-height | string | | 当shape="card"时才生效。每个选项的高度,默认自适应元素高度。 |
|
44 |
+
| img-height | string | '160px' | 当shape="card"时才生效。每个选项中图片的高度。 |
|
45 |
+
| equal-height | boolean | false | 当shape="card"时才生效。是否每一行的选项高度都相等,会使用高度最高的选项。 |
|
components/Markdown/custom_tags/select-box.md
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# select-box
|
2 |
+
|
3 |
+
Add a selection interaction box to markdown text.
|
4 |
+
|
5 |
+
## How to Use
|
6 |
+
|
7 |
+
### Basic Usage
|
8 |
+
|
9 |
+
<demo name="custom_tags/select-box/basic"></demo>
|
10 |
+
|
11 |
+
### Card Style
|
12 |
+
|
13 |
+
<demo name="custom_tags/select-box/card_shape"></demo>
|
14 |
+
|
15 |
+
### Card Adapts to Width of Inner Elements
|
16 |
+
|
17 |
+
<demo name="custom_tags/select-box/card_shape_width_auto"></demo>
|
18 |
+
|
19 |
+
### Listening to Python Events
|
20 |
+
|
21 |
+
<demo name="custom_tags/select-box/python_events"></demo>
|
22 |
+
|
23 |
+
## API and Parameter List
|
24 |
+
|
25 |
+
### value
|
26 |
+
|
27 |
+
The corresponding value of custom_data value in the custom event; returns the user's inputted value for options. If type="checkbox", it returns a list.
|
28 |
+
|
29 |
+
### props
|
30 |
+
|
31 |
+
| Attribute | Type | Default Value | Description |
|
32 |
+
| ------------ | ------------------------------------------------------------------------------------------- | ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
33 |
+
| type | 'checkbox' \| 'radio' | 'radio' | The type of selection box: 'radio' for radio buttons, 'checkbox' for checkboxes. |
|
34 |
+
| disabled | boolean | | Disable the selection, usually used when reading historical information for re-rendering. |
|
35 |
+
| value | string | | The default selected value, usually used to pre-select some options for the user when `type="checkbox"` and to set the default value for rendering after setting `disabled`. |
|
36 |
+
| direction | 'horizontal' \| 'vertical' | 'horizontal' | Arrange selection boxes horizontally or vertically. |
|
37 |
+
| shape | 'card' \| 'default' | 'default' | The style of the selection box. |
|
38 |
+
| options | (string \| { label?: string, value?: string, imgSrc?: string })[] | | The options provided to the user, each item can be a string or an object. When the value is an object, it can accept more custom values, where imgSrc only takes effect when shape="card". |
|
39 |
+
| select-once | boolean | false | Whether to allow the user to make a selection only once. |
|
40 |
+
| submit-text | string | | The display value of the submit button. When this attribute has a value, the submit button will be displayed, and the selection event will only be triggered after the user clicks the submit button. |
|
41 |
+
| columns | number \| { xs?: number, sm?: number, md?: number, lg?: number, xl?: number, xxl?: number } | { xs: 1, sm: 2, md: 2, lg: 4 } | Effective only when shape="card". The number of columns each row of options occupies, the value range is 1 - 24, it is recommended that this value is divisible by 24, otherwise, the number of columns may not meet expectations. When this item is passed as an object, it can responsively control the number of rendered columns per row, responsive thresholds are as follows: <br/> - xs: screen < 576px <br/> - sm: screen ≥ 576px <br/> - md: screen ≥ 768px <br/> - lg: screen ≥ 992px <br/> - xl: screen ≥ 1200px <br/> - xxl: screen ≥ 1600px This configuration does not take effect when direction is vertical. |
|
42 |
+
| item-width | string | | Effective only when shape="card". The width of each option, such as: 'auto', '100px', the default uses the width automatically allocated by columns. |
|
43 |
+
| item-height | string | | Effective only when shape="card". The height of each option, defaults to adapt to the height of the element. |
|
44 |
+
| img-height | string | '160px' | Effective only when shape="card". The height of the images within each option. |
|
45 |
+
| equal-height | boolean | false | Effective only when shape="card". Whether the height of the options in each row should be equal, using the height of the tallest option. |
|
components/Markdown/demos/accordion.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import modelscope_studio as mgr
|
4 |
+
|
5 |
+
with gr.Blocks() as demo:
|
6 |
+
mgr.Markdown("""
|
7 |
+
<accordion title="Using tool">
|
8 |
+
|
9 |
+
```json
|
10 |
+
{"text": "glorious weather", "resolution": "1024*1024"}
|
11 |
+
```
|
12 |
+
|
13 |
+
</accordion>
|
14 |
+
|
15 |
+
Use `::accordion-title` to support markdown:
|
16 |
+
|
17 |
+
<accordion>
|
18 |
+
|
19 |
+
::accordion-title[Using `tool`]
|
20 |
+
|
21 |
+
```json
|
22 |
+
{"text": "glorious weather", "resolution": "1024*1024"}
|
23 |
+
```
|
24 |
+
</accordion>
|
25 |
+
""")
|
26 |
+
|
27 |
+
if __name__ == "__main__":
|
28 |
+
demo.queue().launch()
|
components/Markdown/demos/basic.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import modelscope_studio as mgr
|
4 |
+
|
5 |
+
with gr.Blocks() as demo:
|
6 |
+
mgr.Markdown(
|
7 |
+
"This _example_ was **written** in [Markdown](https://en.wikipedia.org/wiki/Markdown)\n"
|
8 |
+
)
|
9 |
+
|
10 |
+
if __name__ == "__main__":
|
11 |
+
demo.queue().launch()
|
components/Markdown/demos/custom-tag.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import modelscope_studio as mgr
|
4 |
+
|
5 |
+
with gr.Blocks() as demo:
|
6 |
+
mgr.Markdown(
|
7 |
+
"""
|
8 |
+
custom tag:<custom-tag value="aaa"></custom-tag>
|
9 |
+
""",
|
10 |
+
custom_components={
|
11 |
+
# Key is the tag name
|
12 |
+
"custom-tag": {
|
13 |
+
# The tag props.
|
14 |
+
"props": ["value"],
|
15 |
+
# The tag template, use `{prop}` as placeholder。
|
16 |
+
"template": "<div>{value}</div>"
|
17 |
+
}
|
18 |
+
})
|
19 |
+
|
20 |
+
if __name__ == "__main__":
|
21 |
+
demo.queue().launch()
|
components/Markdown/demos/custom-tag2.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import modelscope_studio as mgr
|
4 |
+
|
5 |
+
with gr.Blocks() as demo:
|
6 |
+
mgr.Markdown(
|
7 |
+
"""
|
8 |
+
custom tag:<custom-tag value="aaa"></custom-tag>
|
9 |
+
""",
|
10 |
+
custom_components={
|
11 |
+
"custom-tag": {
|
12 |
+
"props": ["value"],
|
13 |
+
"template":
|
14 |
+
"<button onclick='{onClick}'>{value}</button>",
|
15 |
+
# The `js` property should be a string containing a JavaScript Function.
|
16 |
+
"js":
|
17 |
+
"""
|
18 |
+
(props, cc, { el, onMount }) => {
|
19 |
+
// `onMount` will be called after the template rendered
|
20 |
+
onMount(() => {
|
21 |
+
// `el` is the container element
|
22 |
+
console.log(el)
|
23 |
+
})
|
24 |
+
console.log(props.children) // By default, `props` will be passed a property named `children`, which can get the content in the tag, such as 'xx' in '<tag>xx</tag>'.
|
25 |
+
|
26 |
+
// The return value will be merged with `props` and passed to the template.
|
27 |
+
return {
|
28 |
+
value: 'Click Me: ' + props.value,
|
29 |
+
onClick: () => {
|
30 |
+
alert('hello')
|
31 |
+
}
|
32 |
+
}
|
33 |
+
}"""
|
34 |
+
}
|
35 |
+
})
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
demo.queue().launch()
|
components/Markdown/demos/custom-tag3.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
import modelscope_studio as mgr
|
7 |
+
|
8 |
+
options = ["a", "b", "c"]
|
9 |
+
|
10 |
+
|
11 |
+
def resolve_assets(relative_path):
|
12 |
+
return os.path.join(os.path.dirname(__file__), "../resources",
|
13 |
+
relative_path)
|
14 |
+
|
15 |
+
|
16 |
+
with open(resolve_assets("./custom_components/custom_select.js"), 'r') as f:
|
17 |
+
custom_select_js = f.read()
|
18 |
+
|
19 |
+
with gr.Blocks() as demo:
|
20 |
+
mgr.Markdown(value=f"""
|
21 |
+
custom tag: <custom-select options='{json.dumps(options)}'></custom-select>
|
22 |
+
""",
|
23 |
+
custom_components={
|
24 |
+
"custom-select": {
|
25 |
+
"props": ["options"],
|
26 |
+
"js": custom_select_js,
|
27 |
+
}
|
28 |
+
})
|
29 |
+
|
30 |
+
if __name__ == "__main__":
|
31 |
+
demo.queue().launch()
|
components/Markdown/demos/custom-tag4.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
import modelscope_studio as mgr
|
7 |
+
|
8 |
+
options = ["a", "b", "c"]
|
9 |
+
|
10 |
+
|
11 |
+
def resolve_assets(relative_path):
|
12 |
+
return os.path.join(os.path.dirname(__file__), "../resources",
|
13 |
+
relative_path)
|
14 |
+
|
15 |
+
|
16 |
+
with open(resolve_assets("./custom_components/custom_select.js"), 'r') as f:
|
17 |
+
custom_select_js = f.read()
|
18 |
+
|
19 |
+
|
20 |
+
def fn(data: gr.EventData):
|
21 |
+
# custom {'index': [0, 1, 0], 'tag': 'custom-select', 'tag_index': 0, 'value': 'option A'}
|
22 |
+
print("custom value", data._data)
|
23 |
+
|
24 |
+
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
md = mgr.Markdown(value=f"""
|
27 |
+
custom tag: <custom-select options='{json.dumps(options)}'></custom-select>
|
28 |
+
""",
|
29 |
+
custom_components={
|
30 |
+
"custom-select": {
|
31 |
+
"props": ["options"],
|
32 |
+
"js": custom_select_js,
|
33 |
+
}
|
34 |
+
})
|
35 |
+
md.custom(fn=fn)
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
demo.queue().launch()
|
components/Markdown/demos/custom_tags/accordion/accordion-title.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import modelscope_studio as mgr
|
4 |
+
|
5 |
+
with gr.Blocks() as demo:
|
6 |
+
mgr.Markdown("""
|
7 |
+
<accordion>
|
8 |
+
|
9 |
+
::accordion-title[Using `tool`]
|
10 |
+
|
11 |
+
```json
|
12 |
+
{"text": "glorious weather", "resolution": "1024*1024"}
|
13 |
+
```
|
14 |
+
|
15 |
+
</accordion>
|
16 |
+
""")
|
17 |
+
|
18 |
+
if __name__ == "__main__":
|
19 |
+
demo.queue().launch()
|
components/Markdown/demos/custom_tags/accordion/basic.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import modelscope_studio as mgr
|
4 |
+
|
5 |
+
with gr.Blocks() as demo:
|
6 |
+
mgr.Markdown("""
|
7 |
+
<accordion title="Using tool">
|
8 |
+
|
9 |
+
```json
|
10 |
+
{"text": "glorious weather", "resolution": "1024*1024"}
|
11 |
+
```
|
12 |
+
|
13 |
+
</accordion>
|
14 |
+
""")
|
15 |
+
|
16 |
+
if __name__ == "__main__":
|
17 |
+
demo.queue().launch()
|
components/Markdown/demos/custom_tags/select-box/basic.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
import modelscope_studio as mgr
|
6 |
+
|
7 |
+
options = [{"label": "A", "value": "a"}, "b", "c"]
|
8 |
+
|
9 |
+
with gr.Blocks() as demo:
|
10 |
+
mgr.Markdown(
|
11 |
+
f"""Single Select: <select-box options='{json.dumps(options)}' select-once></select-box>
|
12 |
+
|
13 |
+
Multiple Select:<select-box type="checkbox" options='{json.dumps(options)}' select-once submit-text="Submit"></select-box>
|
14 |
+
|
15 |
+
Vertical Direction:
|
16 |
+
|
17 |
+
<select-box direction="vertical" type="checkbox" options='{json.dumps(options)}' select-once submit-text="Submit"></select-box>
|
18 |
+
""", )
|
19 |
+
|
20 |
+
if __name__ == "__main__":
|
21 |
+
demo.queue().launch()
|
components/Markdown/demos/custom_tags/select-box/card_shape.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
import modelscope_studio as mgr
|
7 |
+
|
8 |
+
# Card shape supports setting `imgSrc` as the cover.
|
9 |
+
options = [{
|
10 |
+
"label":
|
11 |
+
"A",
|
12 |
+
"imgSrc":
|
13 |
+
os.path.join(os.path.dirname(__file__), '../../../resources/screen.jpeg'),
|
14 |
+
"value":
|
15 |
+
"a"
|
16 |
+
}, "b", "c", "d"]
|
17 |
+
|
18 |
+
with gr.Blocks() as demo:
|
19 |
+
mgr.Markdown(
|
20 |
+
f"""<select-box shape="card" options='{json.dumps(options)}' select-once equal-height></select-box>
|
21 |
+
|
22 |
+
Custom Columns:
|
23 |
+
|
24 |
+
<select-box shape="card" columns="2" options='{json.dumps(options)}' select-once equal-height></select-box>
|
25 |
+
|
26 |
+
Vertical Direction:
|
27 |
+
|
28 |
+
<select-box shape="card" direction="vertical" options='{json.dumps(options)}' select-once equal-height></select-box>
|
29 |
+
""")
|
30 |
+
|
31 |
+
if __name__ == "__main__":
|
32 |
+
demo.queue().launch()
|
components/Markdown/demos/custom_tags/select-box/card_shape_width_auto.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
import modelscope_studio as mgr
|
7 |
+
|
8 |
+
# Card shape supports setting `imgSrc` as the cover.
|
9 |
+
options = [{
|
10 |
+
"label":
|
11 |
+
"A",
|
12 |
+
"imgSrc":
|
13 |
+
os.path.join(os.path.dirname(__file__), '../../../resources/screen.jpeg'),
|
14 |
+
"value":
|
15 |
+
"a"
|
16 |
+
}, "b", "c", "d"]
|
17 |
+
|
18 |
+
with gr.Blocks() as demo:
|
19 |
+
mgr.Markdown(
|
20 |
+
# item-width="auto"
|
21 |
+
f"""
|
22 |
+
<select-box shape="card" direction="vertical" options='{json.dumps(options)}' select-once item-width="auto"></select-box>
|
23 |
+
""", )
|
24 |
+
|
25 |
+
if __name__ == "__main__":
|
26 |
+
demo.queue().launch()
|
components/Markdown/demos/custom_tags/select-box/python_events.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
import modelscope_studio as mgr
|
6 |
+
|
7 |
+
options = [{"label": "A", "value": "a"}, "b", "c"]
|
8 |
+
|
9 |
+
|
10 |
+
def fn(data: gr.EventData):
|
11 |
+
custom_data = data._data
|
12 |
+
if (custom_data["tag"] == "select-box"):
|
13 |
+
print(custom_data["value"]
|
14 |
+
) # 'a' or 'b' or 'c', the value set in the options.
|
15 |
+
|
16 |
+
|
17 |
+
with gr.Blocks() as demo:
|
18 |
+
md = mgr.Markdown(
|
19 |
+
f"<select-box options='{json.dumps(options)}' select-once></select-box>"
|
20 |
+
)
|
21 |
+
md.custom(fn=fn)
|
22 |
+
|
23 |
+
if __name__ == "__main__":
|
24 |
+
demo.queue().launch()
|
components/Markdown/demos/multimodal.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
import modelscope_studio as mgr
|
6 |
+
|
7 |
+
|
8 |
+
def resolve_assets(relative_path):
|
9 |
+
return os.path.join(os.path.dirname(__file__), "../resources",
|
10 |
+
relative_path)
|
11 |
+
|
12 |
+
|
13 |
+
with gr.Blocks() as demo:
|
14 |
+
mgr.Markdown(f"""
|
15 |
+
Image
|
16 |
+
|
17 |
+
![image]({resolve_assets("bot.jpeg")})
|
18 |
+
|
19 |
+
<img src="{resolve_assets("user.jpeg")}" />
|
20 |
+
|
21 |
+
Video
|
22 |
+
|
23 |
+
<video src="{resolve_assets("dog.mp4")}"></video>
|
24 |
+
|
25 |
+
Audio
|
26 |
+
|
27 |
+
<audio src="{resolve_assets("audio.wav")}"></audio>
|
28 |
+
""")
|
29 |
+
|
30 |
+
if __name__ == "__main__":
|
31 |
+
demo.queue().launch()
|
components/Markdown/demos/select-box.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
import modelscope_studio as mgr
|
6 |
+
|
7 |
+
# `label` will display on the page, and `value` is the actual selected value.
|
8 |
+
options = [{"label": "A", "value": "a"}, "b", "c"]
|
9 |
+
|
10 |
+
with gr.Blocks() as demo:
|
11 |
+
mgr.Markdown(f"""
|
12 |
+
Single Select: <select-box options='{json.dumps(options)}' select-once></select-box>
|
13 |
+
|
14 |
+
Multiple Select:<select-box type="checkbox" options='{json.dumps(options)}' select-once submit-text="Submit"></select-box>
|
15 |
+
|
16 |
+
Vertical Direction:
|
17 |
+
|
18 |
+
<select-box direction="vertical" type="checkbox" options='{json.dumps(options)}' select-once submit-text="Submit"></select-box>
|
19 |
+
|
20 |
+
Card Shape:
|
21 |
+
|
22 |
+
<select-box shape="card" options='{json.dumps(options)}' select-once equal-height></select-box>
|
23 |
+
|
24 |
+
|
25 |
+
<select-box shape="card" columns="2" options='{json.dumps(options)}' select-once equal-height></select-box>
|
26 |
+
|
27 |
+
|
28 |
+
<select-box shape="card" direction="vertical" options='{json.dumps(options)}' select-once equal-height></select-box>
|
29 |
+
""")
|
30 |
+
|
31 |
+
if __name__ == "__main__":
|
32 |
+
demo.queue().launch()
|
components/Markdown/resources/audio.wav
ADDED
Binary file (44.8 kB). View file
|
|
components/Markdown/resources/bot.jpeg
ADDED
components/Markdown/resources/custom_components/custom_select.js
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
(props, cc, { el, onMount }) => {
|
2 |
+
const options = JSON.parse(props.options);
|
3 |
+
el.innerHTML = `
|
4 |
+
${options
|
5 |
+
.map((option) => {
|
6 |
+
return `<div>
|
7 |
+
<label>${option} <input type="radio"/></label>
|
8 |
+
<div>`;
|
9 |
+
})
|
10 |
+
.join('')}
|
11 |
+
`;
|
12 |
+
onMount(() => {
|
13 |
+
const inputs = Array.from(el.getElementsByTagName('input'));
|
14 |
+
Array.from(el.getElementsByTagName('label')).forEach((label, i) => {
|
15 |
+
label.addEventListener('click', () => {
|
16 |
+
inputs.forEach((input) => {
|
17 |
+
input.checked = false;
|
18 |
+
});
|
19 |
+
const input = label.getElementsByTagName('input')[0];
|
20 |
+
input.checked = true;
|
21 |
+
// Use cc.dispatch to trigger events.
|
22 |
+
cc.dispatch(options[i]);
|
23 |
+
});
|
24 |
+
});
|
25 |
+
});
|
26 |
+
};
|
components/Markdown/resources/dog.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:39d086ce29e48cf76e5042d2f3f0611ee46575f70fa3dc0c40dd4cfffde3d933
|
3 |
+
size 8626383
|
components/Markdown/resources/screen.jpeg
ADDED