Spaces:
Sleeping
Sleeping
Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from "express";
|
| 2 |
+
import { loadModel } from "./model.js";
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
app.use(express.json());
|
| 6 |
+
|
| 7 |
+
const PORT = 7860;
|
| 8 |
+
|
| 9 |
+
let session;
|
| 10 |
+
|
| 11 |
+
(async () => {
|
| 12 |
+
session = await loadModel();
|
| 13 |
+
})();
|
| 14 |
+
|
| 15 |
+
/* -------------------------
|
| 16 |
+
TEST ENDPOINT
|
| 17 |
+
------------------------- */
|
| 18 |
+
|
| 19 |
+
app.get("/test", (req, res) => {
|
| 20 |
+
res.json({
|
| 21 |
+
status: "ok",
|
| 22 |
+
message: "API is running"
|
| 23 |
+
});
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
/* -------------------------
|
| 27 |
+
GENERATE ENDPOINT
|
| 28 |
+
------------------------- */
|
| 29 |
+
|
| 30 |
+
app.post("/generate", async (req, res) => {
|
| 31 |
+
|
| 32 |
+
try {
|
| 33 |
+
|
| 34 |
+
const {
|
| 35 |
+
user_input,
|
| 36 |
+
user_temperature,
|
| 37 |
+
user_inst,
|
| 38 |
+
user_max_tokens
|
| 39 |
+
} = req.body;
|
| 40 |
+
|
| 41 |
+
if (!user_input) {
|
| 42 |
+
return res.status(400).json({
|
| 43 |
+
error: "user_input required"
|
| 44 |
+
});
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
const prompt = `
|
| 48 |
+
Instruction:
|
| 49 |
+
${user_inst}
|
| 50 |
+
|
| 51 |
+
User:
|
| 52 |
+
${user_input}
|
| 53 |
+
`;
|
| 54 |
+
|
| 55 |
+
const output = await session.prompt(prompt, {
|
| 56 |
+
|
| 57 |
+
temperature: user_temperature ?? 0.2,
|
| 58 |
+
maxTokens: user_max_tokens ?? 256,
|
| 59 |
+
|
| 60 |
+
topK: 40,
|
| 61 |
+
topP: 0.9,
|
| 62 |
+
repeatPenalty: 1.1
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
res.json({
|
| 66 |
+
success: true,
|
| 67 |
+
response: output
|
| 68 |
+
});
|
| 69 |
+
|
| 70 |
+
} catch (err) {
|
| 71 |
+
|
| 72 |
+
console.error(err);
|
| 73 |
+
|
| 74 |
+
res.status(500).json({
|
| 75 |
+
error: "generation failed"
|
| 76 |
+
});
|
| 77 |
+
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
});
|
| 81 |
+
|
| 82 |
+
app.listen(PORT, () => {
|
| 83 |
+
console.log(`Server running on ${PORT}`);
|
| 84 |
+
});
|