Spaces:
Sleeping
Sleeping
File size: 1,370 Bytes
c861857 207c039 c861857 207c039 c861857 207c039 c861857 5bc1924 c861857 5bc1924 207c039 5bc1924 | 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 | import torch
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
checkpoint = "mr4/bert-base-jp-sentiment-analysis"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForSequenceClassification.from_pretrained(checkpoint)
def classify_comment(comment):
raw_inputs = [comment]
inputs = tokenizer(raw_inputs, padding=True,
truncation=True, return_tensors="pt")
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
print(predictions)
results = []
for i, prediction in enumerate(predictions):
for j, value in enumerate(prediction):
results.append({
'label': model.config.id2label[j],
'score': value.item(),
})
print(results)
sorted_data = sorted(results, key=lambda x: x['score'])
return f"{"❌" if sorted_data[0]['label'].lower() in ["negative"] else '✅'} {comment}\n{sorted_data[0]['label']} ({sorted_data[0]['score']*100:.5f}%)\n{sorted_data[1]['label']} ({sorted_data[1]['score']*100:.5f}%)"
iface = gr.Interface(
fn=classify_comment,
inputs=gr.Textbox(lines=3, placeholder="コメントを入力してください"),
outputs="text",
title="日本の感情分析",
description="日本の感情分析"
)
iface.launch()
|