Spaces:
Runtime error
Runtime error
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] | |
text = st.text_area("Coloque seu texto sobre mercado financeiro em português!") | |
if text: | |
text_en = translate_text(text) | |
sentiment = sentiment_classification(text_en) | |
theme = theme_classification(text_en) | |
st.write("Translation: {}".format(text_en)) | |
st.write("Sentiment: {}".format(sentiment)) | |
st.write("Theme: {}".format(theme)) | |
st.json({ | |
"Translation": text_en, | |
"Sentiment": sentiment, | |
"Theme": theme, | |
}) |