Spaces:
Sleeping
Sleeping
File size: 11,171 Bytes
cff3b13 66e44c7 cff3b13 66e44c7 cff3b13 c4e7690 cff3b13 66e44c7 c4e7690 66e44c7 cff3b13 66e44c7 cff3b13 66e44c7 cff3b13 66e44c7 cff3b13 66e44c7 cff3b13 66e44c7 6324e60 66e44c7 6324e60 eb44dec cff3b13 c4e7690 cff3b13 66e44c7 c4e7690 66e44c7 416d77b 66e44c7 cff3b13 66e44c7 cff3b13 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
import os
import logging
import random
import zipfile
import tempfile
import shutil
import json
from typing import List, Dict, Any, Optional, Union
from PIL import Image
from app.api import get_chat_completion
from app.config import (
STICKER_RERANKING_SYSTEM_PROMPT,
PUBLIC_URL,
TEMP_DIR
)
from app.database import db
from app.image_utils import (
save_image_temp,
generate_temp_image,
upload_folder_to_huggingface,
upload_to_huggingface,
get_image_cdn_url,
get_image_description,
calculate_image_hash
)
from app.gradio_formatter import gradio_formatter
from multiprocessing import Queue
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
logger = logging.getLogger(__name__)
class StickerService:
"""贴纸服务类,处理贴纸的上传、搜索等业务逻辑"""
@staticmethod
def upload_sticker(image_file_path: str, title: str, description: str, tags: str) -> str:
"""上传贴纸"""
try:
# 打开图片
image = Image.open(image_file_path)
# 检查文件名是否已存在
image_hash = calculate_image_hash(image)
if db.check_image_exists(image_hash):
print(f"文件已存在", image_hash)
raise Exception('File_Exists')
# 上传到 HuggingFace
file_path, image_filename = upload_to_huggingface(image_file_path)
# print('>>>> image_file_path', image_file_path)
# print('>>>> image_filename', image_filename)
# print('>>>> file_path', file_path)
# 如果没有描述,获取图片描述
if not description:
image_cdn_url = ''
if (PUBLIC_URL):
image_cdn_url = f'{PUBLIC_URL}/gradio_api/file={image_file_path}'
else:
image_cdn_url = get_image_cdn_url(file_path)
print('image_cdn_url',image_cdn_url)
description = get_image_description(image_cdn_url)
# 清理临时文件
# os.unlink(temp_file_path)
# 存储到 Milvus
db.store_sticker(title, description, tags, file_path, image_hash)
return f"Upload successful! {image_filename}"
except Exception as e:
logger.error(f"Upload failed: {str(e)}")
return f"Upload failed: {str(e)}"
@staticmethod
def import_stickers(
sticker_dataset: str,
upload: bool = False,
save_to_milvus: bool = False,
progress_callback: callable = None,
) -> List[str]:
"""导入表情包数据集
Args:
sticker_dataset (str): 表情包数据集路径
upload (bool, optional): 是否上传到HuggingFace. Defaults to False.
progress_callback (callable, optional): 进度回调函数. Defaults to None.
"""
results = []
descriptions = {}
try:
# 创建临时目录
cache_folder = os.path.join(TEMP_DIR, 'cache/')
img_folder = os.path.join(TEMP_DIR, 'images/')
data_json_path = os.path.join(cache_folder, 'data.json')
stickers = []
logger.info(f"start import dataset")
# 解压数据集
with zipfile.ZipFile(sticker_dataset, 'r') as zip_ref:
zip_ref.extractall(cache_folder)
logger.info(f"Extracted dataset to: {cache_folder}")
# 尝试读取data.json文件
if os.path.exists(data_json_path):
with open(data_json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
descriptions = { x["filename"]: x["content"] for x in data }
logger.info(f"Loaded descriptions from data.json")
# 遍历解压后的目录
for root, dirs, files in os.walk(cache_folder):
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp')):
image_path = os.path.join(root, file)
try:
# 打开图片
image = Image.open(image_path)
image_hash = calculate_image_hash(image)
if db.check_image_exists(image_hash):
results.append(f"跳过已存在的图片: {file}")
if progress_callback:
progress_callback(file, "Skipped (exists)")
continue
# 获取图片描述
description = None
if file in descriptions:
description = descriptions[file]
if not description:
results.append(f"跳过无描述的图片: {file}")
if progress_callback:
progress_callback(file, "Skipped (no description)")
continue
image_filename = f"image_{random.randint(100000, 999999)}.png"
file_path = f"images/{image_filename}"
generate_temp_image(img_folder, image, image_filename)
if save_to_milvus:
db.store_sticker("", description, "", file_path, image_hash)
stickers.append({
"title": "",
"description": description,
"tags": "",
"file_path": file_path,
"image_hash": image_hash
})
if progress_callback:
results.append(f"成功导入: {image_filename}")
progress_callback(file, "Imported")
except Exception as e:
logger.error(f"Failed to process image {file}: {str(e)}")
results.append(f"处理失败 {file}: {str(e)}")
if progress_callback:
progress_callback(file, f"Failed: {str(e)}")
# 上传到 HuggingFace
if upload and len(stickers) > 0:
logger.info(f"upload to huggingface, {len(stickers)} stickers")
upload_folder_to_huggingface(img_folder)
results.append(f"上传到 HuggingFace 成功")
return results
except Exception as e:
logger.error(f"Import failed: {str(e)}")
results.append(f"导入失败: {str(e)}")
return results
finally:
# 清理临时目录
if cache_folder and os.path.exists(cache_folder):
shutil.rmtree(cache_folder)
logger.info(f"Cleaned up temporary directory: {cache_folder}")
if img_folder and os.path.exists(img_folder):
shutil.rmtree(img_folder)
logger.info(f"Cleaned up temporary directory: {img_folder}")
@staticmethod
def search_stickers(description: str, limit: int = 2, reranking : bool = False) -> List[Dict[str, Any]]:
"""搜索贴纸"""
if not description:
return []
try:
results = db.search_stickers(description, limit)
if (reranking):
# 对搜索结果进行重排
results = StickerService.rerank_search_results(description, results, limit)
return results
except Exception as e:
logger.error(f"Search failed: {str(e)}")
return []
@staticmethod
def get_all_stickers(limit: int = 1000) -> List[List]:
"""获取所有贴纸"""
try:
results = db.get_all_stickers(limit)
return gradio_formatter.format_all_stickers(results)
except Exception as e:
logger.error(f"Failed to get all stickers: {str(e)}")
return []
@staticmethod
def delete_sticker(sticker_id: str) -> str:
"""删除贴纸"""
try:
# 首先查询贴纸是否存在
result = db.delete_sticker(sticker_id)
return f"Sticker with ID {sticker_id} deleted successfully"
except Exception as e:
logger.error(f"Delete failed: {str(e)}")
return f"Delete failed: {str(e)}"
@staticmethod
def rerank_search_results(query: str, sticker_list: List[Dict[str, Any]], limit: int = 5) -> List[Dict[str, Any]]:
## 使用 LLM 模型重新排序搜索结果
try:
# 构建提示词
system_prompt = STICKER_RERANKING_SYSTEM_PROMPT
# 构建用户提示词,包含查询和表情包信息
_sticker_list = []
for hit in sticker_list:
_sticker_list.append({
"id": hit["id"],
"description": hit["entity"]["description"]
})
user_prompt = f"请分析关键词 '{query}' 与以下表情包的相关性:\n{_sticker_list}"
print(f">>> 使用 LLM 模型重新排序....", user_prompt, system_prompt)
# 调用 LLM 模型获取重排序结果
response = get_chat_completion(user_prompt, system_prompt)
# 解析 LLM 返回的 JSON 结果
reranked_stickers = json.loads(response)
# 验证返回结果格式
if not isinstance(reranked_stickers, list):
raise ValueError("Invalid response format")
# 按分数排序
reranked_stickers.sort(key=lambda x: float(x.get("score", 0)), reverse=True)
print(f">>> LLM 排序结果", reranked_stickers)
# 将重排序结果与原始结果对应
rerank_results = []
for sticker in reranked_stickers:
for hit in sticker_list:
if str(hit["id"]) == str(sticker["sticker_id"]):
hit["entity"]["score"] = sticker["score"]
hit["entity"]["reason"] = sticker["reason"]
rerank_results.append(hit)
break
print(f">>> rerank_results", rerank_results)
return rerank_results
except Exception as e:
logger.error(f"Reranking failed: {str(e)}")
return []
# 创建服务实例
sticker_service = StickerService() |