Views
No views yet
pip install -U sentence-transformers1from sentence_transformers import SentenceTransformer
2sentences = ["Isto é um exemplo", "Isto é um outro exemplo"]
3
4model = SentenceTransformer('stjiris/bert-large-portuguese-cased-legal-tsdae-gpl-nli-sts-MetaKD-v0')
5embeddings = model.encode(sentences)
6print(embeddings)1from transformers import AutoTokenizer, AutoModel
2import torch
3
4
5#Mean Pooling - Take attention mask into account for correct averaging
6def mean_pooling(model_output, attention_mask):
7 token_embeddings = model_output[0] #First element of model_output contains all token embeddings
8 input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
9 return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
10
11# Sentences we want sentence embeddings for
12sentences = ['This is an example sentence', 'Each sentence is converted']
13
14# Load model from HuggingFace Hub
15tokenizer = AutoTokenizer.from_pretrained('stjiris/bert-large-portuguese-cased-legal-tsdae-gpl-nli-sts-MetaKD-v0')
16model = AutoModel.from_pretrained('stjiris/bert-large-portuguese-cased-legal-tsdae-gpl-nli-sts-MetaKD-v0')
17
18# Tokenize sentences
19encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
20
21# Compute token embeddings
22with torch.no_grad():
23 model_output = model(**encoded_input)
24# Perform pooling. In this case, mean pooling.
25sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
26print("Sentence embeddings:")
27print(sentence_embeddings)SentenceTransformer(
(0): Transformer({'max_seq_length': 514, 'do_lower_case': False}) with Transformer model: BertModel
(1): Pooling({'word_embedding_dimension': 1028, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False})
)1@InProceedings{MeloSemantic,
2 author="Melo, Rui
3 and Santos, Pedro A.
4 and Dias, Jo{\~a}o",
5 editor="Moniz, Nuno
6 and Vale, Zita
7 and Cascalho, Jos{\'e}
8 and Silva, Catarina
9 and Sebasti{\~a}o, Raquel",
10 title="A Semantic Search System for the Supremo Tribunal de Justi{\c{c}}a",
11 booktitle="Progress in Artificial Intelligence",
12 year="2023",
13 publisher="Springer Nature Switzerland",
14 address="Cham",
15 pages="142--154",
16 abstract="Many information retrieval systems use lexical approaches to retrieve information. Such approaches have multiple limitations, and these constraints are exacerbated when tied to specific domains, such as the legal one. Large language models, such as BERT, deeply understand a language and may overcome the limitations of older methodologies, such as BM25. This work investigated and developed a prototype of a Semantic Search System to assist the Supremo Tribunal de Justi{\c{c}}a (Portuguese Supreme Court of Justice) in its decision-making process. We built a Semantic Search System that uses specially trained BERT models (Legal-BERTimbau variants) and a Hybrid Search System that incorporates both lexical and semantic techniques by combining the capabilities of BM25 and the potential of Legal-BERTimbau. In this context, we obtained a {\$}{\$}335{\backslash}{\%}{\$}{\$}335{\%}increase on the discovery metric when compared to BM25 for the first query result. This work also provides information on the most relevant techniques for training a Large Language Model adapted to Portuguese jurisprudence and introduces a new technique of Metadata Knowledge Distillation.",
17 isbn="978-3-031-49011-8"
18}
19
20
21@inproceedings{souza2020bertimbau,
22 author = {F{\'a}bio Souza and
23 Rodrigo Nogueira and
24 Roberto Lotufo},
25 title = {{BERT}imbau: pretrained {BERT} models for {B}razilian {P}ortuguese},
26 booktitle = {9th Brazilian Conference on Intelligent Systems, {BRACIS}, Rio Grande do Sul, Brazil, October 20-23 (to appear)},
27 year = {2020}
28}
29
30@inproceedings{fonseca2016assin,
31 title={ASSIN: Avaliacao de similaridade semantica e inferencia textual},
32 author={Fonseca, E and Santos, L and Criscuolo, Marcelo and Aluisio, S},
33 booktitle={Computational Processing of the Portuguese Language-12th International Conference, Tomar, Portugal},
34 pages={13--15},
35 year={2016}
36}
37
38@inproceedings{real2020assin,
39 title={The assin 2 shared task: a quick overview},
40 author={Real, Livy and Fonseca, Erick and Oliveira, Hugo Goncalo},
41 booktitle={International Conference on Computational Processing of the Portuguese Language},
42 pages={406--412},
43 year={2020},
44 organization={Springer}
45}
46@InProceedings{huggingface:dataset:stsb_multi_mt,
47title = {Machine translated multilingual STS benchmark dataset.},
48author={Philip May},
49year={2021},
50url={https://github.com/PhilipMay/stsb-multi-mt}
51}
52