Commit
•
5db0478
1
Parent(s):
0cbeb30
Upload folder using huggingface_hub
Browse files- Dockerfile +5 -1
- README.md +10 -3
- __pycache__/app.cpython-310.pyc +0 -0
- app.py +3 -2
- css.css +157 -0
- requirements.txt +1 -1
- space.py +171 -0
- src/README.md +412 -3
- src/backend/gradio_multimodalchatbot/multimodalchatbot.py +1 -1
- src/backend/gradio_multimodalchatbot/multimodalchatbot.pyi +22 -7
- src/demo/app.py +3 -2
- src/demo/css.css +157 -0
- src/demo/space.py +171 -0
- src/pyproject.toml +7 -5
Dockerfile
CHANGED
@@ -5,8 +5,12 @@ WORKDIR /code
|
|
5 |
|
6 |
COPY --link --chown=1000 . .
|
7 |
|
|
|
|
|
|
|
|
|
8 |
RUN pip install --no-cache-dir -r requirements.txt
|
9 |
|
10 |
ENV PYTHONUNBUFFERED=1 GRADIO_ALLOW_FLAGGING=never GRADIO_NUM_PORTS=1 GRADIO_SERVER_NAME=0.0.0.0 GRADIO_SERVER_PORT=7860 SYSTEM=spaces
|
11 |
|
12 |
-
CMD ["python", "
|
|
|
5 |
|
6 |
COPY --link --chown=1000 . .
|
7 |
|
8 |
+
RUN mkdir -p /tmp/cache/
|
9 |
+
RUN chmod a+rwx -R /tmp/cache/
|
10 |
+
ENV TRANSFORMERS_CACHE=/tmp/cache/
|
11 |
+
|
12 |
RUN pip install --no-cache-dir -r requirements.txt
|
13 |
|
14 |
ENV PYTHONUNBUFFERED=1 GRADIO_ALLOW_FLAGGING=never GRADIO_NUM_PORTS=1 GRADIO_SERVER_NAME=0.0.0.0 GRADIO_SERVER_PORT=7860 SYSTEM=spaces
|
15 |
|
16 |
+
CMD ["python", "space.py"]
|
README.md
CHANGED
@@ -1,10 +1,17 @@
|
|
1 |
|
2 |
---
|
3 |
-
tags: [gradio-custom-component,
|
4 |
title: gradio_multimodalchatbot V0.0.1
|
5 |
-
colorFrom:
|
6 |
-
colorTo:
|
7 |
sdk: docker
|
8 |
pinned: false
|
9 |
license: apache-2.0
|
10 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
---
|
3 |
+
tags: [gradio-custom-component,Chatbot,Multimodal,Media,gradio-template-Chatbot]
|
4 |
title: gradio_multimodalchatbot V0.0.1
|
5 |
+
colorFrom: green
|
6 |
+
colorTo: blue
|
7 |
sdk: docker
|
8 |
pinned: false
|
9 |
license: apache-2.0
|
10 |
---
|
11 |
+
|
12 |
+
|
13 |
+
# Name: gradio_multimodalchatbot
|
14 |
+
|
15 |
+
Description: Display text and media files (audio, video, images) in the same chat message bubble.
|
16 |
+
|
17 |
+
Install with: pip install gradio_multimodalchatbot
|
__pycache__/app.cpython-310.pyc
CHANGED
Binary files a/__pycache__/app.cpython-310.pyc and b/__pycache__/app.cpython-310.pyc differ
|
|
app.py
CHANGED
@@ -23,7 +23,8 @@ bot_msg3 = {"text": "Here is a video clip of the world",
|
|
23 |
conversation = [[user_msg1, bot_msg1], [user_msg2, bot_msg2], [user_msg3, bot_msg3]]
|
24 |
|
25 |
with gr.Blocks() as demo:
|
26 |
-
MultimodalChatbot(value=conversation)
|
27 |
|
28 |
|
29 |
-
|
|
|
|
23 |
conversation = [[user_msg1, bot_msg1], [user_msg2, bot_msg2], [user_msg3, bot_msg3]]
|
24 |
|
25 |
with gr.Blocks() as demo:
|
26 |
+
MultimodalChatbot(value=conversation, height=800)
|
27 |
|
28 |
|
29 |
+
if __name__ == "__main__":
|
30 |
+
demo.launch()
|
css.css
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
html {
|
2 |
+
font-family: Inter;
|
3 |
+
font-size: 16px;
|
4 |
+
font-weight: 400;
|
5 |
+
line-height: 1.5;
|
6 |
+
-webkit-text-size-adjust: 100%;
|
7 |
+
background: #fff;
|
8 |
+
color: #323232;
|
9 |
+
-webkit-font-smoothing: antialiased;
|
10 |
+
-moz-osx-font-smoothing: grayscale;
|
11 |
+
text-rendering: optimizeLegibility;
|
12 |
+
}
|
13 |
+
|
14 |
+
:root {
|
15 |
+
--space: 1;
|
16 |
+
--vspace: calc(var(--space) * 1rem);
|
17 |
+
--vspace-0: calc(3 * var(--space) * 1rem);
|
18 |
+
--vspace-1: calc(2 * var(--space) * 1rem);
|
19 |
+
--vspace-2: calc(1.5 * var(--space) * 1rem);
|
20 |
+
--vspace-3: calc(0.5 * var(--space) * 1rem);
|
21 |
+
}
|
22 |
+
|
23 |
+
.app {
|
24 |
+
max-width: 748px !important;
|
25 |
+
}
|
26 |
+
|
27 |
+
.prose p {
|
28 |
+
margin: var(--vspace) 0;
|
29 |
+
line-height: var(--vspace * 2);
|
30 |
+
font-size: 1rem;
|
31 |
+
}
|
32 |
+
|
33 |
+
code {
|
34 |
+
font-family: "Inconsolata", sans-serif;
|
35 |
+
font-size: 16px;
|
36 |
+
}
|
37 |
+
|
38 |
+
h1,
|
39 |
+
h1 code {
|
40 |
+
font-weight: 400;
|
41 |
+
line-height: calc(2.5 / var(--space) * var(--vspace));
|
42 |
+
}
|
43 |
+
|
44 |
+
h1 code {
|
45 |
+
background: none;
|
46 |
+
border: none;
|
47 |
+
letter-spacing: 0.05em;
|
48 |
+
padding-bottom: 5px;
|
49 |
+
position: relative;
|
50 |
+
padding: 0;
|
51 |
+
}
|
52 |
+
|
53 |
+
h2 {
|
54 |
+
margin: var(--vspace-1) 0 var(--vspace-2) 0;
|
55 |
+
line-height: 1em;
|
56 |
+
}
|
57 |
+
|
58 |
+
h3,
|
59 |
+
h3 code {
|
60 |
+
margin: var(--vspace-1) 0 var(--vspace-2) 0;
|
61 |
+
line-height: 1em;
|
62 |
+
}
|
63 |
+
|
64 |
+
h4,
|
65 |
+
h5,
|
66 |
+
h6 {
|
67 |
+
margin: var(--vspace-3) 0 var(--vspace-3) 0;
|
68 |
+
line-height: var(--vspace);
|
69 |
+
}
|
70 |
+
|
71 |
+
.bigtitle,
|
72 |
+
h1,
|
73 |
+
h1 code {
|
74 |
+
font-size: calc(8px * 4.5);
|
75 |
+
word-break: break-word;
|
76 |
+
}
|
77 |
+
|
78 |
+
.title,
|
79 |
+
h2,
|
80 |
+
h2 code {
|
81 |
+
font-size: calc(8px * 3.375);
|
82 |
+
font-weight: lighter;
|
83 |
+
word-break: break-word;
|
84 |
+
border: none;
|
85 |
+
background: none;
|
86 |
+
}
|
87 |
+
|
88 |
+
.subheading1,
|
89 |
+
h3,
|
90 |
+
h3 code {
|
91 |
+
font-size: calc(8px * 1.8);
|
92 |
+
font-weight: 600;
|
93 |
+
border: none;
|
94 |
+
background: none;
|
95 |
+
letter-spacing: 0.1em;
|
96 |
+
text-transform: uppercase;
|
97 |
+
}
|
98 |
+
|
99 |
+
h2 code {
|
100 |
+
padding: 0;
|
101 |
+
position: relative;
|
102 |
+
letter-spacing: 0.05em;
|
103 |
+
}
|
104 |
+
|
105 |
+
blockquote {
|
106 |
+
font-size: calc(8px * 1.1667);
|
107 |
+
font-style: italic;
|
108 |
+
line-height: calc(1.1667 * var(--vspace));
|
109 |
+
margin: var(--vspace-2) var(--vspace-2);
|
110 |
+
}
|
111 |
+
|
112 |
+
.subheading2,
|
113 |
+
h4 {
|
114 |
+
font-size: calc(8px * 1.4292);
|
115 |
+
text-transform: uppercase;
|
116 |
+
font-weight: 600;
|
117 |
+
}
|
118 |
+
|
119 |
+
.subheading3,
|
120 |
+
h5 {
|
121 |
+
font-size: calc(8px * 1.2917);
|
122 |
+
line-height: calc(1.2917 * var(--vspace));
|
123 |
+
|
124 |
+
font-weight: lighter;
|
125 |
+
text-transform: uppercase;
|
126 |
+
letter-spacing: 0.15em;
|
127 |
+
}
|
128 |
+
|
129 |
+
h6 {
|
130 |
+
font-size: calc(8px * 1.1667);
|
131 |
+
font-size: 1.1667em;
|
132 |
+
font-weight: normal;
|
133 |
+
font-style: italic;
|
134 |
+
font-family: "le-monde-livre-classic-byol", serif !important;
|
135 |
+
letter-spacing: 0px !important;
|
136 |
+
}
|
137 |
+
|
138 |
+
#start .md > *:first-child {
|
139 |
+
margin-top: 0;
|
140 |
+
}
|
141 |
+
|
142 |
+
h2 + h3 {
|
143 |
+
margin-top: 0;
|
144 |
+
}
|
145 |
+
|
146 |
+
.md hr {
|
147 |
+
border: none;
|
148 |
+
border-top: 1px solid var(--block-border-color);
|
149 |
+
margin: var(--vspace-2) 0 var(--vspace-2) 0;
|
150 |
+
}
|
151 |
+
.prose ul {
|
152 |
+
margin: var(--vspace-2) 0 var(--vspace-1) 0;
|
153 |
+
}
|
154 |
+
|
155 |
+
.gap {
|
156 |
+
gap: 0;
|
157 |
+
}
|
requirements.txt
CHANGED
@@ -1 +1 @@
|
|
1 |
-
gradio_multimodalchatbot
|
|
|
1 |
+
gradio_multimodalchatbot==0.0.1
|
space.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from app import demo as app
|
4 |
+
import os
|
5 |
+
|
6 |
+
_docs = {'MultimodalChatbot': {'description': 'Displays a chatbot output showing both user submitted messages and responses. Supports a subset of Markdown including bold, italics, code, tables. Also supports audio/video/image files, which are displayed in the MultimodalChatbot, and other kinds of files which are displayed as links.\n', 'members': {'__init__': {'value': {'type': 'list[\n list[\n str\n | tuple[str]\n | tuple[str | pathlib.Path, str]\n | None\n ]\n ]\n | Callable\n | None', 'default': 'None', 'description': 'Default value to show in chatbot. If callable, the function will be called whenever the app loads to set the initial value of the component.'}, 'label': {'type': 'str | None', 'default': 'None', 'description': 'The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.'}, 'every': {'type': 'float | None', 'default': 'None', 'description': "If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute."}, 'show_label': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will display label.'}, 'container': {'type': 'bool', 'default': 'True', 'description': 'If True, will place the component in a container - providing some extra padding around the border.'}, 'scale': {'type': 'int | None', 'default': 'None', 'description': 'relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.'}, 'min_width': {'type': 'int', 'default': '160', 'description': 'minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.'}, 'visible': {'type': 'bool', 'default': 'True', 'description': 'If False, component will be hidden.'}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': 'An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': 'An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'render': {'type': 'bool', 'default': 'True', 'description': 'If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.'}, 'height': {'type': 'int | None', 'default': 'None', 'description': 'height of the component in pixels.'}, 'latex_delimiters': {'type': 'list[dict[str, str | bool]] | None', 'default': 'None', 'description': 'A list of dicts of the form {"left": open delimiter (str), "right": close delimiter (str), "display": whether to display in newline (bool)} that will be used to render LaTeX expressions. If not provided, `latex_delimiters` is set to `[{ "left": "$$", "right": "$$", "display": True }]`, so only expressions enclosed in $$ delimiters will be rendered as LaTeX, and in a new line. Pass in an empty list to disable LaTeX rendering. For more information, see the [KaTeX documentation](https://katex.org/docs/autorender.html).'}, 'rtl': {'type': 'bool', 'default': 'False', 'description': 'If True, sets the direction of the rendered text to right-to-left. Default is False, which renders text left-to-right.'}, 'show_share_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.'}, 'show_copy_button': {'type': 'bool', 'default': 'False', 'description': 'If True, will show a copy button for each chatbot message.'}, 'avatar_images': {'type': 'tuple[\n str | pathlib.Path | None, str | pathlib.Path | None\n ]\n | None', 'default': 'None', 'description': 'Tuple of two avatar image paths or URLs for user and bot (in that order). Pass None for either the user or bot image to skip. Must be within the working directory of the Gradio app or an external URL.'}, 'sanitize_html': {'type': 'bool', 'default': 'True', 'description': 'If False, will disable HTML sanitization for chatbot messages. This is not recommended, as it can lead to security vulnerabilities.'}, 'render_markdown': {'type': 'bool', 'default': 'True', 'description': 'If False, will disable Markdown rendering for chatbot messages.'}, 'bubble_full_width': {'type': 'bool', 'default': 'True', 'description': 'If False, the chat bubble will fit to the content of the message. If True (default), the chat bubble will be the full width of the component.'}, 'line_breaks': {'type': 'bool', 'default': 'True', 'description': 'If True (default), will enable Github-flavored Markdown line breaks in chatbot messages. If False, single new lines will be ignored. Only applies if `render_markdown` is True.'}, 'likeable': {'type': 'bool', 'default': 'False', 'description': 'Whether the chat messages display a like or dislike button. Set automatically by the .like method but has to be present in the signature for it to show up in the config.'}, 'layout': {'type': '"panel" | "bubble" | None', 'default': 'None', 'description': 'If "panel", will display the chatbot in a llm style layout. If "bubble", will display the chatbot with message bubbles, with the user and bot messages on alterating sides. Will default to "bubble".'}}, 'postprocess': {'value': {'type': 'list[\n list[str | tuple[str] | tuple[str, str] | None] | tuple\n]', 'description': None}}, 'preprocess': {'return': {'type': 'list[MultimodalMessage] | None', 'description': None}, 'value': None}}, 'events': {'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the MultimodalChatbot changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'select': {'type': None, 'default': None, 'description': 'Event listener for when the user selects or deselects the MultimodalChatbot. Uses event data gradio.SelectData to carry `value` referring to the label of the MultimodalChatbot, and `selected` to refer to state of the MultimodalChatbot. See EventData documentation on how to use this event data'}, 'like': {'type': None, 'default': None, 'description': 'This listener is triggered when the user likes/dislikes from within the MultimodalChatbot. This event has EventData of type gradio.LikeData that carries information, accessible through LikeData.index and LikeData.value. See EventData documentation on how to use this event data.'}}}, '__meta__': {'additional_interfaces': {'MultimodalMessage': {'source': 'class MultimodalMessage(GradioModel):\n text: Optional[str] = None\n files: Optional[List[FileMessage]] = None', 'refs': ['FileMessage']}, 'FileMessage': {'source': 'class FileMessage(GradioModel):\n file: FileData\n alt_text: Optional[str] = None'}}, 'user_fn_refs': {'MultimodalChatbot': ['MultimodalMessage']}}}
|
7 |
+
|
8 |
+
abs_path = os.path.join(os.path.dirname(__file__), "css.css")
|
9 |
+
|
10 |
+
with gr.Blocks(
|
11 |
+
css=abs_path,
|
12 |
+
theme=gr.themes.Default(
|
13 |
+
font_mono=[
|
14 |
+
gr.themes.GoogleFont("Inconsolata"),
|
15 |
+
"monospace",
|
16 |
+
],
|
17 |
+
),
|
18 |
+
) as demo:
|
19 |
+
gr.Markdown(
|
20 |
+
"""
|
21 |
+
# `gradio_multimodalchatbot`
|
22 |
+
|
23 |
+
<div style="display: flex; gap: 7px;">
|
24 |
+
<a href="https://pypi.org/project/gradio_multimodalchatbot/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_multimodalchatbot"></a> <a href="https://huggingface.co/spaces/freddyaboulton/gradio_multimodalchatbot//discussions" target="_blank"><img alt="Static Badge" src="https://img.shields.io/badge/%F0%9F%A4%97%20Discuss-%23097EFF?style=flat&logoColor=black"></a>
|
25 |
+
</div>
|
26 |
+
|
27 |
+
Display text and media files (audio, video, images) in the same chat message bubble.
|
28 |
+
""", elem_classes=["md-custom"], header_links=True)
|
29 |
+
app.render()
|
30 |
+
gr.Markdown(
|
31 |
+
"""
|
32 |
+
## Installation
|
33 |
+
|
34 |
+
```bash
|
35 |
+
pip install gradio_multimodalchatbot
|
36 |
+
```
|
37 |
+
|
38 |
+
## Usage
|
39 |
+
|
40 |
+
```python
|
41 |
+
|
42 |
+
import gradio as gr
|
43 |
+
from gradio_multimodalchatbot import MultimodalChatbot
|
44 |
+
from gradio.data_classes import FileData
|
45 |
+
|
46 |
+
user_msg1 = {"text": "Hello, what is in this image?",
|
47 |
+
"files": [{"file": FileData(path="https://gradio-builds.s3.amazonaws.com/diffusion_image/cute_dog.jpg")}]
|
48 |
+
}
|
49 |
+
bot_msg1 = {"text": "It is a very cute dog",
|
50 |
+
"files": []}
|
51 |
+
|
52 |
+
user_msg2 = {"text": "Describe this audio clip please.",
|
53 |
+
"files": [{"file": FileData(path="cantina.wav")}]}
|
54 |
+
bot_msg2 = {"text": "It is the cantina song from Star Wars",
|
55 |
+
"files": []}
|
56 |
+
|
57 |
+
user_msg3 = {"text": "Give me a video clip please.",
|
58 |
+
"files": []}
|
59 |
+
bot_msg3 = {"text": "Here is a video clip of the world",
|
60 |
+
"files": [{"file": FileData(path="world.mp4")},
|
61 |
+
{"file": FileData(path="cantina.wav")}]}
|
62 |
+
|
63 |
+
conversation = [[user_msg1, bot_msg1], [user_msg2, bot_msg2], [user_msg3, bot_msg3]]
|
64 |
+
|
65 |
+
with gr.Blocks() as demo:
|
66 |
+
MultimodalChatbot(value=conversation, height=800)
|
67 |
+
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
demo.launch()
|
71 |
+
|
72 |
+
```
|
73 |
+
""", elem_classes=["md-custom"], header_links=True)
|
74 |
+
|
75 |
+
|
76 |
+
gr.Markdown("""
|
77 |
+
## `MultimodalChatbot`
|
78 |
+
|
79 |
+
### Initialization
|
80 |
+
""", elem_classes=["md-custom"], header_links=True)
|
81 |
+
|
82 |
+
gr.ParamViewer(value=_docs["MultimodalChatbot"]["members"]["__init__"], linkify=['MultimodalMessage', 'FileMessage'])
|
83 |
+
|
84 |
+
|
85 |
+
gr.Markdown("### Events")
|
86 |
+
gr.ParamViewer(value=_docs["MultimodalChatbot"]["events"], linkify=['Event'])
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
gr.Markdown("""
|
92 |
+
|
93 |
+
### User function
|
94 |
+
|
95 |
+
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
|
96 |
+
|
97 |
+
- When used as an Input, the component only impacts the input signature of the user function.
|
98 |
+
- When used as an output, the component only impacts the return signature of the user function.
|
99 |
+
|
100 |
+
The code snippet below is accurate in cases where the component is used as both an input and an output.
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
```python
|
105 |
+
def predict(
|
106 |
+
value: list[MultimodalMessage] | None
|
107 |
+
) -> list[
|
108 |
+
list[str | tuple[str] | tuple[str, str] | None] | tuple
|
109 |
+
]:
|
110 |
+
return value
|
111 |
+
```
|
112 |
+
""", elem_classes=["md-custom", "MultimodalChatbot-user-fn"], header_links=True)
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
code_MultimodalMessage = gr.Markdown("""
|
118 |
+
## `MultimodalMessage`
|
119 |
+
```python
|
120 |
+
class MultimodalMessage(GradioModel):
|
121 |
+
text: Optional[str] = None
|
122 |
+
files: Optional[List[FileMessage]] = None
|
123 |
+
```""", elem_classes=["md-custom", "MultimodalMessage"], header_links=True)
|
124 |
+
|
125 |
+
code_FileMessage = gr.Markdown("""
|
126 |
+
## `FileMessage`
|
127 |
+
```python
|
128 |
+
class FileMessage(GradioModel):
|
129 |
+
file: FileData
|
130 |
+
alt_text: Optional[str] = None
|
131 |
+
```""", elem_classes=["md-custom", "FileMessage"], header_links=True)
|
132 |
+
|
133 |
+
demo.load(None, js=r"""function() {
|
134 |
+
const refs = {
|
135 |
+
MultimodalMessage: ['FileMessage'],
|
136 |
+
FileMessage: [], };
|
137 |
+
const user_fn_refs = {
|
138 |
+
MultimodalChatbot: ['MultimodalMessage'], };
|
139 |
+
requestAnimationFrame(() => {
|
140 |
+
|
141 |
+
Object.entries(user_fn_refs).forEach(([key, refs]) => {
|
142 |
+
if (refs.length > 0) {
|
143 |
+
const el = document.querySelector(`.${key}-user-fn`);
|
144 |
+
if (!el) return;
|
145 |
+
refs.forEach(ref => {
|
146 |
+
el.innerHTML = el.innerHTML.replace(
|
147 |
+
new RegExp("\\b"+ref+"\\b", "g"),
|
148 |
+
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
|
149 |
+
);
|
150 |
+
})
|
151 |
+
}
|
152 |
+
})
|
153 |
+
|
154 |
+
Object.entries(refs).forEach(([key, refs]) => {
|
155 |
+
if (refs.length > 0) {
|
156 |
+
const el = document.querySelector(`.${key}`);
|
157 |
+
if (!el) return;
|
158 |
+
refs.forEach(ref => {
|
159 |
+
el.innerHTML = el.innerHTML.replace(
|
160 |
+
new RegExp("\\b"+ref+"\\b", "g"),
|
161 |
+
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
|
162 |
+
);
|
163 |
+
})
|
164 |
+
}
|
165 |
+
})
|
166 |
+
})
|
167 |
+
}
|
168 |
+
|
169 |
+
""")
|
170 |
+
|
171 |
+
demo.launch()
|
src/README.md
CHANGED
@@ -1,10 +1,419 @@
|
|
1 |
|
2 |
-
# gradio_multimodalchatbot
|
3 |
-
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
```python
|
|
|
8 |
import gradio as gr
|
9 |
from gradio_multimodalchatbot import MultimodalChatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
```
|
|
|
1 |
|
2 |
+
# `gradio_multimodalchatbot`
|
3 |
+
<a href="https://pypi.org/project/gradio_multimodalchatbot/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_multimodalchatbot"></a> <a href="https://huggingface.co/spaces/freddyaboulton/gradio_multimodalchatbot//discussions" target="_blank"><img alt="Static Badge" src="https://img.shields.io/badge/%F0%9F%A4%97%20Discuss-%23097EFF?style=flat&logoColor=black"></a>
|
4 |
|
5 |
+
Display text and media files (audio, video, images) in the same chat message bubble.
|
6 |
+
|
7 |
+
## Installation
|
8 |
+
|
9 |
+
```bash
|
10 |
+
pip install gradio_multimodalchatbot
|
11 |
+
```
|
12 |
+
|
13 |
+
## Usage
|
14 |
|
15 |
```python
|
16 |
+
|
17 |
import gradio as gr
|
18 |
from gradio_multimodalchatbot import MultimodalChatbot
|
19 |
+
from gradio.data_classes import FileData
|
20 |
+
|
21 |
+
user_msg1 = {"text": "Hello, what is in this image?",
|
22 |
+
"files": [{"file": FileData(path="https://gradio-builds.s3.amazonaws.com/diffusion_image/cute_dog.jpg")}]
|
23 |
+
}
|
24 |
+
bot_msg1 = {"text": "It is a very cute dog",
|
25 |
+
"files": []}
|
26 |
+
|
27 |
+
user_msg2 = {"text": "Describe this audio clip please.",
|
28 |
+
"files": [{"file": FileData(path="cantina.wav")}]}
|
29 |
+
bot_msg2 = {"text": "It is the cantina song from Star Wars",
|
30 |
+
"files": []}
|
31 |
+
|
32 |
+
user_msg3 = {"text": "Give me a video clip please.",
|
33 |
+
"files": []}
|
34 |
+
bot_msg3 = {"text": "Here is a video clip of the world",
|
35 |
+
"files": [{"file": FileData(path="world.mp4")},
|
36 |
+
{"file": FileData(path="cantina.wav")}]}
|
37 |
+
|
38 |
+
conversation = [[user_msg1, bot_msg1], [user_msg2, bot_msg2], [user_msg3, bot_msg3]]
|
39 |
+
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
MultimodalChatbot(value=conversation, height=800)
|
42 |
+
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
demo.launch()
|
46 |
+
|
47 |
+
```
|
48 |
+
|
49 |
+
## `MultimodalChatbot`
|
50 |
+
|
51 |
+
### Initialization
|
52 |
+
|
53 |
+
<table>
|
54 |
+
<thead>
|
55 |
+
<tr>
|
56 |
+
<th align="left">name</th>
|
57 |
+
<th align="left" style="width: 25%;">type</th>
|
58 |
+
<th align="left">default</th>
|
59 |
+
<th align="left">description</th>
|
60 |
+
</tr>
|
61 |
+
</thead>
|
62 |
+
<tbody>
|
63 |
+
<tr>
|
64 |
+
<td align="left"><code>value</code></td>
|
65 |
+
<td align="left" style="width: 25%;">
|
66 |
+
|
67 |
+
```python
|
68 |
+
list[
|
69 |
+
list[
|
70 |
+
str
|
71 |
+
| tuple[str]
|
72 |
+
| tuple[str | pathlib.Path, str]
|
73 |
+
| None
|
74 |
+
]
|
75 |
+
]
|
76 |
+
| Callable
|
77 |
+
| None
|
78 |
+
```
|
79 |
+
|
80 |
+
</td>
|
81 |
+
<td align="left"><code>None</code></td>
|
82 |
+
<td align="left">Default value to show in chatbot. If callable, the function will be called whenever the app loads to set the initial value of the component.</td>
|
83 |
+
</tr>
|
84 |
+
|
85 |
+
<tr>
|
86 |
+
<td align="left"><code>label</code></td>
|
87 |
+
<td align="left" style="width: 25%;">
|
88 |
+
|
89 |
+
```python
|
90 |
+
str | None
|
91 |
+
```
|
92 |
+
|
93 |
+
</td>
|
94 |
+
<td align="left"><code>None</code></td>
|
95 |
+
<td align="left">The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.</td>
|
96 |
+
</tr>
|
97 |
+
|
98 |
+
<tr>
|
99 |
+
<td align="left"><code>every</code></td>
|
100 |
+
<td align="left" style="width: 25%;">
|
101 |
+
|
102 |
+
```python
|
103 |
+
float | None
|
104 |
+
```
|
105 |
+
|
106 |
+
</td>
|
107 |
+
<td align="left"><code>None</code></td>
|
108 |
+
<td align="left">If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.</td>
|
109 |
+
</tr>
|
110 |
+
|
111 |
+
<tr>
|
112 |
+
<td align="left"><code>show_label</code></td>
|
113 |
+
<td align="left" style="width: 25%;">
|
114 |
+
|
115 |
+
```python
|
116 |
+
bool | None
|
117 |
+
```
|
118 |
+
|
119 |
+
</td>
|
120 |
+
<td align="left"><code>None</code></td>
|
121 |
+
<td align="left">if True, will display label.</td>
|
122 |
+
</tr>
|
123 |
+
|
124 |
+
<tr>
|
125 |
+
<td align="left"><code>container</code></td>
|
126 |
+
<td align="left" style="width: 25%;">
|
127 |
+
|
128 |
+
```python
|
129 |
+
bool
|
130 |
+
```
|
131 |
+
|
132 |
+
</td>
|
133 |
+
<td align="left"><code>True</code></td>
|
134 |
+
<td align="left">If True, will place the component in a container - providing some extra padding around the border.</td>
|
135 |
+
</tr>
|
136 |
+
|
137 |
+
<tr>
|
138 |
+
<td align="left"><code>scale</code></td>
|
139 |
+
<td align="left" style="width: 25%;">
|
140 |
+
|
141 |
+
```python
|
142 |
+
int | None
|
143 |
+
```
|
144 |
+
|
145 |
+
</td>
|
146 |
+
<td align="left"><code>None</code></td>
|
147 |
+
<td align="left">relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.</td>
|
148 |
+
</tr>
|
149 |
+
|
150 |
+
<tr>
|
151 |
+
<td align="left"><code>min_width</code></td>
|
152 |
+
<td align="left" style="width: 25%;">
|
153 |
+
|
154 |
+
```python
|
155 |
+
int
|
156 |
+
```
|
157 |
+
|
158 |
+
</td>
|
159 |
+
<td align="left"><code>160</code></td>
|
160 |
+
<td align="left">minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.</td>
|
161 |
+
</tr>
|
162 |
+
|
163 |
+
<tr>
|
164 |
+
<td align="left"><code>visible</code></td>
|
165 |
+
<td align="left" style="width: 25%;">
|
166 |
+
|
167 |
+
```python
|
168 |
+
bool
|
169 |
+
```
|
170 |
+
|
171 |
+
</td>
|
172 |
+
<td align="left"><code>True</code></td>
|
173 |
+
<td align="left">If False, component will be hidden.</td>
|
174 |
+
</tr>
|
175 |
+
|
176 |
+
<tr>
|
177 |
+
<td align="left"><code>elem_id</code></td>
|
178 |
+
<td align="left" style="width: 25%;">
|
179 |
+
|
180 |
+
```python
|
181 |
+
str | None
|
182 |
+
```
|
183 |
+
|
184 |
+
</td>
|
185 |
+
<td align="left"><code>None</code></td>
|
186 |
+
<td align="left">An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
|
187 |
+
</tr>
|
188 |
+
|
189 |
+
<tr>
|
190 |
+
<td align="left"><code>elem_classes</code></td>
|
191 |
+
<td align="left" style="width: 25%;">
|
192 |
+
|
193 |
+
```python
|
194 |
+
list[str] | str | None
|
195 |
+
```
|
196 |
+
|
197 |
+
</td>
|
198 |
+
<td align="left"><code>None</code></td>
|
199 |
+
<td align="left">An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
|
200 |
+
</tr>
|
201 |
+
|
202 |
+
<tr>
|
203 |
+
<td align="left"><code>render</code></td>
|
204 |
+
<td align="left" style="width: 25%;">
|
205 |
+
|
206 |
+
```python
|
207 |
+
bool
|
208 |
+
```
|
209 |
+
|
210 |
+
</td>
|
211 |
+
<td align="left"><code>True</code></td>
|
212 |
+
<td align="left">If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.</td>
|
213 |
+
</tr>
|
214 |
+
|
215 |
+
<tr>
|
216 |
+
<td align="left"><code>height</code></td>
|
217 |
+
<td align="left" style="width: 25%;">
|
218 |
+
|
219 |
+
```python
|
220 |
+
int | None
|
221 |
+
```
|
222 |
+
|
223 |
+
</td>
|
224 |
+
<td align="left"><code>None</code></td>
|
225 |
+
<td align="left">height of the component in pixels.</td>
|
226 |
+
</tr>
|
227 |
+
|
228 |
+
<tr>
|
229 |
+
<td align="left"><code>latex_delimiters</code></td>
|
230 |
+
<td align="left" style="width: 25%;">
|
231 |
+
|
232 |
+
```python
|
233 |
+
list[dict[str, str | bool]] | None
|
234 |
+
```
|
235 |
+
|
236 |
+
</td>
|
237 |
+
<td align="left"><code>None</code></td>
|
238 |
+
<td align="left">A list of dicts of the form {"left": open delimiter (str), "right": close delimiter (str), "display": whether to display in newline (bool)} that will be used to render LaTeX expressions. If not provided, `latex_delimiters` is set to `[{ "left": "$$", "right": "$$", "display": True }]`, so only expressions enclosed in $$ delimiters will be rendered as LaTeX, and in a new line. Pass in an empty list to disable LaTeX rendering. For more information, see the [KaTeX documentation](https://katex.org/docs/autorender.html).</td>
|
239 |
+
</tr>
|
240 |
+
|
241 |
+
<tr>
|
242 |
+
<td align="left"><code>rtl</code></td>
|
243 |
+
<td align="left" style="width: 25%;">
|
244 |
+
|
245 |
+
```python
|
246 |
+
bool
|
247 |
+
```
|
248 |
+
|
249 |
+
</td>
|
250 |
+
<td align="left"><code>False</code></td>
|
251 |
+
<td align="left">If True, sets the direction of the rendered text to right-to-left. Default is False, which renders text left-to-right.</td>
|
252 |
+
</tr>
|
253 |
+
|
254 |
+
<tr>
|
255 |
+
<td align="left"><code>show_share_button</code></td>
|
256 |
+
<td align="left" style="width: 25%;">
|
257 |
+
|
258 |
+
```python
|
259 |
+
bool | None
|
260 |
+
```
|
261 |
+
|
262 |
+
</td>
|
263 |
+
<td align="left"><code>None</code></td>
|
264 |
+
<td align="left">If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.</td>
|
265 |
+
</tr>
|
266 |
+
|
267 |
+
<tr>
|
268 |
+
<td align="left"><code>show_copy_button</code></td>
|
269 |
+
<td align="left" style="width: 25%;">
|
270 |
+
|
271 |
+
```python
|
272 |
+
bool
|
273 |
+
```
|
274 |
+
|
275 |
+
</td>
|
276 |
+
<td align="left"><code>False</code></td>
|
277 |
+
<td align="left">If True, will show a copy button for each chatbot message.</td>
|
278 |
+
</tr>
|
279 |
+
|
280 |
+
<tr>
|
281 |
+
<td align="left"><code>avatar_images</code></td>
|
282 |
+
<td align="left" style="width: 25%;">
|
283 |
+
|
284 |
+
```python
|
285 |
+
tuple[
|
286 |
+
str | pathlib.Path | None, str | pathlib.Path | None
|
287 |
+
]
|
288 |
+
| None
|
289 |
+
```
|
290 |
+
|
291 |
+
</td>
|
292 |
+
<td align="left"><code>None</code></td>
|
293 |
+
<td align="left">Tuple of two avatar image paths or URLs for user and bot (in that order). Pass None for either the user or bot image to skip. Must be within the working directory of the Gradio app or an external URL.</td>
|
294 |
+
</tr>
|
295 |
+
|
296 |
+
<tr>
|
297 |
+
<td align="left"><code>sanitize_html</code></td>
|
298 |
+
<td align="left" style="width: 25%;">
|
299 |
+
|
300 |
+
```python
|
301 |
+
bool
|
302 |
+
```
|
303 |
+
|
304 |
+
</td>
|
305 |
+
<td align="left"><code>True</code></td>
|
306 |
+
<td align="left">If False, will disable HTML sanitization for chatbot messages. This is not recommended, as it can lead to security vulnerabilities.</td>
|
307 |
+
</tr>
|
308 |
+
|
309 |
+
<tr>
|
310 |
+
<td align="left"><code>render_markdown</code></td>
|
311 |
+
<td align="left" style="width: 25%;">
|
312 |
+
|
313 |
+
```python
|
314 |
+
bool
|
315 |
+
```
|
316 |
+
|
317 |
+
</td>
|
318 |
+
<td align="left"><code>True</code></td>
|
319 |
+
<td align="left">If False, will disable Markdown rendering for chatbot messages.</td>
|
320 |
+
</tr>
|
321 |
+
|
322 |
+
<tr>
|
323 |
+
<td align="left"><code>bubble_full_width</code></td>
|
324 |
+
<td align="left" style="width: 25%;">
|
325 |
+
|
326 |
+
```python
|
327 |
+
bool
|
328 |
+
```
|
329 |
+
|
330 |
+
</td>
|
331 |
+
<td align="left"><code>True</code></td>
|
332 |
+
<td align="left">If False, the chat bubble will fit to the content of the message. If True (default), the chat bubble will be the full width of the component.</td>
|
333 |
+
</tr>
|
334 |
+
|
335 |
+
<tr>
|
336 |
+
<td align="left"><code>line_breaks</code></td>
|
337 |
+
<td align="left" style="width: 25%;">
|
338 |
+
|
339 |
+
```python
|
340 |
+
bool
|
341 |
+
```
|
342 |
+
|
343 |
+
</td>
|
344 |
+
<td align="left"><code>True</code></td>
|
345 |
+
<td align="left">If True (default), will enable Github-flavored Markdown line breaks in chatbot messages. If False, single new lines will be ignored. Only applies if `render_markdown` is True.</td>
|
346 |
+
</tr>
|
347 |
+
|
348 |
+
<tr>
|
349 |
+
<td align="left"><code>likeable</code></td>
|
350 |
+
<td align="left" style="width: 25%;">
|
351 |
+
|
352 |
+
```python
|
353 |
+
bool
|
354 |
+
```
|
355 |
+
|
356 |
+
</td>
|
357 |
+
<td align="left"><code>False</code></td>
|
358 |
+
<td align="left">Whether the chat messages display a like or dislike button. Set automatically by the .like method but has to be present in the signature for it to show up in the config.</td>
|
359 |
+
</tr>
|
360 |
+
|
361 |
+
<tr>
|
362 |
+
<td align="left"><code>layout</code></td>
|
363 |
+
<td align="left" style="width: 25%;">
|
364 |
+
|
365 |
+
```python
|
366 |
+
"panel" | "bubble" | None
|
367 |
+
```
|
368 |
+
|
369 |
+
</td>
|
370 |
+
<td align="left"><code>None</code></td>
|
371 |
+
<td align="left">If "panel", will display the chatbot in a llm style layout. If "bubble", will display the chatbot with message bubbles, with the user and bot messages on alterating sides. Will default to "bubble".</td>
|
372 |
+
</tr>
|
373 |
+
</tbody></table>
|
374 |
+
|
375 |
+
|
376 |
+
### Events
|
377 |
+
|
378 |
+
| name | description |
|
379 |
+
|:-----|:------------|
|
380 |
+
| `change` | Triggered when the value of the MultimodalChatbot changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input. |
|
381 |
+
| `select` | Event listener for when the user selects or deselects the MultimodalChatbot. Uses event data gradio.SelectData to carry `value` referring to the label of the MultimodalChatbot, and `selected` to refer to state of the MultimodalChatbot. See EventData documentation on how to use this event data |
|
382 |
+
| `like` | This listener is triggered when the user likes/dislikes from within the MultimodalChatbot. This event has EventData of type gradio.LikeData that carries information, accessible through LikeData.index and LikeData.value. See EventData documentation on how to use this event data. |
|
383 |
+
|
384 |
+
|
385 |
+
|
386 |
+
### User function
|
387 |
+
|
388 |
+
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
|
389 |
+
|
390 |
+
- When used as an Input, the component only impacts the input signature of the user function.
|
391 |
+
- When used as an output, the component only impacts the return signature of the user function.
|
392 |
+
|
393 |
+
The code snippet below is accurate in cases where the component is used as both an input and an output.
|
394 |
+
|
395 |
+
|
396 |
+
|
397 |
+
```python
|
398 |
+
def predict(
|
399 |
+
value: list[MultimodalMessage] | None
|
400 |
+
) -> list[
|
401 |
+
list[str | tuple[str] | tuple[str, str] | None] | tuple
|
402 |
+
]:
|
403 |
+
return value
|
404 |
+
```
|
405 |
+
|
406 |
+
|
407 |
+
## `MultimodalMessage`
|
408 |
+
```python
|
409 |
+
class MultimodalMessage(GradioModel):
|
410 |
+
text: Optional[str] = None
|
411 |
+
files: Optional[List[FileMessage]] = None
|
412 |
+
```
|
413 |
+
|
414 |
+
## `FileMessage`
|
415 |
+
```python
|
416 |
+
class FileMessage(GradioModel):
|
417 |
+
file: FileData
|
418 |
+
alt_text: Optional[str] = None
|
419 |
```
|
src/backend/gradio_multimodalchatbot/multimodalchatbot.py
CHANGED
@@ -144,7 +144,7 @@ class MultimodalChatbot(Component):
|
|
144 |
return None
|
145 |
if isinstance(chat_message, dict):
|
146 |
chat_message = MultimodalMessage(**chat_message)
|
147 |
-
chat_message.text = inspect.cleandoc(chat_message.text)
|
148 |
for file_ in chat_message.files:
|
149 |
file_.file.mime_type = client_utils.get_mimetype(file_.file.path)
|
150 |
return chat_message
|
|
|
144 |
return None
|
145 |
if isinstance(chat_message, dict):
|
146 |
chat_message = MultimodalMessage(**chat_message)
|
147 |
+
chat_message.text = inspect.cleandoc(chat_message.text or "")
|
148 |
for file_ in chat_message.files:
|
149 |
file_.file.mime_type = client_utils.get_mimetype(file_.file.path)
|
150 |
return chat_message
|
src/backend/gradio_multimodalchatbot/multimodalchatbot.pyi
CHANGED
@@ -144,7 +144,7 @@ class MultimodalChatbot(Component):
|
|
144 |
return None
|
145 |
if isinstance(chat_message, dict):
|
146 |
chat_message = MultimodalMessage(**chat_message)
|
147 |
-
chat_message.text = inspect.cleandoc(chat_message.text)
|
148 |
for file_ in chat_message.files:
|
149 |
file_.file.mime_type = client_utils.get_mimetype(file_.file.path)
|
150 |
return chat_message
|
@@ -182,7 +182,6 @@ class MultimodalChatbot(Component):
|
|
182 |
inputs: Component | Sequence[Component] | set[Component] | None = None,
|
183 |
outputs: Component | Sequence[Component] | None = None,
|
184 |
api_name: str | None | Literal[False] = None,
|
185 |
-
status_tracker: None = None,
|
186 |
scroll_to_output: bool = False,
|
187 |
show_progress: Literal["full", "minimal", "hidden"] = "full",
|
188 |
queue: bool | None = None,
|
@@ -193,7 +192,10 @@ class MultimodalChatbot(Component):
|
|
193 |
cancels: dict[str, Any] | list[dict[str, Any]] | None = None,
|
194 |
every: float | None = None,
|
195 |
trigger_mode: Literal["once", "multiple", "always_last"] | None = None,
|
196 |
-
js: str | None = None,
|
|
|
|
|
|
|
197 |
"""
|
198 |
Parameters:
|
199 |
fn: the function to call when this event is triggered. Often a machine learning model's prediction function. Each parameter of the function corresponds to one input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to one output component.
|
@@ -211,6 +213,9 @@ class MultimodalChatbot(Component):
|
|
211 |
every: Run this event 'every' number of seconds while the client connection is open. Interpreted in seconds. Queue must be enabled.
|
212 |
trigger_mode: If "once" (default for all events except `.change()`) would not allow any submissions while an event is pending. If set to "multiple", unlimited submissions are allowed while pending, and "always_last" (default for `.change()` event) would allow a second submission after the pending event is complete.
|
213 |
js: Optional frontend js method to run before running 'fn'. Input arguments for js method are values of 'inputs' and 'outputs', return should be a list of values for output components.
|
|
|
|
|
|
|
214 |
"""
|
215 |
...
|
216 |
|
@@ -219,7 +224,6 @@ class MultimodalChatbot(Component):
|
|
219 |
inputs: Component | Sequence[Component] | set[Component] | None = None,
|
220 |
outputs: Component | Sequence[Component] | None = None,
|
221 |
api_name: str | None | Literal[False] = None,
|
222 |
-
status_tracker: None = None,
|
223 |
scroll_to_output: bool = False,
|
224 |
show_progress: Literal["full", "minimal", "hidden"] = "full",
|
225 |
queue: bool | None = None,
|
@@ -230,7 +234,10 @@ class MultimodalChatbot(Component):
|
|
230 |
cancels: dict[str, Any] | list[dict[str, Any]] | None = None,
|
231 |
every: float | None = None,
|
232 |
trigger_mode: Literal["once", "multiple", "always_last"] | None = None,
|
233 |
-
js: str | None = None,
|
|
|
|
|
|
|
234 |
"""
|
235 |
Parameters:
|
236 |
fn: the function to call when this event is triggered. Often a machine learning model's prediction function. Each parameter of the function corresponds to one input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to one output component.
|
@@ -248,6 +255,9 @@ class MultimodalChatbot(Component):
|
|
248 |
every: Run this event 'every' number of seconds while the client connection is open. Interpreted in seconds. Queue must be enabled.
|
249 |
trigger_mode: If "once" (default for all events except `.change()`) would not allow any submissions while an event is pending. If set to "multiple", unlimited submissions are allowed while pending, and "always_last" (default for `.change()` event) would allow a second submission after the pending event is complete.
|
250 |
js: Optional frontend js method to run before running 'fn'. Input arguments for js method are values of 'inputs' and 'outputs', return should be a list of values for output components.
|
|
|
|
|
|
|
251 |
"""
|
252 |
...
|
253 |
|
@@ -256,7 +266,6 @@ class MultimodalChatbot(Component):
|
|
256 |
inputs: Component | Sequence[Component] | set[Component] | None = None,
|
257 |
outputs: Component | Sequence[Component] | None = None,
|
258 |
api_name: str | None | Literal[False] = None,
|
259 |
-
status_tracker: None = None,
|
260 |
scroll_to_output: bool = False,
|
261 |
show_progress: Literal["full", "minimal", "hidden"] = "full",
|
262 |
queue: bool | None = None,
|
@@ -267,7 +276,10 @@ class MultimodalChatbot(Component):
|
|
267 |
cancels: dict[str, Any] | list[dict[str, Any]] | None = None,
|
268 |
every: float | None = None,
|
269 |
trigger_mode: Literal["once", "multiple", "always_last"] | None = None,
|
270 |
-
js: str | None = None,
|
|
|
|
|
|
|
271 |
"""
|
272 |
Parameters:
|
273 |
fn: the function to call when this event is triggered. Often a machine learning model's prediction function. Each parameter of the function corresponds to one input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to one output component.
|
@@ -285,5 +297,8 @@ class MultimodalChatbot(Component):
|
|
285 |
every: Run this event 'every' number of seconds while the client connection is open. Interpreted in seconds. Queue must be enabled.
|
286 |
trigger_mode: If "once" (default for all events except `.change()`) would not allow any submissions while an event is pending. If set to "multiple", unlimited submissions are allowed while pending, and "always_last" (default for `.change()` event) would allow a second submission after the pending event is complete.
|
287 |
js: Optional frontend js method to run before running 'fn'. Input arguments for js method are values of 'inputs' and 'outputs', return should be a list of values for output components.
|
|
|
|
|
|
|
288 |
"""
|
289 |
...
|
|
|
144 |
return None
|
145 |
if isinstance(chat_message, dict):
|
146 |
chat_message = MultimodalMessage(**chat_message)
|
147 |
+
chat_message.text = inspect.cleandoc(chat_message.text or "")
|
148 |
for file_ in chat_message.files:
|
149 |
file_.file.mime_type = client_utils.get_mimetype(file_.file.path)
|
150 |
return chat_message
|
|
|
182 |
inputs: Component | Sequence[Component] | set[Component] | None = None,
|
183 |
outputs: Component | Sequence[Component] | None = None,
|
184 |
api_name: str | None | Literal[False] = None,
|
|
|
185 |
scroll_to_output: bool = False,
|
186 |
show_progress: Literal["full", "minimal", "hidden"] = "full",
|
187 |
queue: bool | None = None,
|
|
|
192 |
cancels: dict[str, Any] | list[dict[str, Any]] | None = None,
|
193 |
every: float | None = None,
|
194 |
trigger_mode: Literal["once", "multiple", "always_last"] | None = None,
|
195 |
+
js: str | None = None,
|
196 |
+
concurrency_limit: int | None | Literal["default"] = "default",
|
197 |
+
concurrency_id: str | None = None,
|
198 |
+
show_api: bool = True) -> Dependency:
|
199 |
"""
|
200 |
Parameters:
|
201 |
fn: the function to call when this event is triggered. Often a machine learning model's prediction function. Each parameter of the function corresponds to one input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to one output component.
|
|
|
213 |
every: Run this event 'every' number of seconds while the client connection is open. Interpreted in seconds. Queue must be enabled.
|
214 |
trigger_mode: If "once" (default for all events except `.change()`) would not allow any submissions while an event is pending. If set to "multiple", unlimited submissions are allowed while pending, and "always_last" (default for `.change()` event) would allow a second submission after the pending event is complete.
|
215 |
js: Optional frontend js method to run before running 'fn'. Input arguments for js method are values of 'inputs' and 'outputs', return should be a list of values for output components.
|
216 |
+
concurrency_limit: If set, this is the maximum number of this event that can be running simultaneously. Can be set to None to mean no concurrency_limit (any number of this event can be running simultaneously). Set to "default" to use the default concurrency limit (defined by the `default_concurrency_limit` parameter in `Blocks.queue()`, which itself is 1 by default).
|
217 |
+
concurrency_id: If set, this is the id of the concurrency group. Events with the same concurrency_id will be limited by the lowest set concurrency_limit.
|
218 |
+
show_api: whether to show this event in the "view API" page of the Gradio app, or in the ".view_api()" method of the Gradio clients. Unlike setting api_name to False, setting show_api to False will still allow downstream apps to use this event. If fn is None, show_api will automatically be set to False.
|
219 |
"""
|
220 |
...
|
221 |
|
|
|
224 |
inputs: Component | Sequence[Component] | set[Component] | None = None,
|
225 |
outputs: Component | Sequence[Component] | None = None,
|
226 |
api_name: str | None | Literal[False] = None,
|
|
|
227 |
scroll_to_output: bool = False,
|
228 |
show_progress: Literal["full", "minimal", "hidden"] = "full",
|
229 |
queue: bool | None = None,
|
|
|
234 |
cancels: dict[str, Any] | list[dict[str, Any]] | None = None,
|
235 |
every: float | None = None,
|
236 |
trigger_mode: Literal["once", "multiple", "always_last"] | None = None,
|
237 |
+
js: str | None = None,
|
238 |
+
concurrency_limit: int | None | Literal["default"] = "default",
|
239 |
+
concurrency_id: str | None = None,
|
240 |
+
show_api: bool = True) -> Dependency:
|
241 |
"""
|
242 |
Parameters:
|
243 |
fn: the function to call when this event is triggered. Often a machine learning model's prediction function. Each parameter of the function corresponds to one input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to one output component.
|
|
|
255 |
every: Run this event 'every' number of seconds while the client connection is open. Interpreted in seconds. Queue must be enabled.
|
256 |
trigger_mode: If "once" (default for all events except `.change()`) would not allow any submissions while an event is pending. If set to "multiple", unlimited submissions are allowed while pending, and "always_last" (default for `.change()` event) would allow a second submission after the pending event is complete.
|
257 |
js: Optional frontend js method to run before running 'fn'. Input arguments for js method are values of 'inputs' and 'outputs', return should be a list of values for output components.
|
258 |
+
concurrency_limit: If set, this is the maximum number of this event that can be running simultaneously. Can be set to None to mean no concurrency_limit (any number of this event can be running simultaneously). Set to "default" to use the default concurrency limit (defined by the `default_concurrency_limit` parameter in `Blocks.queue()`, which itself is 1 by default).
|
259 |
+
concurrency_id: If set, this is the id of the concurrency group. Events with the same concurrency_id will be limited by the lowest set concurrency_limit.
|
260 |
+
show_api: whether to show this event in the "view API" page of the Gradio app, or in the ".view_api()" method of the Gradio clients. Unlike setting api_name to False, setting show_api to False will still allow downstream apps to use this event. If fn is None, show_api will automatically be set to False.
|
261 |
"""
|
262 |
...
|
263 |
|
|
|
266 |
inputs: Component | Sequence[Component] | set[Component] | None = None,
|
267 |
outputs: Component | Sequence[Component] | None = None,
|
268 |
api_name: str | None | Literal[False] = None,
|
|
|
269 |
scroll_to_output: bool = False,
|
270 |
show_progress: Literal["full", "minimal", "hidden"] = "full",
|
271 |
queue: bool | None = None,
|
|
|
276 |
cancels: dict[str, Any] | list[dict[str, Any]] | None = None,
|
277 |
every: float | None = None,
|
278 |
trigger_mode: Literal["once", "multiple", "always_last"] | None = None,
|
279 |
+
js: str | None = None,
|
280 |
+
concurrency_limit: int | None | Literal["default"] = "default",
|
281 |
+
concurrency_id: str | None = None,
|
282 |
+
show_api: bool = True) -> Dependency:
|
283 |
"""
|
284 |
Parameters:
|
285 |
fn: the function to call when this event is triggered. Often a machine learning model's prediction function. Each parameter of the function corresponds to one input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to one output component.
|
|
|
297 |
every: Run this event 'every' number of seconds while the client connection is open. Interpreted in seconds. Queue must be enabled.
|
298 |
trigger_mode: If "once" (default for all events except `.change()`) would not allow any submissions while an event is pending. If set to "multiple", unlimited submissions are allowed while pending, and "always_last" (default for `.change()` event) would allow a second submission after the pending event is complete.
|
299 |
js: Optional frontend js method to run before running 'fn'. Input arguments for js method are values of 'inputs' and 'outputs', return should be a list of values for output components.
|
300 |
+
concurrency_limit: If set, this is the maximum number of this event that can be running simultaneously. Can be set to None to mean no concurrency_limit (any number of this event can be running simultaneously). Set to "default" to use the default concurrency limit (defined by the `default_concurrency_limit` parameter in `Blocks.queue()`, which itself is 1 by default).
|
301 |
+
concurrency_id: If set, this is the id of the concurrency group. Events with the same concurrency_id will be limited by the lowest set concurrency_limit.
|
302 |
+
show_api: whether to show this event in the "view API" page of the Gradio app, or in the ".view_api()" method of the Gradio clients. Unlike setting api_name to False, setting show_api to False will still allow downstream apps to use this event. If fn is None, show_api will automatically be set to False.
|
303 |
"""
|
304 |
...
|
src/demo/app.py
CHANGED
@@ -23,7 +23,8 @@ bot_msg3 = {"text": "Here is a video clip of the world",
|
|
23 |
conversation = [[user_msg1, bot_msg1], [user_msg2, bot_msg2], [user_msg3, bot_msg3]]
|
24 |
|
25 |
with gr.Blocks() as demo:
|
26 |
-
MultimodalChatbot(value=conversation)
|
27 |
|
28 |
|
29 |
-
|
|
|
|
23 |
conversation = [[user_msg1, bot_msg1], [user_msg2, bot_msg2], [user_msg3, bot_msg3]]
|
24 |
|
25 |
with gr.Blocks() as demo:
|
26 |
+
MultimodalChatbot(value=conversation, height=800)
|
27 |
|
28 |
|
29 |
+
if __name__ == "__main__":
|
30 |
+
demo.launch()
|
src/demo/css.css
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
html {
|
2 |
+
font-family: Inter;
|
3 |
+
font-size: 16px;
|
4 |
+
font-weight: 400;
|
5 |
+
line-height: 1.5;
|
6 |
+
-webkit-text-size-adjust: 100%;
|
7 |
+
background: #fff;
|
8 |
+
color: #323232;
|
9 |
+
-webkit-font-smoothing: antialiased;
|
10 |
+
-moz-osx-font-smoothing: grayscale;
|
11 |
+
text-rendering: optimizeLegibility;
|
12 |
+
}
|
13 |
+
|
14 |
+
:root {
|
15 |
+
--space: 1;
|
16 |
+
--vspace: calc(var(--space) * 1rem);
|
17 |
+
--vspace-0: calc(3 * var(--space) * 1rem);
|
18 |
+
--vspace-1: calc(2 * var(--space) * 1rem);
|
19 |
+
--vspace-2: calc(1.5 * var(--space) * 1rem);
|
20 |
+
--vspace-3: calc(0.5 * var(--space) * 1rem);
|
21 |
+
}
|
22 |
+
|
23 |
+
.app {
|
24 |
+
max-width: 748px !important;
|
25 |
+
}
|
26 |
+
|
27 |
+
.prose p {
|
28 |
+
margin: var(--vspace) 0;
|
29 |
+
line-height: var(--vspace * 2);
|
30 |
+
font-size: 1rem;
|
31 |
+
}
|
32 |
+
|
33 |
+
code {
|
34 |
+
font-family: "Inconsolata", sans-serif;
|
35 |
+
font-size: 16px;
|
36 |
+
}
|
37 |
+
|
38 |
+
h1,
|
39 |
+
h1 code {
|
40 |
+
font-weight: 400;
|
41 |
+
line-height: calc(2.5 / var(--space) * var(--vspace));
|
42 |
+
}
|
43 |
+
|
44 |
+
h1 code {
|
45 |
+
background: none;
|
46 |
+
border: none;
|
47 |
+
letter-spacing: 0.05em;
|
48 |
+
padding-bottom: 5px;
|
49 |
+
position: relative;
|
50 |
+
padding: 0;
|
51 |
+
}
|
52 |
+
|
53 |
+
h2 {
|
54 |
+
margin: var(--vspace-1) 0 var(--vspace-2) 0;
|
55 |
+
line-height: 1em;
|
56 |
+
}
|
57 |
+
|
58 |
+
h3,
|
59 |
+
h3 code {
|
60 |
+
margin: var(--vspace-1) 0 var(--vspace-2) 0;
|
61 |
+
line-height: 1em;
|
62 |
+
}
|
63 |
+
|
64 |
+
h4,
|
65 |
+
h5,
|
66 |
+
h6 {
|
67 |
+
margin: var(--vspace-3) 0 var(--vspace-3) 0;
|
68 |
+
line-height: var(--vspace);
|
69 |
+
}
|
70 |
+
|
71 |
+
.bigtitle,
|
72 |
+
h1,
|
73 |
+
h1 code {
|
74 |
+
font-size: calc(8px * 4.5);
|
75 |
+
word-break: break-word;
|
76 |
+
}
|
77 |
+
|
78 |
+
.title,
|
79 |
+
h2,
|
80 |
+
h2 code {
|
81 |
+
font-size: calc(8px * 3.375);
|
82 |
+
font-weight: lighter;
|
83 |
+
word-break: break-word;
|
84 |
+
border: none;
|
85 |
+
background: none;
|
86 |
+
}
|
87 |
+
|
88 |
+
.subheading1,
|
89 |
+
h3,
|
90 |
+
h3 code {
|
91 |
+
font-size: calc(8px * 1.8);
|
92 |
+
font-weight: 600;
|
93 |
+
border: none;
|
94 |
+
background: none;
|
95 |
+
letter-spacing: 0.1em;
|
96 |
+
text-transform: uppercase;
|
97 |
+
}
|
98 |
+
|
99 |
+
h2 code {
|
100 |
+
padding: 0;
|
101 |
+
position: relative;
|
102 |
+
letter-spacing: 0.05em;
|
103 |
+
}
|
104 |
+
|
105 |
+
blockquote {
|
106 |
+
font-size: calc(8px * 1.1667);
|
107 |
+
font-style: italic;
|
108 |
+
line-height: calc(1.1667 * var(--vspace));
|
109 |
+
margin: var(--vspace-2) var(--vspace-2);
|
110 |
+
}
|
111 |
+
|
112 |
+
.subheading2,
|
113 |
+
h4 {
|
114 |
+
font-size: calc(8px * 1.4292);
|
115 |
+
text-transform: uppercase;
|
116 |
+
font-weight: 600;
|
117 |
+
}
|
118 |
+
|
119 |
+
.subheading3,
|
120 |
+
h5 {
|
121 |
+
font-size: calc(8px * 1.2917);
|
122 |
+
line-height: calc(1.2917 * var(--vspace));
|
123 |
+
|
124 |
+
font-weight: lighter;
|
125 |
+
text-transform: uppercase;
|
126 |
+
letter-spacing: 0.15em;
|
127 |
+
}
|
128 |
+
|
129 |
+
h6 {
|
130 |
+
font-size: calc(8px * 1.1667);
|
131 |
+
font-size: 1.1667em;
|
132 |
+
font-weight: normal;
|
133 |
+
font-style: italic;
|
134 |
+
font-family: "le-monde-livre-classic-byol", serif !important;
|
135 |
+
letter-spacing: 0px !important;
|
136 |
+
}
|
137 |
+
|
138 |
+
#start .md > *:first-child {
|
139 |
+
margin-top: 0;
|
140 |
+
}
|
141 |
+
|
142 |
+
h2 + h3 {
|
143 |
+
margin-top: 0;
|
144 |
+
}
|
145 |
+
|
146 |
+
.md hr {
|
147 |
+
border: none;
|
148 |
+
border-top: 1px solid var(--block-border-color);
|
149 |
+
margin: var(--vspace-2) 0 var(--vspace-2) 0;
|
150 |
+
}
|
151 |
+
.prose ul {
|
152 |
+
margin: var(--vspace-2) 0 var(--vspace-1) 0;
|
153 |
+
}
|
154 |
+
|
155 |
+
.gap {
|
156 |
+
gap: 0;
|
157 |
+
}
|
src/demo/space.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from app import demo as app
|
4 |
+
import os
|
5 |
+
|
6 |
+
_docs = {'MultimodalChatbot': {'description': 'Displays a chatbot output showing both user submitted messages and responses. Supports a subset of Markdown including bold, italics, code, tables. Also supports audio/video/image files, which are displayed in the MultimodalChatbot, and other kinds of files which are displayed as links.\n', 'members': {'__init__': {'value': {'type': 'list[\n list[\n str\n | tuple[str]\n | tuple[str | pathlib.Path, str]\n | None\n ]\n ]\n | Callable\n | None', 'default': 'None', 'description': 'Default value to show in chatbot. If callable, the function will be called whenever the app loads to set the initial value of the component.'}, 'label': {'type': 'str | None', 'default': 'None', 'description': 'The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.'}, 'every': {'type': 'float | None', 'default': 'None', 'description': "If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute."}, 'show_label': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will display label.'}, 'container': {'type': 'bool', 'default': 'True', 'description': 'If True, will place the component in a container - providing some extra padding around the border.'}, 'scale': {'type': 'int | None', 'default': 'None', 'description': 'relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.'}, 'min_width': {'type': 'int', 'default': '160', 'description': 'minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.'}, 'visible': {'type': 'bool', 'default': 'True', 'description': 'If False, component will be hidden.'}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': 'An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': 'An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'render': {'type': 'bool', 'default': 'True', 'description': 'If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.'}, 'height': {'type': 'int | None', 'default': 'None', 'description': 'height of the component in pixels.'}, 'latex_delimiters': {'type': 'list[dict[str, str | bool]] | None', 'default': 'None', 'description': 'A list of dicts of the form {"left": open delimiter (str), "right": close delimiter (str), "display": whether to display in newline (bool)} that will be used to render LaTeX expressions. If not provided, `latex_delimiters` is set to `[{ "left": "$$", "right": "$$", "display": True }]`, so only expressions enclosed in $$ delimiters will be rendered as LaTeX, and in a new line. Pass in an empty list to disable LaTeX rendering. For more information, see the [KaTeX documentation](https://katex.org/docs/autorender.html).'}, 'rtl': {'type': 'bool', 'default': 'False', 'description': 'If True, sets the direction of the rendered text to right-to-left. Default is False, which renders text left-to-right.'}, 'show_share_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.'}, 'show_copy_button': {'type': 'bool', 'default': 'False', 'description': 'If True, will show a copy button for each chatbot message.'}, 'avatar_images': {'type': 'tuple[\n str | pathlib.Path | None, str | pathlib.Path | None\n ]\n | None', 'default': 'None', 'description': 'Tuple of two avatar image paths or URLs for user and bot (in that order). Pass None for either the user or bot image to skip. Must be within the working directory of the Gradio app or an external URL.'}, 'sanitize_html': {'type': 'bool', 'default': 'True', 'description': 'If False, will disable HTML sanitization for chatbot messages. This is not recommended, as it can lead to security vulnerabilities.'}, 'render_markdown': {'type': 'bool', 'default': 'True', 'description': 'If False, will disable Markdown rendering for chatbot messages.'}, 'bubble_full_width': {'type': 'bool', 'default': 'True', 'description': 'If False, the chat bubble will fit to the content of the message. If True (default), the chat bubble will be the full width of the component.'}, 'line_breaks': {'type': 'bool', 'default': 'True', 'description': 'If True (default), will enable Github-flavored Markdown line breaks in chatbot messages. If False, single new lines will be ignored. Only applies if `render_markdown` is True.'}, 'likeable': {'type': 'bool', 'default': 'False', 'description': 'Whether the chat messages display a like or dislike button. Set automatically by the .like method but has to be present in the signature for it to show up in the config.'}, 'layout': {'type': '"panel" | "bubble" | None', 'default': 'None', 'description': 'If "panel", will display the chatbot in a llm style layout. If "bubble", will display the chatbot with message bubbles, with the user and bot messages on alterating sides. Will default to "bubble".'}}, 'postprocess': {'value': {'type': 'list[\n list[str | tuple[str] | tuple[str, str] | None] | tuple\n]', 'description': None}}, 'preprocess': {'return': {'type': 'list[MultimodalMessage] | None', 'description': None}, 'value': None}}, 'events': {'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the MultimodalChatbot changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'select': {'type': None, 'default': None, 'description': 'Event listener for when the user selects or deselects the MultimodalChatbot. Uses event data gradio.SelectData to carry `value` referring to the label of the MultimodalChatbot, and `selected` to refer to state of the MultimodalChatbot. See EventData documentation on how to use this event data'}, 'like': {'type': None, 'default': None, 'description': 'This listener is triggered when the user likes/dislikes from within the MultimodalChatbot. This event has EventData of type gradio.LikeData that carries information, accessible through LikeData.index and LikeData.value. See EventData documentation on how to use this event data.'}}}, '__meta__': {'additional_interfaces': {'MultimodalMessage': {'source': 'class MultimodalMessage(GradioModel):\n text: Optional[str] = None\n files: Optional[List[FileMessage]] = None', 'refs': ['FileMessage']}, 'FileMessage': {'source': 'class FileMessage(GradioModel):\n file: FileData\n alt_text: Optional[str] = None'}}, 'user_fn_refs': {'MultimodalChatbot': ['MultimodalMessage']}}}
|
7 |
+
|
8 |
+
abs_path = os.path.join(os.path.dirname(__file__), "css.css")
|
9 |
+
|
10 |
+
with gr.Blocks(
|
11 |
+
css=abs_path,
|
12 |
+
theme=gr.themes.Default(
|
13 |
+
font_mono=[
|
14 |
+
gr.themes.GoogleFont("Inconsolata"),
|
15 |
+
"monospace",
|
16 |
+
],
|
17 |
+
),
|
18 |
+
) as demo:
|
19 |
+
gr.Markdown(
|
20 |
+
"""
|
21 |
+
# `gradio_multimodalchatbot`
|
22 |
+
|
23 |
+
<div style="display: flex; gap: 7px;">
|
24 |
+
<a href="https://pypi.org/project/gradio_multimodalchatbot/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_multimodalchatbot"></a> <a href="https://huggingface.co/spaces/freddyaboulton/gradio_multimodalchatbot//discussions" target="_blank"><img alt="Static Badge" src="https://img.shields.io/badge/%F0%9F%A4%97%20Discuss-%23097EFF?style=flat&logoColor=black"></a>
|
25 |
+
</div>
|
26 |
+
|
27 |
+
Display text and media files (audio, video, images) in the same chat message bubble.
|
28 |
+
""", elem_classes=["md-custom"], header_links=True)
|
29 |
+
app.render()
|
30 |
+
gr.Markdown(
|
31 |
+
"""
|
32 |
+
## Installation
|
33 |
+
|
34 |
+
```bash
|
35 |
+
pip install gradio_multimodalchatbot
|
36 |
+
```
|
37 |
+
|
38 |
+
## Usage
|
39 |
+
|
40 |
+
```python
|
41 |
+
|
42 |
+
import gradio as gr
|
43 |
+
from gradio_multimodalchatbot import MultimodalChatbot
|
44 |
+
from gradio.data_classes import FileData
|
45 |
+
|
46 |
+
user_msg1 = {"text": "Hello, what is in this image?",
|
47 |
+
"files": [{"file": FileData(path="https://gradio-builds.s3.amazonaws.com/diffusion_image/cute_dog.jpg")}]
|
48 |
+
}
|
49 |
+
bot_msg1 = {"text": "It is a very cute dog",
|
50 |
+
"files": []}
|
51 |
+
|
52 |
+
user_msg2 = {"text": "Describe this audio clip please.",
|
53 |
+
"files": [{"file": FileData(path="cantina.wav")}]}
|
54 |
+
bot_msg2 = {"text": "It is the cantina song from Star Wars",
|
55 |
+
"files": []}
|
56 |
+
|
57 |
+
user_msg3 = {"text": "Give me a video clip please.",
|
58 |
+
"files": []}
|
59 |
+
bot_msg3 = {"text": "Here is a video clip of the world",
|
60 |
+
"files": [{"file": FileData(path="world.mp4")},
|
61 |
+
{"file": FileData(path="cantina.wav")}]}
|
62 |
+
|
63 |
+
conversation = [[user_msg1, bot_msg1], [user_msg2, bot_msg2], [user_msg3, bot_msg3]]
|
64 |
+
|
65 |
+
with gr.Blocks() as demo:
|
66 |
+
MultimodalChatbot(value=conversation, height=800)
|
67 |
+
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
demo.launch()
|
71 |
+
|
72 |
+
```
|
73 |
+
""", elem_classes=["md-custom"], header_links=True)
|
74 |
+
|
75 |
+
|
76 |
+
gr.Markdown("""
|
77 |
+
## `MultimodalChatbot`
|
78 |
+
|
79 |
+
### Initialization
|
80 |
+
""", elem_classes=["md-custom"], header_links=True)
|
81 |
+
|
82 |
+
gr.ParamViewer(value=_docs["MultimodalChatbot"]["members"]["__init__"], linkify=['MultimodalMessage', 'FileMessage'])
|
83 |
+
|
84 |
+
|
85 |
+
gr.Markdown("### Events")
|
86 |
+
gr.ParamViewer(value=_docs["MultimodalChatbot"]["events"], linkify=['Event'])
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
gr.Markdown("""
|
92 |
+
|
93 |
+
### User function
|
94 |
+
|
95 |
+
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
|
96 |
+
|
97 |
+
- When used as an Input, the component only impacts the input signature of the user function.
|
98 |
+
- When used as an output, the component only impacts the return signature of the user function.
|
99 |
+
|
100 |
+
The code snippet below is accurate in cases where the component is used as both an input and an output.
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
```python
|
105 |
+
def predict(
|
106 |
+
value: list[MultimodalMessage] | None
|
107 |
+
) -> list[
|
108 |
+
list[str | tuple[str] | tuple[str, str] | None] | tuple
|
109 |
+
]:
|
110 |
+
return value
|
111 |
+
```
|
112 |
+
""", elem_classes=["md-custom", "MultimodalChatbot-user-fn"], header_links=True)
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
code_MultimodalMessage = gr.Markdown("""
|
118 |
+
## `MultimodalMessage`
|
119 |
+
```python
|
120 |
+
class MultimodalMessage(GradioModel):
|
121 |
+
text: Optional[str] = None
|
122 |
+
files: Optional[List[FileMessage]] = None
|
123 |
+
```""", elem_classes=["md-custom", "MultimodalMessage"], header_links=True)
|
124 |
+
|
125 |
+
code_FileMessage = gr.Markdown("""
|
126 |
+
## `FileMessage`
|
127 |
+
```python
|
128 |
+
class FileMessage(GradioModel):
|
129 |
+
file: FileData
|
130 |
+
alt_text: Optional[str] = None
|
131 |
+
```""", elem_classes=["md-custom", "FileMessage"], header_links=True)
|
132 |
+
|
133 |
+
demo.load(None, js=r"""function() {
|
134 |
+
const refs = {
|
135 |
+
MultimodalMessage: ['FileMessage'],
|
136 |
+
FileMessage: [], };
|
137 |
+
const user_fn_refs = {
|
138 |
+
MultimodalChatbot: ['MultimodalMessage'], };
|
139 |
+
requestAnimationFrame(() => {
|
140 |
+
|
141 |
+
Object.entries(user_fn_refs).forEach(([key, refs]) => {
|
142 |
+
if (refs.length > 0) {
|
143 |
+
const el = document.querySelector(`.${key}-user-fn`);
|
144 |
+
if (!el) return;
|
145 |
+
refs.forEach(ref => {
|
146 |
+
el.innerHTML = el.innerHTML.replace(
|
147 |
+
new RegExp("\\b"+ref+"\\b", "g"),
|
148 |
+
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
|
149 |
+
);
|
150 |
+
})
|
151 |
+
}
|
152 |
+
})
|
153 |
+
|
154 |
+
Object.entries(refs).forEach(([key, refs]) => {
|
155 |
+
if (refs.length > 0) {
|
156 |
+
const el = document.querySelector(`.${key}`);
|
157 |
+
if (!el) return;
|
158 |
+
refs.forEach(ref => {
|
159 |
+
el.innerHTML = el.innerHTML.replace(
|
160 |
+
new RegExp("\\b"+ref+"\\b", "g"),
|
161 |
+
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
|
162 |
+
);
|
163 |
+
})
|
164 |
+
}
|
165 |
+
})
|
166 |
+
})
|
167 |
+
}
|
168 |
+
|
169 |
+
""")
|
170 |
+
|
171 |
+
demo.launch()
|
src/pyproject.toml
CHANGED
@@ -9,16 +9,15 @@ build-backend = "hatchling.build"
|
|
9 |
[project]
|
10 |
name = "gradio_multimodalchatbot"
|
11 |
version = "0.0.1"
|
12 |
-
description = "
|
13 |
readme = "README.md"
|
14 |
license = "Apache-2.0"
|
15 |
requires-python = ">=3.8"
|
16 |
authors = [{ name = "Freddy Boulton", email = "[email protected]" }]
|
17 |
keywords = [
|
18 |
-
"
|
19 |
-
"
|
20 |
-
"
|
21 |
-
"gradio",
|
22 |
"gradio custom component",
|
23 |
"gradio-template-Chatbot"
|
24 |
]
|
@@ -42,6 +41,9 @@ classifiers = [
|
|
42 |
[project.optional-dependencies]
|
43 |
dev = ["build", "twine"]
|
44 |
|
|
|
|
|
|
|
45 |
[tool.hatch.build]
|
46 |
artifacts = ["/backend/gradio_multimodalchatbot/templates", "*.pyi", "backend/gradio_multimodalchatbot/templates"]
|
47 |
|
|
|
9 |
[project]
|
10 |
name = "gradio_multimodalchatbot"
|
11 |
version = "0.0.1"
|
12 |
+
description = "Display text and media files (audio, video, images) in the same chat message bubble."
|
13 |
readme = "README.md"
|
14 |
license = "Apache-2.0"
|
15 |
requires-python = ">=3.8"
|
16 |
authors = [{ name = "Freddy Boulton", email = "[email protected]" }]
|
17 |
keywords = [
|
18 |
+
"Chatbot",
|
19 |
+
"Multimodal",
|
20 |
+
"Media",
|
|
|
21 |
"gradio custom component",
|
22 |
"gradio-template-Chatbot"
|
23 |
]
|
|
|
41 |
[project.optional-dependencies]
|
42 |
dev = ["build", "twine"]
|
43 |
|
44 |
+
[project.urls]
|
45 |
+
space = "https://huggingface.co/spaces/freddyaboulton/gradio_multimodalchatbot/"
|
46 |
+
|
47 |
[tool.hatch.build]
|
48 |
artifacts = ["/backend/gradio_multimodalchatbot/templates", "*.pyi", "backend/gradio_multimodalchatbot/templates"]
|
49 |
|