Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +51 -27
static/script.js
CHANGED
|
@@ -28,10 +28,7 @@ function sendMessage() {
|
|
| 28 |
addMessage('user', message);
|
| 29 |
conversation.push({ role: 'user', message: message });
|
| 30 |
userInput.value = '';
|
| 31 |
-
|
| 32 |
-
setTimeout(() => {
|
| 33 |
-
handleResponse(message);
|
| 34 |
-
}, 500);
|
| 35 |
} else {
|
| 36 |
console.warn('Empty message!');
|
| 37 |
}
|
|
@@ -49,27 +46,20 @@ function handleResponse(userInput) {
|
|
| 49 |
{ text: 'Non-Vegetarian', class: 'red' }
|
| 50 |
];
|
| 51 |
} else if (lastMessage.includes('vegetarian')) {
|
| 52 |
-
botResponse = 'Great choice! 🍽️
|
| 53 |
-
|
| 54 |
-
{ text: 'Vegan', class: '' },
|
| 55 |
-
{ text: 'Gluten-Free', class: '' },
|
| 56 |
-
{ text: 'Vegetarian', class: '' },
|
| 57 |
-
{ text: 'Low Carb', class: '' },
|
| 58 |
-
{ text: 'Dairy-Free', class: '' },
|
| 59 |
-
{ text: 'Keto', class: '' },
|
| 60 |
-
{ text: 'Halal', class: '' }
|
| 61 |
-
];
|
| 62 |
} else if (lastMessage.includes('non-vegetarian')) {
|
| 63 |
-
botResponse = 'Great choice! 🍽️
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
options = [
|
| 65 |
-
{ text: '
|
| 66 |
-
{ text: '
|
| 67 |
-
{ text: 'Keto', class: '' },
|
| 68 |
-
{ text: 'Halal', class: '' }
|
| 69 |
];
|
| 70 |
-
} 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')) {
|
| 71 |
-
fetchIngredients(lastMessage);
|
| 72 |
-
return; // Exit early to handle ingredients fetch
|
| 73 |
}
|
| 74 |
|
| 75 |
addMessage('bot', botResponse);
|
|
@@ -92,9 +82,9 @@ function fetchIngredients(dietaryPreference) {
|
|
| 92 |
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
|
| 93 |
} else {
|
| 94 |
const ingredients = data.ingredients || [];
|
| 95 |
-
addMessage('bot', '
|
| 96 |
displayIngredientsList(ingredients);
|
| 97 |
-
|
| 98 |
}
|
| 99 |
})
|
| 100 |
.catch(error => {
|
|
@@ -109,7 +99,6 @@ function displayIngredientsList(ingredients) {
|
|
| 109 |
return;
|
| 110 |
}
|
| 111 |
|
| 112 |
-
// Create or update ingredients list
|
| 113 |
let ingredientsList = document.querySelector('.ingredients-list');
|
| 114 |
if (!ingredientsList) {
|
| 115 |
ingredientsList = document.createElement('div');
|
|
@@ -139,7 +128,6 @@ function displaySelectedIngredients() {
|
|
| 139 |
return;
|
| 140 |
}
|
| 141 |
|
| 142 |
-
// Create or update selected ingredients area
|
| 143 |
let selectedArea = document.querySelector('.selected-ingredients');
|
| 144 |
if (!selectedArea) {
|
| 145 |
selectedArea = document.createElement('div');
|
|
@@ -158,6 +146,33 @@ function displaySelectedIngredients() {
|
|
| 158 |
}
|
| 159 |
}
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
function displayOptions(options) {
|
| 162 |
const chatMessages = document.getElementById('chatMessages');
|
| 163 |
if (!chatMessages) {
|
|
@@ -173,7 +188,16 @@ function displayOptions(options) {
|
|
| 173 |
conversation.push({ role: 'user', message: opt.text });
|
| 174 |
chatMessages.innerHTML = ''; // Clear previous messages
|
| 175 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
};
|
| 178 |
chatMessages.appendChild(button);
|
| 179 |
});
|
|
|
|
| 28 |
addMessage('user', message);
|
| 29 |
conversation.push({ role: 'user', message: message });
|
| 30 |
userInput.value = '';
|
| 31 |
+
setTimeout(() => handleResponse(message), 500);
|
|
|
|
|
|
|
|
|
|
| 32 |
} else {
|
| 33 |
console.warn('Empty message!');
|
| 34 |
}
|
|
|
|
| 46 |
{ text: 'Non-Vegetarian', class: 'red' }
|
| 47 |
];
|
| 48 |
} else if (lastMessage.includes('vegetarian')) {
|
| 49 |
+
botResponse = 'Great choice! 🍽️ Fetching vegetarian ingredients...';
|
| 50 |
+
fetchIngredients('veg');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
} else if (lastMessage.includes('non-vegetarian')) {
|
| 52 |
+
botResponse = 'Great choice! 🍽️ Fetching non-vegetarian ingredients...';
|
| 53 |
+
fetchIngredients('non-vegetarian');
|
| 54 |
+
} else if (selectedIngredients.length > 0 && lastMessage === 'suggest') {
|
| 55 |
+
botResponse = 'Generating food suggestions based on your ingredients...';
|
| 56 |
+
getFoodSuggestions();
|
| 57 |
+
} else if (lastMessage === 'done') {
|
| 58 |
+
botResponse = 'Thank you! Would you like to start over or suggest recipes with your selected ingredients?';
|
| 59 |
options = [
|
| 60 |
+
{ text: 'Start Over', class: '' },
|
| 61 |
+
{ text: 'Suggest', class: '' }
|
|
|
|
|
|
|
| 62 |
];
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
addMessage('bot', botResponse);
|
|
|
|
| 82 |
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
|
| 83 |
} else {
|
| 84 |
const ingredients = data.ingredients || [];
|
| 85 |
+
addMessage('bot', 'Here are the available ingredients:');
|
| 86 |
displayIngredientsList(ingredients);
|
| 87 |
+
addMessage('bot', 'Please select ingredients (click to add) and type "done" when finished, or type "suggest" to get recipe ideas.');
|
| 88 |
}
|
| 89 |
})
|
| 90 |
.catch(error => {
|
|
|
|
| 99 |
return;
|
| 100 |
}
|
| 101 |
|
|
|
|
| 102 |
let ingredientsList = document.querySelector('.ingredients-list');
|
| 103 |
if (!ingredientsList) {
|
| 104 |
ingredientsList = document.createElement('div');
|
|
|
|
| 128 |
return;
|
| 129 |
}
|
| 130 |
|
|
|
|
| 131 |
let selectedArea = document.querySelector('.selected-ingredients');
|
| 132 |
if (!selectedArea) {
|
| 133 |
selectedArea = document.createElement('div');
|
|
|
|
| 146 |
}
|
| 147 |
}
|
| 148 |
|
| 149 |
+
function getFoodSuggestions() {
|
| 150 |
+
fetch('/get_food_suggestions', {
|
| 151 |
+
method: 'POST',
|
| 152 |
+
headers: {
|
| 153 |
+
'Content-Type': 'application/json',
|
| 154 |
+
},
|
| 155 |
+
body: JSON.stringify({ ingredients: selectedIngredients })
|
| 156 |
+
})
|
| 157 |
+
.then(response => response.json())
|
| 158 |
+
.then(data => {
|
| 159 |
+
if (data.error) {
|
| 160 |
+
addMessage('bot', `Sorry, there was an error getting suggestions: ${data.error}`);
|
| 161 |
+
} else {
|
| 162 |
+
addMessage('bot', 'Here are some food suggestions based on your ingredients:');
|
| 163 |
+
data.suggestions.forEach(suggestion => addMessage('bot', suggestion));
|
| 164 |
+
addMessage('bot', 'Would you like to start over or select more ingredients?');
|
| 165 |
+
displayOptions([
|
| 166 |
+
{ text: 'Start Over', class: '' },
|
| 167 |
+
{ text: 'Select More', class: '' }
|
| 168 |
+
]);
|
| 169 |
+
}
|
| 170 |
+
})
|
| 171 |
+
.catch(error => {
|
| 172 |
+
addMessage('bot', `Error: Unable to get food suggestions. ${error.message}`);
|
| 173 |
+
});
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
function displayOptions(options) {
|
| 177 |
const chatMessages = document.getElementById('chatMessages');
|
| 178 |
if (!chatMessages) {
|
|
|
|
| 188 |
conversation.push({ role: 'user', message: opt.text });
|
| 189 |
chatMessages.innerHTML = ''; // Clear previous messages
|
| 190 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
| 191 |
+
if (opt.text.toLowerCase() === 'suggest') {
|
| 192 |
+
getFoodSuggestions();
|
| 193 |
+
} else if (opt.text.toLowerCase() === 'start over') {
|
| 194 |
+
conversation = [{ role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' }];
|
| 195 |
+
selectedIngredients = [];
|
| 196 |
+
chatMessages.innerHTML = '';
|
| 197 |
+
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
| 198 |
+
} else {
|
| 199 |
+
setTimeout(() => handleResponse(opt.text), 500);
|
| 200 |
+
}
|
| 201 |
};
|
| 202 |
chatMessages.appendChild(button);
|
| 203 |
});
|