Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +18 -5
static/script.js
CHANGED
@@ -42,6 +42,7 @@ function handleResponse(userInput) {
|
|
42 |
let botResponse = '';
|
43 |
let options = [];
|
44 |
|
|
|
45 |
if (conversation.length === 2) { // After name input
|
46 |
botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
|
47 |
options = [
|
@@ -49,6 +50,7 @@ function handleResponse(userInput) {
|
|
49 |
{ text: 'Non-Vegetarian', class: 'red' }
|
50 |
];
|
51 |
} else if (lastMessage.includes('non-vegetarian') || lastMessage.includes('non veg') || lastMessage.includes('nonveg')) {
|
|
|
52 |
botResponse = 'Great choice! 🍽️ We have some amazing non-vegetarian options! What\'s your dietary preference?';
|
53 |
options = [
|
54 |
{ text: 'Low Carb', class: '' },
|
@@ -57,16 +59,26 @@ function handleResponse(userInput) {
|
|
57 |
{ text: 'Halal', class: '' }
|
58 |
];
|
59 |
} else if (lastMessage.includes('vegetarian')) {
|
|
|
60 |
botResponse = 'Great choice! 🍽️ We have some amazing vegetarian options! What\'s your dietary preference?';
|
61 |
options = [
|
62 |
{ text: 'Vegan', class: '' },
|
63 |
{ text: 'Gluten-Free', class: '' },
|
64 |
-
{ text: 'Vegetarian', class: '' },
|
65 |
{ text: 'Low Carb', class: '' },
|
66 |
{ text: 'Dairy-Free', class: '' },
|
67 |
{ text: 'Keto', class: '' },
|
68 |
{ text: 'Halal', class: '' }
|
69 |
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
}
|
71 |
|
72 |
addMessage('bot', botResponse);
|
@@ -76,14 +88,13 @@ function handleResponse(userInput) {
|
|
76 |
}
|
77 |
|
78 |
|
79 |
-
|
80 |
-
function fetchIngredients(dietaryPreference) {
|
81 |
fetch('/get_ingredients', {
|
82 |
method: 'POST',
|
83 |
headers: {
|
84 |
'Content-Type': 'application/json',
|
85 |
},
|
86 |
-
body: JSON.stringify({ dietary_preference:
|
87 |
})
|
88 |
.then(response => response.json())
|
89 |
.then(data => {
|
@@ -91,7 +102,7 @@ function fetchIngredients(dietaryPreference) {
|
|
91 |
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
|
92 |
} else {
|
93 |
const ingredients = data.ingredients || [];
|
94 |
-
addMessage('bot', '
|
95 |
displayIngredientsList(ingredients);
|
96 |
displaySelectedIngredients();
|
97 |
}
|
@@ -101,6 +112,7 @@ function fetchIngredients(dietaryPreference) {
|
|
101 |
});
|
102 |
}
|
103 |
|
|
|
104 |
function displayIngredientsList(ingredients) {
|
105 |
const chatMessages = document.getElementById('chatMessages');
|
106 |
if (!chatMessages) {
|
@@ -144,6 +156,7 @@ function displayIngredientsList(ingredients) {
|
|
144 |
});
|
145 |
}
|
146 |
|
|
|
147 |
function displaySelectedIngredients() {
|
148 |
const chatMessages = document.getElementById('chatMessages');
|
149 |
if (!chatMessages) {
|
|
|
42 |
let botResponse = '';
|
43 |
let options = [];
|
44 |
|
45 |
+
// Store food preference (Vegetarian or Non-Vegetarian)
|
46 |
if (conversation.length === 2) { // After name input
|
47 |
botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
|
48 |
options = [
|
|
|
50 |
{ text: 'Non-Vegetarian', class: 'red' }
|
51 |
];
|
52 |
} else if (lastMessage.includes('non-vegetarian') || lastMessage.includes('non veg') || lastMessage.includes('nonveg')) {
|
53 |
+
conversation.push({ role: 'user', message: 'Non-Vegetarian' }); // Store Non-Vegetarian preference
|
54 |
botResponse = 'Great choice! 🍽️ We have some amazing non-vegetarian options! What\'s your dietary preference?';
|
55 |
options = [
|
56 |
{ text: 'Low Carb', class: '' },
|
|
|
59 |
{ text: 'Halal', class: '' }
|
60 |
];
|
61 |
} else if (lastMessage.includes('vegetarian')) {
|
62 |
+
conversation.push({ role: 'user', message: 'Vegetarian' }); // Store Vegetarian preference
|
63 |
botResponse = 'Great choice! 🍽️ We have some amazing vegetarian options! What\'s your dietary preference?';
|
64 |
options = [
|
65 |
{ text: 'Vegan', class: '' },
|
66 |
{ text: 'Gluten-Free', class: '' },
|
|
|
67 |
{ text: 'Low Carb', class: '' },
|
68 |
{ text: 'Dairy-Free', class: '' },
|
69 |
{ text: 'Keto', class: '' },
|
70 |
{ text: 'Halal', class: '' }
|
71 |
];
|
72 |
+
} else if (lastMessage.includes('low carb') || lastMessage.includes('dairy-free') || lastMessage.includes('keto') || lastMessage.includes('halal') || lastMessage.includes('gluten-free') || lastMessage.includes('vegan') || lastMessage.includes('vegetarian')) {
|
73 |
+
// Fetch ingredients based on previous food preference (Vegetarian or Non-Vegetarian)
|
74 |
+
const foodPreference = conversation.find(msg => msg.role === 'user' && (msg.message.includes('vegetarian') || msg.message.includes('non-vegetarian')))?.message.toLowerCase();
|
75 |
+
|
76 |
+
if (foodPreference === 'vegetarian') {
|
77 |
+
fetchIngredients('veg'); // Fetch Vegetarian ingredients
|
78 |
+
} else if (foodPreference === 'non-vegetarian') {
|
79 |
+
fetchIngredients('non-vegetarian'); // Fetch Non-Vegetarian ingredients
|
80 |
+
}
|
81 |
+
return; // Exit early to fetch ingredients
|
82 |
}
|
83 |
|
84 |
addMessage('bot', botResponse);
|
|
|
88 |
}
|
89 |
|
90 |
|
91 |
+
function fetchIngredients(foodPreference) {
|
|
|
92 |
fetch('/get_ingredients', {
|
93 |
method: 'POST',
|
94 |
headers: {
|
95 |
'Content-Type': 'application/json',
|
96 |
},
|
97 |
+
body: JSON.stringify({ dietary_preference: foodPreference }) // Send the food preference to the backend
|
98 |
})
|
99 |
.then(response => response.json())
|
100 |
.then(data => {
|
|
|
102 |
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
|
103 |
} else {
|
104 |
const ingredients = data.ingredients || [];
|
105 |
+
addMessage('bot', 'Here are some available ingredients:');
|
106 |
displayIngredientsList(ingredients);
|
107 |
displaySelectedIngredients();
|
108 |
}
|
|
|
112 |
});
|
113 |
}
|
114 |
|
115 |
+
|
116 |
function displayIngredientsList(ingredients) {
|
117 |
const chatMessages = document.getElementById('chatMessages');
|
118 |
if (!chatMessages) {
|
|
|
156 |
});
|
157 |
}
|
158 |
|
159 |
+
|
160 |
function displaySelectedIngredients() {
|
161 |
const chatMessages = document.getElementById('chatMessages');
|
162 |
if (!chatMessages) {
|