EnzoBustos commited on
Commit
5ba5a89
1 Parent(s): 1bd8d5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -118
app.py CHANGED
@@ -1,112 +1,124 @@
 
 
 
1
  from textblob import TextBlob
2
  from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
3
- from transformers import pipeline
4
- import pandas as pd
5
  import re
6
- import streamlit as st
7
-
8
 
9
  def translate_text_blob(text):
10
  blob = TextBlob(text)
11
  return str(blob.translate(from_lang="pt", to="en"))
12
 
 
13
 
14
- def translate_text_transformer(text, pipeline):
15
- return pipeline(text)[0]["translation_text"]
16
-
17
-
18
- def sentiment_vader(sentence, vader_object):
19
 
20
- sentiment_dict = vader_object.polarity_scores(sentence)
21
  negative = sentiment_dict['neg']
22
  neutral = sentiment_dict['neu']
23
  positive = sentiment_dict['pos']
24
  compound = sentiment_dict['compound']
25
 
26
- if sentiment_dict['compound'] >= 0.05:
27
  overall_sentiment = "Positive"
28
 
29
- elif sentiment_dict['compound'] <= - 0.05:
30
  overall_sentiment = "Negative"
31
 
32
- else:
33
  overall_sentiment = "Neutral"
34
-
35
  return overall_sentiment.upper()
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- def sentiment_finbert(text, pipeline):
39
- result = pipeline(text)[0]
40
- return result["label"].upper()
41
-
42
-
43
- def theme_classification(text, text_classifier):
44
- if re.search(r"/Biomm|Nortcquimica|Ourofino|Alliar|Dasa|Fleury|Hapvida|Ihpardini|Kora Saude|Mater Dei|Odontoprev|Oncoclinicas|Qualicorp |Rede D Or|Baumer|Lifemed|Blau|D1000vfarma|Dimed|Hypera|Pague Menos|Profarma|Raiadrogasil|Viveo|Drogaraia|Ultrafarma|Drogasil|Raia/gm", text):
45
- return "Health"
46
- elif re.search(r"/Aura 360|Bradespar|Cba|Csnmineracao|Litel|Litela|Mmx Miner|Vale|Ferbasa|Gerdau|Gerdau Met|Sid Nacional|Usiminas|Mangels Indl|Panatlantica|Tekno|Paranapanema|Braskem|Dexxos Par|Fer Heringer|Nutriplant|Vittia|Cristal|Unipar|Dexco|Eucatex|Klabin|Melhor Sp|Suzano Hold|Suzano|Irani|Metal Iguacu|Sansuy|Eternit|Haga|Portobello|Azevedo|Sondotecnica|Tecnosolo|Embraer|Fras-le|Marcopolo|Randon Part|Recrusul|Riosulense|Tupy|Wetzel S\/a|Schulz|Weg|Aco Altona|Aeris|Armac|Bardella|Inepar|Kepler Weber|Metalfrio|Mills|Nordon Met|Pratica|Romi|Metisa|Stara|Taurus Armas|Azul|Gol|All Norte|All Paulista|Fer C Atlant|Mrs Logist|Rumo|Hidrovias|Log-in|Trevisa|Jsl|Tegma|Autoban|Ccr Sa|Conc Raposo|Conc Rio Ter|Ecopistas|Ecorodovias|Ecovias|Rod Colinas|Rod Tiete|Rt Bandeiras|Triangulosol|Triunfo Part|Viaoeste|Gruairport|Hmobi S.a|Invepar|Porto Vm|Salus Infra|Santos Brp|Wilson Sons|Atmasa|Bbmlogistica|Dtcom-direct|Estapar|Flex|Gps|Priner|Sequoia Log|Valid|Embpar|Minasmaquina|Rodobens|Wlm Ind Com/gm", text):
47
- return "Industrial Goods"
48
- elif re.search(r"/Algar Telec|Brisanet|Desktop|Oi|Telebras|Telef Brasil|Tim|Unifique|Cinesystem|Eletromidia|Vivo|Claro|Tim/gm", text):
49
- return "Communications"
50
- elif re.search(r"/Alphaville|Const A Lind|Cr2Cury|Cyrela Realt|DirecionalEvenEztecGafisaHelborInter SaJhsf PartJoao FortesKallas|Lavvi|Melnick|Mitre Realty|Moura Dubeux|MrvPdg RealtPlanoeplano|Rni|Rossi ResidTecnisaTegra Incorp|Tenda|TrisulViverCedroCoteminasDohlerInd Cataguas|KarstenPettenatiSantanenseSpringsTekaTex RenauxTrack Field|AlpargatasCambuciGrendeneVulcabrasMundialTechnosVivara|WhirlpoolUnicasa|Hercules|Iochp-maxion|Metal Leve|Plascar Part|Hoteis Othon|Bk Brasil|Imc|Bic MonarkEstrelaSpturisTime For Fun|Cvc Brasil|Smart Fit|Anima|BahemaCogna On|Cruzeiro Edu|Ser Educa|Yduqs Part|LocalizaLocamerica|Maestroloc|Movida|UnidasVamos|Dotz Sa|Arezzo CoCea Modas|GrazziotinGrupo Soma|GuararapesLe Lis Blanc|Lojas Marisa|Lojas Renner|Allied|Magaz LuizaVia|Americanas|Espacolaser|Grupo Sbf|Le Biscuit|Petz|Quero-quero|Saraiva Livr|Saraiva|Burguer King/gm", text):
51
- return "Cyclic Consumption"
52
- elif re.search(r"/3tentos|Agribrasil|Agrogalaxy|Aliperti|Boa Safra|Brasilagro|Pomifrutas|Raizen|Slc Agricola|Terrasantapa|Jallesmachad|Raizen Energ|Sao Martinho|Carnes E Derivados|Brf Sa|Excelsior|Jbs|Marfrig|Minerva|Minupar|Camil|J.macedo|Josapar|M.diasbranco|Oderich|Ambev|Grupo Natura|Bombril|Assai|Carrefour Br|Grupo Mateus|P.acucar-cbd|Natura|Pão de Açúcar|Raizen|JBS|Carrefour/gm", text):
53
- return "Non-cyclical Consumption"
54
- elif re.search(r"/Abc Brasil|Alfa Holding|Alfa Invest|Amazonia|Banco Bmg|Banco Inter|Banco Pan|Banese|Banestes|Banpara|Banrisul|Br Partners|Bradesco|Brasil|Brb Banco|Btgp Banco|Itausa|Itauunibanco|Merc Brasil|Merc Invest|Modalmais|Nord Brasil|Nu-nubank|Parana|Pine|Santander Br|Alfa Financ|Finansinos|Merc Financ|Bradesco Lsg|Dibens Lsg|Brazil Realt|Brazilian Sc|Brpr 56 Sec|Cibrasec|Eco Sec Agro|Gaia Agro|Gaia Securit|Octante Sec|Pdg Securit|Polo Cap Sec|Rbcapitalres|Truesec|Vertciasec|Wtorre Pic|Bndespar|Brazilian Fr|G2d Invest|Gp Invest|Ppla|B3|Boa Vista|Cielo|Clearsale|Csu Cardsyst|Getnet Br|Stone Co|Xp Inc|Alfa Consorc|Bbseguridade|Caixa Seguri|Porto Seguro|Seg Al Bahia|Sul America|Irbbrasil Re|Alper|Wiz|Alianscsonae|Br Malls Par|Br Propert|Cor Ribeiro|Cyre Com-ccp|Generalshopp|Habitasul|Hbr Realty|Igb|Iguatemi|Log Com Prop|Menezes Cort|Multiplan|Sao Carlos|Syn Prop Tec|Lopes Brasil|Nexpe|Mont Aranha|Par Al Bahia|Simpar|Cepac - Ctba|Cepac - Mcrj|Cepac - Pmsp|Inter|Pan|Banco do Brasil|BTG|Itaú|Nubank|Santander|Alfa|Merc|BNDES|/gm", text):
55
- return "Financial"
56
- elif re.search(r"/Aura 360|Bradespar|CBA|Csnmineracao|Litel|Litela|Mmx Miner|Vale|Ferbasa|Gerdau|Gerdau Met|Sid Nacional|Usiminas|Mangels Indl|Panatlantica|Tekno|Paranapanema|Braskem|Dexxos Par|Fer Heringer|Nutriplant|Vittia|Cristal|Unipar|Dexco|Eucatex|Klabin|Melhor Sp|Suzano Hold|Suzano|Irani|Metal Iguacu|Sansuy/gm", text):
57
- return "Basic Materials"
58
- #elif re.search(r"/524 Particip|Atompar|B Tech Eqi|Betapart |Cabinda Part|Caconde Part|Cemepe |Cims |Gama Part |Invest Bemge|Polpar |Prompt Part |Sudeste|Sul 116 Part|Ybyra/gm", text):
59
- #return "Others"
60
- elif re.search(r"/3r Petroleum|Cosan|Dommo|Enauta Part|Pet Manguinh|Petrobras|Petrorecsa|Petrorio|Ultrapar|Vibra|Lupatech|Oceanpact|Osx Brasil/gm", text):
61
- return "Oil, Gas and Biofuels"
62
- elif re.search(r"/Intelbras|Multilaser|Positivo Tec|Bemobi Tech|Brq|Enjoei|Getninjas|Infracomm|Locaweb|Meliuz|Mobly|Neogrid|Padtec|Quality Soft|Sinqia|Tc|Totvs |Wdc Networks|Westwing/gm", text):
63
- return "Information Technology"
64
- elif re.search(r"/Aes Brasil|Aes Sul|Aesoperacoes|Afluente T|Alupar|Ampla Energ|Auren|Cachoeira|Ceb|Ceee-d|Ceee-t|Celesc|Celgpar|Celpe|Cemig|Cemig Dist|Cemig Gt|Coelba|Coelce|Comerc Par|Copel|Cosern|Cpfl Energia|Cpfl Geracao|Cpfl Piratin|Cpfl Renovav|Ebe|Elektro|Eletrobras|Eletropar|Emae|Energias Br|Energisa|Energisa Mt|Enersul|Eneva|Engie Brasil|Eqtl Para|Eqtlmaranhao|Equatorial|Escelsa|Fgenergia|Ger Paranap|Itapebi|Light|Light S/a|Neoenergia|Omegaenergia|Paul F Luz|Proman|Rede Energia|Renova|Statkraft|Sto Antonio|Taesa|Term. Pe Iii|Termope|Tran Paulist|Uptick|Ambipar|Casan|Copasa|Igua Sa|Orizon|Sabesp|Sanepar|Sanesalto|Ceg|Comgas|Compass|AES|Cemig|CEMIG/gm", text):
65
- return "Public utility"
66
- else:
67
- labels = [
68
- "Industrial Goods",
69
- "Communications",
70
- "Cyclic Consumption",
71
- "Non-cyclical Consumption",
72
- "Financial",
73
- "Basic Materials",
74
- #"Others",
75
- "Oil, Gas and Biofuels",
76
- "Health",
77
- #"Initial Sector",
78
- "Information Technology",
79
- "Public utility"
80
- ]
81
-
82
- template = "The economic sector of this set of words is {}."
83
-
84
- results = text_classifier(text, labels, hypothesis_template=template)
85
-
86
- index = results["scores"].index(max(results["scores"]))
87
-
88
- return str(results["labels"][index])
89
-
90
- m2m100 = pipeline("translation_pt_to_en", src_lang="pt", tgt_lang="en", model="models/m2m100")
91
- opus = pipeline("translation_pt_to_en", src_lang="pt", tgt_lang="en", model="models/opus")
92
- finbert = pipeline("sentiment-analysis",model="models/finbert")
93
- classifier = pipeline("zero-shot-classification", model="models/roberta")
94
- sid_obj = SentimentIntensityAnalyzer()
95
-
96
-
97
- sheet_id = "1IGFSKnnmQndKVmGOWMCbsvJJMU_2jvnm"
98
- sheet_name = "Sheet1"
99
- url = f"https://docs.google.com/spreadsheets/d/{sheet_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}"
100
- df = pd.read_csv(url)
101
 
