Spaces:
Sleeping
Sleeping
import os | |
from subprocess import run | |
from calculate_cosine_similarity import ( | |
load_trained_model, | |
find_most_similar_anchor, | |
find_most_similar_product, | |
recommend_shop_product, | |
) | |
def execute_script(script_name): | |
""" | |
Helper function to execute a Python script. | |
""" | |
print(f"Executing {script_name}...") | |
result = run(["python", script_name], capture_output=True, text=True) | |
if result.returncode == 0: | |
print(f"{script_name} executed successfully.") | |
else: | |
print(f"Error executing {script_name}:") | |
print(result.stderr) | |
def main(): | |
# # Step 0: 모델 학습 | |
print("Step 1: 모델 학습 중...") | |
execute_script("train_model.py") | |
# Step 1: 쇼핑물 상품과 사용자 임베딩 | |
print("Step 2: 쇼핑물 상품과 사용자 임베딩...") | |
execute_script("embed_data.py") | |
# Step 2: product_model.pth 불러오기 | |
print("Step 3: product_model.pth 불러오는 중...") | |
model = load_trained_model("product_model.pth") | |
# Step 3: 추천을 위한 사용자 ID 입력 | |
user_id = input("사용자 ID 입력: ").strip() | |
print(f"사용자 ID: {user_id}에게 추천해줄 상품 찾는 중...") | |
try: | |
# Step 4: 사용자와 가장 유사한 anchor 찾기 | |
print(f"사용자 ID: {user_id} 와 가장 유사한 anchor 찾는 중...") | |
most_similar_anchor, most_similar_anchor_embedding = find_most_similar_anchor( | |
user_id, model | |
) | |
print(f"가장 유사한 anchor: {most_similar_anchor}") | |
# Step 5: anchor와 가장 유사한 상품 찾기 | |
print("anchor와 가장 유사한 학습 상품 찾는 중...") | |
most_similar_product, most_similar_product_embedding = ( | |
find_most_similar_product(most_similar_anchor_embedding, model) | |
) | |
print(f"anchor와 가장 유사한 학습 상품 ID: {most_similar_product}") | |
# Step 6: 쇼핑몰 상품 추천 | |
print("추천 쇼핑몰 상품 찾는 중...") | |
recommended_product_id = recommend_shop_product(most_similar_product_embedding) | |
print(f"추천 쇼핑몰 상품 ID: {recommended_product_id}") | |
except Exception as e: | |
print(f"An error occurred during the recommendation process: {e}") | |
if __name__ == "__main__": | |
main() | |