Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import gdown
|
| 4 |
+
from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
|
| 5 |
+
from safetensors.torch import load_file
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# ================================
|
| 9 |
+
# 1. Google Drive FILE ID (model.safetensors)
|
| 10 |
+
# ================================
|
| 11 |
+
FILE_ID = "1eMR7jxkj5XLLIV6t9IIllpfegxHWCi_A"
|
| 12 |
+
MODEL_DIR = "model_folder"
|
| 13 |
+
MODEL_FILE = os.path.join(MODEL_DIR, "model.safetensors")
|
| 14 |
+
|
| 15 |
+
# ================================
|
| 16 |
+
# 2. Download model file (jika belum ada)
|
| 17 |
+
# ================================
|
| 18 |
+
if not os.path.exists(MODEL_DIR):
|
| 19 |
+
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 20 |
+
|
| 21 |
+
if not os.path.exists(MODEL_FILE):
|
| 22 |
+
st.write("Mengunduh model.safetensors dari Google Drive...")
|
| 23 |
+
url = f"https://drive.google.com/uc?id={FILE_ID}"
|
| 24 |
+
gdown.download(url, MODEL_FILE, quiet=False)
|
| 25 |
+
st.success("model.safetensors berhasil di-download!")
|
| 26 |
+
|
| 27 |
+
# ================================
|
| 28 |
+
# 3. Load model & tokenizer TANPA META MODE
|
| 29 |
+
# ================================
|
| 30 |
+
st.write("Memuat model...")
|
| 31 |
+
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 33 |
+
|
| 34 |
+
# 3A — Load config
|
| 35 |
+
config = AutoConfig.from_pretrained(MODEL_DIR)
|
| 36 |
+
|
| 37 |
+
# 3B — Buat model kosong
|
| 38 |
+
model = AutoModelForSequenceClassification.from_config(config)
|
| 39 |
+
|
| 40 |
+
# 3C — Load bobot SAFETENSORS
|
| 41 |
+
state_dict = load_file(MODEL_FILE)
|
| 42 |
+
model.load_state_dict(state_dict, strict=True)
|
| 43 |
+
|
| 44 |
+
model.to("cpu")
|
| 45 |
+
model.eval()
|
| 46 |
+
|
| 47 |
+
st.success("Model siap digunakan!")
|
| 48 |
+
|
| 49 |
+
# ================================
|
| 50 |
+
# 4. Label Mapping
|
| 51 |
+
# ================================
|
| 52 |
+
label_map = {
|
| 53 |
+
0: "Negatif",
|
| 54 |
+
1: "Positif"
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# ================================
|
| 58 |
+
# 5. Streamlit UI
|
| 59 |
+
# ================================
|
| 60 |
+
st.title("🚀 Klasifikasi Kalimat dengan Model dari Google Drive")
|
| 61 |
+
|
| 62 |
+
text = st.text_area("Masukkan kalimat:")
|
| 63 |
+
|
| 64 |
+
if st.button("Klasifikasi"):
|
| 65 |
+
if text.strip() == "":
|
| 66 |
+
st.warning("Tolong masukkan kalimat terlebih dahulu.")
|
| 67 |
+
else:
|
| 68 |
+
# Tokenisasi
|
| 69 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 70 |
+
|
| 71 |
+
# Prediksi
|
| 72 |
+
with torch.no_grad():
|
| 73 |
+
outputs = model(**inputs)
|
| 74 |
+
probs = torch.softmax(outputs.logits, dim=1)
|
| 75 |
+
|
| 76 |
+
pred_tensor = torch.argmax(probs, dim=1)
|
| 77 |
+
pred = int(pred_tensor.cpu().numpy()[0])
|
| 78 |
+
|
| 79 |
+
# Ambil label
|
| 80 |
+
label = label_map.get(pred, "Unknown")
|
| 81 |
+
|
| 82 |
+
st.subheader("Hasil Prediksi:")
|
| 83 |
+
st.write("Kelas:", f"**{label}**")
|
| 84 |
+
st.write("Probabilitas:", probs.tolist()[0])
|