102
  header = st.container()
103
  model = st.container()
104
  model_1, model_2 = st.columns(2)
105
  dataset = st.container()
106
  analysis = st.container()
107
-
108
- with st.sidebar:
109
- st.markdown("# Lorem Ipsum\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent sapien tortor, suscipit quis ornare ut, laoreet vitae nisi. Mauris quis consectetur risus, non blandit mauris. Sed ut odio tempor, ullamcorper leo eu, mollis eros. Curabitur pretium sollicitudin sapien, vel mattis augue convallis quis. Suspendisse eleifend turpis non nunc gravida, aliquet hendrerit orci viverra. Sed aliquet, nunc eu posuere tempor, libero ex dignissim velit, ut ultricies erat felis at urna. Proin metus augue, commodo in faucibus sed, aliquet ac eros. Nullam turpis leo, dictum eu tellus a, aliquam egestas velit. Suspendisse cursus augue a sem dapibus, eu faucibus nisl ultrices. Cras tortor ipsum, luctus vitae tincidunt id, dapibus id justo. Sed mi nunc, tempor eu iaculis in, tristique cursus massa. Integer metus felis, pulvinar ut aliquam ut, consectetur in nulla.")
110
 
111
  with header:
112
  st.title("IC 2022 Classificação de Dados Financeiros")
