| import streamlit as st
|
| import os
|
| import gdown
|
| from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
|
| from safetensors.torch import load_file
|
| import torch
|
|
|
|
|
|
|
|
|
| FILE_ID = "1eMR7jxkj5XLLIV6t9IIllpfegxHWCi_A"
|
| MODEL_DIR = "model_folder"
|
| MODEL_FILE = os.path.join(MODEL_DIR, "model.safetensors")
|
|
|
|
|
|
|
|
|
| if not os.path.exists(MODEL_DIR):
|
| os.makedirs(MODEL_DIR, exist_ok=True)
|
|
|
| if not os.path.exists(MODEL_FILE):
|
| st.write("Mengunduh model.safetensors dari Google Drive...")
|
| url = f"https://drive.google.com/uc?id={FILE_ID}"
|
| gdown.download(url, MODEL_FILE, quiet=False)
|
| st.success("model.safetensors berhasil di-download!")
|
|
|
|
|
|
|
|
|
| st.write("Memuat model...")
|
|
|
| tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
|
|
|
|
| config = AutoConfig.from_pretrained(MODEL_DIR)
|
|
|
|
|
| model = AutoModelForSequenceClassification.from_config(config)
|
|
|
|
|
| state_dict = load_file(MODEL_FILE)
|
| model.load_state_dict(state_dict, strict=True)
|
|
|
| model.to("cpu")
|
| model.eval()
|
|
|
| st.success("Model siap digunakan!")
|
|
|
|
|
|
|
|
|
| label_map = {
|
| 0: "Negatif",
|
| 1: "Positif"
|
| }
|
|
|
|
|
|
|
|
|
| st.title("π Klasifikasi Kalimat dengan Model dari Google Drive")
|
|
|
| text = st.text_area("Masukkan kalimat:")
|
|
|
| if st.button("Klasifikasi"):
|
| if text.strip() == "":
|
| st.warning("Tolong masukkan kalimat terlebih dahulu.")
|
| else:
|
|
|
| inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
|
|
|
| with torch.no_grad():
|
| outputs = model(**inputs)
|
| probs = torch.softmax(outputs.logits, dim=1)
|
|
|
| pred_tensor = torch.argmax(probs, dim=1)
|
| pred = int(pred_tensor.cpu().numpy()[0])
|
|
|
|
|
| label = label_map.get(pred, "Unknown")
|
|
|
| st.subheader("Hasil Prediksi:")
|
| st.write("Kelas:", f"**{label}**")
|
| st.write("Probabilitas:", probs.tolist()[0])
|
|
|