Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,9 +21,7 @@ CHECKPOINTS = {
|
|
| 21 |
|
| 22 |
# 全局变量
|
| 23 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 24 |
-
|
| 25 |
-
current_bpe = []
|
| 26 |
-
current_index = 0
|
| 27 |
|
| 28 |
|
| 29 |
def load_model(check_type):
|
|
@@ -55,7 +53,7 @@ def load_model(check_type):
|
|
| 55 |
return model.to(device), tokenizer, transform, device
|
| 56 |
|
| 57 |
def process_image(model, tokenizer, transform, device, check_type, image, text):
|
| 58 |
-
|
| 59 |
src_size = image.size
|
| 60 |
if 'TokenOCR' in check_type:
|
| 61 |
images, target_ratio = dynamic_preprocess(image, min_num=1, max_num=12,
|
|
@@ -109,21 +107,13 @@ def process_image(model, tokenizer, transform, device, check_type, image, text):
|
|
| 109 |
# current_bpe[-1] = 'Input text'
|
| 110 |
current_bpe[-1] = text
|
| 111 |
|
| 112 |
-
return image, current_vis
|
| 113 |
|
| 114 |
# 事件处理函数
|
| 115 |
def update_index(change):
|
| 116 |
-
global current_vis, current_bpe, current_index
|
| 117 |
-
|
| 118 |
-
# 限制索引范围
|
| 119 |
-
current_index = max(0, min(len(current_vis) - 1, current_index + change))
|
| 120 |
-
|
| 121 |
-
# 处理按钮可见性
|
| 122 |
-
prev_visible = current_index > 0
|
| 123 |
-
next_visible = current_index < len(current_vis) - 1
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
|
| 128 |
def format_bpe_display(bpe):
|
| 129 |
# 使用HTML标签来设置字体大小、颜色,加粗,并居中
|
|
@@ -131,8 +121,6 @@ def format_bpe_display(bpe):
|
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
# Gradio界面
|
| 137 |
with gr.Blocks(title="BPE Visualization Demo") as demo:
|
| 138 |
gr.Markdown("## BPE Visualization Demo - TokenFD基座模型能力可视化")
|
|
@@ -175,36 +163,38 @@ with gr.Blocks(title="BPE Visualization Demo") as demo:
|
|
| 175 |
# 事件处理
|
| 176 |
@spaces.GPU
|
| 177 |
def on_run_clicked(model_type, image, text):
|
| 178 |
-
global current_vis, current_bpe, current_index
|
| 179 |
-
current_index = 0 # 重新处理新图片时,索引归零
|
| 180 |
-
image, vis, bpe = process_image(*load_model(model_type), model_type, image, text)
|
| 181 |
-
|
| 182 |
-
if len(current_vis) > 1:
|
| 183 |
-
prev_visible, next_visible = True, True
|
| 184 |
-
else:
|
| 185 |
-
prev_visible, next_visible = False, False
|
| 186 |
-
print("len_current_vis",len(current_vis))
|
| 187 |
-
print("len_current_bpe",len(current_bpe))
|
| 188 |
-
print("current_vis",current_vis)
|
| 189 |
-
print("current_bpe",current_bpe)
|
| 190 |
-
return image, vis, format_bpe_display(bpe), prev_visible, next_visible
|
| 191 |
-
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
prev_btn.click(
|
| 196 |
-
lambda: update_index(-1),
|
| 197 |
-
outputs=[heatmap, bpe_display
|
| 198 |
)
|
| 199 |
|
| 200 |
next_btn.click(
|
| 201 |
-
lambda: update_index(1),
|
| 202 |
-
outputs=[heatmap, bpe_display
|
| 203 |
)
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
|
|
|
| 209 |
if __name__ == "__main__":
|
| 210 |
demo.launch()
|
|
|
|
| 21 |
|
| 22 |
# 全局变量
|
| 23 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 24 |
+
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
def load_model(check_type):
|
|
|
|
| 53 |
return model.to(device), tokenizer, transform, device
|
| 54 |
|
| 55 |
def process_image(model, tokenizer, transform, device, check_type, image, text):
|
| 56 |
+
|
| 57 |
src_size = image.size
|
| 58 |
if 'TokenOCR' in check_type:
|
| 59 |
images, target_ratio = dynamic_preprocess(image, min_num=1, max_num=12,
|
|
|
|
| 107 |
# current_bpe[-1] = 'Input text'
|
| 108 |
current_bpe[-1] = text
|
| 109 |
|
| 110 |
+
return image, current_vis, current_bpe
|
| 111 |
|
| 112 |
# 事件处理函数
|
| 113 |
def update_index(change):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
aa=1
|
| 116 |
+
return 1
|
| 117 |
|
| 118 |
def format_bpe_display(bpe):
|
| 119 |
# 使用HTML标签来设置字体大小、颜色,加粗,并居中
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
|
|
|
|
|
|
|
| 124 |
# Gradio界面
|
| 125 |
with gr.Blocks(title="BPE Visualization Demo") as demo:
|
| 126 |
gr.Markdown("## BPE Visualization Demo - TokenFD基座模型能力可视化")
|
|
|
|
| 163 |
# 事件处理
|
| 164 |
@spaces.GPU
|
| 165 |
def on_run_clicked(model_type, image, text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
current_index = 0 # Reset index when new image is processed
|
| 168 |
+
image, current_vis, current_bpe = process_image(*load_model(model_type), model_type, image, text)
|
| 169 |
+
|
| 170 |
+
bpe_text = format_bpe_display(current_bpe)
|
| 171 |
+
|
| 172 |
+
print("current_vis",len(current_vis))
|
| 173 |
+
print("current_bpe",len(current_bpe))
|
| 174 |
+
|
| 175 |
+
return image, current_vis[0], current_bpe[0]
|
| 176 |
|
| 177 |
|
| 178 |
+
run_btn.click(
|
| 179 |
+
on_run_clicked,
|
| 180 |
+
inputs=[model_type, image_input, text_input],
|
| 181 |
+
outputs=[orig_img, heatmap, bpe_display, prev_btn, next_btn], # 直接更新 prev_btn 和 next_btn
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
prev_btn.click(
|
| 185 |
+
lambda: (*update_index(-1), current_index),
|
| 186 |
+
outputs=[heatmap, bpe_display]
|
| 187 |
)
|
| 188 |
|
| 189 |
next_btn.click(
|
| 190 |
+
lambda: (*update_index(1), current_index),
|
| 191 |
+
outputs=[heatmap, bpe_display]
|
| 192 |
)
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
+
|
| 199 |
if __name__ == "__main__":
|
| 200 |
demo.launch()
|