Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/components/ChatTile.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<template>
<div :class="chatIsOpen ? 'chat-wrapper open' : 'chat-wrapper'">
<div>
<button class="chat" @click="handleChatClick">
<template v-if="chatIsOpen">
<button class="chat" @click="handleChatClick" :disabled="!model">
Copy link
Contributor Author

@lazeratops lazeratops May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is where the main logic is to account for the potential timing issue of model load, basically disabling chat until the model exists. Let me know if that makes sense!

<template v-if="!model">
<img class="icon" :src="close" alt="" />
</template>
<template v-else-if="chatIsOpen">
<img class="icon" :src="close" alt="" />
</template>
<template v-else>
Expand All @@ -23,7 +26,8 @@
</div>

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

<form @submit="submitForm">
Expand All @@ -47,7 +51,7 @@
</template>

<script>
import { load } from '@tensorflow-models/toxicity';
import { load } from "@tensorflow-models/toxicity";

import close from "../assets/x.svg";
import chat from "../assets/chat.svg";
Expand All @@ -69,9 +73,13 @@ export default {
},
beforeCreate() {
const threshold = 0.9;
load(threshold).then(model => {
this.model = Object.freeze(model);
});
load(threshold)
.then((model) => {
this.model = Object.freeze(model);
})
.catch((e) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just adding some error logging in case something goes wrong, for easier debugging should that happen on the user's end!

console.error("failed to load toxicity model", e);
});
},
methods: {
// Toggle chat's view (open/closed)
Expand All @@ -84,12 +92,16 @@ export default {
this.displayWarning = false;

const hasToxicContent = await this.analyzeInputText(this.text);
hasToxicContent ? this.displayWarning = true : this.sendMessage(this.text);
hasToxicContent
? (this.displayWarning = true)
: this.sendMessage(this.text);
this.text = "";
},
async analyzeInputText(text) {
const predictions = await this.model.classify(text);
const containsToxicContent = predictions.some(prediction => prediction.results[0].match);
const containsToxicContent = predictions.some(
(prediction) => prediction.results[0].match
);
return containsToxicContent;
},
},
Expand Down