@@ -117,10 +129,9 @@ with model:
117
  st.header("Modelo para Tradução e Classificação!")
118
 
119
  with model_1:
120
- translator = st.selectbox(
121
- 'Qual modelo você deseja usar para tradução?', ('TextBlob', 'M2M100', 'OPUS'))
122
- sentimentor = st.selectbox(
123
- 'Qual modelo você deseja usar para a análise de sentimentos?', ('Vader', 'FinBERT'))
124
 
125
  text = st.text_input(label="Coloque seu texto sobre mercado financeiro em português!", value=r"As ações da Raia Drogasil subiram em 98% desde o último bimestre, segundo as avaliações da revista!")
126
 
@@ -128,40 +139,13 @@ with model:
128
 
129
  with model_2:
130
  if submit:
131
- if translator == "TextBlob":
132
- text_en = translate_text_blob(text)
133
- elif translator == "M2M100":
134
- text_en = translate_text_transformer(text, m2m100)
135
- elif translator == "OPUS":
136
- text_en = translate_text_transformer(text, opus)
137
-
138
- if sentimentor == "Vader":
139
- sentiment = sentiment_vader(text_en, sid_obj)
140
- elif sentimentor == "FinBERT":
141
- sentiment = sentiment_finbert(text_en, finbert)
142
-
143
- theme = theme_classification(text_en, classifier)
144
-
145
- st.text_area(label="Translation:", value=text_en, height=150)
146
- st.text_area(label="Sentiment:", value=sentiment, height=150)
147
- st.text_area(label="Theme:", value=theme, height=150)
148
-
149
- '''
150
- with model:
151
- st.header("Modelo para Tradução e Classificação!")
152
- text = st.text_area(
153
- "Coloque seu texto sobre mercado financeiro em português!")
154
-
155
- submit = st.button('Generate')
156
 
157
- if text:
158
- text_en = translate_text(text)
159
- st.write("*Translation:* {}".format(text_en))
160
- sentiment = sentiment_classification(text_en, sid_obj)
161
- st.write("*Sentiment:* {} - {}".format(sentiment[0], sentiment[1]))
162
- theme = theme_classification(text_en, classifier)
163
- st.write("*Theme:* {}".format(theme))
164
- '''
165
 
