File size: 2,290 Bytes
2dfec3c
 
d917b06
2dfec3c
d917b06
 
 
 
 
2dfec3c
 
 
 
 
 
 
 
 
 
 
 
 
 
d917b06
2dfec3c
067bb59
2dfec3c
 
 
067bb59
2dfec3c
 
 
067bb59
2dfec3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4a0526
 
d917b06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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("모델 학습 중...")
    execute_script("train_model.py")

    # Step 1: 쇼핑물 상품과 사용자 임베딩
    print("쇼핑물 상품과 사용자 임베딩...")
    execute_script("embed_data.py")

    # Step 2: product_model.pth 불러오기
    print("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()