File size: 897 Bytes
2a23a8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""Run masked-word prediction, NLI and embeddings from a downloaded snapshot."""
from pathlib import Path

from unified_encoder import UnifiedArabicEncoder


def main():
    model = UnifiedArabicEncoder(Path(__file__).resolve().parent)
    print(model.fill_mask(['تقع القاهرة على ضفاف نهر [MASK].'], top_k=5))
    probabilities = model.classify(
        ['يقرأ الطفل كتاباً في المكتبة.'],
        ['الطفل يقرأ كتاباً.'],
    )[0]
    print(dict(zip(('entailment', 'neutral', 'contradiction'), probabilities.tolist())))
    texts = ['يقرأ الطفل كتاباً.', 'الطفل يقرأ كتاباً.', 'توقفت الحافلة عند المحطة.']
    embeddings = model.encode(texts)
    print('Embeddings:', embeddings.shape, 'query scores:', (embeddings[0] @ embeddings[1:].T).tolist())


if __name__ == '__main__':
    main()