Spaces:
Sleeping
Sleeping
Commit
·
b1e9ca7
1
Parent(s):
5fbeef1
🚚 Movendo backend FastAPI para subpasta para ativar Gradio
Browse files
app.py
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
from transformers import pipeline
|
| 4 |
import uuid
|
| 5 |
|
|
|
|
| 6 |
modeloHF = "ViniciusKhan/bert-nps-feedback-analyzer"
|
| 7 |
sentiment_pipeline = pipeline("text-classification", model=modeloHF)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
def analisar_sentimento(texto):
|
|
|
|
| 11 |
resultado = sentiment_pipeline([texto[:512]])[0]
|
| 12 |
sentimento = resultado["label"]
|
| 13 |
confianca = round(resultado["score"], 4)
|
| 14 |
-
return f"Sentimento: {sentimento}\
|
| 15 |
|
| 16 |
-
#
|
| 17 |
def analisar_arquivo(file):
|
| 18 |
try:
|
| 19 |
df = pd.read_excel(file.name)
|
|
@@ -23,35 +26,42 @@ def analisar_arquivo(file):
|
|
| 23 |
if "Resumo" not in df.columns:
|
| 24 |
return "❌ A coluna obrigatória 'Resumo' não foi encontrada.", None
|
| 25 |
|
| 26 |
-
textos = df["Resumo"].fillna("").astype(str).str[:512].tolist()
|
| 27 |
resultados = sentiment_pipeline(textos, truncation=True)
|
| 28 |
|
| 29 |
df["Sentimento_Class"] = [r["label"] for r in resultados]
|
| 30 |
df["Confianca"] = [round(r["score"], 4) for r in resultados]
|
| 31 |
|
| 32 |
-
# Salvar novo arquivo
|
| 33 |
nome_saida = f"/tmp/resultado_sentimento_{uuid.uuid4().hex[:8]}.xlsx"
|
| 34 |
df.to_excel(nome_saida, index=False)
|
| 35 |
|
| 36 |
return df, nome_saida
|
| 37 |
|
| 38 |
-
|
| 39 |
-
# 🎛️ Interface Gradio
|
| 40 |
with gr.Blocks(title="Senticore – Análise de Sentimentos NPS") as demo:
|
| 41 |
-
gr.Markdown(
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
with gr.Tab("📝 Texto Único"):
|
| 45 |
-
entrada_texto = gr.Textbox(lines=6, label="Cole aqui o feedback do cliente...")
|
| 46 |
-
saida_texto = gr.Textbox(label="Resultado", lines=2)
|
| 47 |
-
botao_texto = gr.Button("🔍 Analisar Sentimento")
|
| 48 |
botao_texto.click(analisar_sentimento, entrada_texto, saida_texto)
|
| 49 |
|
| 50 |
-
with gr.Tab("📂
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
botao_excel = gr.Button("📊
|
|
|
|
|
|
|
| 55 |
|
| 56 |
botao_excel.click(
|
| 57 |
fn=analisar_arquivo,
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
from transformers import pipeline
|
| 5 |
import uuid
|
| 6 |
|
| 7 |
+
# 🔍 Carrega modelo do Hugging Face
|
| 8 |
modeloHF = "ViniciusKhan/bert-nps-feedback-analyzer"
|
| 9 |
sentiment_pipeline = pipeline("text-classification", model=modeloHF)
|
| 10 |
|
| 11 |
+
# 🧠 Função de inferência para texto simples
|
| 12 |
def analisar_sentimento(texto):
|
| 13 |
+
texto = texto.lower() # Normaliza para evitar sensibilidade a maiúsculas
|
| 14 |
resultado = sentiment_pipeline([texto[:512]])[0]
|
| 15 |
sentimento = resultado["label"]
|
| 16 |
confianca = round(resultado["score"], 4)
|
| 17 |
+
return f"🔹 Sentimento: {sentimento}\n🔹 Confiança: {confianca}"
|
| 18 |
|
| 19 |
+
# 🧠 Função de inferência para planilhas
|
| 20 |
def analisar_arquivo(file):
|
| 21 |
try:
|
| 22 |
df = pd.read_excel(file.name)
|
|
|
|
| 26 |
if "Resumo" not in df.columns:
|
| 27 |
return "❌ A coluna obrigatória 'Resumo' não foi encontrada.", None
|
| 28 |
|
| 29 |
+
textos = df["Resumo"].fillna("").astype(str).str.lower().str[:512].tolist()
|
| 30 |
resultados = sentiment_pipeline(textos, truncation=True)
|
| 31 |
|
| 32 |
df["Sentimento_Class"] = [r["label"] for r in resultados]
|
| 33 |
df["Confianca"] = [round(r["score"], 4) for r in resultados]
|
| 34 |
|
|
|
|
| 35 |
nome_saida = f"/tmp/resultado_sentimento_{uuid.uuid4().hex[:8]}.xlsx"
|
| 36 |
df.to_excel(nome_saida, index=False)
|
| 37 |
|
| 38 |
return df, nome_saida
|
| 39 |
|
| 40 |
+
# 🎨 Interface Gradio com layout aprimorado
|
|
|
|
| 41 |
with gr.Blocks(title="Senticore – Análise de Sentimentos NPS") as demo:
|
| 42 |
+
gr.Markdown(
|
| 43 |
+
"""
|
| 44 |
+
<h1 style='text-align: center; color: #00A86B;'>🤖 Senticore</h1>
|
| 45 |
+
<p style='text-align: center;'>Classificador BERT inteligente para análise de sentimentos em feedbacks NPS</p>
|
| 46 |
+
""")
|
| 47 |
+
|
| 48 |
+
with gr.Tab("📝 Texto Individual"):
|
| 49 |
+
with gr.Row():
|
| 50 |
+
entrada_texto = gr.Textbox(lines=5, label="Texto do cliente", placeholder="Cole aqui o feedback...")
|
| 51 |
+
with gr.Row():
|
| 52 |
+
botao_texto = gr.Button("🔍 Analisar")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
saida_texto = gr.Textbox(label="Resultado da Análise", lines=2)
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
botao_texto.click(analisar_sentimento, entrada_texto, saida_texto)
|
| 57 |
|
| 58 |
+
with gr.Tab("📂 Lote via Excel"):
|
| 59 |
+
gr.Markdown("💡 O arquivo deve conter uma coluna chamada **Resumo** com os textos a serem analisados.")
|
| 60 |
+
|
| 61 |
+
entrada_arquivo = gr.File(file_types=[".xlsx"], label="📁 Envie um arquivo Excel")
|
| 62 |
+
botao_excel = gr.Button("📊 Analisar Arquivo")
|
| 63 |
+
saida_tabela = gr.Dataframe(label="📄 Tabela com Sentimentos", interactive=False)
|
| 64 |
+
download_link = gr.File(label="📥 Download do Excel Processado")
|
| 65 |
|
| 66 |
botao_excel.click(
|
| 67 |
fn=analisar_arquivo,
|