diff --git a/app/api/api_recommendation.py b/app/api/api_recommendation.py index faeb780..4084e46 100644 --- a/app/api/api_recommendation.py +++ b/app/api/api_recommendation.py @@ -40,34 +40,44 @@ def softmax(scores): sum_exp = sum(exp_scores) return [s / sum_exp for s in exp_scores] +# def get_random_recommendations(category: str, num: int) -> List[str]: +# """根据预加载热度向量推荐(冷启动)""" +# try: +# heat_data = matrix_data.get("heat_data", {}) +# +# if category not in heat_data: +# raise ValueError(f"热度数据缺少类别 {category},使用随机推荐") +# +# heat_dict = heat_data[category] # {url: score} +# urls = list(heat_dict.keys()) +# scores = list(heat_dict.values()) +# +# if not urls: +# raise ValueError("该类别下无热度记录,使用随机推荐") +# +# probs = softmax(scores) +# sample_size = min(num, len(urls)) +# sampled_urls = random.choices(urls, weights=probs, k=sample_size) +# +# return sampled_urls +# +# except Exception as e: +# # 回退:完全随机推荐 +# all_iids = list(matrix_data["iid_to_sketch"].keys()) +# category_iids = matrix_data["category_to_iids"].get(category, all_iids) +# sample_size = min(num, len(category_iids)) +# sampled = np.random.choice(category_iids, size=sample_size, replace=False) +# return [matrix_data["iid_to_sketch"][iid] for iid in sampled] + def get_random_recommendations(category: str, num: int) -> List[str]: - """根据预加载热度向量推荐(冷启动)""" - try: - heat_data = matrix_data.get("heat_data", {}) - - if category not in heat_data: - raise ValueError(f"热度数据缺少类别 {category},使用随机推荐") - - heat_dict = heat_data[category] # {url: score} - urls = list(heat_dict.keys()) - scores = list(heat_dict.values()) - - if not urls: - raise ValueError("该类别下无热度记录,使用随机推荐") - - probs = softmax(scores) - sample_size = min(num, len(urls)) - sampled_urls = random.choices(urls, weights=probs, k=sample_size) - - return sampled_urls - - except Exception as e: - # 回退:完全随机推荐 - all_iids = list(matrix_data["iid_to_sketch"].keys()) - category_iids = matrix_data["category_to_iids"].get(category, all_iids) - sample_size = min(num, len(category_iids)) - sampled = np.random.choice(category_iids, size=sample_size, replace=False) - return [matrix_data["iid_to_sketch"][iid] for iid in sampled] + """全品类随机推荐""" + all_iids = list(matrix_data["iid_to_sketch"].keys()) + # 优先从当前品类选择 + category_iids = matrix_data["category_to_iids"].get(category, all_iids) + # 确保不超出实际数量 + sample_size = min(num, len(category_iids)) + sampled = np.random.choice(category_iids, size=sample_size, replace=False) + return [matrix_data["iid_to_sketch"][iid] for iid in sampled] @router.get("/recommend/{user_id}/{category}/{num_recommendations}/{brand_id}/{brand_scale}", response_model=List[str]) diff --git a/app/service/recommend/scheduled_task.py b/app/service/recommend/scheduled_task.py index 6dc67e8..d3174ed 100644 --- a/app/service/recommend/scheduled_task.py +++ b/app/service/recommend/scheduled_task.py @@ -527,12 +527,12 @@ def update_heat_matrices(): if __name__ == "__main__": try: - update_user_matrices() - update_heat_matrices() - # scheduler = BlockingScheduler() - # scheduler.add_job(update_user_matrices, 'cron', hour=12, timezone='Asia/Shanghai') - # logging.info("定时任务已启动,每天12:00执行") - # scheduler.start() + # update_user_matrices() + # update_heat_matrices() + scheduler = BlockingScheduler() + scheduler.add_job(update_user_matrices, 'cron', hour=12, timezone='Asia/Shanghai') + logging.info("定时任务已启动,每天12:00执行") + scheduler.start() except KeyboardInterrupt: logging.info("定时任务已停止") except Exception as e: