Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -10,6 +10,15 @@ from joblib import dump, load
|
|
10 |
from sklearn.preprocessing import normalize
|
11 |
import re
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def get_next_version(file_prefix, folder='RecommendationFiles/'):
|
14 |
"""Find the latest version of a file and return the next version's filename."""
|
15 |
if not os.path.exists(folder):
|
@@ -46,7 +55,6 @@ def get_latest_version(file_prefix, folder='RecommendationFiles/'):
|
|
46 |
else:
|
47 |
raise FileNotFoundError(f"No versions found for {file_prefix} in folder '{folder}'")
|
48 |
|
49 |
-
|
50 |
def recomienda_tf(new_basket, cestas, productos):
|
51 |
|
52 |
# Get the latest versions of the matrix and vectorizer from the folder
|
@@ -117,34 +125,38 @@ def retroalimentacion(cestas, cesta_nueva):
|
|
117 |
# Debugging message
|
118 |
st.write(f"DEBUG: La nueva cesta es {cesta_unida}")
|
119 |
|
120 |
-
#
|
121 |
-
if
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
cestas.to_csv(file_path, index=False)
|
128 |
|
129 |
-
|
|
|
|
|
130 |
else:
|
131 |
-
st.
|
132 |
-
|
133 |
-
# Re-vectorize the basket DataFrame
|
134 |
-
count_vectorizer = CountVectorizer()
|
135 |
-
count_vectorizer.fit(cestas['Cestas'])
|
136 |
-
count_matrix = count_vectorizer.transform(cestas['Cestas'])
|
137 |
-
tf_matrix = normalize(count_matrix, norm='l1')
|
138 |
-
|
139 |
-
# Save new versions of the vectorizer and matrix
|
140 |
-
count_vectorizer_file = get_next_version('count_vectorizer')
|
141 |
-
tf_matrix_file = get_next_version('tf_matrix')
|
142 |
-
|
143 |
-
dump(count_vectorizer, count_vectorizer_file)
|
144 |
-
dump(tf_matrix, tf_matrix_file)
|
145 |
-
|
146 |
-
# Debugging messages
|
147 |
-
st.write(f"DEBUG: Se ha generado la nueva versión del count_vectorizer: {count_vectorizer_file}")
|
148 |
-
st.write(f"DEBUG: Se ha generado la nueva versión del tf_matrix: {tf_matrix_file}")
|
149 |
|
150 |
-
return None
|
|
|
10 |
from sklearn.preprocessing import normalize
|
11 |
import re
|
12 |
|
13 |
+
def check_write_permissions(folder='RecommendationFiles/'):
|
14 |
+
"""Check if the folder has write permissions."""
|
15 |
+
if os.access(folder, os.W_OK):
|
16 |
+
st.write(f"DEBUG: Tienes permisos de escritura en '{folder}'.")
|
17 |
+
return True
|
18 |
+
else:
|
19 |
+
st.write(f"DEBUG: No tienes permisos de escritura en '{folder}'.")
|
20 |
+
return False
|
21 |
+
|
22 |
def get_next_version(file_prefix, folder='RecommendationFiles/'):
|
23 |
"""Find the latest version of a file and return the next version's filename."""
|
24 |
if not os.path.exists(folder):
|
|
|
55 |
else:
|
56 |
raise FileNotFoundError(f"No versions found for {file_prefix} in folder '{folder}'")
|
57 |
|
|
|
58 |
def recomienda_tf(new_basket, cestas, productos):
|
59 |
|
60 |
# Get the latest versions of the matrix and vectorizer from the folder
|
|
|
125 |
# Debugging message
|
126 |
st.write(f"DEBUG: La nueva cesta es {cesta_unida}")
|
127 |
|
128 |
+
# Check if we have write permissions in the folder
|
129 |
+
if check_write_permissions('RecommendationFiles/'):
|
130 |
+
# Add the new basket to the historical baskets if it doesn't already exist
|
131 |
+
if not cestas['Cestas'].isin([cesta_unida]).any():
|
132 |
+
cestas.loc[len(cestas)] = cesta_unida
|
133 |
+
st.success("✓ Cesta añadida al DataFrame.")
|
134 |
+
|
135 |
+
# Re-save the updated baskets DataFrame
|
136 |
+
file_path = 'RecommendationFiles/cestas_final.csv'
|
137 |
+
cestas.to_csv(file_path, index=False)
|
138 |
+
|
139 |
+
st.write(f"DEBUG: Se ha guardado la nueva cesta en {file_path}")
|
140 |
+
else:
|
141 |
+
st.warning("⚠️ La cesta ya existe en el DataFrame.")
|
142 |
+
|
143 |
+
# Re-vectorize the basket DataFrame
|
144 |
+
count_vectorizer = CountVectorizer()
|
145 |
+
count_vectorizer.fit(cestas['Cestas'])
|
146 |
+
count_matrix = count_vectorizer.transform(cestas['Cestas'])
|
147 |
+
tf_matrix = normalize(count_matrix, norm='l1')
|
148 |
+
|
149 |
+
# Save new versions of the vectorizer and matrix
|
150 |
+
count_vectorizer_file = get_next_version('count_vectorizer')
|
151 |
+
tf_matrix_file = get_next_version('count_matrix')
|
152 |
|
153 |
+
dump(count_vectorizer, count_vectorizer_file)
|
154 |
+
dump(tf_matrix, tf_matrix_file)
|
|
|
155 |
|
156 |
+
# Debugging messages
|
157 |
+
st.write(f"DEBUG: Se ha generado la nueva versión del count_vectorizer: {count_vectorizer_file}")
|
158 |
+
st.write(f"DEBUG: Se ha generado la nueva versión del tf_matrix: {tf_matrix_file}")
|
159 |
else:
|
160 |
+
st.error("No se puede escribir en la carpeta 'RecommendationFiles/'. Verifica los permisos.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
+
return None
|