Spaces:
Runtime error
Runtime error
| 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() | |