|
from transformers import pipeline
|
|
import gradio as gr
|
|
|
|
|
|
model_id = "nlptown/bert-base-multilingual-uncased-sentiment"
|
|
|
|
|
|
sentiment_model = pipeline("sentiment-analysis", model=model_id)
|
|
|
|
|
|
def map_label(star_label):
|
|
star = int(star_label.split()[0])
|
|
if star <= 2:
|
|
return "Negative π "
|
|
elif star == 3:
|
|
return "Neutral π"
|
|
else:
|
|
return "Positive π"
|
|
|
|
|
|
def predict_sentiment(text):
|
|
result = sentiment_model(text)[0]
|
|
label = map_label(result['label'])
|
|
confidence = round(result['score'] * 100, 2)
|
|
return f"Sentiment: {label}\nConfidence: {confidence}%"
|
|
|
|
|
|
iface = gr.Interface(
|
|
fn=predict_sentiment,
|
|
inputs=gr.Textbox(lines=4, placeholder="Tulis pendapatmu di sini..."),
|
|
outputs="text",
|
|
title="π Multilingual Sentiment Analyzer",
|
|
description="Analisis sentimen teks dalam berbagai bahasa menggunakan model multilingual BERT."
|
|
)
|
|
|
|
iface.launch()
|
|
|