Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -114,20 +114,22 @@ def process_image(model, tokenizer, transform, device, check_type, image, text):
|
|
114 |
# 事件处理函数
|
115 |
def update_index(change):
|
116 |
global current_vis, current_bpe, current_index
|
|
|
|
|
117 |
current_index = max(0, min(len(current_vis) - 1, current_index + change))
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
def format_bpe_display(bpe):
|
121 |
# 使用HTML标签来设置字体大小、颜色,加粗,并居中
|
122 |
return f"<div style='text-align:center; font-size:20px;'><strong>Current BPE: <span style='color:red;'>{bpe}</span></strong></div>"
|
123 |
|
124 |
-
|
125 |
-
global current_vis, current_bpe, current_index
|
126 |
-
print(f"x: {x}, current_vis length: {len(current_vis)}, current_bpe length: {len(current_bpe)}")
|
127 |
-
if 0 <= x < len(current_vis) and 0 <= x < len(current_bpe):
|
128 |
-
return current_vis[x], format_bpe_display(current_bpe[x])
|
129 |
-
else:
|
130 |
-
return None, "索引超出范围"
|
131 |
|
132 |
|
133 |
|
@@ -166,7 +168,6 @@ with gr.Blocks(title="BPE Visualization Demo") as demo:
|
|
166 |
|
167 |
with gr.Row() as controls:
|
168 |
prev_btn = gr.Button("⬅ Last", visible=False)
|
169 |
-
index_slider = gr.Slider(0, 1, value=0, step=1, label="BPE index", visible=False)
|
170 |
next_btn = gr.Button("⮕ Next", visible=False)
|
171 |
|
172 |
bpe_display = gr.Markdown("Current BPE: ", visible=False)
|
@@ -175,44 +176,33 @@ with gr.Blocks(title="BPE Visualization Demo") as demo:
|
|
175 |
@spaces.GPU
|
176 |
def on_run_clicked(model_type, image, text):
|
177 |
global current_vis, current_bpe, current_index
|
178 |
-
current_index = 0 #
|
179 |
image, vis, bpe = process_image(*load_model(model_type), model_type, image, text)
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
183 |
print("len_current_vis",len(current_vis))
|
184 |
print("len_current_bpe",len(current_bpe))
|
185 |
print("current_vis",current_vis)
|
186 |
print("current_bpe",current_bpe)
|
187 |
-
return image, vis,
|
188 |
-
|
189 |
|
190 |
-
|
191 |
-
|
192 |
-
inputs=[model_type, image_input, text_input],
|
193 |
-
outputs=[orig_img, heatmap, bpe_display, index_slider],
|
194 |
-
).then(
|
195 |
-
lambda max_val: (gr.update(visible=True), gr.update(visible=True, maximum=max_val, value=0), gr.update(visible=True), gr.update(visible=True)),
|
196 |
-
inputs=index_slider,
|
197 |
-
outputs=[prev_btn, index_slider, next_btn, bpe_display],
|
198 |
-
)
|
199 |
|
200 |
prev_btn.click(
|
201 |
-
lambda:
|
202 |
-
outputs=[heatmap, bpe_display,
|
203 |
)
|
204 |
|
205 |
next_btn.click(
|
206 |
-
lambda:
|
207 |
-
outputs=[heatmap, bpe_display,
|
208 |
)
|
209 |
|
210 |
|
211 |
-
index_slider.change(
|
212 |
-
update_slider_index,
|
213 |
-
inputs=index_slider,
|
214 |
-
outputs=[heatmap, bpe_display]
|
215 |
-
)
|
216 |
|
217 |
|
218 |
|
|
|
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 |
+
return current_vis[current_index], format_bpe_display(current_bpe[current_index]), prev_visible, next_visible
|
126 |
+
|
127 |
|
128 |
def format_bpe_display(bpe):
|
129 |
# 使用HTML标签来设置字体大小、颜色,加粗,并居中
|
130 |
return f"<div style='text-align:center; font-size:20px;'><strong>Current BPE: <span style='color:red;'>{bpe}</span></strong></div>"
|
131 |
|
132 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
|
135 |
|
|
|
168 |
|
169 |
with gr.Row() as controls:
|
170 |
prev_btn = gr.Button("⬅ Last", visible=False)
|
|
|
171 |
next_btn = gr.Button("⮕ Next", visible=False)
|
172 |
|
173 |
bpe_display = gr.Markdown("Current BPE: ", visible=False)
|
|
|
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, prev_btn, next_btn]
|
198 |
)
|
199 |
|
200 |
next_btn.click(
|
201 |
+
lambda: update_index(1),
|
202 |
+
outputs=[heatmap, bpe_display, prev_btn, next_btn]
|
203 |
)
|
204 |
|
205 |
|
|
|
|
|
|
|
|
|
|
|
206 |
|
207 |
|
208 |
|