fix: 文档翻译一直转圈 — relay分块并发翻译(8线程, 大文档从几分钟缩到几十秒), 前端fetch加8分钟超时+明确超时提示, nginx read/send timeout 300→600s
This commit is contained in:
1 parent
17c71d4259
commit
8538520548
3 files changed
+31
-7
No files matched your search
@@ -1724,13 +1724,22 @@ class Handler(BaseHTTPRequestHandler):
|
||||
cur = cur + "\n" + para if cur else para
|
||||
if cur:
|
||||
chunks.append(cur)
|
||||
translated = []
|
||||
for i, ch in enumerate(chunks):
|
||||
# 并发翻译分块(大文档不再串行等待,几十块也能几十秒完成)
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
def _tr(item):
|
||||
idx, ch = item
|
||||
out = self._llm_chat([
|
||||
{"role": "system", "content": f"你是专业文档翻译。把用户提供的文档片段翻译成{to_lang}:保留原有换行、段落结构、编号和列表格式;专有名词、数字、日期原样保留;只输出译文,不要解释。"},
|
||||
{"role": "user", "content": ch},
|
||||
])
|
||||
translated.append(out.strip())
|
||||
return idx, out.strip()
|
||||
|
||||
workers = min(8, max(1, len(chunks)))
|
||||
with ThreadPoolExecutor(max_workers=workers) as ex:
|
||||
results = list(ex.map(_tr, list(enumerate(chunks))))
|
||||
results.sort(key=lambda x: x[0])
|
||||
translated = [t for _, t in results]
|
||||
result_text = "\n\n".join(translated)
|
||||
shutil.rmtree(wd, ignore_errors=True)
|
||||
log_usage(machine, "translator")
|
||||
|
||||
+2
-1
@@ -22,7 +22,8 @@ server {
|
||||
client_max_body_size 2g;
|
||||
proxy_pass http://it-tools-relay:8091/;
|
||||
proxy_connect_timeout 5s;
|
||||
proxy_read_timeout 300s;
|
||||
proxy_read_timeout 600s;
|
||||
proxy_send_timeout 600s;
|
||||
}
|
||||
|
||||
# AI retouch (iopaint container on host) - protected
|
||||
|
||||
@@ -173,7 +173,16 @@ async function translateDoc() {
|
||||
const fd = new FormData();
|
||||
fd.append('file', docFile.value);
|
||||
fd.append('to', langName(docTo.value));
|
||||
const res = await fetch('/relay/translate-doc', { method: 'POST', body: fd, headers: licenseHeaders('translator') });
|
||||
// 前端超时保护:大文档超过 8 分钟视为失败,避免一直转圈
|
||||
const ctrl = new AbortController();
|
||||
const timer = setTimeout(() => ctrl.abort(), 8 * 60 * 1000);
|
||||
let res: Response;
|
||||
try {
|
||||
res = await fetch('/relay/translate-doc', { method: 'POST', body: fd, headers: licenseHeaders('translator'), signal: ctrl.signal });
|
||||
}
|
||||
finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
const j = await res.json().catch(() => ({}));
|
||||
if (!res.ok || j.code !== 200) {
|
||||
throw new Error(j.message || `HTTP ${res.status}`);
|
||||
@@ -183,7 +192,12 @@ async function translateDoc() {
|
||||
genHist.add({ title: `文档翻译: ${docFile.value.name}`, detail: `${j.data.chars} 字 → ${langName(docTo.value)}` });
|
||||
}
|
||||
catch (e: any) {
|
||||
docError.value = String(e?.message || e);
|
||||
if (e?.name === 'AbortError') {
|
||||
docError.value = '翻译超时(超过 8 分钟):文档过大,请拆分后重试,或减小文档篇幅。';
|
||||
}
|
||||
else {
|
||||
docError.value = String(e?.message || e);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
docLoading.value = false;
|
||||
@@ -307,7 +321,7 @@ onUnmounted(() => {
|
||||
</select>
|
||||
</div>
|
||||
<button class="doc-go" :disabled="docLoading || !docFile" @click="translateDoc()">
|
||||
{{ docLoading ? '⏳ AI 整篇翻译中…(长文档请耐心等待)' : '🚀 翻译文档' }}
|
||||
{{ docLoading ? '⏳ AI 并发翻译中…(大文档约需 1-2 分钟)' : '🚀 翻译文档' }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user