diff --git a/app/api/api_recommendation.py b/app/api/api_recommendation.py index 5f71d38..faeb780 100644 --- a/app/api/api_recommendation.py +++ b/app/api/api_recommendation.py @@ -134,21 +134,36 @@ async def get_recommendations(user_id: int, category: str, brand_id: int, brand_ # 合并分数 if brand_id is not None: - if brand_id is not None: - brand_idx_feature = matrix_data["brand_index_map"].get(brand_id) - if brand_idx_feature is not None and valid_sketch_idxs_feature: - raw_brand_feat_scores = matrix_data["brand_feature_matrix"][ - brand_idx_feature, valid_sketch_idxs_feature] - raw_brand_feat_scores = (raw_brand_feat_scores - np.min(raw_brand_feat_scores)) / ( - np.max(raw_brand_feat_scores) - np.min(raw_brand_feat_scores) + 1e-8) - processed_brand_feat = raw_brand_feat_scores - final_scores = processed_inter + 0.3 * ((1 - brand_scale) * processed_feat + brand_scale * processed_brand_feat) - else: - final_scores = processed_inter + 0.3 * processed_feat + brand_idx_feature = matrix_data["brand_index_map"].get(brand_id) + + brand_feat_valid = ( + matrix_data["brand_feature_matrix"].size > 0 and # 矩阵非空 + brand_idx_feature is not None and + valid_sketch_idxs_feature # 有可用索引 + ) + + if brand_feat_valid: + raw_brand_feat_scores = matrix_data["brand_feature_matrix"][ + brand_idx_feature, valid_sketch_idxs_feature + ] + raw_brand_feat_scores = (raw_brand_feat_scores - np.min(raw_brand_feat_scores)) / ( + np.max(raw_brand_feat_scores) - np.min(raw_brand_feat_scores) + 1e-8 + ) + processed_brand_feat = raw_brand_feat_scores + + # 如果 processed_feat 是空的,替换为全 0,避免 shape 不一致 + if processed_feat.size == 0: + processed_feat = np.zeros_like(processed_brand_feat) + + final_scores = processed_inter + 0.3 * ( + (1 - brand_scale) * processed_feat + brand_scale * processed_brand_feat + ) else: - final_scores = processed_inter + 0.3 * processed_feat + # brand 信息不可用 + final_scores = processed_inter + 0.3 * processed_feat if processed_feat.size > 0 else processed_inter else: - final_scores = processed_inter + 0.3 * processed_feat + final_scores = processed_inter + 0.3 * processed_feat if processed_feat.size > 0 else processed_inter + valid_sketch_idxs = matrix_data["cached_valid_idxs"][cache_key] # 概率采样 diff --git a/app/service/recommend/scheduled_task.py b/app/service/recommend/scheduled_task.py index f6b52ef..6dc67e8 100644 --- a/app/service/recommend/scheduled_task.py +++ b/app/service/recommend/scheduled_task.py @@ -527,7 +527,7 @@ def update_heat_matrices(): if __name__ == "__main__": try: - # update_user_matrices() + update_user_matrices() update_heat_matrices() # scheduler = BlockingScheduler() # scheduler.add_job(update_user_matrices, 'cron', hour=12, timezone='Asia/Shanghai') diff --git a/app/service/recommend/service.py b/app/service/recommend/service.py index b3545f2..0db64dd 100644 --- a/app/service/recommend/service.py +++ b/app/service/recommend/service.py @@ -55,9 +55,20 @@ def load_resources(): matrix_data["feature_matrix"] = np.load(f"{RECOMMEND_PATH_PREFIX}feature_matrix.npy", allow_pickle=True) - matrix_data["brand_feature_matrix"] = np.load(f"{RECOMMEND_PATH_PREFIX}brand_feature_matrix.npy", allow_pickle=True) + brand_feature_path = f"{RECOMMEND_PATH_PREFIX}brand_feature_matrix.npy" + if os.path.exists(brand_feature_path): + matrix_data["brand_feature_matrix"] = np.load(brand_feature_path, allow_pickle=True) + else: + logger.warning("brand_feature_matrix 文件不存在,使用空数组") + matrix_data["brand_feature_matrix"] = np.array([]) - matrix_data["brand_index_map"] = np.load(f"{RECOMMEND_PATH_PREFIX}brand_index_map.npy",allow_pickle=True).item() + # brand_index_map + brand_index_path = f"{RECOMMEND_PATH_PREFIX}brand_index_map.npy" + if os.path.exists(brand_index_path): + matrix_data["brand_index_map"] = np.load(brand_index_path, allow_pickle=True).item() + else: + logger.warning("brand_index_map 文件不存在,使用空字典") + matrix_data["brand_index_map"] = {} matrix_data["user_index_feature"] = np.load(f"{RECOMMEND_PATH_PREFIX}user_index_feature_matrix.npy", allow_pickle=True).item()