Spaces:
Runtime error
Runtime error
File size: 1,122 Bytes
56b62de |
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 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# Load model dan tokenizer dari Hugging Face
model = AutoModelForSequenceClassification.from_pretrained("kelompokjavonlp/sentiment_analysis")
tokenizer = AutoTokenizer.from_pretrained("kelompokjavonlp/sentiment_analysis")
# Update label sesuai model kamu
labels = ["Very Negative", "Negative", "Neutral", "Positive", "Very Positive"]
# Fungsi prediksi
def predict_sentiment(text):
inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
pred = torch.argmax(probs).item()
return {labels[i]: float(probs[0][i]) for i in range(len(labels))}
# Interface Gradio
iface = gr.Interface(
fn=predict_sentiment,
inputs=gr.Textbox(label="Masukkan Kalimat"),
outputs=gr.Label(label="Hasil Sentimen"),
title="Demo Analisis Sentimen Bahasa Indonesia",
description="Model klasifikasi sentimen: Very Negative, Negative, Neutral, Positive, Very Positive."
)
iface.launch()
|