-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallergendetection.py
More file actions
175 lines (164 loc) · 6.2 KB
/
Copy pathallergendetection.py
File metadata and controls
175 lines (164 loc) · 6.2 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report
# Load the dataset
dataset_path = 'dataset/final_allergen_detection_dataset.csv' # Replace with your dataset path
df = pd.read_csv(dataset_path)
# Prepare the data
X = df['Ingredients'] # Input features (ingredients)
y = df['Allergy?'] # Target label (Yes/No)
# Manual allergen-to-type mapping
allergen_type_mapping = {
"soy": "Soy Allergy",
"peanut": "Peanut Allergy",
"almond": "Nut Allergy",
"wheat": "Gluten Allergy",
"milk": "Milk Allergy / Lactose Intolerance",
"shellfish": "Shellfish Allergy",
"fish": "Fish Allergy",
"egg": "Egg Allergy",
"sesame": "Seed Allergy",
"walnut": "Nut Allergy",
"cashew": "Nut Allergy",
"hazelnut": "Nut Allergy",
"shrimp": "Shellfish Allergy",
"lobster": "Shellfish Allergy",
"crab": "Shellfish Allergy",
"butter": "Milk Allergy / Lactose Intolerance",
"cheese": "Milk Allergy / Lactose Intolerance",
"yogurt": "Milk Allergy / Lactose Intolerance",
"corn": "Corn Allergy",
"barley": "Gluten Allergy",
"nut": "Nut Allergy",
"coconut": "Seed Allergy",
"soybean": "Soy Allergy",
"mustard": "Mustard Allergy",
"celery": "Hypersensitivity",
"apple": "Oral Allergy Syndrome",
"kiwi": "Oral Allergy Syndrome",
"banana": "Banana Allergy",
"avocado": "Oral Allergy Syndrome",
"garlic": "Allium Allergy",
"onion": "Allium Allergy",
"carrot": "Hypersensitivity",
"casein": "Milk Allergy / Lactose Intolerance",
"cattle": "Alpha-gal Syndrome",
"cauliflower": "Cruciferous Allergy",
"brussels sprouts": "Cruciferous Allergy",
"broccoli": "Broccoli Allergy",
"cucumber": "Unknown",
"cream": "Milk Allergy / Lactose Intolerance",
"eggplant": "Nightshade Allergy",
"endive": "Insulin Allergy",
"fructose": "Sugar Allergy / Intolerance",
"ginkgo nut": "Nut Allergy",
"horseradish": "Cruciferous Allergy",
"grape": "LTP Allergy",
"grapefruit": "Citrus Allergy",
"honey": "Honey Allergy",
"lettuce": "LTP Allergy",
"leek": "Allium Allergy",
"lemon": "Citrus Allergy",
"lime": "Citrus Allergy",
"mango": "Oral Allergy Syndrome",
"milk powder": "Milk Allergy / Lactose Intolerance",
"mushroom": "Mushroom Allergy",
"okra": "Histamine Allergy",
"olive oil": "Unknown",
"orange": "Citrus Allergy",
"papaya": "Oral Allergy Syndrome",
"parsley": "Hypersensitivity",
"parsnip": "Hypersensitivity",
"peach": "Stone Fruit Allergy",
"pecan": "Nut Allergy",
"pistachio": "Nut Allergy",
"pineapple": "Oral Allergy Syndrome",
"potato": "Potato Allergy",
"quince": "Oral Allergy Syndrome",
"raspberry": "Salicylate Allergy",
"rice": "Rice Allergy",
"rye": "Gluten Allergy",
"salmon": "Fish Allergy",
"sardine": "Fish Allergy",
"shallot": "Allium Allergy",
"spinach": "Histamine Allergy",
"strawberry": "Salicylate Allergy",
"sugar": "Sugar Allergy / Intolerance",
"squid": "Shellfish Allergy",
"sweet potato": "Potato Allergy",
"tomato": "Nightshade Allergy",
"trout": "Fish Allergy",
"tuna": "Fish Allergy",
"turkey": "Poultry Allergy",
"walnut": "Nut Allergy",
"watermelon": "Unknown",
"wheat flour": "Gluten Allergy",
"white bean": "Legume Allergy",
"yam": "Potato Allergy",
"yogurt": "Milk Allergy / Lactose Intolerance",
"zucchini": "Unknown",
"anchovy": "Fish Allergy",
"brie": "Milk Allergy / Lactose Intolerance",
"croutons": "Gluten Allergy",
"tahini": "Seed Allergy",
"almond milk": "Nut Allergy",
"chickpeas": "Legume Allergy",
"lentils": "Legume Allergy",
"tofu": "Soy Allergy",
"miso": "Soy Allergy",
"peanut butter": "Peanut Allergy",
"prawns": "Shellfish Allergy",
"lobster bisque": "Shellfish Allergy",
"parmesan": "Milk Allergy / Lactose Intolerance",
"basil": "Hypersensitivity",
"cream cheese": "Milk Allergy / Lactose Intolerance",
"buttermilk": "Milk Allergy / Lactose Intolerance",
"paneer": "Milk Allergy / Lactose Intolerance",
"sour cream": "Milk Allergy / Lactose Intolerance",
"margarine": "Milk Allergy / Lactose Intolerance",
"soy milk": "Soy Allergy",
"oats": "Gluten Allergy",
"buckwheat": "Gluten Allergy",
"clam": "Shellfish Allergy",
"lobster": "Shellfish Allergy",
}
# Preprocess the data: Vectorize the Ingredients column
vectorizer = TfidfVectorizer()
X_vectorized = vectorizer.fit_transform(X)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_vectorized, y, test_size=0.2, random_state=42)
# Train the Naive Bayes model
model = MultinomialNB(alpha=2.5) # Adjust alpha for smoothing
model.fit(X_train, y_train)
# Evaluate the model
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy of the Allergen Detection System: {accuracy * 100:.2f}%")
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
# Function to predict allergy status and type
def predict_allergy_with_type(ingredients):
"""
Predict whether a recipe contains allergens and identify the type.
"""
ingredients_vectorized = vectorizer.transform([ingredients])
prediction = model.predict(ingredients_vectorized)[0]
if prediction == 'Yes':
# Match ingredients to allergens in the mapping
detected_allergens = [
allergen for allergen in allergen_type_mapping.keys()
if allergen in ingredients.lower()
]
allergy_types = [allergen_type_mapping[allergen] for allergen in detected_allergens]
return prediction, ', '.join(detected_allergens), ', '.join(allergy_types)
else:
return prediction, None, None
# Example usage
new_ingredients = "chicken, butter, soy sauce"
allergy_status, potential_allergens, allergy_type = predict_allergy_with_type(new_ingredients)
print(f"\nPrediction for '{new_ingredients}':")
print(f"Allergy?: {allergy_status}")
print(f"Potential Allergy: {potential_allergens}")
print(f"Type: {allergy_type}")