Spaces:
Sleeping
Sleeping
| import express from "express"; | |
| import { pipeline, env } from "@xenova/transformers"; | |
| env.cacheDir = "/tmp/transformers-cache"; | |
| const app = express(); | |
| app.use(express.json()); | |
| let extractor = null; | |
| async function getExtractor() { | |
| if (!extractor) { | |
| console.log("Loading embedding model..."); | |
| extractor = await pipeline( | |
| "feature-extraction", | |
| "Xenova/all-MiniLM-L6-v2" | |
| ); | |
| console.log("Model loaded successfully"); | |
| } | |
| return extractor; | |
| } | |
| app.post("/embed", async (req, res) => { | |
| try { | |
| const { text } = req.body; | |
| if (!text) { | |
| return res.status(400).json({ error: "Text is required" }); | |
| } | |
| const extractor = await getExtractor(); | |
| const result = await extractor(text, { | |
| pooling: "mean", | |
| normalize: true, | |
| }); | |
| return res.json({ | |
| embedding: Array.from(result.data), | |
| }); | |
| } catch (error) { | |
| console.error("Embedding error:", error); | |
| return res.status(500).json({ error: "Embedding failed" }); | |
| } | |
| }); | |
| const PORT = 7860; | |
| app.listen(PORT, () => { | |
| console.log(`Server running on port ${PORT}`); | |
| }); |