Bernardo Damiani
teste de alteração de script
47f4dd2
raw
history blame
2.73 kB
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 Sentimento com Transformers", layout="wide")
# Título
st.title("🔍 Análise de Sentimentos com Transformers")
st.markdown("Insira textos e escolha um modelo de linguagem para realizar a classificação de sentimentos.")
# Modelos disponíveis
modelos_disponiveis = {
"FinBert (Financeiro - PT-BR)": "turing-usp/FinBertPTBR",
"BERT Base PT-BR (NeuralMind)": "neuralmind/bert-base-portuguese-cased",
"Multilingual BERT": "nlptown/bert-base-multilingual-uncased-sentiment"
}
# Sidebar com seleção de modelo
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}
# Entrada de textos
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!")
# Mostrar tabela
st.subheader("📋 Resultados")
st.dataframe(df_resultados, use_container_width=True)
# Gráfico
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)