166
  with dataset:
167
  st.header("Dados utilizados no projeto!")
@@ -175,4 +159,7 @@ with dataset:
175
  st.write("\t*- Texts:* Coluna que mostra os textos financeiros")
176
 
177
  with analysis:
178
- st.header("Visualização dos dados utilizados")
 
 
 
 
1
+ from transformers import pipeline
2
+ import torch
3
+ import streamlit as st
4
  from textblob import TextBlob
5
  from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
6
+ import os
 
7
  import re
8
+ import pandas as pd
 
9
 
10
  def translate_text_blob(text):
11
  blob = TextBlob(text)
12
  return str(blob.translate(from_lang="pt", to="en"))
13
 
14
+ def sentiment_vader(text):
15
 
16
+ vader_object = SentimentIntensityAnalyzer()
 
 
 
 
17
 
18
+ sentiment_dict = vader_object.polarity_scores(text)
19
  negative = sentiment_dict['neg']
20
  neutral = sentiment_dict['neu']
21
  positive = sentiment_dict['pos']
22
  compound = sentiment_dict['compound']
23
 
24
+ if sentiment_dict['compound'] >= 0.05 :
25
  overall_sentiment = "Positive"
26
 
27
+ elif sentiment_dict['compound'] <= - 0.05 :
28
  overall_sentiment = "Negative"
29
 
30
+ else :
31
  overall_sentiment = "Neutral"
32
+
33
  return overall_sentiment.upper()
34
 
