Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +30 -10
static/script.js
CHANGED
@@ -66,16 +66,9 @@ function handleResponse(userInput) {
|
|
66 |
{ text: 'Keto', class: '' },
|
67 |
{ text: 'Halal', class: '' }
|
68 |
];
|
69 |
-
} else if (lastMessage.includes('low carb') || lastMessage.includes('dairy-free') || lastMessage.includes('keto') || lastMessage.includes('halal') || lastMessage.includes('gluten-free') || lastMessage.includes('vegan')) {
|
70 |
-
|
71 |
-
|
72 |
-
{ text: 'Potatoes', class: '' },
|
73 |
-
{ text: 'Tomatoes', class: '' },
|
74 |
-
{ text: 'Spinach', class: '' },
|
75 |
-
{ text: 'Cauliflower', class: '' },
|
76 |
-
{ text: 'Paneer', class: '' },
|
77 |
-
{ text: 'Tofu', class: '' }
|
78 |
-
];
|
79 |
}
|
80 |
|
81 |
addMessage('bot', botResponse);
|
@@ -84,6 +77,33 @@ function handleResponse(userInput) {
|
|
84 |
}
|
85 |
}
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
function displayOptions(options) {
|
88 |
const chatMessages = document.getElementById('chatMessages');
|
89 |
if (!chatMessages) {
|
|
|
66 |
{ text: 'Keto', class: '' },
|
67 |
{ text: 'Halal', class: '' }
|
68 |
];
|
69 |
+
} 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')) {
|
70 |
+
fetchIngredients(lastMessage);
|
71 |
+
return; // Exit early to handle ingredients fetch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
}
|
73 |
|
74 |
addMessage('bot', botResponse);
|
|
|
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: dietaryPreference })
|
87 |
+
})
|
88 |
+
.then(response => response.json())
|
89 |
+
.then(data => {
|
90 |
+
if (data.error) {
|
91 |
+
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
|
92 |
+
} else {
|
93 |
+
const ingredients = data.ingredients || [];
|
94 |
+
addMessage('bot', 'Great choice! These are available ingredients:');
|
95 |
+
if (ingredients.length > 0) {
|
96 |
+
ingredients.forEach(ingredient => addMessage('bot', `- ${ingredient}`));
|
97 |
+
} else {
|
98 |
+
addMessage('bot', '- No ingredients available.');
|
99 |
+
}
|
100 |
+
}
|
101 |
+
})
|
102 |
+
.catch(error => {
|
103 |
+
addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
|
104 |
+
});
|
105 |
+
}
|
106 |
+
|
107 |
function displayOptions(options) {
|
108 |
const chatMessages = document.getElementById('chatMessages');
|
109 |
if (!chatMessages) {
|