-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (50 loc) · 1.6 KB
/
app.js
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
53
54
55
56
57
58
59
60
61
62
// Load environment variables from .env file
import "dotenv/config";
import express from "express";
import path from "node:path";
import fileUpload from "express-fileupload";
import { env } from "@xenova/transformers";
import { T5Pipeline, translateRouter } from "./routes/translate.js";
import { ImageClassificationPipeline, imageClassifyRouter } from "./routes/image-classify.js";
import { quoteRouter } from "./routes/quote.js";
const __dirName = path.dirname(new URL(import.meta.url).pathname);
// Configure Transformers.js
// Specify a custom location for models
env.localModelPath = "./models";
// Disable the loading of remote models from the Hugging Face Hub
env.allowRemoteModels = false;
const app = express();
// Setup view engine
app.set("views", path.join(__dirName, "views"));
app.set("view engine", "ejs");
// Serve static files in public folder
app.use(express.static(path.join(__dirName, "public")));
// Parse form data
app.use(express.urlencoded());
// Add file upload middleware
app.use(
fileUpload({
useTempFiles: true,
tempFileDir: path.join(__dirName, ".uploads-temp"),
})
);
// Register routes
app.get("/", (req, res) => {
res.render("index");
});
app.use(translateRouter);
app.use(imageClassifyRouter);
app.use(quoteRouter);
async function startServer() {
try {
console.log("Loading models...");
// Preload models
await Promise.all([T5Pipeline.getInstance(), ImageClassificationPipeline.getInstance()]);
} catch (err) {
console.error("Failed to load pipelines", err);
}
app.listen(3000, () => {
console.log("Server started on port 3000");
});
}
startServer();