From 7c10e68b61e24dc1bb38409754ae3b3588e2af53 Mon Sep 17 00:00:00 2001 From: Felix Date: Tue, 9 Dec 2025 21:57:03 +0800 Subject: [PATCH] fix cost --- backend/app/ai/model/image_text.py | 2 +- backend/app/ai/schema/image_task.py | 2 +- backend/app/ai/service/image_service.py | 18 +++++++++++------- backend/common/const.py | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/backend/app/ai/model/image_text.py b/backend/app/ai/model/image_text.py index ecde35b..6fcc9ea 100644 --- a/backend/app/ai/model/image_text.py +++ b/backend/app/ai/model/image_text.py @@ -22,7 +22,7 @@ class ImageText(Base): content: Mapped[str] = mapped_column(Text, nullable=False, comment="文本内容") standard_audio_id: Mapped[Optional[int]] = mapped_column(BigInteger, ForeignKey('file.id'), nullable=True, comment="标准朗读音频文件ID") ipa: Mapped[Optional[str]] = mapped_column(String(1000), default=None, comment="ipa") - zh: Mapped[Optional[str]] = mapped_column(String(100), default=None, comment="中文") + zh: Mapped[Optional[str]] = mapped_column(String(500), default=None, comment="中文") position: Mapped[Optional[dict]] = mapped_column(MySQLJSON, default=None, comment="文本在图片中的位置信息或文章中的位置信息") dict_level: Mapped[Optional[str]] = mapped_column(String(20), default=None, comment="词典等级") source: Mapped[Optional[str]] = mapped_column(String(20), default=None, comment="文本来源 (ref_word/description/article)") diff --git a/backend/app/ai/schema/image_task.py b/backend/app/ai/schema/image_task.py index ceedf44..fc9e573 100644 --- a/backend/app/ai/schema/image_task.py +++ b/backend/app/ai/schema/image_task.py @@ -39,5 +39,5 @@ class ImageTaskStatusResponse(BaseModel): task_id: str image_id: str status: ImageTaskStatus - result: Optional[dict] = None + # result: Optional[dict] = None error_message: Optional[str] = None \ No newline at end of file diff --git a/backend/app/ai/service/image_service.py b/backend/app/ai/service/image_service.py index b9ecca3..85e072e 100755 --- a/backend/app/ai/service/image_service.py +++ b/backend/app/ai/service/image_service.py @@ -1,4 +1,5 @@ import asyncio +import math import re import base64 import hashlib @@ -336,9 +337,11 @@ class ImageService: if task: image = await image_dao.get(db, task.image_id) if image: + total_tokens = task.result.get("token_usage", {}).get("total_tokens", 0) + points = math.ceil(max(total_tokens, 1)/1000)*0.15 * IMAGE_RECOGNITION_COST points_deducted = await points_service.deduct_points_with_db( user_id=task.user_id, - amount=IMAGE_RECOGNITION_COST, + amount=math.ceil(points), db=db, related_id=image.id, details={"task_id": task_id}, @@ -484,6 +487,7 @@ class ImageService: # Make the external API call without holding a database connection recognize_response = await Qwen.recognize_image(recognize_params) recognition_result = recognize_response.get("result").strip().replace("```json", "").replace("```", "").strip() + token_usage=recognize_response.get("token_usage", {}) # 使用新的数据库会话处理API响应 async with background_db_session() as db: @@ -573,7 +577,7 @@ class ImageService: # 更新任务状态为处理中(不扣减积分) await update_task_status_with_retry( db, task_id, ImageTaskStatus.PROCESSING, - result=transformed_result + result={"recognition_result": transformed_result, "token_usage": token_usage} ) # 提交事务 @@ -661,19 +665,19 @@ class ImageService: "task_id": str(task.id), "image_id": str(task.image_id), "status": task.status, - "result": {}, + # "result": {}, "error_message": None } - if task.status == ImageTaskStatus.COMPLETED: + # if task.status == ImageTaskStatus.COMPLETED: # Handle both the new structure and potential legacy structures - if task.result: + # if task.result: # if isinstance(task.result, dict) and task.dict_level in task.result: # response["result"] = task.result[task.dict_level] # else: # response["result"] = task.result - response["result"] = task.result - elif task.status == ImageTaskStatus.FAILED: + # response["result"] = task.result + if task.status == ImageTaskStatus.FAILED: response["error_message"] = task.error_message return response diff --git a/backend/common/const.py b/backend/common/const.py index d02c892..d718052 100644 --- a/backend/common/const.py +++ b/backend/common/const.py @@ -1,7 +1,7 @@ """Constants used throughout the application.""" # Image recognition service cost in points -IMAGE_RECOGNITION_COST = 5 +IMAGE_RECOGNITION_COST = 10 # 0.0015/1000 * 10 # Speech assessment service cost in points SPEECH_ASSESSMENT_COST = 1