Pixcribe / prompt_library_manager.py
DawnC's picture
Upload 22 files
6a3bd1f verified
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")