Spaces:
Running
Running
clean up design
Browse files
app.py
CHANGED
|
@@ -22,6 +22,7 @@ def _build_header_markdown(doc) -> str:
|
|
| 22 |
if url:
|
| 23 |
header += f"\n\n[{url}]({url})"
|
| 24 |
return header
|
|
|
|
| 25 |
def matches_filters(doc, require_has_math: bool | None, require_has_infobox: bool | None) -> bool:
|
| 26 |
meta = doc.metadata or {}
|
| 27 |
if require_has_math and not bool(meta.get("has_math")):
|
|
@@ -186,7 +187,7 @@ def show_doc(doc):
|
|
| 186 |
info = meta.get("infoboxes", [])
|
| 187 |
right = render_iframe(_safe_url_from_metadata(meta))
|
| 188 |
header = _build_header_markdown(doc)
|
| 189 |
-
return left, meta, md_clean, info, right, header
|
| 190 |
|
| 191 |
|
| 192 |
def render_idx(docs, idx: int):
|
|
@@ -195,7 +196,7 @@ def render_idx(docs, idx: int):
|
|
| 195 |
idx = max(0, min(idx, len(docs) - 1))
|
| 196 |
doc = docs[idx]
|
| 197 |
left, left_meta, md, info, right, header = show_doc(doc)
|
| 198 |
-
return left, left_meta, md, info, right, header
|
| 199 |
|
| 200 |
|
| 201 |
def on_prev(docs_cache, idx: int, reader_iter, require_has_math: bool, require_has_infobox: bool):
|
|
@@ -213,7 +214,7 @@ def on_prev(docs_cache, idx: int, reader_iter, require_has_math: bool, require_h
|
|
| 213 |
filtered_idx = i
|
| 214 |
break
|
| 215 |
left, left_meta, md, info, right, header = render_idx(docs_cache, filtered_idx)
|
| 216 |
-
return filtered_idx, docs_cache, reader_iter, left, left_meta, header, md, info, right
|
| 217 |
|
| 218 |
|
| 219 |
def on_next(docs_cache, idx: int, reader_iter, require_has_math: bool, require_has_infobox: bool):
|
|
@@ -227,7 +228,7 @@ def on_next(docs_cache, idx: int, reader_iter, require_has_math: bool, require_h
|
|
| 227 |
if new_idx == -1:
|
| 228 |
return idx, docs_cache, reader_iter, "No documents match filters.", {}, "", [], render_iframe("")
|
| 229 |
left, left_meta, md, info, right, header = render_idx(docs_cache, new_idx)
|
| 230 |
-
return new_idx, docs_cache, reader_iter, left, left_meta, header, md, info, right
|
| 231 |
|
| 232 |
|
| 233 |
SCROLL_TO_TOP_JS = """
|
|
@@ -265,11 +266,13 @@ with gr.Blocks() as demo:
|
|
| 265 |
require_has_infobox = gr.Checkbox(label="Has infobox", value=False)
|
| 266 |
with gr.Row():
|
| 267 |
with gr.Column():
|
| 268 |
-
with gr.Tab("FineWiki"):
|
| 269 |
-
|
| 270 |
-
with gr.Tab("
|
|
|
|
|
|
|
| 271 |
left_meta = gr.JSON(label="Metadata")
|
| 272 |
-
with gr.Tab("
|
| 273 |
right_infoboxes = gr.JSON(label="Infoboxes")
|
| 274 |
|
| 275 |
with gr.Row():
|
|
@@ -282,20 +285,20 @@ with gr.Blocks() as demo:
|
|
| 282 |
right_markdown = gr.Textbox(label="wikimedia/wikipedia extraction", lines=30, elem_id="right_markdown_box")
|
| 283 |
|
| 284 |
|
| 285 |
-
_ev1 = language_select.change(on_select_language, inputs=[language_select, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state,
|
| 286 |
_ev1.then(js=SCROLL_TO_TOP_JS)
|
| 287 |
-
_ev2 = demo.load(on_select_language, inputs=[language_select, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state,
|
| 288 |
_ev2.then(js=SCROLL_TO_TOP_JS)
|
| 289 |
# find_btn.click(on_find, inputs=[docs_state, idx_state, iter_state, id_input, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state, left_text, left_meta, header_md, right_markdown, right_infoboxes, right_iframe], concurrency_limit=1)
|
| 290 |
|
| 291 |
# Visibility toggles driven directly by checkbox changes
|
| 292 |
-
_ev4 = prev_btn.click(on_prev, inputs=[docs_state, idx_state, iter_state, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state,
|
| 293 |
_ev4.then(js=SCROLL_TO_TOP_JS)
|
| 294 |
-
_ev5 = next_btn.click(on_next, inputs=[docs_state, idx_state, iter_state, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state,
|
| 295 |
_ev5.then(js=SCROLL_TO_TOP_JS)
|
| 296 |
-
_ev4 = prev_btn2.click(on_prev, inputs=[docs_state, idx_state, iter_state, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state,
|
| 297 |
_ev4.then(js=SCROLL_TO_TOP_JS)
|
| 298 |
-
_ev5 = next_btn2.click(on_next, inputs=[docs_state, idx_state, iter_state, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state,
|
| 299 |
_ev5.then(js=SCROLL_TO_TOP_JS)
|
| 300 |
|
| 301 |
# Enable global queue to coordinate concurrent requests safely
|
|
|
|
| 22 |
if url:
|
| 23 |
header += f"\n\n[{url}]({url})"
|
| 24 |
return header
|
| 25 |
+
|
| 26 |
def matches_filters(doc, require_has_math: bool | None, require_has_infobox: bool | None) -> bool:
|
| 27 |
meta = doc.metadata or {}
|
| 28 |
if require_has_math and not bool(meta.get("has_math")):
|
|
|
|
| 187 |
info = meta.get("infoboxes", [])
|
| 188 |
right = render_iframe(_safe_url_from_metadata(meta))
|
| 189 |
header = _build_header_markdown(doc)
|
| 190 |
+
return left, left, meta, md_clean, info, right, header
|
| 191 |
|
| 192 |
|
| 193 |
def render_idx(docs, idx: int):
|
|
|
|
| 196 |
idx = max(0, min(idx, len(docs) - 1))
|
| 197 |
doc = docs[idx]
|
| 198 |
left, left_meta, md, info, right, header = show_doc(doc)
|
| 199 |
+
return left, left, left_meta, md, info, right, header
|
| 200 |
|
| 201 |
|
| 202 |
def on_prev(docs_cache, idx: int, reader_iter, require_has_math: bool, require_has_infobox: bool):
|
|
|
|
| 214 |
filtered_idx = i
|
| 215 |
break
|
| 216 |
left, left_meta, md, info, right, header = render_idx(docs_cache, filtered_idx)
|
| 217 |
+
return filtered_idx, docs_cache, reader_iter, left, left, left_meta, header, md, info, right
|
| 218 |
|
| 219 |
|
| 220 |
def on_next(docs_cache, idx: int, reader_iter, require_has_math: bool, require_has_infobox: bool):
|
|
|
|
| 228 |
if new_idx == -1:
|
| 229 |
return idx, docs_cache, reader_iter, "No documents match filters.", {}, "", [], render_iframe("")
|
| 230 |
left, left_meta, md, info, right, header = render_idx(docs_cache, new_idx)
|
| 231 |
+
return new_idx, docs_cache, reader_iter, left, left, left_meta, header, md, info, right
|
| 232 |
|
| 233 |
|
| 234 |
SCROLL_TO_TOP_JS = """
|
|
|
|
| 266 |
require_has_infobox = gr.Checkbox(label="Has infobox", value=False)
|
| 267 |
with gr.Row():
|
| 268 |
with gr.Column():
|
| 269 |
+
with gr.Tab("FineWiki raw"):
|
| 270 |
+
left_text_raw = gr.Textbox(label="FineWiki extraction", lines=30, elem_id="left_text_box")
|
| 271 |
+
with gr.Tab("FineWiki markdown"):
|
| 272 |
+
left_text_md = gr.Markdown(label="FineWiki extraction", lines=30)
|
| 273 |
+
with gr.Tab("FineWiki metadata"):
|
| 274 |
left_meta = gr.JSON(label="Metadata")
|
| 275 |
+
with gr.Tab("FineWiki infoboxes"):
|
| 276 |
right_infoboxes = gr.JSON(label="Infoboxes")
|
| 277 |
|
| 278 |
with gr.Row():
|
|
|
|
| 285 |
right_markdown = gr.Textbox(label="wikimedia/wikipedia extraction", lines=30, elem_id="right_markdown_box")
|
| 286 |
|
| 287 |
|
| 288 |
+
_ev1 = language_select.change(on_select_language, inputs=[language_select, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state, left_text_raw, left_text_md, left_meta, header_md, right_markdown, right_infoboxes, right_iframe], concurrency_limit=1)
|
| 289 |
_ev1.then(js=SCROLL_TO_TOP_JS)
|
| 290 |
+
_ev2 = demo.load(on_select_language, inputs=[language_select, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state, left_text_raw, left_text_md, left_meta, header_md, right_markdown, right_infoboxes, right_iframe], concurrency_limit=1)
|
| 291 |
_ev2.then(js=SCROLL_TO_TOP_JS)
|
| 292 |
# find_btn.click(on_find, inputs=[docs_state, idx_state, iter_state, id_input, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state, left_text, left_meta, header_md, right_markdown, right_infoboxes, right_iframe], concurrency_limit=1)
|
| 293 |
|
| 294 |
# Visibility toggles driven directly by checkbox changes
|
| 295 |
+
_ev4 = prev_btn.click(on_prev, inputs=[docs_state, idx_state, iter_state, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state, left_text_raw, left_text_md, left_meta, header_md, right_markdown, right_infoboxes, right_iframe], concurrency_limit=1)
|
| 296 |
_ev4.then(js=SCROLL_TO_TOP_JS)
|
| 297 |
+
_ev5 = next_btn.click(on_next, inputs=[docs_state, idx_state, iter_state, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state, left_text_raw, left_text_md, left_meta, header_md, right_markdown, right_infoboxes, right_iframe], concurrency_limit=1)
|
| 298 |
_ev5.then(js=SCROLL_TO_TOP_JS)
|
| 299 |
+
_ev4 = prev_btn2.click(on_prev, inputs=[docs_state, idx_state, iter_state, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state, left_text_raw, left_text_md, left_meta, header_md, right_markdown, right_infoboxes, right_iframe], concurrency_limit=1)
|
| 300 |
_ev4.then(js=SCROLL_TO_TOP_JS)
|
| 301 |
+
_ev5 = next_btn2.click(on_next, inputs=[docs_state, idx_state, iter_state, require_has_math, require_has_infobox], outputs=[idx_state, docs_state, iter_state, left_text_raw, left_text_md, left_meta, header_md, right_markdown, right_infoboxes, right_iframe], concurrency_limit=1)
|
| 302 |
_ev5.then(js=SCROLL_TO_TOP_JS)
|
| 303 |
|
| 304 |
# Enable global queue to coordinate concurrent requests safely
|