-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipegenerator.py
530 lines (409 loc) · 17.4 KB
/
recipegenerator.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# **Web Scraping**
! python -m pip install BeautifulSoup4
! python -m pip install requests
!pip install bs4 selenium
!apt install chronium-chromedriver
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import selenium
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chromium.webdriver import ChromiumDriver
import csv
import time
import pandas as pd
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import requests
from bs4 import BeautifulSoup
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
options.add_argument('no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options)
url = "https://www.foodfusion.com/recipe-category/lunch-dinner/"
driver.get(url)
driver.implicitly_wait(5)
import time
scroll=0;
while True:
driver.execute_script('window.scrollBy(0,6000);')
scroll+=1
time.sleep(5)
if scroll==5:
break
"""Extracting further WebLinks for extracting Recipes and its Directions"""
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import time
# List to store the links
recipe_links = []
# Extract links from all pages
while True:
# Get the page source
html = driver.page_source
# Parse the HTML using Beautiful Soup
soup = BeautifulSoup(html, "html.parser")
# Find all the div elements with class "uk-card uk-card-default card-border"
recipe_divs = soup.find_all("div", class_="uk-card uk-card-default card-border")
# Extract the links for each title
for div in recipe_divs:
link = div.find("a", class_="grid-img-link")
if link:
recipe_links.append(link['href'])
# Find the next page button and click it
try:
next_page_button = driver.find_elements(By.CLASS_NAME, "next-page")
if next_page_button and "inactive" in next_page_button[0].get_attribute("class"):
break # If the next page button is inactive, we have reached the last page
else:
next_page_button[0].click()
time.sleep(2)
except IndexError:
break # If the next page button is not found, we have reached the last page
# Print the list of links
for link in recipe_links:
print(link)
"""Extracting Ingredients and Directions from Recipe Links"""
import csv
recipe_data = []
for link in recipe_links:
driver.get(link)
driver.implicitly_wait(5)
scroll = 0
while True:
driver.execute_script('window.scrollBy(0,6000);')
scroll += 1
time.sleep(5)
if scroll == 5:
break
recipe = {}
ingredients = []
directions = []
# Extract ingredients
ingredients_tags = driver.find_elements(By.XPATH, "//div[@class='english-detail-ff']//p[text()='Ingredients:']/following-sibling::p")
for ingredient in ingredients_tags:
if ingredient.text.strip():
if "Directions:" in ingredient.text:
break
ingredients.append(ingredient.text.strip())
# Extract directions
directions_tags = driver.find_elements(By.XPATH, "//div[@class='english-detail-ff']//p[text()='Directions:']/following-sibling::p")
for direction in directions_tags:
if direction.text.strip():
directions.append(direction.text.strip())
recipe['ingredients'] = ingredients
recipe['directions'] = directions
recipe_data.append(recipe)
"""Saving Scrapped data in CSV File"""
filename = 'recipe_data.csv'
with open(filename, 'a', newline='', encoding='utf-8') as csvfile:
fieldnames = ['Recipe', 'Ingredients', 'Directions']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for idx, recipe in enumerate(recipe_data, start=1649):
writer.writerow({'Recipe': f"Recipe {idx}", 'Ingredients': '\n'.join(recipe['ingredients']), 'Directions': '\n'.join(recipe['directions'])})
print(f"Recipe data has been saved to {filename}.")
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pandas as pd
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
from tensorflow.keras.preprocessing.sequence import pad_sequences
import re
"""# **Preprocessing**"""
df = pd.read_csv('recipes_data.csv')
df.head()
df.describe()
df.dropna()
def extract_ingredients(ingredients):
# Check if ingredients is a string
if isinstance(ingredients, str):
# Remove all special characters except parentheses
ingredients = re.sub(r'[^\w\s()]','', ingredients)
# Find all occurrences of text within parentheses
matches = re.findall(r'\((.*?)\)', ingredients)
# Join the matches with commas
return ', '.join(matches)
else:
# If ingredients is not a string, return an empty string
return ''
# Apply the function to the 'ingredients' column
df['Ingredients'] = df['Ingredients'].apply(extract_ingredients)
# Write the DataFrame back to the CSV file
df.to_csv('recipe_data.csv', index=False)
df = pd.read_csv('recipe_data.csv')
df.head()
df.describe()
"""# **applying models**
## **ML**
**TfidfVectorizer for feature extraction and a Multinomial Naive Bayes classifier, which is commonly used for text classification tasks.**
The TfidfVectorizer converts the text data into numerical features, and the Multinomial Naive Bayes model is then trained on these features.
"""
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn import metrics
import plotly.express as px
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import plotly.graph_objects as go
import plotly.figure_factory as ff
from sklearn.pipeline import make_pipeline
from sklearn.metrics import confusion_matrix
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score, classification_report
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# Drop rows with missing values in the 'Ingredients' column
df.dropna(subset=['Ingredients'], inplace=True)
# Convert 'Ingredients' and 'Recipes' to strings
df['Ingredients'] = df['Ingredients'].astype(str)
df['Directions'] = df['Directions'].astype(str)
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(df['Ingredients'], df['Directions'], test_size=0.2, random_state=42)
# Create a pipeline with TfidfVectorizer and Multinomial Naive Bayes
model = make_pipeline(TfidfVectorizer(), MultinomialNB())
# Train the model
model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
# Display classification report
print("Classification Report:\n", classification_report(y_test, y_pred))
# Visualization 1: Bar chart for Recipe Frequency
recipe_counts = df['Directions'].value_counts()
fig_recipe_counts = px.bar(recipe_counts, x=recipe_counts.index, y=recipe_counts.values, labels={'x': 'Recipe', 'y': 'Frequency'},
title='Recipe Frequency', color=recipe_counts.values, color_continuous_scale='Viridis')
fig_recipe_counts.show()
# Visualization 2: Word Cloud for Ingredients
all_ingredients = ' '.join(df['Ingredients'])
wordcloud = WordCloud(width=800, height=400, random_state=21, max_font_size=110).generate(all_ingredients)
plt.figure(figsize=(10, 6))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
# Checking model output for specific input
input_ingredients = "Tomatoes, Green chillies, Garlic, Ginger, Onion, Cumin seeds, Coriander seeds, Red chilli powder, Turmeric powder, Spinach, Potatoes, Yogurt, Fresh coriander, Green chilli"
predicted_recipe = model.predict([input_ingredients])[0]
print(f"Input Ingredients: {input_ingredients}")
print(f"Predicted Recipe: {predicted_recipe}")
# Calculate evaluation metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
f1 = f1_score(y_test, y_pred, average='weighted')
# Display metrics
print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"F1 Score: {f1:.2f}")
# Create a line chart for metrics
metrics_data = {
'Metric': ['Accuracy', 'Precision', 'Recall', 'F1 Score'],
'Score': [accuracy, precision, recall, f1]
}
fig_metrics_line = go.Figure()
fig_metrics_line.add_trace(go.Scatter(x=metrics_data['Metric'], y=metrics_data['Score'], mode='lines+markers', name='Metrics'))
fig_metrics_line.update_layout(title='Model Evaluation Metrics (Line Chart)',
xaxis_title='Metric', yaxis_title='Score')
fig_metrics_line.show()
# Visualization of Evaluation Metrics
metrics_data = {
'Metric': ['Accuracy', 'Precision', 'Recall', 'F1 Score'],
'Score': [accuracy, precision, recall, f1]
}
fig_metrics = px.bar(metrics_data, x='Metric', y='Score', labels={'Score': 'Score Value'},
title='Model Evaluation Metrics', color='Score', color_continuous_scale='Viridis')
fig_metrics.show()
"""confusion matrix with plt"""
# Calculate the confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred, labels=df['Directions'].unique())
# Create a confusion matrix heatmap with matplotlib and seaborn
plt.figure(figsize=(10, 8))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='viridis',
xticklabels=df['Directions'].unique(), yticklabels=df['Directions'].unique())
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()
"""confusion matrix with plotly"""
conf_matrix = confusion_matrix(y_test, y_pred, labels=df['Directions'].unique())
# Create a confusion matrix heatmap
fig_conf_matrix = ff.create_annotated_heatmap(
z=conf_matrix,
x=list(df['Directions'].unique()),
y=list(df['Directions'].unique()),
colorscale='Viridis',
showscale=True,
reversescale=True,
)
fig_conf_matrix.update_layout(
title='Confusion Matrix',
xaxis_title='Predicted',
yaxis_title='Actual',
)
fig_conf_matrix.show()
"""# **DL**
## **ANN**
"""
import numpy as np
import pandas as pd
import plotly.express as px
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Embedding, Flatten, Dense
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.figure_factory as ff
from sklearn.metrics import confusion_matrix
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import train_test_split
df = pd.read_csv('recipe_data.csv')
df['Ingredients'] = df['Ingredients'].astype(str)
df['Directions'] = df['Directions'].astype(str)
# Tokenize ingredients
tokenizer = Tokenizer()
tokenizer.fit_on_texts(df['Ingredients'])
X = tokenizer.texts_to_sequences(df['Ingredients'])
# Pad sequences to have consistent length
X_padded = pad_sequences(X)
# Encode recipes
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(df['Directions'])
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_padded, y_encoded, test_size=0.2, random_state=42)
# Build a simple neural network
model = Sequential()
model.add(Embedding(input_dim=len(tokenizer.word_index) + 1, output_dim=50, input_length=X_padded.shape[1]))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(len(df['Directions'].unique()), activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
Model = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_split=0.2)
metrics = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
values = [0.0031, 0.0125, 0.0133, 0.0148, 0.0179, 0.0405 , 0.1068, 0.1964, 0.3172,0.4669]
# Line Chart
line_chart = go.Figure()
line_chart.add_trace(go.Scatter(x=metrics, y=values, mode='lines+markers', name='Line Chart'))
line_chart.update_layout(title='Performance Metrics',
xaxis_title='Epochs',
yaxis_title='accuracy')
line_chart.show()
epochs = 10
# Extract accuracy values from the history object
accuracy_values = Model.history['accuracy']
# Generate the list of epochs
epochs_list = list(range(1, epochs + 1))
# Line Chart
line_chart = go.Figure()
line_chart.add_trace(go.Scatter(x=epochs_list, y=accuracy_values, mode='lines+markers', name='Line Chart'))
line_chart.update_layout(title='Performance Metrics',
xaxis_title='Epochs',
yaxis_title='Accuracy')
line_chart.show()
# Evaluate the model
loss, accuracy = model.evaluate(X_test, y_test)
print(f"Accuracy: {accuracy:.2f}")
# Example prediction
input_ingredients = ["Tomatoes, Green chillies, Garlic, Ginger, Onion, Cumin seeds, Coriander seeds, Red chilli powder, Turmeric powder, Spinach, Potatoes, Yogurt, Fresh coriander, Green chilli"]
input_sequence = tokenizer.texts_to_sequences(input_ingredients)
input_padded = pad_sequences(input_sequence, maxlen=X_padded.shape[1])
predicted_probs = model.predict(input_padded)
predicted_class = np.argmax(predicted_probs[0])
predicted_recipe = label_encoder.inverse_transform([predicted_class])[0]
print(f"Input Ingredients: {input_ingredients}")
print(f"Predicted Recipe: {predicted_recipe}")
"""-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Evaluation"""
# Encode recipes for y_test
y_test_encoded = label_encoder.transform(df['Directions'].iloc[y_test])
# Evaluate the model
y_pred_probs = model.predict(X_test)
y_pred = np.argmax(y_pred_probs, axis=1)
# Encode recipes for y_pred
y_pred_encoded = label_encoder.transform(df['Directions'].iloc[y_pred])
# Calculate evaluation metrics
accuracy = accuracy_score(y_test_encoded, y_pred_encoded)
precision = precision_score(y_test_encoded, y_pred_encoded, average='weighted')
recall = recall_score(y_test_encoded, y_pred_encoded, average='weighted')
f1 = f1_score(y_test_encoded, y_pred_encoded, average='weighted')
# Display metrics
print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"F1 Score: {f1:.2f}")
# Visualization of Evaluation Metrics
metrics_data = {
'Metric': ['Accuracy', 'Precision', 'Recall', 'F1 Score'],
'Score': [accuracy, precision, recall, f1]
}
# Bar graph
fig_metrics_bar = px.bar(metrics_data, x='Metric', y='Score', labels={'Score': 'Score Value'},
title='Model Evaluation Metrics (Bar Graph)',
color='Score', color_continuous_scale='Viridis')
fig_metrics_bar.show()
# Line chart
fig_metrics_line = px.line(metrics_data, x='Metric', y='Score', labels={'Score': 'Score Value'},
title='Model Evaluation Metrics (Line Chart)')
fig_metrics_line.show()
# Evaluate the model
y_pred_probs = model.predict(X_test)
y_pred = np.argmax(y_pred_probs, axis=1)
# Encode recipes for y_pred
y_pred_encoded = label_encoder.transform(df['Directions'].iloc[y_pred])
y_test_encoded = label_encoder.transform(df['Directions'].iloc[y_test])
# Get unique labels from y_test_encoded
unique_labels = np.unique(np.concatenate((y_test_encoded, y_pred_encoded)))
# Calculate the confusion matrix
conf_matrix = confusion_matrix(y_test_encoded, y_pred_encoded, labels=unique_labels)
# Create a confusion matrix heatmap
fig_conf_matrix = ff.create_annotated_heatmap(
z=conf_matrix,
x=unique_labels,
y=unique_labels,
colorscale='Viridis',
showscale=True,
reversescale=True,
)
fig_conf_matrix.update_layout(
title='Confusion Matrix Heatmap',
xaxis_title='Predicted',
yaxis_title='Actual',
)
fig_conf_matrix.show()
model.summary()
"""--------------------------------------------------------------------------------------------------------------------------------------
## **Comparison**
"""
categories = ['Accuracy of ML Model', 'Accuracy of DL Model']
accuracy = [0.4, 0.8]
plt.bar(categories, accuracy, color=['blue', 'orange'])
plt.xlabel('Model Type')
plt.ylabel('Accuracy')
plt.title('Comparing ML with DL Models ')
plt.show()