fix cost
This commit is contained in:
@@ -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)")
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user