Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e5ba13980f |
21
app.py
21
app.py
@@ -229,13 +229,8 @@ def upload_pdf():
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
output_path = os.path.join(output_dir, f"{filename}_translated.md")
|
||||
|
||||
# 创建异步翻译任务
|
||||
# 创建异步翻译任务(先不创建,等translation_id生成后)
|
||||
task_id = str(uuid.uuid4())
|
||||
TranslationTask.create_task(
|
||||
task_id, upload_path, output_path,
|
||||
{'LLM_CONFIG': LLM_CONFIG},
|
||||
instruction
|
||||
)
|
||||
|
||||
# 创建翻译记录
|
||||
translation = Translation(
|
||||
@@ -252,6 +247,20 @@ def upload_pdf():
|
||||
)
|
||||
db.session.add(translation)
|
||||
|
||||
# 预先提交获取 translation_id
|
||||
if not from_cache:
|
||||
db.session.flush() # 获取 ID 但不提交完整事务
|
||||
|
||||
# 创建异步翻译任务(需要翻译时)
|
||||
if not from_cache:
|
||||
TranslationTask.create_task(
|
||||
task_id, upload_path, output_path,
|
||||
{'LLM_CONFIG': LLM_CONFIG},
|
||||
instruction,
|
||||
translation_id=translation.id,
|
||||
app=app
|
||||
)
|
||||
|
||||
# 更新用户/访客计数
|
||||
if user:
|
||||
user.increment_count()
|
||||
|
||||
10
models.py
10
models.py
@@ -67,8 +67,10 @@ class User(db.Model):
|
||||
if max_pages > 0 and pages > max_pages:
|
||||
return False, f"PDF页数超出限制(最大{max_pages}页)"
|
||||
|
||||
# 检查每日次数限制
|
||||
today = datetime.utcnow().date()
|
||||
# 检查每日次数限制 - 使用上海时间(UTC+8)
|
||||
from datetime import timezone, timedelta
|
||||
shanghai_tz = timezone(timedelta(hours=8))
|
||||
today = datetime.now(shanghai_tz).date()
|
||||
if self.last_translate_date != today:
|
||||
self.daily_count = 0
|
||||
self.last_translate_date = today
|
||||
@@ -81,7 +83,9 @@ class User(db.Model):
|
||||
|
||||
def increment_count(self):
|
||||
"""增加翻译计数"""
|
||||
today = datetime.utcnow().date()
|
||||
from datetime import timezone, timedelta
|
||||
shanghai_tz = timezone(timedelta(hours=8))
|
||||
today = datetime.now(shanghai_tz).date()
|
||||
if self.last_translate_date != today:
|
||||
self.daily_count = 0
|
||||
self.last_translate_date = today
|
||||
|
||||
42
services.py
42
services.py
@@ -255,7 +255,7 @@ class TranslationTask:
|
||||
lock = threading.Lock()
|
||||
|
||||
@classmethod
|
||||
def create_task(cls, task_id, pdf_path, output_path, config, instruction=None):
|
||||
def create_task(cls, task_id, pdf_path, output_path, config, instruction=None, translation_id=None, app=None):
|
||||
"""创建翻译任务"""
|
||||
task = {
|
||||
'id': task_id,
|
||||
@@ -266,6 +266,7 @@ class TranslationTask:
|
||||
'error': None,
|
||||
'started_at': None,
|
||||
'completed_at': None,
|
||||
'translation_id': translation_id,
|
||||
}
|
||||
|
||||
with cls.lock:
|
||||
@@ -277,11 +278,29 @@ class TranslationTask:
|
||||
task['status'] = 'processing'
|
||||
task['started_at'] = datetime.now().isoformat()
|
||||
|
||||
# 更新数据库状态为 processing
|
||||
if app and translation_id:
|
||||
with app.app_context():
|
||||
from models import db, Translation
|
||||
trans = Translation.query.get(translation_id)
|
||||
if trans:
|
||||
trans.status = 'processing'
|
||||
db.session.commit()
|
||||
|
||||
def progress_callback(progress, total, message):
|
||||
with cls.lock:
|
||||
task['progress'] = progress
|
||||
task['message'] = message
|
||||
|
||||
# 更新数据库进度
|
||||
if app and translation_id:
|
||||
with app.app_context():
|
||||
from models import db, Translation
|
||||
trans = Translation.query.get(translation_id)
|
||||
if trans:
|
||||
trans.progress = progress
|
||||
db.session.commit()
|
||||
|
||||
try:
|
||||
result = service.translate_pdf(
|
||||
pdf_path, output_path, instruction, progress_callback
|
||||
@@ -292,11 +311,32 @@ class TranslationTask:
|
||||
task['completed_at'] = datetime.now().isoformat()
|
||||
task['result'] = result
|
||||
|
||||
# 更新数据库状态为 completed
|
||||
if app and translation_id:
|
||||
with app.app_context():
|
||||
from models import db, Translation
|
||||
trans = Translation.query.get(translation_id)
|
||||
if trans:
|
||||
trans.status = 'completed'
|
||||
trans.progress = 100
|
||||
trans.completed_at = datetime.now()
|
||||
db.session.commit()
|
||||
|
||||
except Exception as e:
|
||||
task['status'] = 'failed'
|
||||
task['error'] = str(e)
|
||||
task['message'] = f'翻译失败: {e}'
|
||||
|
||||
# 更新数据库状态为 failed
|
||||
if app and translation_id:
|
||||
with app.app_context():
|
||||
from models import db, Translation
|
||||
trans = Translation.query.get(translation_id)
|
||||
if trans:
|
||||
trans.status = 'failed'
|
||||
trans.error_message = str(e)
|
||||
db.session.commit()
|
||||
|
||||
thread = threading.Thread(target=run_translation)
|
||||
thread.start()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user