File size: 9,319 Bytes
6a3bd1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from typing import Dict, List, Optional
from landmark_prompts import LandmarkPrompts
from brand_prompts import BrandPrompts
from scene_prompts import ScenePrompts
from universal_object_prompts import UniversalObjectPrompts

class PromptLibraryManager:
    """
    Facade 模式:統一管理所有 Prompt 子模組
    提供單一介面存取品牌、地標、場景、通用物品等 prompts
    """

    def __init__(self):
        """初始化所有 Prompt 子模組"""

        print("Initializing Prompt Library Manager (Facade)...")

        # 載入所有子模組
        self.brand_prompts = BrandPrompts()
        self.landmark_prompts = LandmarkPrompts()
        self.scene_prompts = ScenePrompts()
        self.object_prompts = UniversalObjectPrompts()

        # 統計資訊
        total_brands = self._count_brands()
        total_landmarks = len(self.landmark_prompts.landmarks)
        total_scenes = len(self.scene_prompts.scene_vocabularies)
        total_objects = len(self.object_prompts.object_vocabularies)

        print(f"✓ Prompt Library Manager initialized:")
        print(f"  - {total_brands} brands across {len(self.brand_prompts.brand_prompts)} categories")
        print(f"  - {total_landmarks} world landmarks")
        print(f"  - {total_scenes} scene categories")
        print(f"  - {total_objects} universal object categories")

    def _count_brands(self) -> int:
        """計算總品牌數量"""
        total = 0
        for category in self.brand_prompts.brand_prompts.values():
            total += len(category)
        return total

    # ===== 品牌相關方法 Brand Methods =====

    def get_brand_prompts(self, brand_name: str) -> Optional[Dict]:
        """
        取得特定品牌的完整 prompt 資料

        Args:
            brand_name: 品牌名稱

        Returns:
            品牌資料字典
        """
        return self.brand_prompts.get_prompts(brand_name)

    def get_brand_category(self, brand_name: str) -> str:
        """取得品牌類別"""
        return self.brand_prompts.get_brand_category(brand_name)

    def get_all_brands(self) -> Dict:
        """取得所有品牌的扁平化字典"""
        return self.brand_prompts.get_all_brands()

    def get_brands_by_category(self, category: str) -> Dict:
        """取得特定類別的所有品牌"""
        return self.brand_prompts.get_brands_by_category(category)

    def search_brand_by_alias(self, alias: str) -> Optional[str]:
        """根據別名搜尋品牌名稱"""
        return self.brand_prompts.search_brand_by_alias(alias)

    # ===== 地標相關方法 Landmark Methods =====

    def get_landmark_prompts(self, landmark_name: str) -> Optional[Dict]:
        """
        取得特定地標的完整 prompt 資料

        Args:
            landmark_name: 地標名稱

        Returns:
            地標資料字典
        """
        return self.landmark_prompts.get_prompts(landmark_name)

    def get_all_landmarks(self) -> Dict:
        """取得所有地標資料"""
        return self.landmark_prompts.get_all_landmarks()

    def search_landmark_by_location(self, city: str = None, country: str = None) -> List[str]:
        """
        根據地理位置搜尋地標

        Args:
            city: 城市名稱
            country: 國家名稱

        Returns:
            符合條件的地標名稱列表
        """
        return self.landmark_prompts.search_by_location(city, country)

    def get_landmark_visual_prompts(self, landmark_name: str, context: str = 'iconic_view') -> List[str]:
        """
        取得地標的視覺描述 prompts

        Args:
            landmark_name: 地標名稱
            context: 情境類型

        Returns:
            視覺描述列表
        """
        return self.landmark_prompts.get_visual_prompts(landmark_name, context)

    # Scene Methods
    def get_scene_prompts(self, scene_category: str, subcategory: str = None) -> List[str]:
        """
        取得場景 prompts

        Args:
            scene_category: 場景類別
            subcategory: 子類別(可選)

        Returns:
            Prompt 列表
        """
        return self.scene_prompts.get_prompts(scene_category, subcategory)

    def get_all_scene_categories(self) -> List[str]:
        """取得所有場景類別"""
        return self.scene_prompts.get_all_categories()

    def get_scene_subcategories(self, scene_category: str) -> List[str]:
        """取得場景的子類別"""
        return self.scene_prompts.get_subcategories(scene_category)

    # Universal Object Methods
    def get_object_prompts(self, category: str, subcategory: str = None) -> List[str]:
        """
        取得通用物品 prompts

        Args:
            category: 物品類別 (如 'animals', 'vehicles')
            subcategory: 子類別 (如 'dogs', 'cats')

        Returns:
            Prompt 列表
        """
        return self.object_prompts.get_prompts(category, subcategory)

    def get_all_object_categories(self) -> List[str]:
        """取得所有通用物品類別"""
        return self.object_prompts.get_all_categories()

    def get_object_subcategories(self, category: str) -> List[str]:
        """取得物品的子類別"""
        return self.object_prompts.get_subcategories(category)

    def detect_object_category(self, detected_objects: List[str]) -> Optional[str]:
        """根據檢測到的物體推測主要類別"""
        return self.object_prompts.detect_object_category(detected_objects)

    # Smart Hashtag Generation
    def get_hashtags_for_content(self, detected_items: Dict, language: str = 'zh') -> List[str]:
        """
        智能標籤生成:整合品牌、地標、場景的標籤

        Args:
            detected_items: 檢測到的內容字典
                {
                    'landmarks': ['Big Ben', ...],
                    'brands': ['Apple', ...],
                    'scene_category': 'urban',
                    'scene_subcategory': 'city_canyon'
                }
            language: 語言 ('zh', 'en', 或 'zh-en')

        Returns:
            Hashtag 列表(去重並排序)
        """
        hashtags = []

        # 1. 地標標籤(最高優先級)
        landmarks = detected_items.get('landmarks', [])
        for landmark in landmarks:
            landmark_tags = self.landmark_prompts.get_hashtags(landmark, language)
            hashtags.extend(landmark_tags)

        # 2. 品牌標籤(高優先級)
        brands = detected_items.get('brands', [])
        for brand in brands:
            brand_tags = self.brand_prompts.get_hashtags(brand, language)
            hashtags.extend(brand_tags)

        # 3. 場景標籤(中優先級)
        scene_category = detected_items.get('scene_category')
        if scene_category:
            scene_tags = self.scene_prompts.get_hashtags(scene_category, language)
            hashtags.extend(scene_tags)

        # 去重並保持順序(地標 > 品牌 > 場景)
        seen = set()
        unique_hashtags = []
        for tag in hashtags:
            if tag not in seen:
                seen.add(tag)
                unique_hashtags.append(tag)

        # 返回前 10 個
        return unique_hashtags[:10]

    # Search Functions
    def search_by_location(self, city: str = None, country: str = None) -> Dict:
        """
        根據地點搜尋所有相關內容(地標、品牌)

        Args:
            city: 城市名稱
            country: 國家名稱

        Returns:
            搜尋結果字典
        """
        results = {
            'landmarks': [],
            'brands': []
        }

        # 搜尋地標
        landmarks = self.landmark_prompts.search_by_location(city, country)
        results['landmarks'] = landmarks

        # 品牌通常不按地理位置分類,但可以擴展此功能

        return results

    def detect_landmark_from_image_context(self, detected_objects: List[str],
                                          scene_analysis: Dict) -> Optional[str]:
        """
        根據檢測到的物體和場景分析推測可能的地標

        Args:
            detected_objects: 檢測到的物體列表
            scene_analysis: 場景分析結果

        Returns:
            推測的地標名稱,若無法推測則返回 None
        """
        # 關鍵字映射到地標
        landmark_keywords = {
            'Big Ben': ['clock tower', 'tower', 'bridge', 'river'],
            'Eiffel Tower': ['tower', 'iron structure', 'landmark'],
            'Statue of Liberty': ['statue', 'monument', 'island', 'harbor'],
            'Sydney Opera House': ['building', 'harbor', 'architecture'],
            'Taj Mahal': ['building', 'monument', 'dome'],
            'Pyramids of Giza': ['pyramid', 'desert', 'monument'],
            # 可以擴展更多
        }

        # 簡單的關鍵字匹配
        for landmark, keywords in landmark_keywords.items():
            match_count = sum(1 for obj in detected_objects
                            if any(kw in obj.lower() for kw in keywords))
            if match_count >= 2:  # 至少匹配 2 個關鍵字
                return landmark

        return None

print("✓ PromptLibraryManager (Facade) defined")