modelo_ner / app.py
eldavid's picture
Update app.py
31f4b3c verified
raw
history blame contribute delete
No virus
1.27 kB
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
# Carregar o tokenizer e o modelo pré-treinado RoBERTa para análise de sentimentos
model_name = "cardiffnlp/twitter-roberta-base-sentiment"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Criar um pipeline de análise de sentimentos
sentiment_classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
# Mapeamento de rótulos
label_mapping = {
"LABEL_0": "negativo",
"LABEL_1": "neutro",
"LABEL_2": "positivo"
}
# Definir a função para a interface do Gradio
def analyze_sentiment(text):
result = sentiment_classifier(text)
formatted_result = {}
for item in result:
label = label_mapping.get(item['label'], item['label'])
score = f"{item['score']:.3f}".replace(".", ",")
formatted_result[label] = score
return formatted_result
# Criar a interface do Gradio
iface = gr.Interface(
fn=analyze_sentiment,
inputs="text",
outputs="json",
title="Análise de Sentimento com RoBERTa",
description="Digite um texto para analisar seu sentimento."
)
# Lançar a interface do Gradio
iface.launch()