| | 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) |