Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| # 1. Load model TFLite Anda (pastikan file .tflite ada di folder yang sama) | |
| interpreter = tf.lite.Interpreter(model_path="tiny_sentiment_model_imdb.tflite") | |
| interpreter.allocate_tensors() | |
| def predict_sentiment(text): | |
| # --- BAGIAN PENTING: PROSES TEXT --- | |
| # TFLite memerlukan teks diubah ke angka (tokenizing) sesuai cara Anda melatih model. | |
| # Contoh di bawah ini adalah placeholder logika inferensi: | |
| input_details = interpreter.get_input_details() | |
| output_details = interpreter.get_output_details() | |
| # (Opsional) Tambahkan kode tokenizing teks Anda di sini agar sesuai input_details | |
| # input_data = tokenizer(text) | |
| # Jalankan model (contoh dummy input) | |
| # interpreter.set_tensor(input_details[0]['index'], input_data) | |
| interpreter.invoke() | |
| output_data = interpreter.get_tensor(output_details[0]['index']) | |
| prediction = output_data[0] # Misal: 0 untuk Negative, 1 untuk Positive | |
| return "Positive" if prediction > 0.5 else "Negative" | |
| # 2. Buat UI Sederhana: 1 Input Box -> 1 Output Text | |
| demo = gr.Interface( | |
| fn=predict_sentiment, | |
| inputs=gr.Textbox(label="Masukkan Kalimat", placeholder="Contoh: Saya sangat senang hari ini!"), | |
| outputs=gr.Textbox(label="Hasil Analisis"), | |
| title="Sentimen Analisis TFLite", | |
| allow_flagging="never" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |