feat:修复gemini tool问题,简化表情包识别,修复非多模态plan图片识别
This commit is contained in:
@@ -6,6 +6,7 @@ import io
|
||||
import os
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
import re
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from fastapi import APIRouter, Cookie, HTTPException, Query
|
||||
@@ -55,6 +56,19 @@ from .support import (
|
||||
router = APIRouter(prefix="/emoji", tags=["Emoji"])
|
||||
|
||||
|
||||
def _normalize_emoji_description(description: str = "", emotion: str = "") -> str:
|
||||
"""将上传参数中的描述/情绪标签归一化为可存储 description。"""
|
||||
normalized_description = str(description or "").strip()
|
||||
normalized_emotion = str(emotion or "").strip()
|
||||
if normalized_description:
|
||||
return normalized_description
|
||||
if not normalized_emotion:
|
||||
return ""
|
||||
|
||||
tags = re.split(r"[,,、;;\s]+", normalized_emotion)
|
||||
return ",".join(item.strip() for item in tags if item.strip())
|
||||
|
||||
|
||||
@router.get("/list", response_model=EmojiListResponse)
|
||||
async def get_emoji_list(
|
||||
page: int = Query(1, ge=1, description="页码"),
|
||||
@@ -173,6 +187,14 @@ async def update_emoji(
|
||||
if "is_registered" in update_data and update_data["is_registered"] and not emoji.is_registered:
|
||||
update_data["register_time"] = datetime.now()
|
||||
|
||||
if "emotion" in update_data:
|
||||
normalized_description = _normalize_emoji_description(
|
||||
description=update_data.get("description", ""),
|
||||
emotion=update_data.get("emotion", ""),
|
||||
)
|
||||
update_data["description"] = normalized_description
|
||||
update_data.pop("emotion", None)
|
||||
|
||||
for field, value in update_data.items():
|
||||
setattr(emoji, field, value)
|
||||
|
||||
@@ -543,7 +565,7 @@ async def upload_emoji(
|
||||
_ = output_file.write(file_content)
|
||||
|
||||
logger.info(f"表情包文件已保存: {full_path}")
|
||||
emotion_str = ",".join(item.strip() for item in emotion.split(",") if item.strip()) if emotion else ""
|
||||
final_description = _normalize_emoji_description(description=description, emotion=emotion)
|
||||
|
||||
current_time = datetime.now()
|
||||
with get_db_session() as session:
|
||||
@@ -551,8 +573,8 @@ async def upload_emoji(
|
||||
image_type=ImageType.EMOJI,
|
||||
full_path=full_path,
|
||||
image_hash=emoji_hash,
|
||||
description=description,
|
||||
emotion=emotion_str or None,
|
||||
description=final_description,
|
||||
emotion=None,
|
||||
query_count=0,
|
||||
is_registered=is_registered,
|
||||
is_banned=False,
|
||||
@@ -654,16 +676,16 @@ async def batch_upload_emoji(
|
||||
with open(full_path, "wb") as output_file:
|
||||
_ = output_file.write(file_content)
|
||||
|
||||
emotion_str = ",".join(item.strip() for item in emotion.split(",") if item.strip()) if emotion else ""
|
||||
current_time = datetime.now()
|
||||
final_description = _normalize_emoji_description(emotion=emotion)
|
||||
|
||||
with get_db_session() as session:
|
||||
emoji = Images(
|
||||
image_type=ImageType.EMOJI,
|
||||
full_path=full_path,
|
||||
image_hash=emoji_hash,
|
||||
description="",
|
||||
emotion=emotion_str or None,
|
||||
description=final_description,
|
||||
emotion=None,
|
||||
query_count=0,
|
||||
is_registered=is_registered,
|
||||
is_banned=False,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import re
|
||||
from typing import Annotated, List, Optional
|
||||
|
||||
from fastapi import File, Form, UploadFile
|
||||
@@ -5,15 +6,15 @@ from pydantic import BaseModel
|
||||
|
||||
from src.common.database.database_model import Images
|
||||
|
||||
EmojiFile = Annotated[UploadFile, File(description="表情包图片文件")]
|
||||
EmojiFiles = Annotated[List[UploadFile], File(description="多个表情包图片文件")]
|
||||
EmojiFile = Annotated[UploadFile, File(description="表情包上传文件")]
|
||||
EmojiFiles = Annotated[List[UploadFile], File(description="多个表情包上传文件")]
|
||||
DescriptionForm = Annotated[str, Form(description="表情包描述")]
|
||||
EmotionForm = Annotated[str, Form(description="情感标签,多个用逗号分隔")]
|
||||
EmotionForm = Annotated[str, Form(description="情绪标签,多个使用逗号分隔")]
|
||||
IsRegisteredForm = Annotated[bool, Form(description="是否直接注册")]
|
||||
|
||||
|
||||
class EmojiResponse(BaseModel):
|
||||
"""表情包响应"""
|
||||
"""表情包响应结构"""
|
||||
|
||||
id: int
|
||||
full_path: str
|
||||
@@ -124,7 +125,20 @@ class ThumbnailPreheatResponse(BaseModel):
|
||||
|
||||
|
||||
def emoji_to_response(image: Images) -> EmojiResponse:
|
||||
"""将数据库表情包模型转换为响应对象。"""
|
||||
emotions: list[str] = []
|
||||
if image.description:
|
||||
emotions.extend(
|
||||
item.strip() for item in re.split(r"[,,、;;\s]+", image.description) if item and item.strip()
|
||||
)
|
||||
if not emotions and image.emotion:
|
||||
emotions.extend(item.strip() for item in re.split(r"[,,、;;\s]+", image.emotion) if item and item.strip())
|
||||
|
||||
deduped_emotions: list[str] = []
|
||||
for item in emotions:
|
||||
if item not in deduped_emotions:
|
||||
deduped_emotions.append(item)
|
||||
emotion = ",".join(deduped_emotions) if deduped_emotions else None
|
||||
|
||||
return EmojiResponse(
|
||||
id=image.id if image.id is not None else 0,
|
||||
full_path=image.full_path,
|
||||
@@ -133,7 +147,7 @@ def emoji_to_response(image: Images) -> EmojiResponse:
|
||||
query_count=image.query_count,
|
||||
is_registered=image.is_registered,
|
||||
is_banned=image.is_banned,
|
||||
emotion=image.emotion,
|
||||
emotion=emotion,
|
||||
record_time=image.record_time.timestamp() if image.record_time else 0.0,
|
||||
register_time=image.register_time.timestamp() if image.register_time else None,
|
||||
last_used_time=image.last_used_time.timestamp() if image.last_used_time else None,
|
||||
|
||||
Reference in New Issue
Block a user