Create core_model.py
Browse files- core_model.py +28 -0
core_model.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class CoreModel:
|
| 6 |
+
def __init__(self, model_name="all-MiniLM-L6-v2"):
|
| 7 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
self.model = SentenceTransformer(model_name).to(self.device)
|
| 9 |
+
|
| 10 |
+
def compute_similarity(self, input_text: str, candidates: list[str]) -> list[tuple[str, float]]:
|
| 11 |
+
"""
|
| 12 |
+
Расчитывает сходство входного текста с каждым из кандидатов и возвращает отсортированный список.
|
| 13 |
+
"""
|
| 14 |
+
input_emb = self.model.encode(input_text, convert_to_tensor=True).to(self.device)
|
| 15 |
+
candidates_emb = self.model.encode(candidates, convert_to_tensor=True).to(self.device)
|
| 16 |
+
|
| 17 |
+
similarities = util.cos_sim(input_emb, candidates_emb)[0]
|
| 18 |
+
scored_candidates = [(candidates[i], float(similarities[i])) for i in range(len(candidates))]
|
| 19 |
+
scored_candidates.sort(key=lambda x: x[1], reverse=True)
|
| 20 |
+
|
| 21 |
+
return scored_candidates
|
| 22 |
+
|
| 23 |
+
def extract_top_k(self, input_text: str, candidates: list[str], top_k: int = 3) -> list[str]:
|
| 24 |
+
"""
|
| 25 |
+
Возвращает топ-K наиболее релевантных кандидатов на основе сходства.
|
| 26 |
+
"""
|
| 27 |
+
similarities = self.compute_similarity(input_text, candidates)
|
| 28 |
+
return [candidate for candidate, _ in similarities[:top_k]]
|