diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2efba06..a8f510b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,11 +5,15 @@ - + + + + - + + @@ -41,76 +45,76 @@ - { - "keyToString": { - "DefaultHtmlFileTemplate": "HTML File", - "JavaScript 调试.output.html (1).executor": "Run", - "JavaScript 调试.output.html.executor": "Run", - "JavaScript 调试.regex.md_中文.html.executor": "Run", - "JavaScript 调试.regex_中文.html.executor": "Run", - "JavaScript 调试.test.html.executor": "Run", - "JavaScript 调试.test2.html.executor": "Run", - "JavaScript 调试.test2_英文.html.executor": "Run", - "JavaScript 调试.test4-1_中文.html.executor": "Run", - "JavaScript 调试.互联网认证授权机制.html.executor": "Run", - "JavaScript 调试.互联网认证授权机制_英文.html.executor": "Run", - "JavaScript 调试.毕业论文_英文.html.executor": "Run", - "ModuleVcsDetector.initialDetectionPerformed": "true", - "Python 测试.Python 测试 (markdown_mask.py 内).executor": "Run", - "Python 测试.markdown_mask.Test.test_basic_link_masking 的 Python 测试.executor": "Run", - "Python 测试.pytest (test_html.py 内).executor": "Run", - "Python.1test.executor": "Run", - "Python.2test2 (1).executor": "Run", - "Python.PDFtranslater (1).executor": "Run", - "Python.PDFtranslater (2).executor": "Run", - "Python.agent.executor": "Debug", - "Python.agent_utils.executor": "Run", - "Python.app (1).executor": "Run", - "Python.app.executor": "Run", - "Python.app2.executor": "Run", - "Python.app_test (1).executor": "Run", - "Python.convert.executor": "Run", - "Python.converter_docling.executor": "Run", - "Python.converter_mineru.executor": "Run", - "Python.markdown_splitter.executor": "Debug", - "Python.markdown_utils.executor": "Run", - "Python.test.executor": "Run", - "Python.test1.executor": "Run", - "Python.test2.executor": "Run", - "Python.test3.executor": "Run", - "Python.test4.executor": "Run", - "Python.testhtml.executor": "Run", - "Python.translater.executor": "Run", - "Python.切分测试.executor": "Run", - "RunOnceActivity.ShowReadmeOnStart": "true", - "RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager": "true", - "RunOnceActivity.git.unshallow": "true", - "git-widget-placeholder": "dev", - "last_opened_file_path": "C:/Users/jxgm/Desktop/translate/docutranslate", - "list.type.of.created.stylesheet": "CSS", - "node.js.detected.package.eslint": "true", - "node.js.detected.package.tslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "node.js.selected.package.tslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "settings.editor.selected.configurable": "preferences.pluginManager", - "vue.rearranger.settings.migration": "true" + +}]]> + - + - @@ -607,6 +611,7 @@ + @@ -625,7 +630,7 @@ - + diff --git a/README.md b/README.md index c58fc46..b700b57 100644 --- a/README.md +++ b/README.md @@ -19,18 +19,18 @@ # 安装 使用pip -`pip install docutranslate` -`pip install docling`#如果使用docling进行文档解析 +1. `pip install docutranslate` +2. `pip install docling`#如果使用docling进行文档解析 使用uv -`uv init` -`uv add docutranslate` -`uv add docling`#如果使用docling进行文档解析 +1. `uv init` +2. `uv add docutranslate` +3. `uv add docling`#如果使用docling进行文档解析 使用git -`git clone https://github.com/xunbu/docutranslate.git` -`uv sync` - +1. `git clone https://github.com/xunbu/docutranslate.git` +2. `uv sync` +3. `uv pip install -U -e .` # 支持的文件格式 | 输入格式 | 输出格式 | diff --git a/docutranslate/app.py b/docutranslate/app.py index 8c8d600..faf27a1 100644 --- a/docutranslate/app.py +++ b/docutranslate/app.py @@ -2,6 +2,7 @@ import asyncio import io import logging import time +from contextlib import asynccontextmanager from pathlib import Path from typing import List, Dict, Any, Optional from urllib.parse import quote @@ -10,17 +11,12 @@ import uvicorn from fastapi import FastAPI, File, Form, UploadFile, Request, HTTPException from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse,FileResponse from fastapi.staticfiles import StaticFiles -from docutranslate import FileTranslater # Assuming FileTranslater is in docutranslate module +from docutranslate import FileTranslater from docutranslate.logger import translater_logger from docutranslate.utils.resource_utils import resource_path from docutranslate.global_values import available_packages - DOCLING_EXIST=True if available_packages.get("docling") else False -app = FastAPI() -STATIC_DIR=resource_path("static") - -app.mount("/static",StaticFiles(directory=STATIC_DIR), name="static") # --- 全局配置 --- log_queue: Optional[asyncio.Queue] = None @@ -69,8 +65,8 @@ class QueueAndHistoryHandler(logging.Handler): # --- 应用生命周期事件 --- -@app.on_event("startup") -async def startup_event(): +@asynccontextmanager +async def lifespan(app: FastAPI): global log_queue app.state.main_event_loop = asyncio.get_running_loop() log_queue = asyncio.Queue() @@ -94,7 +90,13 @@ async def startup_event(): break translater_logger.info("应用启动完成,日志队列/历史处理器已正确配置。") + yield +app = FastAPI(lifespan=lifespan) + +STATIC_DIR=resource_path("static") + +app.mount("/static",StaticFiles(directory=STATIC_DIR), name="static") # --- Background Task Logic --- async def _perform_translation(params: Dict[str, Any], file_contents: bytes, original_filename: str): @@ -403,7 +405,7 @@ async def download_html(filename_with_ext: str): headers={"Content-Disposition": f"attachment; filename*=UTF-8''{quote(actual_filename, safe='', encoding='utf-8')}"} ) - +#TODO:端口被占用时使用其他端口 def run_app(): print("正在启动 DocuTranslate WebUI") print("请访问 http://127.0.0.1:8010 (ctrl+点击链接即可打开)") diff --git a/docutranslate/converter/__init__.py b/docutranslate/converter/__init__.py index a631540..25cb957 100644 --- a/docutranslate/converter/__init__.py +++ b/docutranslate/converter/__init__.py @@ -4,3 +4,6 @@ from .converter_mineru import ConverterMineru from docutranslate.global_values import conditional_import if conditional_import("docling"): from .converter_docling import ConverterDocling + +# 打包docling时取消下面一行注释 +# from .converter_docling import ConverterDocling diff --git a/docutranslate/static/DocuTranslate.ico b/docutranslate/static/DocuTranslate.ico new file mode 100644 index 0000000..b2acb24 Binary files /dev/null and b/docutranslate/static/DocuTranslate.ico differ diff --git a/docutranslate/static/index.html b/docutranslate/static/index.html index e5c165d..1fb9ed9 100644 --- a/docutranslate/static/index.html +++ b/docutranslate/static/index.html @@ -4,6 +4,7 @@ DocuTranslate + @@ -321,9 +331,9 @@
- 文档转换引擎配置 + 文档解析引擎配置
- + @@ -363,7 +371,9 @@
- +
@@ -393,7 +403,8 @@