-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.ts
74 lines (62 loc) · 2.16 KB
/
delete.ts
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
63
64
65
66
67
68
69
70
71
72
import express from 'express';
import fs from 'fs';
import path from 'path';
import cors from 'cors';
import { parse } from 'csv-parse/sync';
import axios from 'axios';
import { fetchGPTResponse } from './src/llm';
import { extractAndParseJSON } from './src/utils/parse_json'
const app = express();
app.use(cors()); // Enable CORS for all routes
const PORT = 3000;
const datasetsDir = path.join(__dirname, '../datasets');
app.use(express.static('./main.ts'));
app.use(express.json()); // Parse JSON bodies
app.get('/datasets/:datasetName/fields', (req, res) => {
const datasetName = req.params.datasetName;
const datasetPath = path.join(datasetsDir, datasetName);
fs.readFile(datasetPath, 'utf8', (err, data) => {
if (err) {
res.status(500).send('Error reading dataset');
return;
}
try {
let fields = [];
// Check the file extension to determine the format
if (datasetPath.endsWith('.json')) {
const jsonData = JSON.parse(data);
fields = jsonData.length > 0 ? Object.keys(jsonData[0]) : [];
} else if (datasetPath.endsWith('.csv')) {
const records = parse(data, { to_line: 1 });
fields = records[0];
console.log(records[0])
}
res.json(fields); // Send the field names
} catch (parseError) {
res.status(500).send('Error parsing data');
}
});
});
app.get('/datasets', (req, res) => {
fs.readdir(datasetsDir, (err, files) => {
if (err) {
res.status(500).send('Error reading directory');
return;
}
res.json(files);
});
});
app.post('/bin', express.json(), async (req, res) => {
const { dataset, field } = req.body;
try {
const field_bins = await fetchGPTResponse(dataset, field);
const response = extractAndParseJSON(field_bins)
res.json({ response });
} catch (error) {
console.error(error);
res.status(500).send('Error processing GPT request');
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});