khushininebit commited on
Commit
4948b43
Β·
verified Β·
1 Parent(s): dc4bc1a

Update serve.js

Browse files
Files changed (1) hide show
  1. serve.js +14 -44
serve.js CHANGED
@@ -1,61 +1,31 @@
1
- // server.js
2
- import { CIQClient } from '@ninebit/ciq';
3
  import express from 'express';
4
- import dotenv from 'dotenv';
5
- import path from 'path';
6
- import multer from 'multer';
7
- import fs from 'fs';
8
-
9
- // Load env vars from .env or Hugging Face secrets
10
- dotenv.config();
11
 
12
  const app = express();
13
- const port = process.env.PORT || 7860;
14
- const apiKey = '99edded2-a45d-456e-bf49-af2c9b1ccd2f'; // πŸ” For testing only
15
- const client = new CIQClient(apiKey);
16
 
17
- // Middleware
18
- app.use(express.json());
19
- app.use(express.urlencoded({ extended: true }));
20
 
21
- // File upload setup
22
- const uploadDir = 'uploads';
23
- if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir);
24
- const storage = multer.diskStorage({
25
- destination: (req, file, cb) => cb(null, uploadDir),
26
- filename: (req, file, cb) => cb(null, file.originalname)
27
- });
28
- const upload = multer({ storage });
29
 
30
- // Ingest endpoint
31
- app.post('/ingest', upload.single('file'), async (req, res) => {
32
- try {
33
- const filePath = req.file.path;
34
- const result = await client.ingestFile(filePath);
35
- res.json({ success: true, result });
36
- } catch (err) {
37
- console.error(err);
38
- res.status(500).json({ error: 'Ingestion failed' });
39
- }
40
  });
41
 
42
- // Query endpoint
43
  app.post('/query', async (req, res) => {
 
 
44
  try {
45
- const { query } = req.body;
46
  const result = await client.ragQuery(query);
47
  res.json({ result });
48
- } catch (err) {
49
- console.error(err);
50
- res.status(500).json({ error: 'Query failed' });
51
  }
52
  });
53
 
54
- // Health check
55
- app.get('/', (req, res) => {
56
- res.send('πŸš€ CIQ Chatbot backend is running!');
57
- });
58
-
59
  app.listen(port, () => {
60
- console.log(`πŸš€ CIQ Chatbot backend is running on http://localhost:${port}`);
61
  });
 
 
 
1
  import express from 'express';
2
+ import { CIQClient } from '@ninebit/ciq';
3
+ import bodyParser from 'body-parser';
 
 
 
 
 
4
 
5
  const app = express();
6
+ const port = 7860;
 
 
7
 
8
+ const apiKey = '99edded2-a45d-456e-bf49-af2c9b1ccd2f'; // or use process.env.API_KEY
9
+ const client = new CIQClient(apiKey);
 
10
 
11
+ app.use(bodyParser.json());
 
 
 
 
 
 
 
12
 
13
+ app.get('/', (req, res) => {
14
+ res.send('πŸš€ CIQ Chatbot backend is running!');
 
 
 
 
 
 
 
 
15
  });
16
 
 
17
  app.post('/query', async (req, res) => {
18
+ const { filePath, query } = req.body;
19
+
20
  try {
21
+ if (filePath) await client.ingestFile(filePath);
22
  const result = await client.ragQuery(query);
23
  res.json({ result });
24
+ } catch (error) {
25
+ res.status(500).json({ error: error.message });
 
26
  }
27
  });
28
 
 
 
 
 
 
29
  app.listen(port, () => {
30
+ console.log(`Server running on http://localhost:${port}`);
31
  });