File size: 2,712 Bytes
aecf7b3 47f4dd2 2a258c5 47f4dd2 2a258c5 47f4dd2 bc22ea4 47f4dd2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from scipy.special import softmax
import torch
st.set_page_config(page_title="Análise de Sentimentos", layout="wide")
st.title("🔍 Análise de Sentimentos")
st.markdown("Insira textos e escolha um modelo de linguagem para realizar a classificação de sentimentos.")
modelos_disponiveis = {
"FinBertPTBR (turing-usp) ": "turing-usp/FinBertPTBR",
"Multilingual Uncased BERT (nlptown)": "nlptown/bert-base-multilingual-uncased-sentiment",
"Multilingual Cased BERT (lxyuan)": "lxyuan/distilbert-base-multilingual-cased-sentiments-student",
"Multilingual Sentiment Analysis (tabularisai)" : "tabularisai/multilingual-sentiment-analysis"
}
modelo_escolhido = st.sidebar.selectbox("Escolha o modelo:", list(modelos_disponiveis.keys()))
model_name = modelos_disponiveis[modelo_escolhido]
@st.cache_resource(show_spinner=False)
def carregar_modelo(model_name):
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
return tokenizer, model
tokenizer, model = carregar_modelo(model_name)
def avaliar_sentimento(texto):
inputs = tokenizer(texto, return_tensors="pt", padding=True, truncation=True, max_length=512)
with torch.no_grad():
outputs = model(**inputs)
scores = softmax(outputs.logits[0].numpy())
labels = model.config.id2label
resultado = {labels[i]: float(scores[i]) for i in range(len(scores))}
sentimento_previsto = max(resultado, key=resultado.get)
return {"sentimento": sentimento_previsto, "scores": resultado}
textos = st.text_area("Digite os textos (um por linha):", height=200)
if st.button("🔍 Analisar Sentimentos"):
linhas = [linha.strip() for linha in textos.strip().split("\n") if linha.strip()]
if not linhas:
st.warning("Por favor, insira ao menos um texto.")
else:
resultados = []
for t in linhas:
res = avaliar_sentimento(t)
resultados.append({
"Texto": t,
"Sentimento": res["sentimento"],
**res["scores"]
})
df_resultados = pd.DataFrame(resultados)
st.success("Análise concluída!")
st.subheader("📋 Resultados")
st.dataframe(df_resultados, use_container_width=True)
st.subheader("📊 Distribuição de Sentimentos")
fig, ax = plt.subplots()
df_resultados["Sentimento"].value_counts().plot(kind='bar', ax=ax, rot=0)
ax.set_xlabel("Sentimento")
ax.set_ylabel("Frequência")
st.pyplot(fig) |