@@ -57,8 +57,11 @@ def calculate_coordinates(h, s, l):
5757
5858 return round (final_warm , 2 ), round (final_deep , 2 )
5959
60+ from django .conf import settings
61+ import os , json
6062# 데이터 로드
61- with open ("static/data/test_products.json" , "r" , encoding = "utf-8" ) as f :
63+ file_path = os .path .join (settings .STATIC_ROOT , "data" , "test_products.json" )
64+ with open (file_path , "r" , encoding = "utf-8" ) as f :
6265 products = json .load (f )
6366
6467coordinates = []
@@ -96,11 +99,25 @@ def calculate_coordinates(h, s, l):
9699 valid_products [i ]["cluster" ] = int (label )
97100
98101# 클러스터링 결과를 JSON 파일로 저장
99- with open ("static/data/products_clustered.json" , "w" , encoding = "utf-8" ) as f :
102+ # with open("static/data/products_clustered.json", "w", encoding="utf-8") as f:
103+ # json.dump(valid_products, f, ensure_ascii=False, indent=2)
104+
105+ # Create a data directory in MEDIA_ROOT
106+ data_dir = os .path .join (settings .MEDIA_ROOT , "data" )
107+ os .makedirs (data_dir , exist_ok = True )
108+ file_path = os .path .join (data_dir , "products_clustered.json" )
109+ with open (file_path , "w" , encoding = "utf-8" ) as f :
100110 json .dump (valid_products , f , ensure_ascii = False , indent = 2 )
101111
102112# 클러스터 중심 좌표 저장
103- with open ("static/data/cluster_centers.json" , "w" , encoding = "utf-8" ) as f :
113+ # with open("static/data/cluster_centers.json", "w", encoding="utf-8") as f:
114+ # json.dump(kmeans.cluster_centers_.tolist(), f, ensure_ascii=False, indent=2)
115+ data_dir = os .path .join (settings .MEDIA_ROOT , "data" )
116+ os .makedirs (data_dir , exist_ok = True )
117+
118+ file_path = os .path .join (data_dir , "cluster_centers.json" )
119+
120+ with open (file_path , "w" , encoding = "utf-8" ) as f :
104121 json .dump (kmeans .cluster_centers_ .tolist (), f , ensure_ascii = False , indent = 2 )
105122
106123print (" Clustering complete. Files saved." )
0 commit comments