| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  | <template> | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |   <v-container class="pa-0"> | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |     <v-container> | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |       <BaseCardSectionTitle :title="$t('admin.ingredients-natural-language-processor')"> | 
					
						
							|  |  |  |         {{ $t('admin.ingredients-natural-language-processor-explanation') }} | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         <p class="pt-3"> | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |           {{ $t('admin.ingredients-natural-language-processor-explanation-2') }} | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |         </p> | 
					
						
							|  |  |  |       </BaseCardSectionTitle> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |       <div class="d-flex align-center justify-center justify-md-start flex-wrap"> | 
					
						
							|  |  |  |         <v-btn-toggle v-model="parser" dense mandatory @change="processIngredient"> | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |           <v-btn value="nlp"> {{ $t('admin.nlp') }} </v-btn> | 
					
						
							|  |  |  |           <v-btn value="brute"> {{ $t('admin.brute') }} </v-btn> | 
					
						
							| 
									
										
										
										
											2024-05-22 04:45:07 -05:00
										 |  |  |           <v-btn value="openai"> {{ $t('admin.openai') }} </v-btn> | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |         </v-btn-toggle> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |         <v-checkbox v-model="showConfidence" class="ml-5" :label="$t('admin.show-individual-confidence')"></v-checkbox> | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |       </div> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |       <v-card flat> | 
					
						
							|  |  |  |         <v-card-text> | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |           <v-text-field v-model="ingredient" :label="$t('admin.ingredient-text')"> </v-text-field> | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |         </v-card-text> | 
					
						
							|  |  |  |         <v-card-actions> | 
					
						
							|  |  |  |           <BaseButton class="ml-auto" @click="processIngredient"> | 
					
						
							|  |  |  |             <template #icon> {{ $globals.icons.check }}</template> | 
					
						
							|  |  |  |             {{ $t("general.submit") }} | 
					
						
							|  |  |  |           </BaseButton> | 
					
						
							|  |  |  |         </v-card-actions> | 
					
						
							|  |  |  |       </v-card> | 
					
						
							|  |  |  |     </v-container> | 
					
						
							|  |  |  |     <v-container v-if="results"> | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |       <div v-if="parser !== 'brute' && getConfidence('average')" class="d-flex"> | 
					
						
							|  |  |  |         <v-chip dark :color="getColor('average')" class="mx-auto mb-2"> | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |           {{ $t('admin.average-confident', [getConfidence("average")]) }} | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |         </v-chip> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |       <div class="d-flex justify-center flex-wrap" style="gap: 1.5rem"> | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |         <template v-for="(prop, index) in properties"> | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |           <div v-if="prop.value" :key="index" class="flex-grow-1"> | 
					
						
							|  |  |  |             <v-card min-width="200px"> | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |               <v-card-title> {{ prop.value }} </v-card-title> | 
					
						
							|  |  |  |               <v-card-text> | 
					
						
							|  |  |  |                 {{ prop.subtitle }} | 
					
						
							|  |  |  |               </v-card-text> | 
					
						
							|  |  |  |             </v-card> | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |             <v-chip v-if="prop.confidence && showConfidence" dark :color="prop.color" class="mt-2"> | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |               {{ $t('admin.average-confident', [prop.confidence]) }} | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |             </v-chip> | 
					
						
							|  |  |  |           </div> | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |         </template> | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |     </v-container> | 
					
						
							|  |  |  |     <v-container class="narrow-container"> | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |       <v-card-title> {{ $t('admin.try-an-example') }} </v-card-title> | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |       <v-card v-for="(text, idx) in tryText" :key="idx" class="my-2" hover @click="processTryText(text)"> | 
					
						
							|  |  |  |         <v-card-text> {{ text }} </v-card-text> | 
					
						
							|  |  |  |       </v-card> | 
					
						
							|  |  |  |     </v-container> | 
					
						
							|  |  |  |   </v-container> | 
					
						
							|  |  |  | </template> | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  | import { defineComponent, reactive, ref, toRefs, useContext } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2024-06-26 05:33:11 -05:00
										 |  |  | import { alert } from "~/composables/use-toast"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2024-05-22 04:45:07 -05:00
										 |  |  | import { IngredientConfidence } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | import { Parser } from "~/lib/api/user/recipes/recipe"; | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  | type ConfidenceAttribute = "average" | "comment" | "name" | "unit" | "quantity" | "food"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  | export default defineComponent({ | 
					
						
							|  |  |  |   layout: "admin", | 
					
						
							|  |  |  |   setup() { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |     const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const state = reactive({ | 
					
						
							|  |  |  |       loading: false, | 
					
						
							|  |  |  |       ingredient: "", | 
					
						
							|  |  |  |       results: false, | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |       parser: "nlp" as Parser, | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |     const { i18n } = useContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  |     const confidence = ref<IngredientConfidence>({}); | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |     function getColor(attribute: ConfidenceAttribute) { | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |       const percentage = getConfidence(attribute); | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  |       if (percentage === undefined) return; | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |       const p_as_num = parseFloat(percentage.replace("%", "")); | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Set color based off range
 | 
					
						
							|  |  |  |       if (p_as_num > 75) { | 
					
						
							|  |  |  |         return "success"; | 
					
						
							|  |  |  |       } else if (p_as_num > 60) { | 
					
						
							|  |  |  |         return "warning"; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         return "error"; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |     function getConfidence(attribute: ConfidenceAttribute) { | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |       if (!confidence.value) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |       const property = confidence.value[attribute]; | 
					
						
							|  |  |  |       if (property !== undefined) { | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |         return `${(property * 100).toFixed(0)}%`; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |       return undefined; | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |     const tryText = [ | 
					
						
							|  |  |  |       "2 tbsp minced cilantro, leaves and stems", | 
					
						
							|  |  |  |       "1 large yellow onion, coarsely chopped", | 
					
						
							|  |  |  |       "1 1/2 tsp garam masala", | 
					
						
							|  |  |  |       "1 inch piece fresh ginger, (peeled and minced)", | 
					
						
							|  |  |  |       "2 cups mango chunks, (2 large mangoes) (fresh or frozen)", | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function processTryText(str: string) { | 
					
						
							|  |  |  |       state.ingredient = str; | 
					
						
							|  |  |  |       processIngredient(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function processIngredient() { | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |       if (state.ingredient === "") { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |       state.loading = true; | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       const { data } = await api.recipes.parseIngredient(state.parser, state.ingredient); | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (data) { | 
					
						
							|  |  |  |         state.results = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  |         if (data.confidence) confidence.value = data.confidence; | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |         // TODO: Remove ts-ignore
 | 
					
						
							|  |  |  |         // ts-ignore because data will likely change significantly once I figure out how to return results
 | 
					
						
							|  |  |  |         // for the parser. For now we'll leave it like this
 | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |         properties.comment.value = data.ingredient.note || ""; | 
					
						
							|  |  |  |         properties.quantity.value = data.ingredient.quantity || ""; | 
					
						
							| 
									
										
										
										
											2021-11-06 15:03:12 -08:00
										 |  |  |         properties.unit.value = data.ingredient?.unit?.name || ""; | 
					
						
							|  |  |  |         properties.food.value = data.ingredient?.food?.name || ""; | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  |         (["comment", "quantity", "unit", "food"] as ConfidenceAttribute[]).forEach((property) => { | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |           const color = getColor(property); | 
					
						
							|  |  |  |           const confidence = getConfidence(property); | 
					
						
							|  |  |  |           if (color) { | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |             // @ts-ignore See above
 | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |             properties[property].color = color; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           if (confidence) { | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |             // @ts-ignore See above
 | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |             properties[property].confidence = confidence; | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2024-06-26 05:33:11 -05:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         alert.error(i18n.t("events.something-went-wrong") as string); | 
					
						
							|  |  |  |         state.results = false; | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |       state.loading = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const properties = reactive({ | 
					
						
							|  |  |  |       quantity: { | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |         subtitle: i18n.t("recipe.quantity"), | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |         value: "" as string | number, | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |         color: null, | 
					
						
							|  |  |  |         confidence: null, | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |       }, | 
					
						
							|  |  |  |       unit: { | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |         subtitle: i18n.t("recipe.unit"), | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |         value: "", | 
					
						
							|  |  |  |         color: null, | 
					
						
							|  |  |  |         confidence: null, | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |       }, | 
					
						
							|  |  |  |       food: { | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |         subtitle: i18n.t("shopping-list.food"), | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |         value: "", | 
					
						
							|  |  |  |         color: null, | 
					
						
							|  |  |  |         confidence: null, | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |       comment: { | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |         subtitle: i18n.t("recipe.comment"), | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |         value: "", | 
					
						
							|  |  |  |         color: null, | 
					
						
							|  |  |  |         confidence: null, | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |       }, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |     const showConfidence = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |       showConfidence, | 
					
						
							|  |  |  |       getColor, | 
					
						
							|  |  |  |       confidence, | 
					
						
							|  |  |  |       getConfidence, | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |       ...toRefs(state), | 
					
						
							|  |  |  |       tryText, | 
					
						
							|  |  |  |       properties, | 
					
						
							|  |  |  |       processTryText, | 
					
						
							|  |  |  |       processIngredient, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   head() { | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |       title: this.$t("admin.parser"), | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | <style scoped></style> |