优化ui
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import asyncio
|
||||
import io
|
||||
import logging
|
||||
import socket
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
from contextlib import asynccontextmanager, closing
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Any, Optional
|
||||
from urllib.parse import quote
|
||||
@@ -15,7 +16,6 @@ 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
|
||||
|
||||
|
||||
# --- 全局配置 ---
|
||||
@@ -328,7 +328,7 @@ async def cancel_translate_task():
|
||||
@app.get("/get-engin-list")
|
||||
async def get_engin_list():
|
||||
engin_list=["mineru"]
|
||||
if DOCLING_EXIST:
|
||||
if available_packages.get("docling"):
|
||||
engin_list.append("docling")
|
||||
return JSONResponse(content=engin_list)
|
||||
|
||||
@@ -405,11 +405,27 @@ 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 find_free_port(start_port):
|
||||
"""从指定端口开始查找可用的端口"""
|
||||
port = start_port
|
||||
while True:
|
||||
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
|
||||
if sock.connect_ex(('127.0.0.1', port)) != 0: # 端口可用
|
||||
return port
|
||||
port += 1 # 端口被占用,尝试下一个端口
|
||||
def run_app():
|
||||
print("正在启动 DocuTranslate WebUI")
|
||||
print("请访问 http://127.0.0.1:8010 (ctrl+点击链接即可打开)")
|
||||
uvicorn.run(app, host="127.0.0.1", port=8010, workers=1)
|
||||
initial_port = 8010
|
||||
try:
|
||||
# 首先检查初始端口是否可用
|
||||
port = find_free_port(initial_port)
|
||||
if port != initial_port:
|
||||
print(f"端口 {initial_port} 被占用,将使用端口 {port} 代替")
|
||||
print("正在启动 DocuTranslate WebUI")
|
||||
print(f"请访问 http://127.0.0.1:{port} (ctrl+点击链接即可打开)")
|
||||
uvicorn.run(app, host="127.0.0.1", port=port, workers=1)
|
||||
except Exception as e:
|
||||
print(f"启动失败: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user