File size: 2,535 Bytes
5f3e194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import streamlit as st
import os
import gdown
from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
from safetensors.torch import load_file
import torch

# ================================
# 1. Google Drive FILE ID (model.safetensors)
# ================================
FILE_ID = "1eMR7jxkj5XLLIV6t9IIllpfegxHWCi_A"
MODEL_DIR = "model_folder"
MODEL_FILE = os.path.join(MODEL_DIR, "model.safetensors")

# ================================
# 2. Download model file (jika belum ada)
# ================================
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!")

# ================================
# 3. Load model & tokenizer TANPA META MODE
# ================================
st.write("Memuat model...")

tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)

# 3A — Load config
config = AutoConfig.from_pretrained(MODEL_DIR)

# 3B — Buat model kosong
model = AutoModelForSequenceClassification.from_config(config)

# 3C — Load bobot SAFETENSORS
state_dict = load_file(MODEL_FILE)
model.load_state_dict(state_dict, strict=True)

model.to("cpu")
model.eval()

st.success("Model siap digunakan!")

# ================================
# 4. Label Mapping
# ================================
label_map = {
    0: "Negatif",
    1: "Positif"
}

# ================================
# 5. Streamlit UI
# ================================
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:
        # Tokenisasi
        inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)

        # Prediksi
        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])

        # Ambil label
        label = label_map.get(pred, "Unknown")

        st.subheader("Hasil Prediksi:")
        st.write("Kelas:", f"**{label}**")
        st.write("Probabilitas:", probs.tolist()[0])