Update app.py
Browse files
app.py
CHANGED
|
@@ -112,10 +112,17 @@ def process_image(model, tokenizer, transform, device, check_type, image, text):
|
|
| 112 |
return image, current_vis, current_bpe
|
| 113 |
|
| 114 |
# 事件处理函数
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
def format_bpe_display(bpe):
|
| 121 |
# 使用HTML标签来设置字体大小、颜色,加粗,并居中
|
|
@@ -162,6 +169,10 @@ with gr.Blocks(title="BPE Visualization Demo") as demo:
|
|
| 162 |
|
| 163 |
bpe_display = gr.Markdown("Current BPE: ")
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
# 事件处理
|
| 166 |
@spaces.GPU
|
| 167 |
def on_run_clicked(model_type, image, text):
|
|
@@ -174,23 +185,36 @@ with gr.Blocks(title="BPE Visualization Demo") as demo:
|
|
| 174 |
print("current_vis",len(current_vis))
|
| 175 |
print("current_bpe",len(current_bpe))
|
| 176 |
|
| 177 |
-
return image, current_vis[0],f"<div style='text-align:center; font-size:20px;'><strong>Current BPE: <span style='color:red;'>{current_bpe[0]}</span></strong></div>", gr.update(visible=True), gr.update(visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
|
|
|
|
| 180 |
run_btn.click(
|
| 181 |
on_run_clicked,
|
| 182 |
inputs=[model_type, image_input, text_input],
|
| 183 |
-
outputs=[orig_img, heatmap, bpe_display, prev_btn, next_btn
|
| 184 |
)
|
| 185 |
|
| 186 |
prev_btn.click(
|
| 187 |
-
|
| 188 |
-
|
|
|
|
| 189 |
)
|
| 190 |
|
| 191 |
next_btn.click(
|
| 192 |
-
|
| 193 |
-
|
|
|
|
| 194 |
)
|
| 195 |
|
| 196 |
|
|
|
|
| 112 |
return image, current_vis, current_bpe
|
| 113 |
|
| 114 |
# 事件处理函数
|
| 115 |
+
# 上一项和下一项按钮
|
| 116 |
+
def update_index(direction, current_vis, current_bpe, current_index):
|
| 117 |
+
# 计算新的索引
|
| 118 |
+
new_index = max(0, min(current_index + direction, len(current_vis) - 1))
|
| 119 |
+
|
| 120 |
+
# 更新可视化内容
|
| 121 |
+
return (
|
| 122 |
+
current_vis[new_index],
|
| 123 |
+
format_bpe_display(current_bpe[new_index]),
|
| 124 |
+
new_index # 更新索引
|
| 125 |
+
)
|
| 126 |
|
| 127 |
def format_bpe_display(bpe):
|
| 128 |
# 使用HTML标签来设置字体大小、颜色,加粗,并居中
|
|
|
|
| 169 |
|
| 170 |
bpe_display = gr.Markdown("Current BPE: ")
|
| 171 |
|
| 172 |
+
current_vis_state = gr.State([])
|
| 173 |
+
current_bpe_state = gr.State([])
|
| 174 |
+
current_index_state = gr.State(0)
|
| 175 |
+
|
| 176 |
# 事件处理
|
| 177 |
@spaces.GPU
|
| 178 |
def on_run_clicked(model_type, image, text):
|
|
|
|
| 185 |
print("current_vis",len(current_vis))
|
| 186 |
print("current_bpe",len(current_bpe))
|
| 187 |
|
| 188 |
+
# return image, current_vis[0],f"<div style='text-align:center; font-size:20px;'><strong>Current BPE: <span style='color:red;'>{current_bpe[0]}</span></strong></div>", gr.update(visible=True), gr.update(visible=True)
|
| 189 |
+
return (
|
| 190 |
+
image,
|
| 191 |
+
current_vis[current_index],
|
| 192 |
+
format_bpe_display(current_bpe[current_index]),
|
| 193 |
+
gr.update(visible=True),
|
| 194 |
+
gr.update(visible=True),
|
| 195 |
+
current_vis, # 存储整个列表
|
| 196 |
+
current_bpe, # 存储整个列表
|
| 197 |
+
current_index # 存储当前索引
|
| 198 |
+
)
|
| 199 |
|
| 200 |
|
| 201 |
+
|
| 202 |
run_btn.click(
|
| 203 |
on_run_clicked,
|
| 204 |
inputs=[model_type, image_input, text_input],
|
| 205 |
+
outputs=[orig_img, heatmap, bpe_display, prev_btn, next_btn, current_vis_state, current_bpe_state, current_index_state]
|
| 206 |
)
|
| 207 |
|
| 208 |
prev_btn.click(
|
| 209 |
+
update_index,
|
| 210 |
+
inputs=[gr.State(-1), current_vis_state, current_bpe_state, current_index_state],
|
| 211 |
+
outputs=[heatmap, bpe_display, current_index_state]
|
| 212 |
)
|
| 213 |
|
| 214 |
next_btn.click(
|
| 215 |
+
update_index,
|
| 216 |
+
inputs=[gr.State(1), current_vis_state, current_bpe_state, current_index_state],
|
| 217 |
+
outputs=[heatmap, bpe_display, current_index_state]
|
| 218 |
)
|
| 219 |
|
| 220 |
|