Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line2345678910111213141516171819202122232425262728293031323334353637381 unmodified lineimport tensorflow as tfimport 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 Positivereturn "Positive" if prediction > 0.5 else "Negative"# 2. Buat UI Sederhana: 1 Input Box -> 1 Output Textdemo = 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__":1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940411 unmodified lineimport tensorflow as tfimport numpy as np# 1. Load model TFLite (Pastikan nama file sesuai dengan yang Anda upload)# Jika file model Anda bernama lain, ganti "model.tflite" di bawah initry:interpreter = tf.lite.Interpreter(model_path="tiny_sentiment_model_imdb.tflite")interpreter.allocate_tensors()except Exception as e:print(f"Error loading model: {e}")def predict_sentiment(text):# Logika inferensi (Sederhana sebagai contoh)# Catatan: Anda perlu menambahkan tokenizer di sini agar teks bisa dibaca modeltry:input_details = interpreter.get_input_details()output_details = interpreter.get_output_details()# Placeholder: Proses input text ke tensor di sini# interpreter.set_tensor(input_details[0]['index'], input_data)interpreter.invoke()output_data = interpreter.get_tensor(output_details[0]['index'])# Contoh logika output (sesuaikan dengan output model Anda)prediction = output_data[0][0]return "Positive" if prediction > 0.5 else "Negative"except Exception as e:return f"Error saat prediksi: {str(e)}"# 2. UI Gradio (Tanpa argumen 'allow_flagging' yang error)demo = gr.Interface(fn=predict_sentiment,inputs=gr.Textbox(label="Masukkan Kalimat", placeholder="Ketik di sini..."),outputs=gr.Textbox(label="Hasil Analisis"),title="Sentimen Analisis TFLite",flagging_mode="never" # Pengganti allow_flagging="never")if __name__ == "__main__":
app.py+27-24