Update app.py
Browse files
app.py
CHANGED
@@ -1,259 +1,259 @@
|
|
1 |
-
from transformers import AutoModel, AutoTokenizer
|
2 |
-
from copy import deepcopy
|
3 |
-
import gradio as gr
|
4 |
-
import mdtex2html
|
5 |
-
from model.openlamm import LAMMPEFTModel
|
6 |
-
import torch
|
7 |
-
import json
|
8 |
-
|
9 |
-
# init the model
|
10 |
-
args = {
|
11 |
-
'model': 'openllama_peft',
|
12 |
-
'
|
13 |
-
'vicuna_ckpt_path': './pretrained_ckpt/llm_7b_v0',
|
14 |
-
'delta_ckpt_path': './pretrained_ckpt/llm7b_lora32_lamm186k/pytorch_model.pt',
|
15 |
-
'stage': 2,
|
16 |
-
'max_tgt_len': 128,
|
17 |
-
'lora_r': 32,
|
18 |
-
'lora_alpha': 32,
|
19 |
-
'lora_dropout': 0.1,
|
20 |
-
'lora_target_modules': ['q_proj', 'k_proj', 'v_proj', 'o_proj'],
|
21 |
-
'vision_type': 'image',
|
22 |
-
'vision_feature_type': 'local',
|
23 |
-
'num_vision_token': 256,
|
24 |
-
'encoder_pretrain': 'clip',
|
25 |
-
'system_header': True,
|
26 |
-
}
|
27 |
-
|
28 |
-
model = LAMMPEFTModel(**args)
|
29 |
-
delta_ckpt = torch.load(args['delta_ckpt_path'], map_location=torch.device('cpu'))
|
30 |
-
model.load_state_dict(delta_ckpt, strict=False)
|
31 |
-
model = model.eval().half().cuda()
|
32 |
-
print(f'[!] init the 13b model over ...')
|
33 |
-
|
34 |
-
"""Override Chatbot.postprocess"""
|
35 |
-
|
36 |
-
|
37 |
-
def postprocess(self, y):
|
38 |
-
if y is None:
|
39 |
-
return []
|
40 |
-
for i, (message, response) in enumerate(y):
|
41 |
-
y[i] = (
|
42 |
-
None if message is None else mdtex2html.convert((message)),
|
43 |
-
None if response is None else mdtex2html.convert(response),
|
44 |
-
)
|
45 |
-
return y
|
46 |
-
|
47 |
-
|
48 |
-
gr.Chatbot.postprocess = postprocess
|
49 |
-
|
50 |
-
|
51 |
-
def parse_text(text):
|
52 |
-
"""copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
|
53 |
-
lines = text.split("\n")
|
54 |
-
lines = [line for line in lines if line != ""]
|
55 |
-
count = 0
|
56 |
-
for i, line in enumerate(lines):
|
57 |
-
if "```" in line:
|
58 |
-
count += 1
|
59 |
-
items = line.split('`')
|
60 |
-
if count % 2 == 1:
|
61 |
-
lines[i] = f'<pre><code class="language-{items[-1]}">'
|
62 |
-
else:
|
63 |
-
lines[i] = f'<br></code></pre>'
|
64 |
-
else:
|
65 |
-
if i > 0:
|
66 |
-
if count % 2 == 1:
|
67 |
-
line = line.replace("`", "\`")
|
68 |
-
line = line.replace("<", "<")
|
69 |
-
line = line.replace(">", ">")
|
70 |
-
line = line.replace(" ", " ")
|
71 |
-
line = line.replace("*", "*")
|
72 |
-
line = line.replace("_", "_")
|
73 |
-
line = line.replace("-", "-")
|
74 |
-
line = line.replace(".", ".")
|
75 |
-
line = line.replace("!", "!")
|
76 |
-
line = line.replace("(", "(")
|
77 |
-
line = line.replace(")", ")")
|
78 |
-
line = line.replace("$", "$")
|
79 |
-
lines[i] = "<br>"+line
|
80 |
-
text = "".join(lines)
|
81 |
-
if text.endswith("##"):
|
82 |
-
text = text[:-2]
|
83 |
-
return text
|
84 |
-
|
85 |
-
|
86 |
-
def re_predict(
|
87 |
-
input,
|
88 |
-
image_path,
|
89 |
-
chatbot,
|
90 |
-
max_length,
|
91 |
-
top_p,
|
92 |
-
temperature,
|
93 |
-
history,
|
94 |
-
modality_cache,
|
95 |
-
):
|
96 |
-
# drop the latest query and answers and generate again
|
97 |
-
q, a = history.pop()
|
98 |
-
chatbot.pop()
|
99 |
-
return predict(q, image_path, chatbot, max_length, top_p, temperature, history, modality_cache)
|
100 |
-
|
101 |
-
|
102 |
-
def predict(
|
103 |
-
input,
|
104 |
-
image_path,
|
105 |
-
chatbot,
|
106 |
-
max_length,
|
107 |
-
top_p,
|
108 |
-
temperature,
|
109 |
-
history,
|
110 |
-
modality_cache,
|
111 |
-
):
|
112 |
-
if image_path is None: #
|
113 |
-
return [(input, "There is no input data provided! Please upload your data and start the conversation.")]
|
114 |
-
else:
|
115 |
-
print(f'[!] image path: {image_path}\n') # [!] audio path: {audio_path}\n[!] video path: {video_path}\n[!] thermal path: {thermal_path}')
|
116 |
-
|
117 |
-
# prepare the prompt
|
118 |
-
prompt_text = ''
|
119 |
-
for idx, (q, a) in enumerate(history):
|
120 |
-
if idx == 0:
|
121 |
-
prompt_text += f'{q}\n### Assistant: {a}\n###'
|
122 |
-
else:
|
123 |
-
prompt_text += f' Human: {q}\n### Assistant: {a}\n###'
|
124 |
-
if len(history) == 0:
|
125 |
-
prompt_text += f'{input}'
|
126 |
-
else:
|
127 |
-
prompt_text += f' Human: {input}'
|
128 |
-
|
129 |
-
response = model.generate({
|
130 |
-
'prompt': [prompt_text] if not isinstance(prompt_text, list) else prompt_text,
|
131 |
-
'image_paths': [image_path] if image_path else [],
|
132 |
-
'top_p': top_p,
|
133 |
-
'temperature': temperature,
|
134 |
-
'max_tgt_len': max_length,
|
135 |
-
'modality_embeds': modality_cache
|
136 |
-
})
|
137 |
-
if isinstance(response, list):
|
138 |
-
response = response[0]
|
139 |
-
chatbot.append((parse_text(input), parse_text(response)))
|
140 |
-
history.append((input, response))
|
141 |
-
return chatbot, history, modality_cache
|
142 |
-
|
143 |
-
|
144 |
-
def reset_user_input():
|
145 |
-
return gr.update(value='')
|
146 |
-
|
147 |
-
def reset_dialog():
|
148 |
-
return [], []
|
149 |
-
|
150 |
-
def reset_state():
|
151 |
-
return None, [], [], []
|
152 |
-
|
153 |
-
|
154 |
-
with gr.Blocks(scale=4) as demo:
|
155 |
-
gr.Image("./images/lamm_title.png", show_label=False, height=50)
|
156 |
-
gr.HTML(
|
157 |
-
"""
|
158 |
-
<p>
|
159 |
-
<p align="center">
|
160 |
-
<font size='4'>
|
161 |
-
<a href="https://openlamm.github.io/" target="_blank">🏠 Home Page</a> • <a href="https://github.com/OpenLAMM/LAMM" target="_blank">🌏 Github</a> • <a href="https://arxiv.org/pdf/2306.06687.pdf" target="_blank">📰 Paper</a> • <a href="https://www.youtube.com/watch?v=M7XlIe8hhPk" target="_blank">▶️ YouTube </a> • <a href="https://www.bilibili.com/video/BV1kN411D7kt/?share_source=copy_web&vd_source=ab4c734425ed0114898300f2c037ac0b" target="_blank"> 📺 Bilibili</a> • <a href="https://opendatalab.com/LAMM" target="_blank">📀 Data</a> • <a href="https://huggingface.co/openlamm" target="_blank">📦 LAMM Models</a>
|
162 |
-
</font>
|
163 |
-
</p>
|
164 |
-
</p>
|
165 |
-
"""
|
166 |
-
)
|
167 |
-
# gr.HTML("""<h1>LAMM: Language-Assisted Multi-Modal Instruction-Tuning Dataset, Framework, and Benchmark</h1>""")
|
168 |
-
# gr.Markdown(
|
169 |
-
# """
|
170 |
-
# <p>
|
171 |
-
|
172 |
-
# <a href="https://arxiv.org/pdf/2306.06687.pdf" target="_blank"><img src="https://img.shields.io/badge/arxiv-PDF-red"/></a>
|
173 |
-
|
174 |
-
# <a href="https://openlamm.github.io" target="_blank"><img src="https://img.shields.io/badge/LAMM-HomePage-blue"/></a>
|
175 |
-
|
176 |
-
# <a href="https://opendatalab.com/LAMM" target="_blank"><img src="https://img.shields.io/badge/LAMM-Dataset-green"/></a>
|
177 |
-
|
178 |
-
# <a href="https://www.youtube.com/watch?v=M7XlIe8hhPk" target="_blank"><img src="https://img.shields.io/badge/video-Youtube-red"/></a>
|
179 |
-
|
180 |
-
# <a href="https://www.bilibili.com/video/BV1kN411D7kt/?share_source=copy_web&vd_source=ab4c734425ed0114898300f2c037ac0b" target="_blank"><img src="https://img.shields.io/badge/video-Bilibili-blue"/></a>
|
181 |
-
|
182 |
-
# <a href="https://github.com/OpenLAMM/LAMM" target="_blank"><img src="https://img.shields.io/badge/Repo-Github-white"/></a>
|
183 |
-
|
184 |
-
# <a href="https://huggingface.co/openlamm" target="_blank"><img src="https://img.shields.io/badge/Models-huggingface-yellow"/></a>
|
185 |
-
|
186 |
-
# <img src="https://img.shields.io/github/stars/OpenLAMM/LAMM.svg?style=social&label=Star"/>
|
187 |
-
# </p>
|
188 |
-
# Drop your image & Start talking with LAMM models.
|
189 |
-
# """)
|
190 |
-
|
191 |
-
with gr.Row(scale=1):
|
192 |
-
with gr.Column(scale=1):
|
193 |
-
image_path = gr.Image(type="filepath", label="Image", value=None).style(height=600)
|
194 |
-
|
195 |
-
chatbot = gr.Chatbot(scale=1).style(height=600)
|
196 |
-
|
197 |
-
with gr.Row():
|
198 |
-
with gr.Column(scale=4):
|
199 |
-
with gr.Column(scale=12):
|
200 |
-
user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(container=False)
|
201 |
-
with gr.Column(min_width=32, scale=1):
|
202 |
-
with gr.Row(scale=1):
|
203 |
-
submitBtn = gr.Button("Submit", variant="primary")
|
204 |
-
with gr.Row(scale=1):
|
205 |
-
resubmitBtn = gr.Button("Resubmit", variant="primary")
|
206 |
-
with gr.Column(scale=1):
|
207 |
-
emptyBtn = gr.Button("Clear History")
|
208 |
-
max_length = gr.Slider(0, 600, value=256, step=1.0, label="Maximum length", interactive=True)
|
209 |
-
top_p = gr.Slider(0, 1, value=0.01, step=0.01, label="Top P", interactive=True)
|
210 |
-
temperature = gr.Slider(0, 1, value=0.9, step=0.01, label="Temperature", interactive=True)
|
211 |
-
|
212 |
-
history = gr.State([])
|
213 |
-
modality_cache = gr.State([])
|
214 |
-
|
215 |
-
submitBtn.click(
|
216 |
-
predict, [
|
217 |
-
user_input,
|
218 |
-
image_path,
|
219 |
-
chatbot,
|
220 |
-
max_length,
|
221 |
-
top_p,
|
222 |
-
temperature,
|
223 |
-
history,
|
224 |
-
modality_cache,
|
225 |
-
], [
|
226 |
-
chatbot,
|
227 |
-
history,
|
228 |
-
modality_cache
|
229 |
-
],
|
230 |
-
show_progress=True
|
231 |
-
)
|
232 |
-
|
233 |
-
resubmitBtn.click(
|
234 |
-
re_predict, [
|
235 |
-
user_input,
|
236 |
-
image_path,
|
237 |
-
chatbot,
|
238 |
-
max_length,
|
239 |
-
top_p,
|
240 |
-
temperature,
|
241 |
-
history,
|
242 |
-
modality_cache,
|
243 |
-
], [
|
244 |
-
chatbot,
|
245 |
-
history,
|
246 |
-
modality_cache
|
247 |
-
],
|
248 |
-
show_progress=True
|
249 |
-
)
|
250 |
-
|
251 |
-
submitBtn.click(reset_user_input, [], [user_input])
|
252 |
-
emptyBtn.click(reset_state, outputs=[
|
253 |
-
image_path,
|
254 |
-
chatbot,
|
255 |
-
history,
|
256 |
-
modality_cache
|
257 |
-
], show_progress=True)
|
258 |
-
|
259 |
-
demo.queue().launch(enable_queue=True)
|
|
|
1 |
+
from transformers import AutoModel, AutoTokenizer
|
2 |
+
from copy import deepcopy
|
3 |
+
import gradio as gr
|
4 |
+
import mdtex2html
|
5 |
+
from model.openlamm import LAMMPEFTModel
|
6 |
+
import torch
|
7 |
+
import json
|
8 |
+
|
9 |
+
# init the model
|
10 |
+
args = {
|
11 |
+
'model': 'openllama_peft',
|
12 |
+
'encoder_ckpt_path': '../pretrained_ckpt/ViT-L-14.pt',
|
13 |
+
'vicuna_ckpt_path': './pretrained_ckpt/llm_7b_v0',
|
14 |
+
'delta_ckpt_path': './pretrained_ckpt/llm7b_lora32_lamm186k/pytorch_model.pt',
|
15 |
+
'stage': 2,
|
16 |
+
'max_tgt_len': 128,
|
17 |
+
'lora_r': 32,
|
18 |
+
'lora_alpha': 32,
|
19 |
+
'lora_dropout': 0.1,
|
20 |
+
'lora_target_modules': ['q_proj', 'k_proj', 'v_proj', 'o_proj'],
|
21 |
+
'vision_type': 'image',
|
22 |
+
'vision_feature_type': 'local',
|
23 |
+
'num_vision_token': 256,
|
24 |
+
'encoder_pretrain': 'clip',
|
25 |
+
'system_header': True,
|
26 |
+
}
|
27 |
+
|
28 |
+
model = LAMMPEFTModel(**args)
|
29 |
+
delta_ckpt = torch.load(args['delta_ckpt_path'], map_location=torch.device('cpu'))
|
30 |
+
model.load_state_dict(delta_ckpt, strict=False)
|
31 |
+
model = model.eval().half().cuda()
|
32 |
+
print(f'[!] init the 13b model over ...')
|
33 |
+
|
34 |
+
"""Override Chatbot.postprocess"""
|
35 |
+
|
36 |
+
|
37 |
+
def postprocess(self, y):
|
38 |
+
if y is None:
|
39 |
+
return []
|
40 |
+
for i, (message, response) in enumerate(y):
|
41 |
+
y[i] = (
|
42 |
+
None if message is None else mdtex2html.convert((message)),
|
43 |
+
None if response is None else mdtex2html.convert(response),
|
44 |
+
)
|
45 |
+
return y
|
46 |
+
|
47 |
+
|
48 |
+
gr.Chatbot.postprocess = postprocess
|
49 |
+
|
50 |
+
|
51 |
+
def parse_text(text):
|
52 |
+
"""copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
|
53 |
+
lines = text.split("\n")
|
54 |
+
lines = [line for line in lines if line != ""]
|
55 |
+
count = 0
|
56 |
+
for i, line in enumerate(lines):
|
57 |
+
if "```" in line:
|
58 |
+
count += 1
|
59 |
+
items = line.split('`')
|
60 |
+
if count % 2 == 1:
|
61 |
+
lines[i] = f'<pre><code class="language-{items[-1]}">'
|
62 |
+
else:
|
63 |
+
lines[i] = f'<br></code></pre>'
|
64 |
+
else:
|
65 |
+
if i > 0:
|
66 |
+
if count % 2 == 1:
|
67 |
+
line = line.replace("`", "\`")
|
68 |
+
line = line.replace("<", "<")
|
69 |
+
line = line.replace(">", ">")
|
70 |
+
line = line.replace(" ", " ")
|
71 |
+
line = line.replace("*", "*")
|
72 |
+
line = line.replace("_", "_")
|
73 |
+
line = line.replace("-", "-")
|
74 |
+
line = line.replace(".", ".")
|
75 |
+
line = line.replace("!", "!")
|
76 |
+
line = line.replace("(", "(")
|
77 |
+
line = line.replace(")", ")")
|
78 |
+
line = line.replace("$", "$")
|
79 |
+
lines[i] = "<br>"+line
|
80 |
+
text = "".join(lines)
|
81 |
+
if text.endswith("##"):
|
82 |
+
text = text[:-2]
|
83 |
+
return text
|
84 |
+
|
85 |
+
|
86 |
+
def re_predict(
|
87 |
+
input,
|
88 |
+
image_path,
|
89 |
+
chatbot,
|
90 |
+
max_length,
|
91 |
+
top_p,
|
92 |
+
temperature,
|
93 |
+
history,
|
94 |
+
modality_cache,
|
95 |
+
):
|
96 |
+
# drop the latest query and answers and generate again
|
97 |
+
q, a = history.pop()
|
98 |
+
chatbot.pop()
|
99 |
+
return predict(q, image_path, chatbot, max_length, top_p, temperature, history, modality_cache)
|
100 |
+
|
101 |
+
|
102 |
+
def predict(
|
103 |
+
input,
|
104 |
+
image_path,
|
105 |
+
chatbot,
|
106 |
+
max_length,
|
107 |
+
top_p,
|
108 |
+
temperature,
|
109 |
+
history,
|
110 |
+
modality_cache,
|
111 |
+
):
|
112 |
+
if image_path is None: #
|
113 |
+
return [(input, "There is no input data provided! Please upload your data and start the conversation.")]
|
114 |
+
else:
|
115 |
+
print(f'[!] image path: {image_path}\n') # [!] audio path: {audio_path}\n[!] video path: {video_path}\n[!] thermal path: {thermal_path}')
|
116 |
+
|
117 |
+
# prepare the prompt
|
118 |
+
prompt_text = ''
|
119 |
+
for idx, (q, a) in enumerate(history):
|
120 |
+
if idx == 0:
|
121 |
+
prompt_text += f'{q}\n### Assistant: {a}\n###'
|
122 |
+
else:
|
123 |
+
prompt_text += f' Human: {q}\n### Assistant: {a}\n###'
|
124 |
+
if len(history) == 0:
|
125 |
+
prompt_text += f'{input}'
|
126 |
+
else:
|
127 |
+
prompt_text += f' Human: {input}'
|
128 |
+
|
129 |
+
response = model.generate({
|
130 |
+
'prompt': [prompt_text] if not isinstance(prompt_text, list) else prompt_text,
|
131 |
+
'image_paths': [image_path] if image_path else [],
|
132 |
+
'top_p': top_p,
|
133 |
+
'temperature': temperature,
|
134 |
+
'max_tgt_len': max_length,
|
135 |
+
'modality_embeds': modality_cache
|
136 |
+
})
|
137 |
+
if isinstance(response, list):
|
138 |
+
response = response[0]
|
139 |
+
chatbot.append((parse_text(input), parse_text(response)))
|
140 |
+
history.append((input, response))
|
141 |
+
return chatbot, history, modality_cache
|
142 |
+
|
143 |
+
|
144 |
+
def reset_user_input():
|
145 |
+
return gr.update(value='')
|
146 |
+
|
147 |
+
def reset_dialog():
|
148 |
+
return [], []
|
149 |
+
|
150 |
+
def reset_state():
|
151 |
+
return None, [], [], []
|
152 |
+
|
153 |
+
|
154 |
+
with gr.Blocks(scale=4) as demo:
|
155 |
+
gr.Image("./images/lamm_title.png", show_label=False, height=50)
|
156 |
+
gr.HTML(
|
157 |
+
"""
|
158 |
+
<p>
|
159 |
+
<p align="center">
|
160 |
+
<font size='4'>
|
161 |
+
<a href="https://openlamm.github.io/" target="_blank">🏠 Home Page</a> • <a href="https://github.com/OpenLAMM/LAMM" target="_blank">🌏 Github</a> • <a href="https://arxiv.org/pdf/2306.06687.pdf" target="_blank">📰 Paper</a> • <a href="https://www.youtube.com/watch?v=M7XlIe8hhPk" target="_blank">▶️ YouTube </a> • <a href="https://www.bilibili.com/video/BV1kN411D7kt/?share_source=copy_web&vd_source=ab4c734425ed0114898300f2c037ac0b" target="_blank"> 📺 Bilibili</a> • <a href="https://opendatalab.com/LAMM" target="_blank">📀 Data</a> • <a href="https://huggingface.co/openlamm" target="_blank">📦 LAMM Models</a>
|
162 |
+
</font>
|
163 |
+
</p>
|
164 |
+
</p>
|
165 |
+
"""
|
166 |
+
)
|
167 |
+
# gr.HTML("""<h1>LAMM: Language-Assisted Multi-Modal Instruction-Tuning Dataset, Framework, and Benchmark</h1>""")
|
168 |
+
# gr.Markdown(
|
169 |
+
# """
|
170 |
+
# <p>
|
171 |
+
|
172 |
+
# <a href="https://arxiv.org/pdf/2306.06687.pdf" target="_blank"><img src="https://img.shields.io/badge/arxiv-PDF-red"/></a>
|
173 |
+
|
174 |
+
# <a href="https://openlamm.github.io" target="_blank"><img src="https://img.shields.io/badge/LAMM-HomePage-blue"/></a>
|
175 |
+
|
176 |
+
# <a href="https://opendatalab.com/LAMM" target="_blank"><img src="https://img.shields.io/badge/LAMM-Dataset-green"/></a>
|
177 |
+
|
178 |
+
# <a href="https://www.youtube.com/watch?v=M7XlIe8hhPk" target="_blank"><img src="https://img.shields.io/badge/video-Youtube-red"/></a>
|
179 |
+
|
180 |
+
# <a href="https://www.bilibili.com/video/BV1kN411D7kt/?share_source=copy_web&vd_source=ab4c734425ed0114898300f2c037ac0b" target="_blank"><img src="https://img.shields.io/badge/video-Bilibili-blue"/></a>
|
181 |
+
|
182 |
+
# <a href="https://github.com/OpenLAMM/LAMM" target="_blank"><img src="https://img.shields.io/badge/Repo-Github-white"/></a>
|
183 |
+
|
184 |
+
# <a href="https://huggingface.co/openlamm" target="_blank"><img src="https://img.shields.io/badge/Models-huggingface-yellow"/></a>
|
185 |
+
|
186 |
+
# <img src="https://img.shields.io/github/stars/OpenLAMM/LAMM.svg?style=social&label=Star"/>
|
187 |
+
# </p>
|
188 |
+
# Drop your image & Start talking with LAMM models.
|
189 |
+
# """)
|
190 |
+
|
191 |
+
with gr.Row(scale=1):
|
192 |
+
with gr.Column(scale=1):
|
193 |
+
image_path = gr.Image(type="filepath", label="Image", value=None).style(height=600)
|
194 |
+
|
195 |
+
chatbot = gr.Chatbot(scale=1).style(height=600)
|
196 |
+
|
197 |
+
with gr.Row():
|
198 |
+
with gr.Column(scale=4):
|
199 |
+
with gr.Column(scale=12):
|
200 |
+
user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(container=False)
|
201 |
+
with gr.Column(min_width=32, scale=1):
|
202 |
+
with gr.Row(scale=1):
|
203 |
+
submitBtn = gr.Button("Submit", variant="primary")
|
204 |
+
with gr.Row(scale=1):
|
205 |
+
resubmitBtn = gr.Button("Resubmit", variant="primary")
|
206 |
+
with gr.Column(scale=1):
|
207 |
+
emptyBtn = gr.Button("Clear History")
|
208 |
+
max_length = gr.Slider(0, 600, value=256, step=1.0, label="Maximum length", interactive=True)
|
209 |
+
top_p = gr.Slider(0, 1, value=0.01, step=0.01, label="Top P", interactive=True)
|
210 |
+
temperature = gr.Slider(0, 1, value=0.9, step=0.01, label="Temperature", interactive=True)
|
211 |
+
|
212 |
+
history = gr.State([])
|
213 |
+
modality_cache = gr.State([])
|
214 |
+
|
215 |
+
submitBtn.click(
|
216 |
+
predict, [
|
217 |
+
user_input,
|
218 |
+
image_path,
|
219 |
+
chatbot,
|
220 |
+
max_length,
|
221 |
+
top_p,
|
222 |
+
temperature,
|
223 |
+
history,
|
224 |
+
modality_cache,
|
225 |
+
], [
|
226 |
+
chatbot,
|
227 |
+
history,
|
228 |
+
modality_cache
|
229 |
+
],
|
230 |
+
show_progress=True
|
231 |
+
)
|
232 |
+
|
233 |
+
resubmitBtn.click(
|
234 |
+
re_predict, [
|
235 |
+
user_input,
|
236 |
+
image_path,
|
237 |
+
chatbot,
|
238 |
+
max_length,
|
239 |
+
top_p,
|
240 |
+
temperature,
|
241 |
+
history,
|
242 |
+
modality_cache,
|
243 |
+
], [
|
244 |
+
chatbot,
|
245 |
+
history,
|
246 |
+
modality_cache
|
247 |
+
],
|
248 |
+
show_progress=True
|
249 |
+
)
|
250 |
+
|
251 |
+
submitBtn.click(reset_user_input, [], [user_input])
|
252 |
+
emptyBtn.click(reset_state, outputs=[
|
253 |
+
image_path,
|
254 |
+
chatbot,
|
255 |
+
history,
|
256 |
+
modality_cache
|
257 |
+
], show_progress=True)
|
258 |
+
|
259 |
+
demo.queue().launch(enable_queue=True)
|