format and clean text classification worker and component
Browse files
public/workers/text-classification.js
CHANGED
|
@@ -6,11 +6,11 @@ class MyTextClassificationPipeline {
|
|
| 6 |
static instance = null
|
| 7 |
|
| 8 |
static async getInstance(model, dtype = 'fp32', progress_callback = null) {
|
| 9 |
-
this.instance = pipeline(
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
)
|
| 14 |
return this.instance
|
| 15 |
}
|
| 16 |
}
|
|
|
|
| 6 |
static instance = null
|
| 7 |
|
| 8 |
static async getInstance(model, dtype = 'fp32', progress_callback = null) {
|
| 9 |
+
this.instance = pipeline(this.task, model, {
|
| 10 |
+
dtype,
|
| 11 |
+
device: 'webgpu',
|
| 12 |
+
progress_callback
|
| 13 |
+
})
|
| 14 |
return this.instance
|
| 15 |
}
|
| 16 |
}
|
src/components/TextClassification.tsx
CHANGED
|
@@ -1,12 +1,9 @@
|
|
| 1 |
-
import { useState,
|
| 2 |
import {
|
| 3 |
-
ClassificationOutput,
|
| 4 |
TextClassificationWorkerInput,
|
| 5 |
-
WorkerMessage
|
| 6 |
} from '../types'
|
| 7 |
import { useModel } from '../contexts/ModelContext'
|
| 8 |
-
import {
|
| 9 |
-
|
| 10 |
const PLACEHOLDER_TEXTS: string[] = [
|
| 11 |
'I absolutely love this product! It exceeded all my expectations.',
|
| 12 |
"This is the worst purchase I've ever made. Complete waste of money.",
|
|
@@ -22,7 +19,7 @@ const PLACEHOLDER_TEXTS: string[] = [
|
|
| 22 |
|
| 23 |
function TextClassification() {
|
| 24 |
const [text, setText] = useState<string>(PLACEHOLDER_TEXTS.join('\n'))
|
| 25 |
-
const { activeWorker, status,
|
| 26 |
|
| 27 |
const classify = useCallback(() => {
|
| 28 |
if (!modelInfo || !activeWorker) {
|
|
@@ -36,7 +33,7 @@ function TextClassification() {
|
|
| 36 |
model: modelInfo.id
|
| 37 |
}
|
| 38 |
activeWorker.postMessage(message)
|
| 39 |
-
}, [text, modelInfo,
|
| 40 |
|
| 41 |
const busy: boolean = status !== 'ready'
|
| 42 |
|
|
|
|
| 1 |
+
import { useState, useCallback } from 'react'
|
| 2 |
import {
|
|
|
|
| 3 |
TextClassificationWorkerInput,
|
|
|
|
| 4 |
} from '../types'
|
| 5 |
import { useModel } from '../contexts/ModelContext'
|
| 6 |
+
import { set } from 'lodash'
|
|
|
|
| 7 |
const PLACEHOLDER_TEXTS: string[] = [
|
| 8 |
'I absolutely love this product! It exceeded all my expectations.',
|
| 9 |
"This is the worst purchase I've ever made. Complete waste of money.",
|
|
|
|
| 19 |
|
| 20 |
function TextClassification() {
|
| 21 |
const [text, setText] = useState<string>(PLACEHOLDER_TEXTS.join('\n'))
|
| 22 |
+
const { activeWorker, status, modelInfo, results, setResults, hasBeenLoaded} = useModel()
|
| 23 |
|
| 24 |
const classify = useCallback(() => {
|
| 25 |
if (!modelInfo || !activeWorker) {
|
|
|
|
| 33 |
model: modelInfo.id
|
| 34 |
}
|
| 35 |
activeWorker.postMessage(message)
|
| 36 |
+
}, [text, modelInfo, activeWorker, set])
|
| 37 |
|
| 38 |
const busy: boolean = status !== 'ready'
|
| 39 |
|