import os from typing import Optional, List import spacy from huggingface_hub import snapshot_download hf_token: str = os.getenv('HF_TOKEN') class NameMatchingInference: @classmethod def get_name_matches(cls, payer: str, company_id: str): model_repo: str = f"siva-valyx/name-matching-model-{company_id}" # Download the model using Hugging Face's API model_path: str = snapshot_download(repo_id=model_repo, revision="main", token=hf_token) # Load the model in spaCy nlp = spacy.load(model_path) doc = nlp(payer) sorted_cats = sorted(doc.cats.items(), key=lambda item: item[1], reverse=True) top_matches: List[str] = [] top_score: float = sorted_cats[0][1] if top_score < 0.03: return top_matches similarity_threshold: float = top_score * 0.3 for customer_id, score in sorted_cats: if top_score - score > similarity_threshold: break top_matches.append(customer_id) return top_matches