analisis-bert-2 / app.py
jeanetrixsiee's picture
Update app.py
db96f9a verified
from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
import tensorflow as tf
import numpy as np
import gradio as gr
# Load tokenizer dan model
tokenizer = AutoTokenizer.from_pretrained("jeanetrixsiee/bert-finetuned-keras")
model = TFAutoModelForSequenceClassification.from_pretrained("jeanetrixsiee/bert-finetuned-keras")
# Label prediksi (urutan sesuai saat fine-tuning ya!)
labels = ['Negative', 'Neutral', 'Positive', 'Very Negative', 'Very Positive']
def predict_sentiment(text):
inputs = tokenizer(text, return_tensors="tf", truncation=True, padding=True, max_length=256)
logits = model(inputs)[0]
probs = tf.nn.softmax(logits, axis=1).numpy()[0]
pred_label = labels[np.argmax(probs)]
confidence = probs[np.argmax(probs)]
return f"{pred_label} (confidence: {confidence:.2f})"
demo = gr.Interface(
fn=predict_sentiment,
inputs=gr.Textbox(label="Enter a comment"),
outputs=gr.Textbox(label="Prediction"),
title="Sentiment Classifier",
description="Fine-tuned BERT using Hugging Face Transformers for sentiment analysis."
)
demo.launch()