File size: 1,024 Bytes
cab461d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# ai_mapper.py

from sentence_transformers import SentenceTransformer, util

# Load model once at module level
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

def ai_map_fields(extracted_keys, object_fields):
    try:
        mappings = {}
        confidence_scores = {}

        field_embeddings = model.encode(object_fields, convert_to_tensor=True)

        for key in extracted_keys:
            if key.lower() in ["name", "email"]:
                continue

            key_embedding = model.encode(key, convert_to_tensor=True)
            cosine_scores = util.pytorch_cos_sim(key_embedding, field_embeddings)[0]
            best_score_idx = cosine_scores.argmax().item()
            best_match = object_fields[best_score_idx]
            confidence = cosine_scores[best_score_idx].item()

            mappings[key] = best_match
            confidence_scores[key] = round(confidence, 2)

        return mappings, confidence_scores, None
    except Exception as e:
        return None, None, str(e)