Spaces:
Sleeping
Sleeping
rdsarjito commited on
Commit Β·
39e7123
1
Parent(s): 2bb0484
29
Browse files
app.py
CHANGED
|
@@ -171,19 +171,37 @@ def load_model():
|
|
| 171 |
def predict_allergen(model, vectorizer, input_text):
|
| 172 |
X_input = vectorizer.transform([input_text])
|
| 173 |
prediction = model.predict(X_input)
|
|
|
|
| 174 |
try:
|
|
|
|
| 175 |
probabilities = model.predict_proba(X_input)
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
# === Scraping bahan dari Cookpad ===
|
| 189 |
def get_ingredients_from_cookpad(url):
|
|
@@ -229,17 +247,18 @@ def display_results(results, probabilities, labels):
|
|
| 229 |
for i, (allergen, status) in enumerate(results.items()):
|
| 230 |
emoji = allergen_emojis.get(allergen, 'π')
|
| 231 |
|
| 232 |
-
#
|
| 233 |
try:
|
| 234 |
-
if isinstance(probabilities
|
| 235 |
-
confidence = probabilities[i]
|
| 236 |
-
elif
|
| 237 |
confidence = probabilities[i] * 100
|
| 238 |
else:
|
| 239 |
-
|
|
|
|
| 240 |
except (IndexError, TypeError):
|
| 241 |
-
# Fallback
|
| 242 |
-
confidence =
|
| 243 |
|
| 244 |
if status == 1: # Detected
|
| 245 |
detected_allergens.append(allergen)
|
|
@@ -249,9 +268,11 @@ def display_results(results, probabilities, labels):
|
|
| 249 |
</div>
|
| 250 |
''', unsafe_allow_html=True)
|
| 251 |
else: # Not detected
|
|
|
|
|
|
|
| 252 |
st.markdown(f'''
|
| 253 |
<div class="allergen-result allergen-safe">
|
| 254 |
-
{emoji} {allergen}: Tidak Terdeteksi β ({
|
| 255 |
</div>
|
| 256 |
''', unsafe_allow_html=True)
|
| 257 |
|
|
|
|
| 171 |
def predict_allergen(model, vectorizer, input_text):
|
| 172 |
X_input = vectorizer.transform([input_text])
|
| 173 |
prediction = model.predict(X_input)
|
| 174 |
+
|
| 175 |
try:
|
| 176 |
+
# Untuk multi-label classification, predict_proba mengembalikan list probabilitas
|
| 177 |
probabilities = model.predict_proba(X_input)
|
| 178 |
+
|
| 179 |
+
# Jika probabilities adalah list of arrays (multi-label)
|
| 180 |
+
if isinstance(probabilities, list):
|
| 181 |
+
# Ambil probabilitas untuk kelas positif dari setiap classifier
|
| 182 |
+
positive_probs = []
|
| 183 |
+
for i, prob_array in enumerate(probabilities):
|
| 184 |
+
if prob_array.shape[1] == 2: # Binary classification
|
| 185 |
+
positive_probs.append(prob_array[0][1]) # Probabilitas kelas positif
|
| 186 |
+
else:
|
| 187 |
+
positive_probs.append(prob_array[0][0]) # Jika hanya 1 kelas
|
| 188 |
+
return prediction[0], positive_probs
|
| 189 |
+
else:
|
| 190 |
+
# Single output
|
| 191 |
+
return prediction[0], probabilities[0]
|
| 192 |
+
|
| 193 |
+
except Exception as e:
|
| 194 |
+
# Jika predict_proba gagal, gunakan decision_function jika tersedia
|
| 195 |
+
try:
|
| 196 |
+
decision_scores = model.decision_function(X_input)
|
| 197 |
+
# Convert decision scores to probabilities using sigmoid
|
| 198 |
+
import numpy as np
|
| 199 |
+
probabilities = 1 / (1 + np.exp(-decision_scores[0]))
|
| 200 |
+
return prediction[0], probabilities
|
| 201 |
+
except:
|
| 202 |
+
# Last fallback - return predictions as confidence (0 or 1 -> 0% or 100%)
|
| 203 |
+
confidence_scores = [float(pred) for pred in prediction[0]]
|
| 204 |
+
return prediction[0], confidence_scores
|
| 205 |
|
| 206 |
# === Scraping bahan dari Cookpad ===
|
| 207 |
def get_ingredients_from_cookpad(url):
|
|
|
|
| 247 |
for i, (allergen, status) in enumerate(results.items()):
|
| 248 |
emoji = allergen_emojis.get(allergen, 'π')
|
| 249 |
|
| 250 |
+
# Get actual probability from model
|
| 251 |
try:
|
| 252 |
+
if isinstance(probabilities, list) and i < len(probabilities):
|
| 253 |
+
confidence = probabilities[i] * 100
|
| 254 |
+
elif hasattr(probabilities, '__getitem__') and i < len(probabilities):
|
| 255 |
confidence = probabilities[i] * 100
|
| 256 |
else:
|
| 257 |
+
# If no probability available, show based on prediction
|
| 258 |
+
confidence = 100.0 if status == 1 else 0.0
|
| 259 |
except (IndexError, TypeError):
|
| 260 |
+
# Fallback to prediction-based confidence
|
| 261 |
+
confidence = 100.0 if status == 1 else 0.0
|
| 262 |
|
| 263 |
if status == 1: # Detected
|
| 264 |
detected_allergens.append(allergen)
|
|
|
|
| 268 |
</div>
|
| 269 |
''', unsafe_allow_html=True)
|
| 270 |
else: # Not detected
|
| 271 |
+
# For negative cases, show (100 - confidence) to represent "not detected" confidence
|
| 272 |
+
negative_confidence = 100 - confidence if confidence > 50 else confidence
|
| 273 |
st.markdown(f'''
|
| 274 |
<div class="allergen-result allergen-safe">
|
| 275 |
+
{emoji} {allergen}: Tidak Terdeteksi β ({negative_confidence:.2f}%)
|
| 276 |
</div>
|
| 277 |
''', unsafe_allow_html=True)
|
| 278 |
|