-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from lobehub/feat/transformer
fix: 修复滚动问题 & 舞蹈动作播放问题
- Loading branch information
Showing
9 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use client'; | ||
|
||
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
|
||
export default function Home() { | ||
// Keep track of the classification result and the model loading status. | ||
const [result, setResult] = useState(null); | ||
const [ready, setReady] = useState(null); | ||
|
||
// Create a reference to the worker object. | ||
const worker = useRef(null); | ||
|
||
// We use the `useEffect` hook to set up the worker as soon as the `App` component is mounted. | ||
useEffect(() => { | ||
if (!worker.current) { | ||
// Create the worker if it does not yet exist. | ||
worker.current = new Worker(new URL('../../workers/classification.ts', import.meta.url), { | ||
type: 'module', | ||
}); | ||
} | ||
|
||
// Create a callback function for messages from the worker thread. | ||
const onMessageReceived = (e) => { | ||
switch (e.data.status) { | ||
case 'initiate': { | ||
setReady(false); | ||
break; | ||
} | ||
case 'ready': { | ||
setReady(true); | ||
break; | ||
} | ||
case 'complete': { | ||
setResult(e.data.output[0]); | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
// Attach the callback function as an event listener. | ||
worker.current.addEventListener('message', onMessageReceived); | ||
|
||
// Define a cleanup function for when the component is unmounted. | ||
return () => worker.current.removeEventListener('message', onMessageReceived); | ||
}); | ||
|
||
const classify = useCallback((text) => { | ||
if (worker.current) { | ||
worker.current.postMessage({ text }); | ||
} | ||
}, []); | ||
return ( | ||
<main className="flex min-h-screen flex-col items-center justify-center p-12"> | ||
<h1 className="text-5xl font-bold mb-2 text-center">Transformers.js</h1> | ||
<h2 className="text-2xl mb-4 text-center">Next.js template (client-side)</h2> | ||
<input | ||
type="text" | ||
className="w-full max-w-xs p-2 border border-gray-300 rounded mb-4" | ||
placeholder="Enter text here" | ||
onChange={(e) => { | ||
classify(e.target.value); | ||
}} | ||
/> | ||
|
||
{ready !== null && ( | ||
<pre className="bg-gray-100 p-2 rounded"> | ||
{!ready || !result ? 'Loading...' : JSON.stringify(result, null, 2)} | ||
</pre> | ||
)} | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { pipeline } from '@xenova/transformers'; | ||
|
||
// Use the Singleton pattern to enable lazy construction of the pipeline. | ||
const PipelineSingleton = { | ||
task: 'text-classification', | ||
model: 'Xenova/distilbert-base-uncased-finetuned-sst-2-english', | ||
instance: null, | ||
|
||
async getInstance(progress_callback = null) { | ||
this.instance ??= await pipeline(this.task, this.model, { progress_callback }); | ||
return this.instance; | ||
}, | ||
}; | ||
|
||
// Listen for messages from the main thread | ||
self.addEventListener('message', async (event) => { | ||
// Retrieve the classification pipeline. When called for the first time, | ||
// this will load the pipeline and save it for future use. | ||
const classifier = await PipelineSingleton.getInstance((x) => { | ||
// We also add a progress callback to the pipeline so that we can | ||
// track model loading. | ||
self.postMessage(x); | ||
}); | ||
|
||
// Actually perform the classification | ||
const output = await classifier(event.data.text); | ||
|
||
// Send the output back to the main thread | ||
self.postMessage({ | ||
status: 'complete', | ||
output: output, | ||
}); | ||
}); |