Spaces:
Sleeping
Sleeping
| import { pipeline } from '@xenova/transformers'; | |
| let extractor = null; | |
| export async function initEmbedder() { | |
| if ( !extractor ) { | |
| console.log( 'Loading embedding model (Xenova/all-MiniLM-L6-v2)...' ); | |
| extractor = await pipeline( | |
| 'feature-extraction', | |
| 'Xenova/all-MiniLM-L6-v2' | |
| ); | |
| console.log( 'Embedding model loaded.' ); | |
| } | |
| return extractor; | |
| } | |
| export async function embedTexts( texts ) { | |
| const ext = await initEmbedder(); | |
| const output = await ext( texts, { pooling: 'mean', normalize: true } ); | |
| return output.tolist(); | |
| } | |
| export async function embedSingle( text ) { | |
| const results = await embedTexts( [ text ] ); | |
| return results[ 0 ]; | |
| } | |