README.md
| 1 | --- |
| 2 | language: pt |
| 3 | license: apache-2.0 |
| 4 | |
| 5 | widget: |
| 6 | - text: "O futuro de DI caiu 20 bps nesta manhã" |
| 7 | example_title: "Example 1" |
| 8 | - text: "O Nubank decidiu cortar a faixa de preço da oferta pública inicial (IPO) após revés no humor dos mercados internacionais com as fintechs." |
| 9 | example_title: "Example 2" |
| 10 | - text: "O Ibovespa acompanha correção do mercado e fecha com alta moderada" |
| 11 | example_title: "Example 3" |
| 12 | --- |
| 13 | |
| 14 | # FinBERT-PT-BR : Financial BERT PT BR |
| 15 | |
| 16 | FinBERT-PT-BR is a pre-trained NLP model to analyze sentiment of Brazilian Portuguese financial texts. |
| 17 | |
| 18 | The model was trained in two main stages: language modeling and sentiment modeling. In the first stage, a language model was trained with more than 1.4 million texts of financial news in Portuguese. |
| 19 | From this first training, it was possible to build a sentiment classifier with few labeled texts (500) that presented a satisfactory convergence. |
| 20 | |
| 21 | At the end of the work, a comparative analysis with other models and the possible applications of the developed model are presented. |
| 22 | In the comparative analysis, it was possible to observe that the developed model presented better results than the current models in the state of the art. |
| 23 | Among the applications, it was demonstrated that the model can be used to build sentiment indices, investment strategies and macroeconomic data analysis, such as inflation. |
| 24 | |
| 25 | ## Applications |
| 26 | |
| 27 | ### Sentiment Index |
| 28 | |
| 29 |  |
| 30 | |
| 31 | |
| 32 | ## Usage |
| 33 | |
| 34 | #### BertForSequenceClassification |
| 35 | |
| 36 | ```python |
| 37 | from transformers import AutoTokenizer, BertForSequenceClassification |
| 38 | import numpy as np |
| 39 | |
| 40 | pred_mapper = { |
| 41 | 0: "POSITIVE", |
| 42 | 1: "NEGATIVE", |
| 43 | 2: "NEUTRAL" |
| 44 | } |
| 45 | |
| 46 | tokenizer = AutoTokenizer.from_pretrained("lucas-leme/FinBERT-PT-BR") |
| 47 | finbertptbr = BertForSequenceClassification.from_pretrained("lucas-leme/FinBERT-PT-BR") |
| 48 | |
| 49 | tokens = tokenizer(["Hoje a bolsa caiu", "Hoje a bolsa subiu"], return_tensors="pt", |
| 50 | padding=True, truncation=True, max_length=512) |
| 51 | finbertptbr_outputs = finbertptbr(**tokens) |
| 52 | |
| 53 | preds = [pred_mapper[np.argmax(pred)] for pred in finbertptbr_outputs.logits.cpu().detach().numpy()] |
| 54 | ``` |
| 55 | |
| 56 | #### Pipeline |
| 57 | |
| 58 | ```python |
| 59 | from transformers import ( |
| 60 | AutoTokenizer, |
| 61 | BertForSequenceClassification, |
| 62 | pipeline, |
| 63 | ) |
| 64 | |
| 65 | finbert_pt_br_tokenizer = AutoTokenizer.from_pretrained("lucas-leme/FinBERT-PT-BR") |
| 66 | finbert_pt_br_model = BertForSequenceClassification.from_pretrained("lucas-leme/FinBERT-PT-BR") |
| 67 | |
| 68 | finbert_pt_br_pipeline = pipeline(task='text-classification', model=finbert_pt_br_model, tokenizer=finbert_pt_br_tokenizer) |
| 69 | finbert_pt_br_pipeline(['Hoje a bolsa caiu', 'Hoje a bolsa subiu']) |
| 70 | ``` |
| 71 | |
| 72 | ## Author |
| 73 | |
| 74 | - [Lucas Leme](https://www.linkedin.com/in/lucas-leme-santos/) - lucaslssantos99@gmail.com |
| 75 | |
| 76 | ## Citation |
| 77 | |
| 78 | ```latex |
| 79 | @inproceedings{santos2023finbert, |
| 80 | title={FinBERT-PT-BR: An{\'a}lise de Sentimentos de Textos em Portugu{\^e}s do Mercado Financeiro}, |
| 81 | author={Santos, Lucas L and Bianchi, Reinaldo AC and Costa, Anna HR}, |
| 82 | booktitle={Anais do II Brazilian Workshop on Artificial Intelligence in Finance}, |
| 83 | pages={144--155}, |
| 84 | year={2023}, |
| 85 | organization={SBC} |
| 86 | } |
| 87 | ``` |
| 88 | |
| 89 | ## Paper |
| 90 | |
| 91 | - Paper: [FinBERT-PT-BR: Sentiment Analysis of Texts in Portuguese from the Financial Market](https://sol.sbc.org.br/index.php/bwaif/article/view/24960) |
| 92 | - Undergraduate thesis: [FinBERT-PT-BR: Análise de sentimentos de textos em português referentes ao mercado financeiro](https://pcs.usp.br/pcspf/wp-content/uploads/sites/8/2022/12/Monografia_PCS3860_COOP_2022_Grupo_C12.pdf) |
| 93 | |