Spaces:
Sleeping
Sleeping
rdsarjito
commited on
Commit
·
f8d91ce
1
Parent(s):
ee0b0e3
[FIRST COMMIT]
Browse files- app.py +47 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# Load model dan vectorizer
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model(model_choice):
|
| 8 |
+
with open(f'models/{model_choice}_model.pkl', 'rb') as f:
|
| 9 |
+
model = pickle.load(f)
|
| 10 |
+
with open('models/tfidf_vectorizer.pkl', 'rb') as f:
|
| 11 |
+
vectorizer = pickle.load(f)
|
| 12 |
+
return model, vectorizer
|
| 13 |
+
|
| 14 |
+
label_names = ['susu', 'kacang', 'telur', 'makanan_laut', 'gandum']
|
| 15 |
+
|
| 16 |
+
# UI
|
| 17 |
+
st.title("🚀 Multi-label Food Classification")
|
| 18 |
+
st.write("Masukkan teks untuk memprediksi kemungkinan alergi makanan.")
|
| 19 |
+
|
| 20 |
+
model_choice = st.selectbox(
|
| 21 |
+
"Pilih model:",
|
| 22 |
+
options=["KNN", "SVM", "RF"]
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
model, vectorizer = load_model(model_choice)
|
| 26 |
+
|
| 27 |
+
user_input = st.text_area("🧾 Masukkan teks makanan:")
|
| 28 |
+
|
| 29 |
+
threshold = st.slider("Threshold prediksi (default 0.5):", 0.0, 1.0, 0.5)
|
| 30 |
+
|
| 31 |
+
if st.button("Prediksi"):
|
| 32 |
+
if user_input.strip() != "":
|
| 33 |
+
user_vector = vectorizer.transform([user_input])
|
| 34 |
+
|
| 35 |
+
if hasattr(model, "predict_proba"):
|
| 36 |
+
user_proba = model.predict_proba(user_vector)
|
| 37 |
+
user_proba = [p[0][1] for p in user_proba] # probability class 1
|
| 38 |
+
else:
|
| 39 |
+
user_proba = model.predict(user_vector)[0]
|
| 40 |
+
user_proba = [float(val) for val in user_proba]
|
| 41 |
+
|
| 42 |
+
st.subheader(f"Hasil Prediksi ({model_choice}):")
|
| 43 |
+
for label, proba in zip(label_names, user_proba):
|
| 44 |
+
status = "✅ Ada" if proba >= threshold else "❌ Tidak Ada"
|
| 45 |
+
st.write(f"- **{label}**: {status} ({proba:.2f})")
|
| 46 |
+
else:
|
| 47 |
+
st.warning("Masukkan teks terlebih dahulu!")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
scikit-learn
|
| 3 |
+
pandas
|
| 4 |
+
pickle5
|
| 5 |
+
numpy
|