Spaces:
Sleeping
Sleeping
ux: default en, move examples section, clean up preview label and API table
Browse files
app.py
CHANGED
|
@@ -28,6 +28,10 @@ T = {
|
|
| 28 |
sec_upload="๐ ไธไผ PDF",
|
| 29 |
upload_label="PDF ๆไปถ๏ผๅๆไปถ๏ผ้ข็ญๆททๆ๏ผๅๆไปถ๏ผ็ฌฌ1ไธช้ข็ฎ๏ผ็ฌฌ2ไธช็ญๆก๏ผ",
|
| 30 |
task_label="ไปปๅกๅ็งฐ",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
sec_llm="โ๏ธ LLM ้
็ฝฎ",
|
| 32 |
api_url_label="API Base URL",
|
| 33 |
llm_key_label="LLM API Key๏ผDF_API_KEY๏ผ",
|
|
@@ -44,17 +48,7 @@ T = {
|
|
| 44 |
status_label="่ฟ่ก็ถๆ",
|
| 45 |
status_ph="็นๅปใๅผๅงๆๅใๅ่ฟๅบฆๆพ็คบๅจ่ฟ้๏ผ่ฟ่ก้ๆฐๅ้๏ผ่ฏท่ๅฟ็ญๅพ
๏ผโฆ",
|
| 46 |
output_label="ไธ่ฝฝ็ปๆ๏ผraw_vqa.jsonl๏ผ",
|
| 47 |
-
preview_label="็ปๆ้ข่ง
|
| 48 |
-
sec_examples="๐ ๅ
็ฝฎ็คบไพ PDF๏ผ็นๅปๅ ่ฝฝๅนถ้ข่ง๏ผ",
|
| 49 |
-
pdf_preview_label="PDF ้ข่ง",
|
| 50 |
-
ex1_label="็คบไพ 1๏ผๅๆไปถ้ข็ญๆททๆ",
|
| 51 |
-
ex2_label="็คบไพ 2๏ผๅๆไปถ๏ผ้ข็ฎ + ็ญๆก๏ผ",
|
| 52 |
-
key_note=(
|
| 53 |
-
"**ไธคไธช API Key ็ๅบๅซ๏ผ**\n\n"
|
| 54 |
-
"| Key | ็จ้ | ็ณ่ฏทๅฐๅ |\n|-----|------|----------|\n"
|
| 55 |
-
"| LLM API Key | ่ฐ็จ GPT/Gemini ็ญๅคงๆจกๅๆๅ QA | ๅฏนๅบ LLM ๆๅกๅ |\n"
|
| 56 |
-
"| **MinerU API Key** | ่งฃๆ PDF ็้ข๏ผๅฎๅ
จ็ฌ็ซ๏ผ | [mineru.net/apiManage/token](https://mineru.net/apiManage/token) |"
|
| 57 |
-
),
|
| 58 |
),
|
| 59 |
"en": dict(
|
| 60 |
lang_btn="ไธญๆ",
|
|
@@ -68,6 +62,10 @@ T = {
|
|
| 68 |
sec_upload="๐ Upload PDF",
|
| 69 |
upload_label="PDF File(s) โ single: Q&A interleaved; two files: 1st questions, 2nd answers",
|
| 70 |
task_label="Task Name",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
sec_llm="โ๏ธ LLM Configuration",
|
| 72 |
api_url_label="API Base URL",
|
| 73 |
llm_key_label="LLM API Key (DF_API_KEY)",
|
|
@@ -84,20 +82,12 @@ T = {
|
|
| 84 |
status_label="Status",
|
| 85 |
status_ph="Click 'Start Extraction' to begin (may take several minutes)โฆ",
|
| 86 |
output_label="Download Result (raw_vqa.jsonl)",
|
| 87 |
-
preview_label="Result Preview
|
| 88 |
-
sec_examples="๐ Example PDFs (click to load & preview)",
|
| 89 |
-
pdf_preview_label="PDF Preview",
|
| 90 |
-
ex1_label="Example 1: Single file (Q&A mixed)",
|
| 91 |
-
ex2_label="Example 2: Two files (questions + answers)",
|
| 92 |
-
key_note=(
|
| 93 |
-
"**API Key Reference:**\n\n"
|
| 94 |
-
"| Key | Purpose | Where to Get |\n|-----|---------|---------------|\n"
|
| 95 |
-
"| LLM API Key | Call GPT/Gemini etc. for QA extraction | Your LLM provider |\n"
|
| 96 |
-
"| **MinerU API Key** | PDF layout parsing (completely independent) | [mineru.net/apiManage/token](https://mineru.net/apiManage/token) |"
|
| 97 |
-
),
|
| 98 |
),
|
| 99 |
}
|
| 100 |
|
|
|
|
|
|
|
| 101 |
EXAMPLES = [
|
| 102 |
("examples/VQA/questionextract_test.pdf",),
|
| 103 |
("examples/VQA/math_question.pdf", "examples/VQA/math_answer.pdf"),
|
|
@@ -107,21 +97,19 @@ EXAMPLES = [
|
|
| 107 |
# โโ Helpers โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 108 |
|
| 109 |
def _pdf_to_iframe(path: str) -> str:
|
| 110 |
-
"""Encode a local PDF as base64 and return an iframe HTML string."""
|
| 111 |
try:
|
| 112 |
with open(os.path.join(_REPO_ROOT, path), "rb") as f:
|
| 113 |
b64 = base64.b64encode(f.read()).decode()
|
| 114 |
return (
|
| 115 |
f'<iframe src="data:application/pdf;base64,{b64}" '
|
| 116 |
f'width="100%" height="520px" '
|
| 117 |
-
f'style="border:1px solid #d1d5db;border-radius:10px;"></iframe>'
|
| 118 |
)
|
| 119 |
except Exception as e:
|
| 120 |
-
return f"<p style='color:red'>
|
| 121 |
|
| 122 |
|
| 123 |
-
def _render_preview(jsonl_path: str, lang: str = "
|
| 124 |
-
"""Render up to 3 JSONL QA items as styled HTML cards."""
|
| 125 |
if not jsonl_path or not os.path.exists(jsonl_path):
|
| 126 |
return ""
|
| 127 |
items = []
|
|
@@ -139,7 +127,7 @@ def _render_preview(jsonl_path: str, lang: str = "zh") -> str:
|
|
| 139 |
|
| 140 |
if not items:
|
| 141 |
label_empty = "๏ผๆ QA ๆฐๆฎ๏ผ" if lang == "zh" else "(No QA data)"
|
| 142 |
-
return f
|
| 143 |
|
| 144 |
label_q = "้ข็ฎ" if lang == "zh" else "Question"
|
| 145 |
label_a = "็ญๆก" if lang == "zh" else "Answer"
|
|
@@ -153,18 +141,16 @@ def _render_preview(jsonl_path: str, lang: str = "zh") -> str:
|
|
| 153 |
name = item.get("name", "")
|
| 154 |
sol_short = (sol[:300] + "โฆ") if len(sol) > 300 else sol
|
| 155 |
sol_block = (
|
| 156 |
-
f'<div style="margin-top:
|
| 157 |
f'<span style="font-weight:600;color:#374151">{label_s}:</span>'
|
| 158 |
f'<div style="margin-top:4px;white-space:pre-wrap;font-size:13px;color:#4b5563">{sol_short}</div>'
|
| 159 |
f'</div>'
|
| 160 |
) if sol and sol != a else ""
|
| 161 |
|
| 162 |
cards.append(f"""
|
| 163 |
-
<div style="border:1px solid #e5e7eb;border-radius:12px;padding:18px;margin-bottom:
|
| 164 |
background:#ffffff;box-shadow:0 1px 4px rgba(0,0,0,.06);">
|
| 165 |
-
<div style="font-size:11px;color:#9ca3af;margin-bottom:10px">
|
| 166 |
-
#{i+1} ยท {name}
|
| 167 |
-
</div>
|
| 168 |
<div style="margin-bottom:10px">
|
| 169 |
<span style="font-weight:600;color:#111827">{label_q}:</span>
|
| 170 |
<div style="margin-top:4px;white-space:pre-wrap;font-size:14px">{q}</div>
|
|
@@ -181,26 +167,25 @@ def _render_preview(jsonl_path: str, lang: str = "zh") -> str:
|
|
| 181 |
with open(jsonl_path, encoding="utf-8") as f:
|
| 182 |
total = sum(1 for l in f if l.strip())
|
| 183 |
if total > 3:
|
| 184 |
-
more = f"๏ผๅ
ฑ {total} ๆก๏ผไป
ๅฑ็คบๅ 3 ๆก๏ผ" if lang == "zh" else f"
|
| 185 |
total_hint = f'<div style="font-size:12px;color:#6b7280;margin-bottom:10px">{more}</div>'
|
| 186 |
except Exception:
|
| 187 |
pass
|
| 188 |
|
|
|
|
| 189 |
return (
|
| 190 |
-
'<div style="
|
| 191 |
-
|
| 192 |
-
+
|
| 193 |
+ "</div>"
|
| 194 |
)
|
| 195 |
|
| 196 |
|
| 197 |
-
# โโ Backend (generator
|
| 198 |
|
| 199 |
def run_vqa_extraction(
|
| 200 |
pdf_files, task_name, api_url, llm_api_key, mineru_api_key, model_name, max_workers, lang,
|
| 201 |
):
|
| 202 |
-
t = T[lang]
|
| 203 |
-
|
| 204 |
if pdf_files is None or (isinstance(pdf_files, list) and len(pdf_files) == 0):
|
| 205 |
yield None, "โ ่ฏทๅ
ไธไผ PDF ๆไปถใ" if lang == "zh" else "โ Please upload a PDF file first.", ""
|
| 206 |
return
|
|
@@ -213,7 +198,7 @@ def run_vqa_extraction(
|
|
| 213 |
msg = (
|
| 214 |
"โ ่ฏทๅกซๅ MinerU API Key๏ผ็ฌ็ซไบ LLM Key๏ผๅป https://mineru.net/apiManage/token ็ณ่ฏท๏ผใ"
|
| 215 |
if lang == "zh" else
|
| 216 |
-
"โ Please enter your MinerU API Key (
|
| 217 |
)
|
| 218 |
yield None, msg, ""; return
|
| 219 |
|
|
@@ -229,8 +214,7 @@ def run_vqa_extraction(
|
|
| 229 |
try:
|
| 230 |
os.chdir(workspace)
|
| 231 |
|
| 232 |
-
|
| 233 |
-
yield None, step1, ""
|
| 234 |
|
| 235 |
if not isinstance(pdf_files, list):
|
| 236 |
pdf_files = [pdf_files]
|
|
@@ -249,12 +233,11 @@ def run_vqa_extraction(
|
|
| 249 |
}
|
| 250 |
fout.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
| 251 |
|
| 252 |
-
|
| 253 |
-
yield None, step2, ""
|
| 254 |
try:
|
| 255 |
from pipelines.vqa_extract_optimized_pipeline import PDF_VQA_extract_optimized_pipeline
|
| 256 |
except Exception:
|
| 257 |
-
err = f"โ
|
| 258 |
yield None, err, ""; return
|
| 259 |
|
| 260 |
try:
|
|
@@ -275,8 +258,11 @@ def run_vqa_extraction(
|
|
| 275 |
yield None, f"โ {msg}", ""
|
| 276 |
return
|
| 277 |
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
try:
|
| 282 |
pipeline.forward()
|
|
@@ -284,22 +270,21 @@ def run_vqa_extraction(
|
|
| 284 |
msg = str(e)
|
| 285 |
if "no api found" in msg.lower() or "Apply upload urls failed" in msg:
|
| 286 |
err = (
|
| 287 |
-
"โ MinerU API Key
|
| 288 |
-
if lang == "
|
| 289 |
-
"โ MinerU API Key
|
| 290 |
)
|
| 291 |
elif "Cannot connect to LLM server" in msg:
|
| 292 |
-
err = ("โ
|
| 293 |
else:
|
| 294 |
err = f"โ {msg}"
|
| 295 |
yield None, err, ""; return
|
| 296 |
|
| 297 |
-
|
| 298 |
-
yield None, step4, ""
|
| 299 |
|
| 300 |
step_files = [f for f in os.listdir(cache_dir) if re.match(r"vqa_step\d+\.jsonl", f)]
|
| 301 |
if not step_files:
|
| 302 |
-
msg = "โ Pipeline
|
| 303 |
yield None, msg, ""; return
|
| 304 |
|
| 305 |
max_step = max(int(re.findall(r"vqa_step(\d+)\.jsonl", f)[0]) for f in step_files)
|
|
@@ -319,12 +304,11 @@ def run_vqa_extraction(
|
|
| 319 |
f_out.write(json.dumps(out, ensure_ascii=False) + "\n")
|
| 320 |
count += 1
|
| 321 |
|
| 322 |
-
done = f"โ
|
| 323 |
-
|
| 324 |
-
yield result_file, done, preview_html
|
| 325 |
|
| 326 |
except Exception:
|
| 327 |
-
yield None, f"โ
|
| 328 |
finally:
|
| 329 |
os.chdir(original_cwd)
|
| 330 |
|
|
@@ -336,90 +320,95 @@ CSS = """
|
|
| 336 |
.example-btn { margin: 4px 0 !important; }
|
| 337 |
"""
|
| 338 |
|
|
|
|
|
|
|
| 339 |
with gr.Blocks(
|
| 340 |
title="FlipVQA-Miner",
|
| 341 |
theme=gr.themes.Soft(primary_hue="blue", neutral_hue="slate"),
|
| 342 |
css=CSS,
|
| 343 |
) as demo:
|
| 344 |
|
| 345 |
-
lang_state = gr.State(
|
| 346 |
|
| 347 |
# โโ Header โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 348 |
with gr.Row(elem_id="title-row"):
|
| 349 |
with gr.Column(scale=5):
|
| 350 |
gr.Markdown("# FlipVQA-Miner: Multimodal Knowledge Extraction")
|
| 351 |
with gr.Column(scale=0, min_width=110):
|
| 352 |
-
lang_btn = gr.Button(T[
|
| 353 |
|
| 354 |
-
subtitle_md = gr.Markdown(T[
|
| 355 |
-
desc_md = gr.Markdown(T[
|
| 356 |
|
| 357 |
# โโ Main layout โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 358 |
with gr.Row():
|
| 359 |
# โโ Left column: inputs โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 360 |
with gr.Column(scale=1):
|
| 361 |
-
|
|
|
|
|
|
|
| 362 |
pdf_files = gr.File(
|
| 363 |
-
label=T[
|
| 364 |
file_types=[".pdf"],
|
| 365 |
file_count="multiple",
|
| 366 |
)
|
| 367 |
-
task_name = gr.Textbox(label=T[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
-
|
|
|
|
| 370 |
api_url = gr.Textbox(
|
| 371 |
-
label=T[
|
| 372 |
value="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 373 |
)
|
| 374 |
llm_api_key = gr.Textbox(
|
| 375 |
-
label=T[
|
| 376 |
type="password",
|
| 377 |
-
placeholder=T[
|
| 378 |
)
|
| 379 |
model_name = gr.Textbox(
|
| 380 |
-
label=T[
|
| 381 |
value="gemini-2.5-pro",
|
| 382 |
-
placeholder=T[
|
| 383 |
)
|
| 384 |
|
| 385 |
-
|
|
|
|
| 386 |
mineru_api_key = gr.Textbox(
|
| 387 |
-
label=T[
|
| 388 |
type="password",
|
| 389 |
placeholder="sk2-...",
|
| 390 |
-
info=T[
|
| 391 |
)
|
| 392 |
-
max_workers = gr.Slider(label=T[
|
| 393 |
-
|
| 394 |
-
with gr.Row():
|
| 395 |
-
run_btn = gr.Button(T["zh"]["run_btn"], variant="primary", scale=4)
|
| 396 |
-
stop_btn = gr.Button(T["zh"]["stop_btn"], variant="stop", scale=1)
|
| 397 |
|
| 398 |
-
# โโ Example PDFs (below upload) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 399 |
-
sec_examples_md = gr.Markdown(f"### {T['zh']['sec_examples']}")
|
| 400 |
with gr.Row():
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
pdf_preview_md = gr.Markdown(f"#### {T['zh']['pdf_preview_label']}")
|
| 405 |
-
pdf_preview = gr.HTML(value="<p style='color:#9ca3af;font-size:13px'>็นๅปไธๆน็คบไพๆ้ฎ้ข่ง PDF</p>")
|
| 406 |
|
| 407 |
# โโ Right column: outputs โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 408 |
with gr.Column(scale=1):
|
| 409 |
-
sec_output_md = gr.Markdown(f"### {T[
|
| 410 |
status_box = gr.Textbox(
|
| 411 |
-
label=T[
|
| 412 |
interactive=False,
|
| 413 |
lines=6,
|
| 414 |
-
placeholder=T[
|
| 415 |
)
|
| 416 |
-
output_file = gr.File(label=T[
|
| 417 |
|
| 418 |
-
preview_md = gr.Markdown(f"#### {T[
|
| 419 |
preview_box = gr.HTML(value="")
|
| 420 |
|
| 421 |
-
key_note_md = gr.Markdown(T["zh"]["key_note"])
|
| 422 |
-
|
| 423 |
# โโ Event: Run โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 424 |
run_event = run_btn.click(
|
| 425 |
fn=run_vqa_extraction,
|
|
@@ -432,26 +421,17 @@ with gr.Blocks(
|
|
| 432 |
|
| 433 |
# โโ Event: Example buttons โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 434 |
def load_example(ex_index, lang):
|
| 435 |
-
paths
|
| 436 |
-
|
| 437 |
iframes = "".join(_pdf_to_iframe(p) for p in EXAMPLES[ex_index])
|
| 438 |
-
label = T[lang]["ex1_label"] if ex_index == 0 else T[lang]["ex2_label"]
|
| 439 |
html = (
|
| 440 |
-
f'<div style="font-size:12px;color:#6b7280;margin-bottom:
|
| 441 |
+ iframes
|
| 442 |
)
|
| 443 |
return paths, html
|
| 444 |
|
| 445 |
-
ex1_btn.click(
|
| 446 |
-
|
| 447 |
-
inputs=[lang_state],
|
| 448 |
-
outputs=[pdf_files, pdf_preview],
|
| 449 |
-
)
|
| 450 |
-
ex2_btn.click(
|
| 451 |
-
fn=lambda lang: load_example(1, lang),
|
| 452 |
-
inputs=[lang_state],
|
| 453 |
-
outputs=[pdf_files, pdf_preview],
|
| 454 |
-
)
|
| 455 |
|
| 456 |
# โโ Event: Language toggle โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 457 |
def toggle_lang(current_lang):
|
|
@@ -465,6 +445,10 @@ with gr.Blocks(
|
|
| 465 |
gr.update(value=f"### {t['sec_upload']}"),
|
| 466 |
gr.update(label=t["upload_label"]),
|
| 467 |
gr.update(label=t["task_label"]),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
gr.update(value=f"### {t['sec_llm']}"),
|
| 469 |
gr.update(label=t["api_url_label"]),
|
| 470 |
gr.update(label=t["llm_key_label"], placeholder=t["llm_key_ph"]),
|
|
@@ -477,12 +461,7 @@ with gr.Blocks(
|
|
| 477 |
gr.update(value=f"### {t['sec_output']}"),
|
| 478 |
gr.update(label=t["status_label"], placeholder=t["status_ph"]),
|
| 479 |
gr.update(label=t["output_label"]),
|
| 480 |
-
gr.update(value=f"### {t['sec_examples']}"),
|
| 481 |
-
gr.update(value=t["ex1_label"]),
|
| 482 |
-
gr.update(value=t["ex2_label"]),
|
| 483 |
-
gr.update(value=f"#### {t['pdf_preview_label']}"),
|
| 484 |
gr.update(value=f"#### {t['preview_label']}"),
|
| 485 |
-
gr.update(value=t["key_note"]),
|
| 486 |
)
|
| 487 |
|
| 488 |
lang_btn.click(
|
|
@@ -492,12 +471,12 @@ with gr.Blocks(
|
|
| 492 |
lang_state, lang_btn,
|
| 493 |
subtitle_md, desc_md,
|
| 494 |
sec_upload_md, pdf_files, task_name,
|
|
|
|
| 495 |
sec_llm_md, api_url, llm_api_key, model_name,
|
| 496 |
sec_mineru_md, mineru_api_key, max_workers,
|
| 497 |
run_btn, stop_btn,
|
| 498 |
sec_output_md, status_box, output_file,
|
| 499 |
-
|
| 500 |
-
preview_md, key_note_md,
|
| 501 |
],
|
| 502 |
)
|
| 503 |
|
|
|
|
| 28 |
sec_upload="๐ ไธไผ PDF",
|
| 29 |
upload_label="PDF ๆไปถ๏ผๅๆไปถ๏ผ้ข็ญๆททๆ๏ผๅๆไปถ๏ผ็ฌฌ1ไธช้ข็ฎ๏ผ็ฌฌ2ไธช็ญๆก๏ผ",
|
| 30 |
task_label="ไปปๅกๅ็งฐ",
|
| 31 |
+
sec_examples="๐ ๅ
็ฝฎ็คบไพ PDF๏ผ็นๅปๅ ่ฝฝๅนถ้ข่ง๏ผ",
|
| 32 |
+
ex1_label="็คบไพ 1๏ผๅๆไปถ้ข็ญๆททๆ",
|
| 33 |
+
ex2_label="็คบไพ 2๏ผๅๆไปถ๏ผ้ข็ฎ + ็ญๆก๏ผ",
|
| 34 |
+
pdf_preview_label="PDF ้ข่ง",
|
| 35 |
sec_llm="โ๏ธ LLM ้
็ฝฎ",
|
| 36 |
api_url_label="API Base URL",
|
| 37 |
llm_key_label="LLM API Key๏ผDF_API_KEY๏ผ",
|
|
|
|
| 48 |
status_label="่ฟ่ก็ถๆ",
|
| 49 |
status_ph="็นๅปใๅผๅงๆๅใๅ่ฟๅบฆๆพ็คบๅจ่ฟ้๏ผ่ฟ่ก้ๆฐๅ้๏ผ่ฏท่ๅฟ็ญๅพ
๏ผโฆ",
|
| 50 |
output_label="ไธ่ฝฝ็ปๆ๏ผraw_vqa.jsonl๏ผ",
|
| 51 |
+
preview_label="็ปๆ้ข่ง",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
),
|
| 53 |
"en": dict(
|
| 54 |
lang_btn="ไธญๆ",
|
|
|
|
| 62 |
sec_upload="๐ Upload PDF",
|
| 63 |
upload_label="PDF File(s) โ single: Q&A interleaved; two files: 1st questions, 2nd answers",
|
| 64 |
task_label="Task Name",
|
| 65 |
+
sec_examples="๐ Example PDFs (click to load & preview)",
|
| 66 |
+
ex1_label="Example 1: Single file (Q&A mixed)",
|
| 67 |
+
ex2_label="Example 2: Two files (questions + answers)",
|
| 68 |
+
pdf_preview_label="PDF Preview",
|
| 69 |
sec_llm="โ๏ธ LLM Configuration",
|
| 70 |
api_url_label="API Base URL",
|
| 71 |
llm_key_label="LLM API Key (DF_API_KEY)",
|
|
|
|
| 82 |
status_label="Status",
|
| 83 |
status_ph="Click 'Start Extraction' to begin (may take several minutes)โฆ",
|
| 84 |
output_label="Download Result (raw_vqa.jsonl)",
|
| 85 |
+
preview_label="Result Preview",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
),
|
| 87 |
}
|
| 88 |
|
| 89 |
+
_DEFAULT_LANG = "en"
|
| 90 |
+
|
| 91 |
EXAMPLES = [
|
| 92 |
("examples/VQA/questionextract_test.pdf",),
|
| 93 |
("examples/VQA/math_question.pdf", "examples/VQA/math_answer.pdf"),
|
|
|
|
| 97 |
# โโ Helpers โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 98 |
|
| 99 |
def _pdf_to_iframe(path: str) -> str:
|
|
|
|
| 100 |
try:
|
| 101 |
with open(os.path.join(_REPO_ROOT, path), "rb") as f:
|
| 102 |
b64 = base64.b64encode(f.read()).decode()
|
| 103 |
return (
|
| 104 |
f'<iframe src="data:application/pdf;base64,{b64}" '
|
| 105 |
f'width="100%" height="520px" '
|
| 106 |
+
f'style="border:1px solid #d1d5db;border-radius:10px;display:block;margin-bottom:8px;"></iframe>'
|
| 107 |
)
|
| 108 |
except Exception as e:
|
| 109 |
+
return f"<p style='color:red'>Cannot preview PDF: {e}</p>"
|
| 110 |
|
| 111 |
|
| 112 |
+
def _render_preview(jsonl_path: str, lang: str = "en") -> str:
|
|
|
|
| 113 |
if not jsonl_path or not os.path.exists(jsonl_path):
|
| 114 |
return ""
|
| 115 |
items = []
|
|
|
|
| 127 |
|
| 128 |
if not items:
|
| 129 |
label_empty = "๏ผๆ QA ๆฐๆฎ๏ผ" if lang == "zh" else "(No QA data)"
|
| 130 |
+
return f'<div style="padding:16px;color:#666">{label_empty}</div>'
|
| 131 |
|
| 132 |
label_q = "้ข็ฎ" if lang == "zh" else "Question"
|
| 133 |
label_a = "็ญๆก" if lang == "zh" else "Answer"
|
|
|
|
| 141 |
name = item.get("name", "")
|
| 142 |
sol_short = (sol[:300] + "โฆ") if len(sol) > 300 else sol
|
| 143 |
sol_block = (
|
| 144 |
+
f'<div style="margin-top:10px">'
|
| 145 |
f'<span style="font-weight:600;color:#374151">{label_s}:</span>'
|
| 146 |
f'<div style="margin-top:4px;white-space:pre-wrap;font-size:13px;color:#4b5563">{sol_short}</div>'
|
| 147 |
f'</div>'
|
| 148 |
) if sol and sol != a else ""
|
| 149 |
|
| 150 |
cards.append(f"""
|
| 151 |
+
<div style="border:1px solid #e5e7eb;border-radius:12px;padding:18px;margin-bottom:12px;
|
| 152 |
background:#ffffff;box-shadow:0 1px 4px rgba(0,0,0,.06);">
|
| 153 |
+
<div style="font-size:11px;color:#9ca3af;margin-bottom:10px">#{i+1} ยท {name}</div>
|
|
|
|
|
|
|
| 154 |
<div style="margin-bottom:10px">
|
| 155 |
<span style="font-weight:600;color:#111827">{label_q}:</span>
|
| 156 |
<div style="margin-top:4px;white-space:pre-wrap;font-size:14px">{q}</div>
|
|
|
|
| 167 |
with open(jsonl_path, encoding="utf-8") as f:
|
| 168 |
total = sum(1 for l in f if l.strip())
|
| 169 |
if total > 3:
|
| 170 |
+
more = f"๏ผๅ
ฑ {total} ๆก๏ผไป
ๅฑ็คบๅ 3 ๆก๏ผ" if lang == "zh" else f"{total} items total โ showing first 3"
|
| 171 |
total_hint = f'<div style="font-size:12px;color:#6b7280;margin-bottom:10px">{more}</div>'
|
| 172 |
except Exception:
|
| 173 |
pass
|
| 174 |
|
| 175 |
+
inner = total_hint + "".join(cards)
|
| 176 |
return (
|
| 177 |
+
'<div style="background:#f9fafb;border:1px solid #e5e7eb;border-radius:12px;'
|
| 178 |
+
'padding:16px;max-height:580px;overflow-y:auto;">'
|
| 179 |
+
+ inner
|
| 180 |
+ "</div>"
|
| 181 |
)
|
| 182 |
|
| 183 |
|
| 184 |
+
# โโ Backend (generator โ stop button works) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 185 |
|
| 186 |
def run_vqa_extraction(
|
| 187 |
pdf_files, task_name, api_url, llm_api_key, mineru_api_key, model_name, max_workers, lang,
|
| 188 |
):
|
|
|
|
|
|
|
| 189 |
if pdf_files is None or (isinstance(pdf_files, list) and len(pdf_files) == 0):
|
| 190 |
yield None, "โ ่ฏทๅ
ไธไผ PDF ๆไปถใ" if lang == "zh" else "โ Please upload a PDF file first.", ""
|
| 191 |
return
|
|
|
|
| 198 |
msg = (
|
| 199 |
"โ ่ฏทๅกซๅ MinerU API Key๏ผ็ฌ็ซไบ LLM Key๏ผๅป https://mineru.net/apiManage/token ็ณ่ฏท๏ผใ"
|
| 200 |
if lang == "zh" else
|
| 201 |
+
"โ Please enter your MinerU API Key (get it at https://mineru.net/apiManage/token)."
|
| 202 |
)
|
| 203 |
yield None, msg, ""; return
|
| 204 |
|
|
|
|
| 214 |
try:
|
| 215 |
os.chdir(workspace)
|
| 216 |
|
| 217 |
+
yield None, "โณ [1/4] Preparing PDF filesโฆ" if lang == "en" else "โณ [1/4] ๆด็ PDF ๆไปถโฆ", ""
|
|
|
|
| 218 |
|
| 219 |
if not isinstance(pdf_files, list):
|
| 220 |
pdf_files = [pdf_files]
|
|
|
|
| 233 |
}
|
| 234 |
fout.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
| 235 |
|
| 236 |
+
yield None, "โณ [2/4] Loading pipeline moduleโฆ" if lang == "en" else "โณ [2/4] ๅ ่ฝฝ Pipeline ๆจกๅโฆ", ""
|
|
|
|
| 237 |
try:
|
| 238 |
from pipelines.vqa_extract_optimized_pipeline import PDF_VQA_extract_optimized_pipeline
|
| 239 |
except Exception:
|
| 240 |
+
err = f"โ Failed to import pipeline:\n{traceback.format_exc()}"
|
| 241 |
yield None, err, ""; return
|
| 242 |
|
| 243 |
try:
|
|
|
|
| 258 |
yield None, f"โ {msg}", ""
|
| 259 |
return
|
| 260 |
|
| 261 |
+
yield None, (
|
| 262 |
+
"โณ [3/4] MinerU parsing + LLM QA extraction (may take several minutes)โฆ"
|
| 263 |
+
if lang == "en" else
|
| 264 |
+
"โณ [3/4] MinerU ่งฃๆ PDF + LLM ๆๅ QA๏ผๅฏ่ฝ้่ฆๆฐๅ้๏ผโฆ"
|
| 265 |
+
), ""
|
| 266 |
|
| 267 |
try:
|
| 268 |
pipeline.forward()
|
|
|
|
| 270 |
msg = str(e)
|
| 271 |
if "no api found" in msg.lower() or "Apply upload urls failed" in msg:
|
| 272 |
err = (
|
| 273 |
+
"โ MinerU API Key invalid or expired. Get a new one at https://mineru.net/apiManage/token\n\n" + msg
|
| 274 |
+
if lang == "en" else
|
| 275 |
+
"โ MinerU API Key ๆ ๆๆๅทฒ่ฟๆใ่ฏทๅฐ https://mineru.net/apiManage/token ้ๆฐ็ณ่ฏทใ\n\n" + msg
|
| 276 |
)
|
| 277 |
elif "Cannot connect to LLM server" in msg:
|
| 278 |
+
err = ("โ Cannot connect to LLM API. Check Base URL.\n\n" if lang == "en" else "โ ๆ ๆณ่ฟๆฅ LLM API๏ผ่ฏทๆฃๆฅ Base URLใ\n\n") + msg
|
| 279 |
else:
|
| 280 |
err = f"โ {msg}"
|
| 281 |
yield None, err, ""; return
|
| 282 |
|
| 283 |
+
yield None, "โณ [4/4] Collecting outputโฆ" if lang == "en" else "โณ [4/4] ๆด็่พๅบ็ปๆโฆ", ""
|
|
|
|
| 284 |
|
| 285 |
step_files = [f for f in os.listdir(cache_dir) if re.match(r"vqa_step\d+\.jsonl", f)]
|
| 286 |
if not step_files:
|
| 287 |
+
msg = "โ Pipeline finished but no output file found." if lang == "en" else "โ Pipeline ๅฎๆไฝๆชๆพๅฐ่พๅบๆไปถใ"
|
| 288 |
yield None, msg, ""; return
|
| 289 |
|
| 290 |
max_step = max(int(re.findall(r"vqa_step(\d+)\.jsonl", f)[0]) for f in step_files)
|
|
|
|
| 304 |
f_out.write(json.dumps(out, ensure_ascii=False) + "\n")
|
| 305 |
count += 1
|
| 306 |
|
| 307 |
+
done = f"โ
Done! Extracted {count} QA pairs." if lang == "en" else f"โ
ๅฎๆ๏ผๅ
ฑๆๅ {count} ๆก QA ๅฏนใ"
|
| 308 |
+
yield result_file, done, _render_preview(result_file, lang)
|
|
|
|
| 309 |
|
| 310 |
except Exception:
|
| 311 |
+
yield None, f"โ Unexpected error:\n{traceback.format_exc()}", ""
|
| 312 |
finally:
|
| 313 |
os.chdir(original_cwd)
|
| 314 |
|
|
|
|
| 320 |
.example-btn { margin: 4px 0 !important; }
|
| 321 |
"""
|
| 322 |
|
| 323 |
+
_L = _DEFAULT_LANG # shorthand for initial render
|
| 324 |
+
|
| 325 |
with gr.Blocks(
|
| 326 |
title="FlipVQA-Miner",
|
| 327 |
theme=gr.themes.Soft(primary_hue="blue", neutral_hue="slate"),
|
| 328 |
css=CSS,
|
| 329 |
) as demo:
|
| 330 |
|
| 331 |
+
lang_state = gr.State(_DEFAULT_LANG)
|
| 332 |
|
| 333 |
# โโ Header โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 334 |
with gr.Row(elem_id="title-row"):
|
| 335 |
with gr.Column(scale=5):
|
| 336 |
gr.Markdown("# FlipVQA-Miner: Multimodal Knowledge Extraction")
|
| 337 |
with gr.Column(scale=0, min_width=110):
|
| 338 |
+
lang_btn = gr.Button(T[_L]["lang_btn"], elem_id="lang-btn", size="sm")
|
| 339 |
|
| 340 |
+
subtitle_md = gr.Markdown(T[_L]["subtitle"])
|
| 341 |
+
desc_md = gr.Markdown(T[_L]["desc"])
|
| 342 |
|
| 343 |
# โโ Main layout โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 344 |
with gr.Row():
|
| 345 |
# โโ Left column: inputs โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 346 |
with gr.Column(scale=1):
|
| 347 |
+
|
| 348 |
+
# 1. Upload PDF
|
| 349 |
+
sec_upload_md = gr.Markdown(f"### {T[_L]['sec_upload']}")
|
| 350 |
pdf_files = gr.File(
|
| 351 |
+
label=T[_L]["upload_label"],
|
| 352 |
file_types=[".pdf"],
|
| 353 |
file_count="multiple",
|
| 354 |
)
|
| 355 |
+
task_name = gr.Textbox(label=T[_L]["task_label"], value="task1")
|
| 356 |
+
|
| 357 |
+
# 2. Example PDFs (between upload and LLM config)
|
| 358 |
+
sec_examples_md = gr.Markdown(f"### {T[_L]['sec_examples']}")
|
| 359 |
+
with gr.Row():
|
| 360 |
+
ex1_btn = gr.Button(T[_L]["ex1_label"], elem_classes="example-btn", scale=1)
|
| 361 |
+
ex2_btn = gr.Button(T[_L]["ex2_label"], elem_classes="example-btn", scale=1)
|
| 362 |
+
pdf_preview_md = gr.Markdown(f"#### {T[_L]['pdf_preview_label']}")
|
| 363 |
+
pdf_preview = gr.HTML(
|
| 364 |
+
value="<p style='color:#9ca3af;font-size:13px;padding:8px 0'>Click an example button to preview a PDF here.</p>"
|
| 365 |
+
)
|
| 366 |
|
| 367 |
+
# 3. LLM config
|
| 368 |
+
sec_llm_md = gr.Markdown(f"### {T[_L]['sec_llm']}")
|
| 369 |
api_url = gr.Textbox(
|
| 370 |
+
label=T[_L]["api_url_label"],
|
| 371 |
value="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 372 |
)
|
| 373 |
llm_api_key = gr.Textbox(
|
| 374 |
+
label=T[_L]["llm_key_label"],
|
| 375 |
type="password",
|
| 376 |
+
placeholder=T[_L]["llm_key_ph"],
|
| 377 |
)
|
| 378 |
model_name = gr.Textbox(
|
| 379 |
+
label=T[_L]["model_label"],
|
| 380 |
value="gemini-2.5-pro",
|
| 381 |
+
placeholder=T[_L]["model_ph"],
|
| 382 |
)
|
| 383 |
|
| 384 |
+
# 4. MinerU config
|
| 385 |
+
sec_mineru_md = gr.Markdown(f"### {T[_L]['sec_mineru']}")
|
| 386 |
mineru_api_key = gr.Textbox(
|
| 387 |
+
label=T[_L]["mineru_key_label"],
|
| 388 |
type="password",
|
| 389 |
placeholder="sk2-...",
|
| 390 |
+
info=T[_L]["mineru_key_info"],
|
| 391 |
)
|
| 392 |
+
max_workers = gr.Slider(label=T[_L]["workers_label"], minimum=1, maximum=30, value=5, step=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
|
|
|
|
|
|
| 394 |
with gr.Row():
|
| 395 |
+
run_btn = gr.Button(T[_L]["run_btn"], variant="primary", scale=4)
|
| 396 |
+
stop_btn = gr.Button(T[_L]["stop_btn"], variant="stop", scale=1)
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
# โโ Right column: outputs โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 399 |
with gr.Column(scale=1):
|
| 400 |
+
sec_output_md = gr.Markdown(f"### {T[_L]['sec_output']}")
|
| 401 |
status_box = gr.Textbox(
|
| 402 |
+
label=T[_L]["status_label"],
|
| 403 |
interactive=False,
|
| 404 |
lines=6,
|
| 405 |
+
placeholder=T[_L]["status_ph"],
|
| 406 |
)
|
| 407 |
+
output_file = gr.File(label=T[_L]["output_label"], interactive=False)
|
| 408 |
|
| 409 |
+
preview_md = gr.Markdown(f"#### {T[_L]['preview_label']}")
|
| 410 |
preview_box = gr.HTML(value="")
|
| 411 |
|
|
|
|
|
|
|
| 412 |
# โโ Event: Run โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 413 |
run_event = run_btn.click(
|
| 414 |
fn=run_vqa_extraction,
|
|
|
|
| 421 |
|
| 422 |
# โโ Event: Example buttons โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 423 |
def load_example(ex_index, lang):
|
| 424 |
+
paths = [os.path.join(_REPO_ROOT, p) for p in EXAMPLES[ex_index]]
|
| 425 |
+
label = T[lang]["ex1_label"] if ex_index == 0 else T[lang]["ex2_label"]
|
| 426 |
iframes = "".join(_pdf_to_iframe(p) for p in EXAMPLES[ex_index])
|
|
|
|
| 427 |
html = (
|
| 428 |
+
f'<div style="font-size:12px;color:#6b7280;margin-bottom:8px">{label}</div>'
|
| 429 |
+ iframes
|
| 430 |
)
|
| 431 |
return paths, html
|
| 432 |
|
| 433 |
+
ex1_btn.click(fn=lambda lang: load_example(0, lang), inputs=[lang_state], outputs=[pdf_files, pdf_preview])
|
| 434 |
+
ex2_btn.click(fn=lambda lang: load_example(1, lang), inputs=[lang_state], outputs=[pdf_files, pdf_preview])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
# โโ Event: Language toggle โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 437 |
def toggle_lang(current_lang):
|
|
|
|
| 445 |
gr.update(value=f"### {t['sec_upload']}"),
|
| 446 |
gr.update(label=t["upload_label"]),
|
| 447 |
gr.update(label=t["task_label"]),
|
| 448 |
+
gr.update(value=f"### {t['sec_examples']}"),
|
| 449 |
+
gr.update(value=t["ex1_label"]),
|
| 450 |
+
gr.update(value=t["ex2_label"]),
|
| 451 |
+
gr.update(value=f"#### {t['pdf_preview_label']}"),
|
| 452 |
gr.update(value=f"### {t['sec_llm']}"),
|
| 453 |
gr.update(label=t["api_url_label"]),
|
| 454 |
gr.update(label=t["llm_key_label"], placeholder=t["llm_key_ph"]),
|
|
|
|
| 461 |
gr.update(value=f"### {t['sec_output']}"),
|
| 462 |
gr.update(label=t["status_label"], placeholder=t["status_ph"]),
|
| 463 |
gr.update(label=t["output_label"]),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
gr.update(value=f"#### {t['preview_label']}"),
|
|
|
|
| 465 |
)
|
| 466 |
|
| 467 |
lang_btn.click(
|
|
|
|
| 471 |
lang_state, lang_btn,
|
| 472 |
subtitle_md, desc_md,
|
| 473 |
sec_upload_md, pdf_files, task_name,
|
| 474 |
+
sec_examples_md, ex1_btn, ex2_btn, pdf_preview_md,
|
| 475 |
sec_llm_md, api_url, llm_api_key, model_name,
|
| 476 |
sec_mineru_md, mineru_api_key, max_workers,
|
| 477 |
run_btn, stop_btn,
|
| 478 |
sec_output_md, status_box, output_file,
|
| 479 |
+
preview_md,
|
|
|
|
| 480 |
],
|
| 481 |
)
|
| 482 |
|