-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_1.py
More file actions
104 lines (95 loc) · 4.67 KB
/
Copy pathapp_1.py
File metadata and controls
104 lines (95 loc) · 4.67 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import time
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import joblib
# Set page config sebagai perintah pertama
st.set_page_config(page_title="cAriKBLI", page_icon="<< 🔍 >>")
# Setup device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load model dan tokenizer dari Hugging Face
tokenizer = AutoTokenizer.from_pretrained("ketut/IndoBERTkbli")
model = AutoModelForSequenceClassification.from_pretrained("ketut/IndoBERTkbli")
# tokenizer = AutoTokenizer.from_pretrained("ketut/dKBLI", trust_remote_code=True)
# model = AutoModelForSequenceClassification.from_pretrained("ketut/dKBLI")
model.to(device)
# Coba memuat label_encoder dari file .pth
try:
# Gunakan weights_only=False untuk memuat objek non-tensor (dengan peringatan keamanan)
# label_encoder = torch.load("label_encoder.pth", map_location=device, weights_only=False)
label_encoder = joblib.load("label_encoder_base_p2_augmented.pkl")
# Verifikasi apakah itu LabelEncoder
if not isinstance(label_encoder, LabelEncoder):
raise ValueError("File label_encoder.pth tidak berisi objek LabelEncoder yang valid.")
except FileNotFoundError:
st.warning("File label_encoder.pth tidak ditemukan. Menggunakan contoh sementara.")
kbli_codes = ["47771", "47772", "47773"] # GANTI DENGAN DAFTAR KODE KBLI ASLI
label_encoder = LabelEncoder()
label_encoder.fit(kbli_codes)
except Exception as e:
st.error(f"Gagal memuat label_encoder.pth: {e}")
st.warning("Menggunakan contoh sementara karena error.")
kbli_codes = ["47771", "47772", "47773"] # GANTI DENGAN DAFTAR KODE KBLI ASLI
label_encoder = LabelEncoder()
label_encoder.fit(kbli_codes)
# Load file konsep_kbli.csv
try:
kbli_df = pd.read_csv("konsep_kbli.csv")
kbli_df["kode_kbli"] = kbli_df["kode_kbli"].astype(str)
except FileNotFoundError:
st.error("File konsep_kbli.csv tidak ditemukan. Harap sediakan file tersebut.")
kbli_df = pd.DataFrame(columns=["kode_kbli", "deskripsi"])
# Fungsi prediksi dengan persentase keyakinan
def predict_r201b(text_r201, text_r202, model, tokenizer, label_encoder, device):
combined_text = f"{text_r201} {text_r202}"
inputs = tokenizer(combined_text, padding=True, truncation=True, max_length=128, return_tensors="pt")
inputs = {key: val.to(device) for key, val in inputs.items()}
model.eval()
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
probabilities = torch.softmax(logits, dim=-1).cpu().numpy()[0]
predicted_class = np.argmax(probabilities)
confidence = probabilities[predicted_class] * 100
st.write(f"Indeks prediksi: {predicted_class}")
try:
predicted_label = label_encoder.inverse_transform([predicted_class])[0]
return str(predicted_label), confidence
except ValueError:
return f"Error: Indeks {predicted_class} tidak ada di label_encoder", 0.0
# Antarmuka Streamlit
st.image("cariKBLI.png", width=120)
st.write("Masukkan Rincian Kegiatan Utama dan Produk Utama untuk mendapatkan kode KBLI.")
st.write("nb: Masih KBLI 2015, KBLI2020 coming soon ya")
# Form input
with st.form(key="kbli_form"):
r201 = st.text_input("Tuliskan secara lengkap jenis kegiatan utama (meliputi proses dan output)", value="Menjual pisang goreng")
r202 = st.text_input("Produk utama (barang atau jasa) yang dihasilkan/dijual", value="Pisang goreng")
submit_button = st.form_submit_button(label="Cari Kode KBLI")
# Proses setelah tombol ditekan
if submit_button:
if r201 and r202:
with st.spinner("Memprediksi kode KBLI..."):
start_time = time.time()
prediction, confidence = predict_r201b(r201, r202, model, tokenizer, label_encoder, device)
inference_time = time.time() - start_time
st.success("Hasil Prediksi:")
st.write(f"**Kegiatan Utama:** {r201}")
st.write(f"**Produk Utama:** {r202}")
deskripsi = kbli_df[kbli_df["kode_kbli"] == prediction]["deskripsi"].values
if len(prediction) == 4:
prediction = '0' + prediction
else:
prediction = prediction
st.write(f"**Kode KBLI:** {prediction}")
if len(deskripsi) > 0:
st.write(f"**Deskripsi:** {deskripsi[0]}")
else:
st.write("**Deskripsi:** Tidak ditemukan deskripsi untuk kode KBLI ini.")
st.write(f"**Keyakinan Model:** {confidence:.2f}%")
st.write(f"**Waktu Inferensi:** {inference_time:.6f} detik")
else:
st.warning("Harap isi kedua rincian sebelum mencari!")