Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
import warnings
|
|
@@ -6,11 +7,50 @@ from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
|
|
| 6 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
from joblib import dump, load
|
| 8 |
from sklearn.preprocessing import normalize
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def recomienda_tf(new_basket, cestas, productos):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Cargar la matriz TF y el modelo
|
| 12 |
-
tf_matrix = load(
|
| 13 |
-
count = load(
|
| 14 |
|
| 15 |
# Convertir la nueva cesta en formato TF (Term Frequency)
|
| 16 |
new_basket_str = ' '.join(new_basket)
|
|
@@ -86,5 +126,4 @@ def retroalimentacion(cestas, cesta_nueva):
|
|
| 86 |
dump(tf_matrix, tf_matrix_file)
|
| 87 |
|
| 88 |
|
| 89 |
-
return None
|
| 90 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import warnings
|
|
|
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
from joblib import dump, load
|
| 9 |
from sklearn.preprocessing import normalize
|
| 10 |
+
import re
|
| 11 |
+
|
| 12 |
+
def get_next_version(file_prefix, folder='RecommendationFiles/'):
|
| 13 |
+
"""Find the latest version of a file and return the next version's filename."""
|
| 14 |
+
# Regular expression to match files like 'file_0001.joblib'
|
| 15 |
+
pattern = re.compile(rf"{file_prefix}_(\d+)\.joblib")
|
| 16 |
+
files = [f for f in os.listdir(folder) if pattern.match(f)]
|
| 17 |
+
|
| 18 |
+
# Extract version numbers from matching files
|
| 19 |
+
versions = [int(pattern.match(f).group(1)) for f in files]
|
| 20 |
+
|
| 21 |
+
# Determine the next version number
|
| 22 |
+
if versions:
|
| 23 |
+
next_version = max(versions) + 1
|
| 24 |
+
else:
|
| 25 |
+
next_version = 1 # If no versions exist, start with 1
|
| 26 |
+
|
| 27 |
+
# Return the next version filename
|
| 28 |
+
return f"{file_prefix}_{next_version:04d}.joblib"
|
| 29 |
+
|
| 30 |
+
def get_latest_version(file_prefix, folder='RecommendationFiles/'):
|
| 31 |
+
"""Find the latest version of a file to load."""
|
| 32 |
+
# Regular expression to match files like 'file_0001.joblib'
|
| 33 |
+
pattern = re.compile(rf"{file_prefix}_(\d+)\.joblib")
|
| 34 |
+
files = [f for f in os.listdir(folder) if pattern.match(f)]
|
| 35 |
+
|
| 36 |
+
# Extract version numbers from matching files
|
| 37 |
+
versions = [int(pattern.match(f).group(1)) for f in files]
|
| 38 |
+
|
| 39 |
+
if versions:
|
| 40 |
+
latest_version = max(versions)
|
| 41 |
+
return f"{file_prefix}_{latest_version:04d}.joblib"
|
| 42 |
+
else:
|
| 43 |
+
raise FileNotFoundError(f"No versions found for {file_prefix}")
|
| 44 |
+
|
| 45 |
|
| 46 |
def recomienda_tf(new_basket, cestas, productos):
|
| 47 |
+
|
| 48 |
+
tf_matrix_file = get_latest_version('count_matrix')
|
| 49 |
+
count_vectorizer_file = get_latest_version('count_vectorizer')
|
| 50 |
+
|
| 51 |
# Cargar la matriz TF y el modelo
|
| 52 |
+
tf_matrix = load(tf_matrix_file)
|
| 53 |
+
count = load(count_vectorizer_file)
|
| 54 |
|
| 55 |
# Convertir la nueva cesta en formato TF (Term Frequency)
|
| 56 |
new_basket_str = ' '.join(new_basket)
|
|
|
|
| 126 |
dump(tf_matrix, tf_matrix_file)
|
| 127 |
|
| 128 |
|
| 129 |
+
return None
|
|
|