Spaces:
Sleeping
Sleeping
feat: implement project embedding engine and feature similarity calculation modules
Browse files
src/similarity_model/embedding_engine.py
CHANGED
|
@@ -11,7 +11,7 @@ from Data.database.sql_connector import load_preprocessed_projects
|
|
| 11 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
DEFAULT_MODEL = "all-
|
| 15 |
TEXT_COL = "clean_text"
|
| 16 |
TITLE_COL = "project_title"
|
| 17 |
TECH_COL = "technologies"
|
|
|
|
| 11 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
+
DEFAULT_MODEL = "all-mpnet-base-v2"
|
| 15 |
TEXT_COL = "clean_text"
|
| 16 |
TITLE_COL = "project_title"
|
| 17 |
TECH_COL = "technologies"
|
src/similarity_model/feature_similarity.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from typing import List, Dict, Any
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
from scipy.optimize import linear_sum_assignment
|
| 5 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
@@ -109,6 +110,17 @@ def empty_result(unique_a=None, unique_b=None):
|
|
| 109 |
"unique_b": unique_b or []
|
| 110 |
}
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
def encode_features(
|
| 113 |
features: List[str],
|
| 114 |
model
|
|
@@ -117,12 +129,10 @@ def encode_features(
|
|
| 117 |
if not features:
|
| 118 |
return np.array([])
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
)
|
| 125 |
-
return vectors.astype("float32")
|
| 126 |
|
| 127 |
def compute_feature_similarity(
|
| 128 |
features_a,
|
|
|
|
| 1 |
from typing import List, Dict, Any
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
from scipy.optimize import linear_sum_assignment
|
| 6 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
| 110 |
"unique_b": unique_b or []
|
| 111 |
}
|
| 112 |
|
| 113 |
+
@lru_cache(maxsize=10000)
|
| 114 |
+
def encode_single_feature(feature: str) -> np.ndarray:
|
| 115 |
+
import numpy as np
|
| 116 |
+
model = load_feature_model()
|
| 117 |
+
return model.encode(
|
| 118 |
+
[feature],
|
| 119 |
+
convert_to_numpy=True,
|
| 120 |
+
normalize_embeddings=True,
|
| 121 |
+
show_progress_bar=False
|
| 122 |
+
)[0].astype("float32")
|
| 123 |
+
|
| 124 |
def encode_features(
|
| 125 |
features: List[str],
|
| 126 |
model
|
|
|
|
| 129 |
if not features:
|
| 130 |
return np.array([])
|
| 131 |
|
| 132 |
+
embeddings = []
|
| 133 |
+
for feat in features:
|
| 134 |
+
embeddings.append(encode_single_feature(feat))
|
| 135 |
+
return np.array(embeddings)
|
|
|
|
|
|
|
| 136 |
|
| 137 |
def compute_feature_similarity(
|
| 138 |
features_a,
|