Spaces:
Sleeping
Sleeping
rdsarjito
commited on
Commit
Β·
3ab25d3
1
Parent(s):
feaf663
4 commit
Browse files
app.py
CHANGED
|
@@ -1,107 +1,84 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import torch
|
| 3 |
-
import
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
MAX_LEN = 128
|
| 13 |
-
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 14 |
|
| 15 |
-
#
|
| 16 |
@st.cache_resource
|
| 17 |
def load_model():
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
# Load state dict dan target columns
|
| 26 |
-
state = torch.load(MODEL_PATH, map_location=DEVICE)
|
| 27 |
-
model.load_state_dict(state['model_state_dict'])
|
| 28 |
-
target_columns = state['target_columns'] # Simpan target_columns
|
| 29 |
-
|
| 30 |
-
model.to(DEVICE)
|
| 31 |
model.eval()
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
return tokenizer, model, target_columns
|
| 34 |
|
| 35 |
-
#
|
| 36 |
def clean_text(text):
|
| 37 |
text = text.replace('--', ' ')
|
| 38 |
text = re.sub(r"http\S+", "", text)
|
| 39 |
-
text = re.sub(
|
| 40 |
text = re.sub("[^a-zA-Z0-9\s]", " ", text)
|
| 41 |
text = re.sub(" {2,}", " ", text)
|
| 42 |
-
return text.
|
| 43 |
-
|
| 44 |
-
# === Scrape dari Cookpad ===
|
| 45 |
-
def scrape_ingredients(url):
|
| 46 |
-
try:
|
| 47 |
-
headers = {'User-Agent': 'Mozilla/5.0'}
|
| 48 |
-
r = requests.get(url, headers=headers)
|
| 49 |
-
soup = BeautifulSoup(r.content, 'html.parser')
|
| 50 |
-
ingredients_div = soup.find('div', id='ingredients')
|
| 51 |
-
if ingredients_div:
|
| 52 |
-
return ingredients_div.get_text(separator=' ')
|
| 53 |
-
except:
|
| 54 |
-
return None
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
def
|
| 58 |
-
|
| 59 |
encoding = tokenizer.encode_plus(
|
| 60 |
-
|
| 61 |
add_special_tokens=True,
|
| 62 |
-
max_length=
|
| 63 |
truncation=True,
|
| 64 |
-
|
| 65 |
-
|
| 66 |
)
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
with torch.no_grad():
|
| 71 |
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 72 |
-
probs = torch.sigmoid(outputs
|
|
|
|
| 73 |
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
st.
|
| 78 |
-
st.title("π² Deteksi Alergen dari Resep Cookpad (IndoBERT)")
|
| 79 |
|
| 80 |
tokenizer, model, target_columns = load_model()
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
else:
|
| 86 |
-
url = st.text_input("π Masukkan URL Cookpad:")
|
| 87 |
-
user_input = ""
|
| 88 |
-
if url:
|
| 89 |
-
scraped = scrape_ingredients(url)
|
| 90 |
-
if scraped:
|
| 91 |
-
user_input = scraped
|
| 92 |
-
st.success("β
Berhasil mengambil bahan dari URL")
|
| 93 |
-
st.text_area("π Bahan dari URL:", value=user_input, height=200)
|
| 94 |
-
else:
|
| 95 |
-
st.error("β Gagal mengambil data dari URL.")
|
| 96 |
-
|
| 97 |
-
threshold = st.slider("π Threshold (default 0.5):", 0.0, 1.0, 0.5)
|
| 98 |
|
| 99 |
-
if
|
| 100 |
-
if
|
| 101 |
-
|
| 102 |
-
st.subheader("π Hasil Prediksi Alergen:")
|
| 103 |
-
for label, prob in result.items():
|
| 104 |
-
status = "β
Ada" if prob >= threshold else "β Tidak Ada"
|
| 105 |
-
st.write(f"- **{label}**: {status} ({prob:.2f})")
|
| 106 |
else:
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# ----- Define model class -----
|
| 8 |
+
class MultilabelBertClassifier(nn.Module):
|
| 9 |
+
def __init__(self, model_name, num_labels):
|
| 10 |
+
super(MultilabelBertClassifier, self).__init__()
|
| 11 |
+
self.bert = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=num_labels)
|
| 12 |
+
self.bert.classifier = nn.Linear(self.bert.config.hidden_size, num_labels)
|
| 13 |
|
| 14 |
+
def forward(self, input_ids, attention_mask):
|
| 15 |
+
outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
|
| 16 |
+
return outputs.logits
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# ----- Load model and tokenizer -----
|
| 19 |
@st.cache_resource
|
| 20 |
def load_model():
|
| 21 |
+
model_path = "model/alergen_model.pt"
|
| 22 |
+
checkpoint = torch.load(model_path, map_location=torch.device("cpu"))
|
| 23 |
+
target_columns = checkpoint["target_columns"]
|
| 24 |
+
|
| 25 |
+
model = MultilabelBertClassifier("indobenchmark/indobert-base-p1", num_labels=len(target_columns))
|
| 26 |
+
model.load_state_dict(checkpoint["model_state_dict"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
model.eval()
|
| 28 |
+
|
| 29 |
+
tokenizer = AutoTokenizer.from_pretrained("indobenchmark/indobert-base-p1")
|
| 30 |
+
|
| 31 |
return tokenizer, model, target_columns
|
| 32 |
|
| 33 |
+
# ----- Preprocessing function -----
|
| 34 |
def clean_text(text):
|
| 35 |
text = text.replace('--', ' ')
|
| 36 |
text = re.sub(r"http\S+", "", text)
|
| 37 |
+
text = re.sub("\n", " ", text)
|
| 38 |
text = re.sub("[^a-zA-Z0-9\s]", " ", text)
|
| 39 |
text = re.sub(" {2,}", " ", text)
|
| 40 |
+
return text.strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# ----- Prediction function -----
|
| 43 |
+
def predict_alergens(text, tokenizer, model, target_columns, max_length=128):
|
| 44 |
+
cleaned = clean_text(text)
|
| 45 |
encoding = tokenizer.encode_plus(
|
| 46 |
+
cleaned,
|
| 47 |
add_special_tokens=True,
|
| 48 |
+
max_length=max_length,
|
| 49 |
truncation=True,
|
| 50 |
+
return_tensors='pt',
|
| 51 |
+
padding='max_length'
|
| 52 |
)
|
| 53 |
+
|
| 54 |
+
input_ids = encoding["input_ids"]
|
| 55 |
+
attention_mask = encoding["attention_mask"]
|
| 56 |
|
| 57 |
with torch.no_grad():
|
| 58 |
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 59 |
+
probs = torch.sigmoid(outputs)
|
| 60 |
+
preds = (probs > 0.5).float().squeeze(0).tolist()
|
| 61 |
|
| 62 |
+
results = {target: bool(preds[i]) for i, target in enumerate(target_columns)}
|
| 63 |
+
return results
|
| 64 |
|
| 65 |
+
# ----- Streamlit App UI -----
|
| 66 |
+
st.title("Deteksi Alergen dari Resep")
|
|
|
|
| 67 |
|
| 68 |
tokenizer, model, target_columns = load_model()
|
| 69 |
|
| 70 |
+
with st.form("alergen_form"):
|
| 71 |
+
input_text = st.text_area("Masukkan daftar bahan (ingredients):", height=200)
|
| 72 |
+
submitted = st.form_submit_button("Deteksi Alergen")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
if submitted:
|
| 75 |
+
if input_text.strip() == "":
|
| 76 |
+
st.warning("Mohon masukkan teks bahan terlebih dahulu.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
else:
|
| 78 |
+
results = predict_alergens(input_text, tokenizer, model, target_columns)
|
| 79 |
+
st.subheader("Hasil Deteksi Alergen:")
|
| 80 |
+
for alergi, status in results.items():
|
| 81 |
+
if status:
|
| 82 |
+
st.error(f"- {alergi.capitalize()}")
|
| 83 |
+
else:
|
| 84 |
+
st.success(f"- {alergi.capitalize()}: Aman")
|