交互式界面可以下载非内嵌markdown、缓存自定义提示词
This commit is contained in:
@@ -28,6 +28,7 @@ current_state: Dict[str, Any] = {
|
|||||||
"error_flag": False,
|
"error_flag": False,
|
||||||
"download_ready": False,
|
"download_ready": False,
|
||||||
"markdown_content": None,
|
"markdown_content": None,
|
||||||
|
"markdown_zip_content": None,
|
||||||
"html_content": None,
|
"html_content": None,
|
||||||
"original_filename_stem": None,
|
"original_filename_stem": None,
|
||||||
"task_start_time": 0,
|
"task_start_time": 0,
|
||||||
@@ -138,6 +139,7 @@ async def _perform_translation(params: Dict[str, Any], file_contents: bytes, ori
|
|||||||
)
|
)
|
||||||
|
|
||||||
md_content = ft.export_to_markdown()
|
md_content = ft.export_to_markdown()
|
||||||
|
md_zip_content = ft.export_to_unembed_markdown()
|
||||||
try:
|
try:
|
||||||
await httpx_client.head("https://s4.zstatic.net/ajax/libs/KaTeX/0.16.9/contrib/auto-render.min.js",
|
await httpx_client.head("https://s4.zstatic.net/ajax/libs/KaTeX/0.16.9/contrib/auto-render.min.js",
|
||||||
timeout=3)
|
timeout=3)
|
||||||
@@ -151,6 +153,7 @@ async def _perform_translation(params: Dict[str, Any], file_contents: bytes, ori
|
|||||||
|
|
||||||
current_state.update({
|
current_state.update({
|
||||||
"markdown_content": md_content,
|
"markdown_content": md_content,
|
||||||
|
"markdown_zip_content": md_zip_content,
|
||||||
"html_content": html_content,
|
"html_content": html_content,
|
||||||
"status_message": f"翻译成功!用时 {duration:.2f} 秒。",
|
"status_message": f"翻译成功!用时 {duration:.2f} 秒。",
|
||||||
"download_ready": True,
|
"download_ready": True,
|
||||||
@@ -168,6 +171,7 @@ async def _perform_translation(params: Dict[str, Any], file_contents: bytes, ori
|
|||||||
"error_flag": False,
|
"error_flag": False,
|
||||||
"download_ready": False,
|
"download_ready": False,
|
||||||
"markdown_content": None,
|
"markdown_content": None,
|
||||||
|
"md_zip_content": None,
|
||||||
"html_content": None,
|
"html_content": None,
|
||||||
"task_end_time": end_time,
|
"task_end_time": end_time,
|
||||||
})
|
})
|
||||||
@@ -181,6 +185,7 @@ async def _perform_translation(params: Dict[str, Any], file_contents: bytes, ori
|
|||||||
"error_flag": True,
|
"error_flag": True,
|
||||||
"download_ready": False,
|
"download_ready": False,
|
||||||
"markdown_content": None,
|
"markdown_content": None,
|
||||||
|
"md_zip_content": None,
|
||||||
"html_content": None,
|
"html_content": None,
|
||||||
"task_end_time": end_time,
|
"task_end_time": end_time,
|
||||||
})
|
})
|
||||||
@@ -255,6 +260,7 @@ async def handle_translate(
|
|||||||
"error_flag": False,
|
"error_flag": False,
|
||||||
"download_ready": False,
|
"download_ready": False,
|
||||||
"markdown_content": None,
|
"markdown_content": None,
|
||||||
|
"md_zip_content": None,
|
||||||
"html_content": None,
|
"html_content": None,
|
||||||
"original_filename_stem": Path(original_filename_for_init).stem,
|
"original_filename_stem": Path(original_filename_for_init).stem,
|
||||||
"task_start_time": time.time(),
|
"task_start_time": time.time(),
|
||||||
@@ -365,10 +371,16 @@ async def get_status():
|
|||||||
"error_flag": current_state["error_flag"],
|
"error_flag": current_state["error_flag"],
|
||||||
"download_ready": current_state["download_ready"],
|
"download_ready": current_state["download_ready"],
|
||||||
"original_filename_stem": current_state["original_filename_stem"],
|
"original_filename_stem": current_state["original_filename_stem"],
|
||||||
|
|
||||||
"markdown_url": f"/download/markdown/{current_state['original_filename_stem']}_translated.md" if current_state[
|
"markdown_url": f"/download/markdown/{current_state['original_filename_stem']}_translated.md" if current_state[
|
||||||
"download_ready"] and
|
"download_ready"] and
|
||||||
current_state[
|
current_state[
|
||||||
"original_filename_stem"] else None,
|
"original_filename_stem"] else None,
|
||||||
|
"markdown_zip_url": f"/download/markdown_zip/{current_state['original_filename_stem']}_translated.md" if
|
||||||
|
current_state[
|
||||||
|
"download_ready"] and
|
||||||
|
current_state[
|
||||||
|
"original_filename_stem"] else None,
|
||||||
"html_url": f"/download/html/{current_state['original_filename_stem']}_translated.html" if current_state[
|
"html_url": f"/download/html/{current_state['original_filename_stem']}_translated.html" if current_state[
|
||||||
"download_ready"] and
|
"download_ready"] and
|
||||||
current_state[
|
current_state[
|
||||||
@@ -413,6 +425,25 @@ async def download_markdown(filename_with_ext: str):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/download/markdown_zip/{filename_with_ext}")
|
||||||
|
async def download_markdown(filename_with_ext: str):
|
||||||
|
if not current_state["download_ready"] or not current_state["markdown_zip_content"] or not current_state[
|
||||||
|
"original_filename_stem"]:
|
||||||
|
print("MarkdownZip 内容尚未准备好或不可用。")
|
||||||
|
raise HTTPException(status_code=404, detail="MarkdownZip 内容尚未准备好或不可用。")
|
||||||
|
|
||||||
|
if Path(filename_with_ext).stem != f"{current_state['original_filename_stem']}_translated":
|
||||||
|
raise HTTPException(status_code=404, detail="请求的文件名与当前结果不符。")
|
||||||
|
|
||||||
|
actual_filename = f"{current_state['original_filename_stem']}_translated.zip"
|
||||||
|
return StreamingResponse(
|
||||||
|
io.BytesIO(current_state["markdown_zip_content"]),
|
||||||
|
media_type="application/zip",
|
||||||
|
headers={
|
||||||
|
"Content-Disposition": f"attachment; filename*=UTF-8''{quote(actual_filename, safe='', encoding='utf-8')}"}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/download/html/{filename_with_ext}")
|
@app.get("/download/html/{filename_with_ext}")
|
||||||
async def download_html(filename_with_ext: str):
|
async def download_html(filename_with_ext: str):
|
||||||
if not current_state["download_ready"] or not current_state["html_content"] or not current_state[
|
if not current_state["download_ready"] or not current_state["html_content"] or not current_state[
|
||||||
|
|||||||
@@ -114,6 +114,9 @@
|
|||||||
display: none;
|
display: none;
|
||||||
margin-top: 1rem;
|
margin-top: 1rem;
|
||||||
}
|
}
|
||||||
|
.smaller-button-group > *{
|
||||||
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
|
||||||
.hidden {
|
.hidden {
|
||||||
display: none !important;
|
display: none !important;
|
||||||
@@ -444,10 +447,9 @@
|
|||||||
</form>
|
</form>
|
||||||
<div id="resultArea">
|
<div id="resultArea">
|
||||||
<p id="statusMessage"></p>
|
<p id="statusMessage"></p>
|
||||||
<div id="downloadButtons" class="button-group">
|
<div id="downloadButtons" class="button-group smaller-button-group" style="justify-content: space-around">
|
||||||
<h4>翻译结果</h4>
|
<a id="downloadMarkdownZip" href="#" role="button" class="outline">下载Markdown</a>
|
||||||
<div class="spacer"></div>
|
<a id="downloadMarkdown" href="#" role="button" class="outline">下载Markdown(嵌图)</a>
|
||||||
<a id="downloadMarkdown" href="#" role="button" class="outline">下载 Markdown</a>
|
|
||||||
<a id="downloadHtml" href="#" role="button" class="outline">下载HTML</a>
|
<a id="downloadHtml" href="#" role="button" class="outline">下载HTML</a>
|
||||||
<button id="downloadPdf" class="outline">下载PDF</button>
|
<button id="downloadPdf" class="outline">下载PDF</button>
|
||||||
<button id="previewHtml" class="outline">预览</button>
|
<button id="previewHtml" class="outline">预览</button>
|
||||||
@@ -528,11 +530,13 @@
|
|||||||
const logArea = document.getElementById('logArea');
|
const logArea = document.getElementById('logArea');
|
||||||
const statusMsg = document.getElementById('statusMessage');
|
const statusMsg = document.getElementById('statusMessage');
|
||||||
const downloadBtns = document.getElementById('downloadButtons');
|
const downloadBtns = document.getElementById('downloadButtons');
|
||||||
|
const markdownZipLink = document.getElementById('downloadMarkdownZip');
|
||||||
const markdownLink = document.getElementById('downloadMarkdown');
|
const markdownLink = document.getElementById('downloadMarkdown');
|
||||||
const htmlLink = document.getElementById('downloadHtml');
|
const htmlLink = document.getElementById('downloadHtml');
|
||||||
const previewHtmlBtn = document.getElementById('previewHtml');
|
const previewHtmlBtn = document.getElementById('previewHtml');
|
||||||
const downloadPdfBtn = document.getElementById('downloadPdf');
|
const downloadPdfBtn = document.getElementById('downloadPdf');
|
||||||
const printFrameEl = document.getElementById('printFrame');
|
const printFrameEl = document.getElementById('printFrame');
|
||||||
|
const customPromptTranslateArea=document.getElementById("custom_prompt_translate");
|
||||||
|
|
||||||
// Modal and preview elements
|
// Modal and preview elements
|
||||||
const modal = document.getElementById('previewModal');
|
const modal = document.getElementById('previewModal');
|
||||||
@@ -612,6 +616,7 @@
|
|||||||
statusMsg.textContent = error.toString();
|
statusMsg.textContent = error.toString();
|
||||||
statusMsg.className = 'error-message';
|
statusMsg.className = 'error-message';
|
||||||
}
|
}
|
||||||
|
customPromptTranslateArea.value=getFromStorage("custom_prompt_translate")
|
||||||
}
|
}
|
||||||
|
|
||||||
await init()
|
await init()
|
||||||
@@ -794,6 +799,9 @@
|
|||||||
setBilingualViewBtn.addEventListener('click', () => setPreviewDisplayMode('bilingual'));
|
setBilingualViewBtn.addEventListener('click', () => setPreviewDisplayMode('bilingual'));
|
||||||
setTranslatedOnlyViewBtn.addEventListener('click', () => setPreviewDisplayMode('translationOnly'));
|
setTranslatedOnlyViewBtn.addEventListener('click', () => setPreviewDisplayMode('translationOnly'));
|
||||||
|
|
||||||
|
customPromptTranslateArea.addEventListener('input',()=>{
|
||||||
|
saveToStorage("custom_prompt_translate",customPromptTranslateArea.value)
|
||||||
|
})
|
||||||
|
|
||||||
fileDropArea.addEventListener('click', () => fileInput.click());
|
fileDropArea.addEventListener('click', () => fileInput.click());
|
||||||
fileInput.addEventListener('change', () => {
|
fileInput.addEventListener('change', () => {
|
||||||
@@ -888,6 +896,8 @@
|
|||||||
isTranslating = false;
|
isTranslating = false;
|
||||||
|
|
||||||
if (status.download_ready && !status.error_flag) {
|
if (status.download_ready && !status.error_flag) {
|
||||||
|
markdownZipLink.href = status.markdown_zip_url;
|
||||||
|
markdownZipLink.setAttribute('download', status.original_filename_stem + '_translated.zip');
|
||||||
markdownLink.href = status.markdown_url;
|
markdownLink.href = status.markdown_url;
|
||||||
markdownLink.setAttribute('download', status.original_filename_stem + '_translated.md');
|
markdownLink.setAttribute('download', status.original_filename_stem + '_translated.md');
|
||||||
htmlLink.href = status.html_url;
|
htmlLink.href = status.html_url;
|
||||||
|
|||||||
Reference in New Issue
Block a user