Skip to content

Commit 9dc764e

Browse files
authored
Account for possible timing issues with model load (#18)
1 parent e3f66d5 commit 9dc764e

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/components/ChatTile.vue

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<template>
22
<div :class="chatIsOpen ? 'chat-wrapper open' : 'chat-wrapper'">
33
<div>
4-
<button class="chat" @click="handleChatClick">
5-
<template v-if="chatIsOpen">
4+
<button class="chat" @click="handleChatClick" :disabled="!model">
5+
<template v-if="!model">
6+
<img class="icon" :src="close" alt="" />
7+
</template>
8+
<template v-else-if="chatIsOpen">
69
<img class="icon" :src="close" alt="" />
710
</template>
811
<template v-else>
@@ -23,7 +26,8 @@
2326
</div>
2427

2528
<p v-if="displayWarning" class="warning-message">
26-
Oops! The message you're trying to send was flagged as potentially offensive. If you think this was flagged in error, please contact us!
29+
Oops! The message you're trying to send was flagged as potentially
30+
offensive. If you think this was flagged in error, please contact us!
2731
</p>
2832

2933
<form @submit="submitForm">
@@ -47,7 +51,7 @@
4751
</template>
4852

4953
<script>
50-
import { load } from '@tensorflow-models/toxicity';
54+
import { load } from "@tensorflow-models/toxicity";
5155
5256
import close from "../assets/x.svg";
5357
import chat from "../assets/chat.svg";
@@ -69,9 +73,13 @@ export default {
6973
},
7074
beforeCreate() {
7175
const threshold = 0.9;
72-
load(threshold).then(model => {
73-
this.model = Object.freeze(model);
74-
});
76+
load(threshold)
77+
.then((model) => {
78+
this.model = Object.freeze(model);
79+
})
80+
.catch((e) => {
81+
console.error("failed to load toxicity model", e);
82+
});
7583
},
7684
methods: {
7785
// Toggle chat's view (open/closed)
@@ -84,12 +92,16 @@ export default {
8492
this.displayWarning = false;
8593
8694
const hasToxicContent = await this.analyzeInputText(this.text);
87-
hasToxicContent ? this.displayWarning = true : this.sendMessage(this.text);
95+
hasToxicContent
96+
? (this.displayWarning = true)
97+
: this.sendMessage(this.text);
8898
this.text = "";
8999
},
90100
async analyzeInputText(text) {
91101
const predictions = await this.model.classify(text);
92-
const containsToxicContent = predictions.some(prediction => prediction.results[0].match);
102+
const containsToxicContent = predictions.some(
103+
(prediction) => prediction.results[0].match
104+
);
93105
return containsToxicContent;
94106
},
95107
},

0 commit comments

Comments
 (0)