|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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 = [] |
|
|
|
|
|
|
|
|
landmarks = detected_items.get('landmarks', []) |
|
|
for landmark in landmarks: |
|
|
landmark_tags = self.landmark_prompts.get_hashtags(landmark, language) |
|
|
hashtags.extend(landmark_tags) |
|
|
|
|
|
|
|
|
brands = detected_items.get('brands', []) |
|
|
for brand in brands: |
|
|
brand_tags = self.brand_prompts.get_hashtags(brand, language) |
|
|
hashtags.extend(brand_tags) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
return unique_hashtags[:10] |
|
|
|
|
|
|
|
|
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: |
|
|
return landmark |
|
|
|
|
|
return None |
|
|
|
|
|
print("✓ PromptLibraryManager (Facade) defined") |
|
|
|