Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,37 +2,40 @@ import gradio as gr
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
# 1. Load model TFLite
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def predict_sentiment(text):
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
# input_data = tokenizer(text)
|
| 19 |
-
|
| 20 |
-
# Jalankan model (contoh dummy input)
|
| 21 |
-
# interpreter.set_tensor(input_details[0]['index'], input_data)
|
| 22 |
-
interpreter.invoke()
|
| 23 |
-
|
| 24 |
-
output_data = interpreter.get_tensor(output_details[0]['index'])
|
| 25 |
-
prediction = output_data[0] # Misal: 0 untuk Negative, 1 untuk Positive
|
| 26 |
-
|
| 27 |
-
return "Positive" if prediction > 0.5 else "Negative"
|
| 28 |
-
|
| 29 |
-
# 2. Buat UI Sederhana: 1 Input Box -> 1 Output Text
|
| 30 |
demo = gr.Interface(
|
| 31 |
fn=predict_sentiment,
|
| 32 |
-
inputs=gr.Textbox(label="Masukkan Kalimat", placeholder="
|
| 33 |
outputs=gr.Textbox(label="Hasil Analisis"),
|
| 34 |
title="Sentimen Analisis TFLite",
|
| 35 |
-
allow_flagging="never"
|
| 36 |
)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
# 1. Load model TFLite (Pastikan nama file sesuai dengan yang Anda upload)
|
| 6 |
+
# Jika file model Anda bernama lain, ganti "model.tflite" di bawah ini
|
| 7 |
+
try:
|
| 8 |
+
interpreter = tf.lite.Interpreter(model_path="tiny_sentiment_model_imdb.tflite")
|
| 9 |
+
interpreter.allocate_tensors()
|
| 10 |
+
except Exception as e:
|
| 11 |
+
print(f"Error loading model: {e}")
|
| 12 |
|
| 13 |
def predict_sentiment(text):
|
| 14 |
+
# Logika inferensi (Sederhana sebagai contoh)
|
| 15 |
+
# Catatan: Anda perlu menambahkan tokenizer di sini agar teks bisa dibaca model
|
| 16 |
+
try:
|
| 17 |
+
input_details = interpreter.get_input_details()
|
| 18 |
+
output_details = interpreter.get_output_details()
|
| 19 |
+
|
| 20 |
+
# Placeholder: Proses input text ke tensor di sini
|
| 21 |
+
# interpreter.set_tensor(input_details[0]['index'], input_data)
|
| 22 |
+
|
| 23 |
+
interpreter.invoke()
|
| 24 |
+
output_data = interpreter.get_tensor(output_details[0]['index'])
|
| 25 |
+
|
| 26 |
+
# Contoh logika output (sesuaikan dengan output model Anda)
|
| 27 |
+
prediction = output_data[0][0]
|
| 28 |
+
return "Positive" if prediction > 0.5 else "Negative"
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error saat prediksi: {str(e)}"
|
| 31 |
|
| 32 |
+
# 2. UI Gradio (Tanpa argumen 'allow_flagging' yang error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
demo = gr.Interface(
|
| 34 |
fn=predict_sentiment,
|
| 35 |
+
inputs=gr.Textbox(label="Masukkan Kalimat", placeholder="Ketik di sini..."),
|
| 36 |
outputs=gr.Textbox(label="Hasil Analisis"),
|
| 37 |
title="Sentimen Analisis TFLite",
|
| 38 |
+
flagging_mode="never" # Pengganti allow_flagging="never"
|
| 39 |
)
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|