|
|
|
|
|
|
|
|
import gradio as gr |
|
|
import numpy as np |
|
|
from transformers import AutoTokenizer, TFAutoModelForSequenceClassification |
|
|
import tensorflow as tf |
|
|
|
|
|
|
|
|
model_path = "jeanetrixsiee/javo_analisis" |
|
|
tokenizer = AutoTokenizer.from_pretrained(model_path) |
|
|
model = TFAutoModelForSequenceClassification.from_pretrained(model_path) |
|
|
|
|
|
|
|
|
id2label = model.config.id2label |
|
|
|
|
|
def predict_sentiment(text): |
|
|
try: |
|
|
|
|
|
inputs = tokenizer(text, return_tensors="tf", padding=True, truncation=True, max_length=256) |
|
|
outputs = model(**inputs) |
|
|
probs = tf.nn.softmax(outputs.logits, axis=-1).numpy()[0] |
|
|
|
|
|
label_id = np.argmax(probs) |
|
|
label = id2label[str(label_id)] if str(label_id) in id2label else "Unknown" |
|
|
|
|
|
prob_dict = {id2label[str(i)]: float(f"{probs[i]*100:.2f}") for i in range(len(probs))} |
|
|
return label, prob_dict |
|
|
|
|
|
except Exception as e: |
|
|
return "Error", {"Error": str(e)} |
|
|
|
|
|
|
|
|
desc = "Model ini memprediksi sentimen dari komentar YouTube dalam 5 kategori: Very Negative, Negative, Neutral, Positive, Very Positive." |
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("## π Analisis Sentimen Komentar YouTube") |
|
|
gr.Markdown(desc) |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
input_text = gr.Textbox(label="Masukkan Komentar YouTube") |
|
|
clear_btn = gr.Button("Clear") |
|
|
submit_btn = gr.Button("Submit", variant="primary") |
|
|
with gr.Column(): |
|
|
output_label = gr.Textbox(label="Hasil Prediksi") |
|
|
output_probs = gr.Label(label="Probabilitas Tiap Label (%)") |
|
|
|
|
|
submit_btn.click(fn=predict_sentiment, inputs=input_text, outputs=[output_label, output_probs]) |
|
|
clear_btn.click(lambda: ("", ""), outputs=[output_label, output_probs]) |
|
|
|
|
|
gr.Markdown("### Share via Link") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |