Spaces:
Sleeping
Sleeping
File size: 1,095 Bytes
908e91a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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}`);
}); |