EnzoBustos's picture
Update app.py
06245e7
raw
history blame
3.09 kB
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from transformers import pipeline
import streamlit as st
def translate_text(text):
blob = TextBlob(text)
return str(blob.translate(from_lang="pt", to="en"))
def sentiment_classification(sentence):
sid_obj = SentimentIntensityAnalyzer()
sentiment_dict = sid_obj.polarity_scores(sentence)
negative = sentiment_dict['neg']
neutral = sentiment_dict['neu']
positive = sentiment_dict['pos']
compound = sentiment_dict['compound']
if sentiment_dict['compound'] >= 0.05 :
overall_sentiment = "Positive"
elif sentiment_dict['compound'] <= - 0.05 :
overall_sentiment = "Negative"
else :
overall_sentiment = "Neutral"
return overall_sentiment, sentiment_dict['compound']
def theme_classification(text):
labels = ["Industrial Goods",
"Communications",
"Cyclic Consumption",
"Non-cyclical Consumption",
"Financial",
"Basic Materials",
#"Others",
"Oil, Gas and Biofuels",
"Health",
#"Initial Sector",
"Information Technology",
"Public utility"]
template = "The economic sector of this set of words is {}."
classifier = pipeline("zero-shot-classification", model="joeddav/xlm-roberta-large-xnli")
results = classifier(text, labels, hypothesis_template=template)
index = results["scores"].index(max(results["scores"]))
return results["labels"][index]
header = st.beta_container()
model = st.beta_container()
with st.sidebar:
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.")
with header:
st.title("IC 2022 Classificação de Dados Financeiros")
with model:
st.header("Modelo para Tradução e Classificação!")
text = st.text_area("*Coloque seu texto sobre mercado financeiro em português!*")
if text:
text_en = translate_text(text)
st.write("Translation: {}".format(text_en))
sentiment = sentiment_classification(text_en)
st.write("Sentiment: {} - {}".format(sentiment[0], sentiment[1]))
theme = theme_classification(text_en)
st.write("Theme: {}".format(theme))