35
+ def classify_by_company(text):
36
+ path = r"\Companies"
37
+
38
+ for filename in os.listdir(path):
39
+ with open(path + '\\' + filename, 'r') as f:
40
+ companies = [word[:-1] for word in f.readlines()]
41
+ companies = "|".join(companies)
42
+ companies = "/" + companies + "/gm"
43
+
44
+ if re.search(companies, text):
45
+ return filename[:-4] + " - Infered by company name in text"
46
+
47
+ return ""
48
+
49
+ def load_models(parameters_list):
50
+
51
+ translation_map = {
52
+ #Translation PT to EN
53
+ "TextBlob" : "TextBlob",
54
+ "M2M100" : "facebook/m2m100_418M",
55
+ "OPUS" : "Helsinki-NLP/opus-mt-mul-en",
56
+ "T5" : "unicamp-dl/translation-pt-en-t5",
57
+ "mBART" : "Narrativa/mbart-large-50-finetuned-opus-en-pt-translation",
58
+ }
59
+
60
+ sentiment_map = {
61
+ #Sentiment Analysis
62
+ "VADER" : "VADER",
63
+ "FinBERT" : "ProsusAI/finbert",
64
+ "DistilBERT" : "distilbert-base-uncased-finetuned-sst-2-english",
65
+ "BERT" : "nlptown/bert-base-multilingual-uncased-sentiment",
66
+ }
67
+
68
+ zeroshot_map = {
69
+ #Zeroshot Classification
70
+ "RoBERTa" : "joeddav/xlm-roberta-large-xnli",
71
+ "mDeBERTa" : "MoritzLaurer/mDeBERTa-v3-base-mnli-xnli",
72
+ "DistilroBERTa" : "cross-encoder/nli-distilroberta-base",
73
+ }
74
+
75
+ candidate_labels = [
76
+ "Industrial Goods",
77
+ "Communications",
78
+ "Cyclic Consumption",
79
+ "Non-cyclical Consumption",
80
+ "Financial",
81
+ "Basic Materials",
82
+ #"Others",
83
+ "Oil, Gas and Biofuels",
84
+ "Health",
85
+ #"Initial Sector",
86
+ "Information Technology",
87
+ "Public utility"
88
+ ]
89
+
90
+ device_num = 0 if torch.cuda.is_available() else -1
91
+
92
+ if parameters_list[0] == "TextBlob":
93
+ out_translation = translate_text_blob(parameters_list[3])
94
+ else:
95
+ translation = pipeline("translation_pt_to_en", model=translation_map[parameters_list[0]], tokenizer=translation_map[parameters_list[0]], device=device_num)
96
+ out_translation = translation(parameters_list[3])[0]["translation_text"]
97
+
98
+ if parameters_list[1] == "VADER":
99
+ out_sentiment = sentiment_vader(out_translation)
100
+ else:
101
+ sentiment = pipeline("sentiment-analysis", model=sentiment_map[parameters_list[1]], tokenizer=sentiment_map[parameters_list[1]], device=device_num)
102
+ out_sentiment = sentiment(out_translation)[0]["label"].upper()
103
+
104
+ company_classification = classify_by_company(parameters_list[3].upper())
105
+
106
+ if company_classification:
107
+ out_classification = company_classification
108
+ else:
109
+ classification = pipeline("zero-shot-classification", model=zeroshot_map[parameters_list[2]], tokenizer=zeroshot_map[parameters_list[2]], device=device_num)
110
+ out_classification = classification(out_translation, candidate_labels)["labels"][0] + " - Infered by {}".format(parameters_list[2])
111
 
112
+ return out_translation, out_sentiment, out_classification
113
+
114
+ df = pd.read_csv(r"\Data\Hugging Face_DF.csv")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  header = st.container()
117
  model = st.container()
118
  model_1, model_2 = st.columns(2)
119
  dataset = st.container()
120
  analysis = st.container()
121
+ analysis_1, analysis_2 = st.columns(2)
 
 
122
 
123
  with header:
124
  st.title("IC 2022 Classificação de Dados Financeiros")
 
129
  st.header("Modelo para Tradução e Classificação!")
130
 
131
  with model_1:
132
+ translation_pt_to_en = st.selectbox('Qual modelo você deseja usar para tradução?', ('TextBlob', 'M2M100', 'OPUS', 'T5', 'mBART'))
133
+ sentiment_analysis = st.selectbox('Qual modelo você deseja usar para análise de sentimento?', ('VADER', 'FinBERT', 'DistilBERT', 'BERT'))
134
+ zero_shot_classification = st.selectbox('Qual modelo você deseja usar para classificação?', ('RoBERTa', 'mDeBERTa', 'DistilroBERTa'))
 
135
 
136
  text = st.text_input(label="Coloque seu texto sobre mercado financeiro em português!", value=r"As ações da Raia Drogasil subiram em 98% desde o último bimestre, segundo as avaliações da revista!")
137
 
 
139
 
140
  with model_2:
141
  if submit:
142
+ with st.spinner('Wait for it...'):
143
+ parameters = [translation_pt_to_en, sentiment_analysis, zero_shot_classification, text]
144
+ outputs = load_models(parameters)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
+ st.write("Translation..................................................................: \n {} \n \n".format(outputs[0]))
147
+ st.write("Sentiment...................................................................: \n {} \n \n".format(outputs[1]))
148
+ st.write("Classification...............................................................: \n {} \n \n".format(outputs[2]))
 
 
 
 
 
149
 
150
  with dataset:
151
  st.header("Dados utilizados no projeto!")
 
159
  st.write("\t*- Texts:* Coluna que mostra os textos financeiros")
160
 
161
  with analysis:
162
+ st.header("Visualização dos dados utilizados através de WordClouds")
163
+
164
+ with analysis_1:
165
+ pass