EnzoBustos commited on
Commit
37d5811
1 Parent(s): caf6a8d
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from textblob import TextBlob
2
+ from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
3
+ from transformers import pipeline
4
+ import streamlit as st
5
+
6
+ def translate_text(text):
7
+ blob = TextBlob(text)
8
+ return str(blob.translate(from_lang="pt", to="en"))
9
+
10
+ def sentiment_classification(sentence):
11
+ sid_obj = SentimentIntensityAnalyzer()
12
+
13
+ sentiment_dict = sid_obj.polarity_scores(sentence)
14
+ negative = sentiment_dict['neg']
15
+ neutral = sentiment_dict['neu']
16
+ positive = sentiment_dict['pos']
17
+ compound = sentiment_dict['compound']
18
+
19
+ if sentiment_dict['compound'] >= 0.05 :
20
+ overall_sentiment = "Positive"
21
+
22
+ elif sentiment_dict['compound'] <= - 0.05 :
23
+ overall_sentiment = "Negative"
24
+
25
+ else :
26
+ overall_sentiment = "Neutral"
27
+
28
+ return overall_sentiment, sentiment_dict['compound']
29
+
30
+ def theme_classification(text):
31
+
32
+ labels = ["Industrial Goods",
33
+ "Communications",
34
+ "Cyclic Consumption",
35
+ "Non-cyclical Consumption",
36
+ "Financial",
37
+ "Basic Materials",
38
+ #"Others",
39
+ "Oil, Gas and Biofuels",
40
+ "Health",
41
+ #"Initial Sector",
42
+ "Information Technology",
43
+ "Public utility"]
44
+
45
+ template = "The economic sector of this set of words is {}."
46
+
47
+ classifier = pipeline("zero-shot-classification", model="joeddav/xlm-roberta-large-xnli")
48
+
49
+ results = classifier(text, labels, hypothesis_template=template)
50
+
51
+ index = results["scores"].index(max(results["scores"]))
52
+
53
+ return results["labels"][index]
54
+
55
+ text = st.text_area("Coloque seu texto sobre mercado financeiro em português!")
56
+
57
+ if text:
58
+ text_en = translate_text(text)
59
+ sentiment = sentiment_classification(text_en)
60
+ theme = theme_classification(text_en)
61
+
62
+ st.json({
63
+ "Translation": text_en,
64
+ "Sentiment": sentiment,
65
+ "Theme": theme,
66
+ })