Spaces:
Runtime error
Runtime error
File size: 1,272 Bytes
5114a54 0920252 09be9ae 0920252 08e3044 0920252 08e3044 31f4b3c 0920252 31f4b3c 0920252 09be9ae 0920252 5114a54 0920252 249433b 0920252 |
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 |
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()
|