tjwrld commited on
Commit
141facc
·
verified ·
1 Parent(s): 2b9b303

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +15 -7
server.js CHANGED
@@ -51,27 +51,31 @@ async function initModel() {
51
  ----------------------- */
52
 
53
  app.post("/generate", async (req, res) => {
54
- // 1. Check if the model is still loading in the background
55
  if (!isModelReady) {
56
  return res.status(503).json({
57
  error: "The AI model is still loading into memory. Please wait a few seconds and try again."
58
  });
59
  }
60
 
 
 
61
  try {
62
  const {
63
  user_input,
64
- user_temp = 0.7,
65
- user_inst = "You are an AI assistant. Give short clear answers.",
66
- user_max_token = 512
67
  } = req.body;
68
 
69
  if (!user_input) {
70
  return res.status(400).json({ error: "Missing required field: user_input" });
71
  }
72
 
 
 
 
73
  const session = new LlamaChatSession({
74
- contextSequence: contextInstance.getSequence(),
75
  systemPrompt: user_inst
76
  });
77
 
@@ -88,6 +92,11 @@ app.post("/generate", async (req, res) => {
88
  } catch (err) {
89
  console.error("Error during generation:", err);
90
  res.status(500).json({ error: "An internal error occurred during text generation." });
 
 
 
 
 
91
  }
92
  });
93
 
@@ -95,12 +104,11 @@ app.post("/generate", async (req, res) => {
95
  STARTUP SEQUENCE
96
  ----------------------- */
97
 
98
- // 1. START THE SERVER IMMEDIATELY (This satisfies Hugging Face's health check)
99
  app.listen(PORT, "0.0.0.0", () => {
100
  console.log(`✅ Web server is listening on port ${PORT}`);
101
  console.log(`⏳ Starting background model load...`);
102
 
103
- // 2. LOAD THE MODEL IN THE BACKGROUND
104
  initModel().catch(err => {
105
  console.error("Critical Failure: Failed to load the AI model.", err);
106
  });
 
51
  ----------------------- */
52
 
53
  app.post("/generate", async (req, res) => {
 
54
  if (!isModelReady) {
55
  return res.status(503).json({
56
  error: "The AI model is still loading into memory. Please wait a few seconds and try again."
57
  });
58
  }
59
 
60
+ let sequence; // Define this here so we can clean it up in the 'finally' block
61
+
62
  try {
63
  const {
64
  user_input,
65
+ user_temp = 0.2,
66
+ user_inst = "You are an Wrld-Gpt AI assistant. Give short clear answers. Do not make assumptions",
67
+ user_max_token = 1024
68
  } = req.body;
69
 
70
  if (!user_input) {
71
  return res.status(400).json({ error: "Missing required field: user_input" });
72
  }
73
 
74
+ // Grab a sequence memory slot for this specific request
75
+ sequence = contextInstance.getSequence();
76
+
77
  const session = new LlamaChatSession({
78
+ contextSequence: sequence,
79
  systemPrompt: user_inst
80
  });
81
 
 
92
  } catch (err) {
93
  console.error("Error during generation:", err);
94
  res.status(500).json({ error: "An internal error occurred during text generation." });
95
+ } finally {
96
+ // CRITICAL FIX: Always free up the sequence slot when done, even if an error occurs!
97
+ if (sequence) {
98
+ sequence.dispose();
99
+ }
100
  }
101
  });
102
 
 
104
  STARTUP SEQUENCE
105
  ----------------------- */
106
 
 
107
  app.listen(PORT, "0.0.0.0", () => {
108
  console.log(`✅ Web server is listening on port ${PORT}`);
109
  console.log(`⏳ Starting background model load...`);
110
 
111
+ //LOAD THE MODEL IN THE BACKGROUND
112
  initModel().catch(err => {
113
  console.error("Critical Failure: Failed to load the AI model.", err);
114
  });