File size: 2,580 Bytes
1c2b077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import List, Dict, Any
from app.image_utils import format_image_url
import logging

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
logger = logging.getLogger(__name__)

class GradioFormatter:
    """贴纸格式化类,处理贴纸数据的格式化逻辑"""
    
    @staticmethod
    def format_all_stickers(results: List[Dict[str, Any]]) -> List[List]:
        """格式化所有贴纸,用于Gradio UI显示"""
        formatted_results = []
        
        for record in results:
            logger.info(f"格式化所有贴纸,用于Gradio UI显示: {record}")
            image_url = format_image_url(record['file_name'])
            formatted_results.append([
                str(record['id']),
                f"![Sticker]({image_url})",
                record.get('title', ''),
                record['description'],
                ", ".join(record['tags']) if isinstance(record['tags'], list) else record['tags'],
                record['file_name'],
                record['image_hash'] if 'image_hash' in record else ''
            ])
        return formatted_results
    

    
    @staticmethod
    def format_search_results(results: List[Dict[str, Any]]) -> List[List]:
        """格式化搜索结果,用于Gradio UI显示"""
        formatted_results = []
        logger.info(f"Formatting search results: {len(results)} items")
        
        for hit in results:
            image_url = format_image_url(hit['entity']['file_name'])
            formatted_results.append([
                f"![Sticker]({image_url})",
                round(hit['distance'], 4),
                hit['entity'].get('description', ''),
                hit['entity'].get('file_name', '')
            ])

        return formatted_results

    @staticmethod
    def format_ai_search_results(results: List[Dict[str, Any]]) -> List[List]:
        """格式化 AI 搜索结果,用于Gradio UI显示"""
        formatted_results = []
        logger.info(f"Formatting AI search results: {len(results)} items")
        
        for hit in results:
            image_url = format_image_url(hit['entity']['file_name'])
            formatted_results.append([
                f"![Sticker]({image_url})",
                hit['entity'].get('score', ''),
                hit['entity'].get('reason', ''),
                hit['entity'].get('description', ''),
                hit['entity'].get('file_name', '')
            ])

        return formatted_results
# 创建格式化器实例
gradio_formatter = GradioFormatter()