Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import TFAutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
import tensorflow as tf
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
|
| 8 |
|
| 9 |
-
model
|
| 10 |
-
|
| 11 |
|
| 12 |
-
#
|
| 13 |
id2label = {
|
| 14 |
0: 'Negative',
|
| 15 |
1: 'Neutral',
|
|
@@ -19,22 +19,18 @@ id2label = {
|
|
| 19 |
}
|
| 20 |
|
| 21 |
def predict_sentiment(text):
|
| 22 |
-
# Tokenisasi input
|
| 23 |
inputs = tokenizer(text, return_tensors="tf", truncation=True, padding=True)
|
| 24 |
-
outputs = model(inputs)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
probs = tf.nn.softmax(outputs.logits, axis=1).numpy()[0]
|
| 28 |
predicted_index = int(np.argmax(probs))
|
| 29 |
predicted_label = id2label[predicted_index]
|
| 30 |
|
| 31 |
-
# Buat dictionary label:probabilitas
|
| 32 |
prob_dict = {id2label[i]: round(float(probs[i]) * 100, 2) for i in range(len(probs))}
|
| 33 |
sorted_probs = dict(sorted(prob_dict.items(), key=lambda x: x[1], reverse=True))
|
| 34 |
|
| 35 |
return f"Prediksi Sentimen: {predicted_label}", sorted_probs
|
| 36 |
|
| 37 |
-
# Gradio UI
|
| 38 |
interface = gr.Interface(
|
| 39 |
fn=predict_sentiment,
|
| 40 |
inputs=gr.Textbox(label="Masukkan Komentar YouTube"),
|
|
@@ -43,8 +39,7 @@ interface = gr.Interface(
|
|
| 43 |
gr.Label(label="Probabilitas Tiap Label (%)")
|
| 44 |
],
|
| 45 |
title="🔍 Analisis Sentimen Komentar YouTube",
|
| 46 |
-
description="Model ini memprediksi sentimen dari komentar YouTube dalam 5 kategori:\nVery Negative, Negative, Neutral, Positive, Very Positive."
|
| 47 |
-
theme="default"
|
| 48 |
)
|
| 49 |
|
| 50 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
# Load tokenizer dari Hugging Face repo
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("jeanetrixsiee/javo_analisis_sentiment")
|
| 8 |
|
| 9 |
+
# Load model dari file .h5 secara langsung (bukan pakai TFAutoModel)
|
| 10 |
+
model = tf.keras.models.load_model("keras_model") # folder ini HARUS ada di root Spaces
|
| 11 |
|
| 12 |
+
# Pastikan label2id cocok
|
| 13 |
id2label = {
|
| 14 |
0: 'Negative',
|
| 15 |
1: 'Neutral',
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
def predict_sentiment(text):
|
|
|
|
| 22 |
inputs = tokenizer(text, return_tensors="tf", truncation=True, padding=True)
|
| 23 |
+
outputs = model(inputs["input_ids"])
|
| 24 |
|
| 25 |
+
probs = tf.nn.softmax(outputs, axis=1).numpy()[0]
|
|
|
|
| 26 |
predicted_index = int(np.argmax(probs))
|
| 27 |
predicted_label = id2label[predicted_index]
|
| 28 |
|
|
|
|
| 29 |
prob_dict = {id2label[i]: round(float(probs[i]) * 100, 2) for i in range(len(probs))}
|
| 30 |
sorted_probs = dict(sorted(prob_dict.items(), key=lambda x: x[1], reverse=True))
|
| 31 |
|
| 32 |
return f"Prediksi Sentimen: {predicted_label}", sorted_probs
|
| 33 |
|
|
|
|
| 34 |
interface = gr.Interface(
|
| 35 |
fn=predict_sentiment,
|
| 36 |
inputs=gr.Textbox(label="Masukkan Komentar YouTube"),
|
|
|
|
| 39 |
gr.Label(label="Probabilitas Tiap Label (%)")
|
| 40 |
],
|
| 41 |
title="🔍 Analisis Sentimen Komentar YouTube",
|
| 42 |
+
description="Model ini memprediksi sentimen dari komentar YouTube dalam 5 kategori:\nVery Negative, Negative, Neutral, Positive, Very Positive."
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
interface.launch()
|