Spaces:
Sleeping
Sleeping
File size: 10,088 Bytes
179eccc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | > [!NOTE]
> ์ด ๋ฌธ์์๋ AI ์์ฑ ์ฝํ
์ธ ๊ฐ ํฌํจ๋ ์ ์์ต๋๋ค. ์ ํ์ฑ์ ์ํด ๋
ธ๋ ฅํ๊ณ ์์ง๋ง ๋ถ์ ํํ ๋ด์ฉ์ด ์์ ์ ์์ต๋๋ค. ๋ฌธ์ ๊ฐ ๋ฐ๊ฒฌ๋๋ฉด ๋ค์์ ํตํด ์ ๊ณ ํด ์ฃผ์ธ์:
>
> - [GitHub Issues](https://github.com/PDFMathTranslate-next/PDFMathTranslate-next/issues)
> - ์ปค๋ฎค๋ํฐ ๊ธฐ์ฌ (PR ํ์ํฉ๋๋ค!)
## Python API: do_translate_async_stream
### ๊ฐ์
- do_translate_async_stream ์ ๋จ์ผ PDF ๋ฅผ ๋ฒ์ญํ๊ณ ์ด๋ฒคํธ (์งํ ์ํฉ/์ค๋ฅ/์๋ฃ) ์คํธ๋ฆผ์ ์์ฑํ๋ ์ ์์ค ๋น๋๊ธฐ ์ง์
์ ์
๋๋ค.
- ์ค์๊ฐ ์งํ ์ํฉ๊ณผ ๊ฒฐ๊ณผ์ ๋ํ ์์ ํ ์ ์ด๋ฅผ ์ํ๋ ์ฌ์ฉ์ ์ ์ UI ๋ CLI ๋ฅผ ๊ตฌ์ถํ๋ ๋ฐ ์ ํฉํฉ๋๋ค.
- ์ ํจ์ฑ์ด ๊ฒ์ฆ๋ SettingsModel ๊ณผ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์๋ฝํ๊ณ , dict ์ด๋ฒคํธ์ ๋น๋๊ธฐ ์์ฑ๊ธฐ๋ฅผ ๋ฐํํฉ๋๋ค.
### ์๋ช
- Import: `from pdf2zh_next.high_level import do_translate_async_stream`
- Call: `async for event in do_translate_async_stream(settings, file): ...`
- Parameters:
- settings: SettingsModel. ์ ํจํด์ผ ํฉ๋๋ค; ํจ์๋ `settings.validate_settings()` ๋ฅผ ํธ์ถํฉ๋๋ค.
- file: str | pathlib.Path. ๋ฒ์ญํ ๋จ์ผ PDF ํ์ผ์
๋๋ค. ์กด์ฌํด์ผ ํฉ๋๋ค.
์ฐธ๊ณ :
- `settings.basic.input_files` ๋ ์ด ํจ์์์ ๋ฌด์๋ฉ๋๋ค; ์ฃผ์ด์ง `file` ๋ง ๋ฒ์ญ๋ฉ๋๋ค.
- `settings.basic.debug` ๊ฐ True ์ธ ๊ฒฝ์ฐ, ๋ฒ์ญ์ ๋ฉ์ธ ํ๋ก์ธ์ค์์ ์คํ๋ฉ๋๋ค; ๊ทธ๋ ์ง ์์ผ๋ฉด ํ์ ํ๋ก์ธ์ค์์ ์คํ๋ฉ๋๋ค. ๋ ๊ฒฝ์ฐ ๋ชจ๋ ์ด๋ฒคํธ ์คํค๋ง๋ ๋์ผํฉ๋๋ค.
### ์ด๋ฒคํธ ์คํธ๋ฆผ ๊ณ์ฝ
๋น๋๊ธฐ ์์ฑ์๋ ๋ค์๊ณผ ๊ฐ์ ์ ํ์ JSON ํ์ dict ์ด๋ฒคํธ๋ฅผ ์์ฑํฉ๋๋ค:
- ๋จ๊ณ ์์ฝ ์ด๋ฒคํธ: `stage_summary` (์ ํ ์ฌํญ, ์ฒ์ ๋ํ๋ ์ ์์)
- ํ๋
- `type`: "stage_summary"
- `stages`: ์์ ์์
๋ถํฌ๋ฅผ ์ค๋ช
ํ๋ ๊ฐ์ฒด `{ "name": str, "percent": float }`์ ๋ชฉ๋ก
- `part_index`: ์ด ์์ฝ ์ด๋ฒคํธ์ ๊ฒฝ์ฐ 0 ์ผ ์ ์์
- `total_parts`: ์ ์ฒด ๋ถ๋ถ ์ (>= 1)
- ์งํ ์ํฉ ์ด๋ฒคํธ: `progress_start`, `progress_update`, `progress_end`
- ๊ณตํต ํ๋
- `type`: ์ ์ค ํ๋
- `stage`: ์ฌ๋์ด ์ฝ์ ์ ์๋ ๋จ๊ณ ์ด๋ฆ (์: "PDF ๊ตฌ๋ฌธ ๋ถ์ ๋ฐ ์ค๊ฐ ํํ ์์ฑ", "๋จ๋ฝ ๋ฒ์ญ", "PDF ์ ์ฅ")
- `stage_progress`: ํ์ฌ ๋จ๊ณ ๋ด ์งํ๋ฅ ์ ๋ํ๋ด๋ [0, 100] ๋ฒ์์ float
- `overall_progress`: ์ ์ฒด ์งํ๋ฅ ์ ๋ํ๋ด๋ [0, 100] ๋ฒ์์ float
- `part_index`: ํ์ฌ ๋ถ๋ถ ์ธ๋ฑ์ค (์ผ๋ฐ์ ์ผ๋ก ์งํ ์ํฉ ์ด๋ฒคํธ์ ๊ฒฝ์ฐ 1 ๋ถํฐ ์์)
- `total_parts`: ์ ์ฒด ๋ถ๋ถ ์ (>= 1). ํฐ ๋ฌธ์๋ ์๋์ผ๋ก ๋ถํ ๋ ์ ์์.
- `stage_current`: ๋จ๊ณ ๋ด ํ์ฌ ๋จ๊ณ
- `stage_total`: ๋จ๊ณ ๋ด ์ ์ฒด ๋จ๊ณ ์
- ์๋ฃ ์ด๋ฒคํธ: `finish`
- ํ๋
- `type`: "finish"
- `translate_result`: ์ต์ข
์ถ๋ ฅ์ ์ ๊ณตํ๋ **๊ฐ์ฒด** (์ฐธ๊ณ : ์ฌ์ ์ด ์๋ ํด๋์ค ์ธ์คํด์ค)
- `original_pdf_path`: ์
๋ ฅ PDF ๊ฒฝ๋ก
- `mono_pdf_path`: ๋จ์ผ ์ธ์ด ๋ฒ์ญ PDF ๊ฒฝ๋ก (๋๋ None)
- `dual_pdf_path`: ์ด์ค ์ธ์ด ๋ฒ์ญ PDF ๊ฒฝ๋ก (๋๋ None)
- `no_watermark_mono_pdf_path`: ์ํฐ๋งํฌ๊ฐ ์๋ ๋จ์ผ ์ธ์ด ์ถ๋ ฅ ๊ฒฝ๋ก (์์ฑ๋ ๊ฒฝ์ฐ), ๊ทธ๋ ์ง ์์ผ๋ฉด None
- `no_watermark_dual_pdf_path`: ์ํฐ๋งํฌ๊ฐ ์๋ ์ด์ค ์ธ์ด ์ถ๋ ฅ ๊ฒฝ๋ก (์์ฑ๋ ๊ฒฝ์ฐ), ๊ทธ๋ ์ง ์์ผ๋ฉด None
- `auto_extracted_glossary_path`: ์๋ ์ถ์ถ ์ฉ์ด์ง CSV ๊ฒฝ๋ก (๋๋ None)
- `total_seconds`: ๊ฒฝ๊ณผ ์๊ฐ (์ด, float)
- `peak_memory_usage`: ๋ฒ์ญ ์ค ๋๋ต์ ์ธ ์ต๋ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ (float; ๊ตฌํ์ ๋ฐ๋ฅธ ๋จ์)
- ์ค๋ฅ ์ด๋ฒคํธ: `error`
- ํ๋
- `type`: "error"
- `error`: ์ฌ๋์ด ์ฝ์ ์ ์๋ ์ค๋ฅ ๋ฉ์์ง
- `error_type`: `BabeldocError`, `SubprocessError`, `IPCError`, `SubprocessCrashError` ๋ฑ ์ค ํ๋
- `details`: ์ ํ์ ์ธ๋ถ ์ ๋ณด (์: ์๋ณธ ์ค๋ฅ ๋๋ ํธ๋ ์ด์ค๋ฐฑ)
์ค์ํ ๋์:
- ์ ํ์ ์ผ๋ก `stage_summary` ๊ฐ ์งํ๋ฅ ์ด ์์๋๊ธฐ ์ ์ ๋ฐ์ํ ์ ์์ต๋๋ค.
- ํน์ ์คํจ ์, ์์ฑ๊ธฐ๋ ๋จผ์ `error` ์ด๋ฒคํธ๋ฅผ yield ํ ๋ค์ `TranslationError` ์์ ํ์๋ ์์ธ๋ฅผ ๋ฐ์์ํต๋๋ค. ์ค๋ฅ ์ด๋ฒคํธ๋ฅผ ํ์ธํ๊ณ ์์ธ๋ฅผ catch ํ ์ค๋น๋ฅผ ๋ชจ๋ ํด์ผ ํฉ๋๋ค.
- `progress_update` ์ด๋ฒคํธ๋ ๋์ผํ ๊ฐ์ผ๋ก ๋ฐ๋ณต๋ ์ ์์ต๋๋ค; ํ์ํ๋ค๋ฉด ์๋น์๋ ๋๋ฐ์ด์ฑ์ ํด์ผ ํฉ๋๋ค.
- `finish` ์ด๋ฒคํธ๋ฅผ ์์ ํ๋ฉด ์คํธ๋ฆผ ์๋น๋ฅผ ์ค์งํ์ธ์.
### ์ต์ ์ฌ์ฉ ์์ (๋น๋๊ธฐ)
```python
import asyncio
from pathlib import Path
from pdf2zh_next.high_level import do_translate_async_stream
# Assume you already have a valid SettingsModel instance named `settings`
# and a PDF file path
async def translate_one(settings, pdf_path: str | Path):
try:
async for event in do_translate_async_stream(settings, pdf_path):
etype = event.get("type")
if etype == "stage_summary":
# Optional pre-flight summary of stages
stages = event.get("stages", [])
print("Stage summary:", ", ".join(f"{s['name']}:{s['percent']:.2f}" for s in stages))
elif etype in {"progress_start", "progress_update", "progress_end"}:
stage = event.get("stage")
stage_prog = event.get("stage_progress") # 0..100
overall = event.get("overall_progress") # 0..100
part_i = event.get("part_index")
part_n = event.get("total_parts")
print(f"[{etype}] {stage} | stage {stage_prog:.1f}% | overall {overall:.1f}% (part {part_i}/{part_n})")
elif etype == "error":
# You will also get a raised exception after this yield
print("[error]", event.get("error"), event.get("error_type"))
elif etype == "finish":
result = event["translate_result"]
print("Done in", getattr(result, "total_seconds", None), "s")
print("Mono:", getattr(result, "mono_pdf_path", None))
print("Dual:", getattr(result, "dual_pdf_path", None))
print("No-watermark Mono:", getattr(result, "no_watermark_mono_pdf_path", None))
print("No-watermark Dual:", getattr(result, "no_watermark_dual_pdf_path", None))
print("Glossary:", getattr(result, "auto_extracted_glossary_path", None))
print("Peak memory:", getattr(result, "peak_memory_usage", None))
break
except Exception as exc:
# Catch exceptions raised by the stream after an error event
print("Translation failed:", exc)
# asyncio.run(translate_one(settings, "/path/to/file.pdf"))
```
### ์ทจ์
์์
์ ์ทจ์ํ๋ฉด ์คํธ๋ฆผ์ ์๋นํ๋ ์์
์ด ์ทจ์๋ฉ๋๋ค. ์ทจ์๋ ๊ธฐ๋ณธ ๋ฒ์ญ ํ๋ก์ธ์ค๋ก ์ ํ๋ฉ๋๋ค.
```python
import asyncio
from pdf2zh_next.high_level import do_translate_async_stream
async def cancellable(settings, pdf):
task = asyncio.create_task(_consume(settings, pdf))
await asyncio.sleep(1.0) # let it start
task.cancel()
try:
await task
except asyncio.CancelledError:
print("Cancelled")
async def _consume(settings, pdf):
async for event in do_translate_async_stream(settings, pdf):
if event["type"] == "finish":
break
```
### ์ด๋ฒคํธ ๋ชจ์ ์์
๋จ๊ณ ์์ฝ ์ด๋ฒคํธ (์์):
```json
{
"type": "stage_summary",
"stages": [
{"name": "Parse PDF and Create Intermediate Representation", "percent": 0.1086},
{"name": "DetectScannedFile", "percent": 0.0188},
{"name": "Parse Page Layout", "percent": 0.1079}
// ... more stages ...
],
"part_index": 0,
"total_parts": 1
}
```
Progress event (์์):
```json
{
"type": "progress_update",
"stage": "Translate Paragraphs",
"stage_progress": 2.04,
"stage_current": 1,
"stage_total": 49,
"overall_progress": 53.44,
"part_index": 1,
"total_parts": 1
}
```
Finish event (์์):
```json
{
"type": "finish",
"translate_result": {
"original_pdf_path": "pdf2zh_files/<session>/table.pdf",
"mono_pdf_path": "pdf2zh_files/<session>/table.zh-CN.mono.pdf",
"dual_pdf_path": "pdf2zh_files/<session>/table.zh-CN.dual.pdf",
"no_watermark_mono_pdf_path": "pdf2zh_files/<session>/table.no_watermark.zh-CN.mono.pdf",
"no_watermark_dual_pdf_path": "pdf2zh_files/<session>/table.no_watermark.zh-CN.dual.pdf",
"auto_extracted_glossary_path": "pdf2zh_files/<session>/table.zh-CN.glossary.csv",
"total_seconds": 42.83,
"peak_memory_usage": 4651.55
}
}
```
Error event (example):
```json
{
"type": "error",
"error": "Babeldoc translation error: <message>",
"error_type": "BabeldocError",
"details": "<optional original error or traceback>"
}
```
### ์ฐธ๊ณ ์ฌํญ ๋ฐ ๋ชจ๋ฒ ์ฌ๋ก
- ํญ์ ์์ฑ๊ธฐ์์ ๋ฐ์ํ๋ ์ค๋ฅ ์ด๋ฒคํธ์ ์์ธ๋ฅผ ๋ชจ๋ ์ฒ๋ฆฌํ์ญ์์ค.
- ๋ถํ์ํ ์์
์ ํผํ๊ธฐ ์ํด `finish` ์ด๋ฒคํธ์์ ๋ฃจํ๋ฅผ ์ค๋จํ์ญ์์ค.
- ํธ์ถํ๊ธฐ ์ ์ `file` ์ด ์กด์ฌํ๊ณ `settings.validate_settings()` ๊ฐ ํต๊ณผํ๋์ง ํ์ธํ์ญ์์ค.
- ๋ํ ๋ฌธ์๋ ๋ถํ ๋ ์ ์์ต๋๋ค. `part_index/total_parts` ์ `overall_progress` ๋ฅผ ์ฌ์ฉํ์ฌ UI ๋ฅผ ๊ตฌ๋ํ์ญ์์ค.
- UI ๊ฐ ๋ฐ๋ณต์ ์ด๊ณ ๋์ผํ ์
๋ฐ์ดํธ์ ๋ฏผ๊ฐํ ๊ฒฝ์ฐ `progress_update` ์ด๋ฒคํธ๋ฅผ ๋๋ฐ์ด์คํ์ญ์์ค.
- `report_interval` (SettingsModel): `progress_update` ์ด๋ฒคํธ์ ๋ฐ์ ๋น๋๋ง ์ ์ดํฉ๋๋ค. `stage_summary`, `progress_start`, `progress_end` ๋๋ `finish` ์๋ ์ํฅ์ ๋ฏธ์น์ง ์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ์ 0.1 ์ด์ด๋ฉฐ ํ์ฉ๋๋ ์ต์๊ฐ์ 0.05 ์ด์
๋๋ค. ์งํ๋ฅ ๋ชจ๋ํฐ ๋ก์ง์ ๋ฐ๋ผ, `stage_total <= 3`์ผ ๋ ์
๋ฐ์ดํธ๋ `report_interval` ์ ์ํด ์ ํ๋์ง ์์ต๋๋ค.
<div align="right">
<h6><small>์ด ํ์ด์ง์ ์ผ๋ถ ๋ด์ฉ์ GPT ์ ์ํด ๋ฒ์ญ๋์์ผ๋ฉฐ ์ค๋ฅ๊ฐ ํฌํจ๋ ์ ์์ต๋๋ค.</small></h6> |