API-Handler commited on
Commit
b3b2ca7
·
verified ·
1 Parent(s): 184406c

Update src/server.js

Browse files
Files changed (1) hide show
  1. src/server.js +147 -147
src/server.js CHANGED
@@ -1,148 +1,148 @@
1
- // ==========================================
2
- // SERVER WITH ENDPOINTS
3
- // ==========================================
4
-
5
- require('dotenv').config();
6
- const express = require('express');
7
- const app = express();
8
- const port = process.env.PORT || 3000;
9
- const apiHandler = require('./apiHandler');
10
- const bodyParser = require('body-parser');
11
- const fs = require('fs');
12
-
13
- // Middleware to parse JSON bodies
14
- app.use(bodyParser.json());
15
-
16
- // Middleware to parse multipart/form-data for file uploads
17
- const multer = require('multer');
18
- const upload = multer({ dest: 'uploads/' });
19
-
20
- // ------------------------------------------
21
- // Chat Completions Endpoint
22
- // ------------------------------------------
23
-
24
- app.post('/v1/chat/completions', (req, res) => {
25
- if (req.body.stream) {
26
- // For streaming, the apiHandler manages the response and error handling
27
- apiHandler.createChatCompletion(req.body, res);
28
- } else {
29
- // Non-streaming
30
- apiHandler
31
- .createChatCompletion(req.body)
32
- .then((data) => {
33
- res.json(data);
34
- })
35
- .catch((error) => {
36
- res.status(500).json(error);
37
- });
38
- }
39
- });
40
-
41
- // ------------------------------------------
42
- // Image Generation Endpoint
43
- // ------------------------------------------
44
-
45
- app.post('/v1/images/generations', async (req, res) => {
46
- try {
47
- const data = await apiHandler.createImage(req.body);
48
- res.json(data);
49
- } catch (error) {
50
- res.status(500).json(error);
51
- }
52
- });
53
-
54
- // ------------------------------------------
55
- // Image Editing Endpoint
56
- // ------------------------------------------
57
-
58
- app.post('/v1/images/edits', upload.fields([{ name: 'image' }, { name: 'mask' }]), async (req, res) => {
59
- try {
60
- // Add file paths to request body
61
- req.body.image = req.files['image'][0].path;
62
- req.body.mask = req.files['mask'][0].path;
63
-
64
- const data = await apiHandler.createImageEdit(req.body);
65
- res.json(data);
66
-
67
- // Clean up uploaded files
68
- fs.unlinkSync(req.body.image);
69
- fs.unlinkSync(req.body.mask);
70
- } catch (error) {
71
- res.status(500).json(error);
72
- }
73
- });
74
-
75
- // ------------------------------------------
76
- // Image Variations Endpoint
77
- // ------------------------------------------
78
-
79
- app.post('/v1/images/variations', upload.single('image'), async (req, res) => {
80
- try {
81
- // Add file path to request body
82
- req.body.image = req.file.path;
83
-
84
- const data = await apiHandler.createImageVariation(req.body);
85
- res.json(data);
86
-
87
- // Clean up uploaded file
88
- fs.unlinkSync(req.body.image);
89
- } catch (error) {
90
- res.status(500).json(error);
91
- }
92
- });
93
-
94
- // ------------------------------------------
95
- // Audio Transcription Endpoint
96
- // ------------------------------------------
97
-
98
- app.post('/v1/audio/transcriptions', upload.single('file'), async (req, res) => {
99
- try {
100
- // Add file path to request body
101
- req.body.file = req.file.path;
102
-
103
- const data = await apiHandler.createTranscription(req.body);
104
- res.json(data);
105
-
106
- // Clean up uploaded file
107
- fs.unlinkSync(req.body.file);
108
- } catch (error) {
109
- res.status(500).json(error);
110
- }
111
- });
112
-
113
- // ------------------------------------------
114
- // Text to Speech Endpoint
115
- // ------------------------------------------
116
-
117
- app.post('/v1/audio/speech', async (req, res) => {
118
- try {
119
- await apiHandler.createSpeech(req.body, res);
120
- } catch (error) {
121
- if (!res.headersSent) {
122
- res.status(500).json(error);
123
- } else {
124
- console.error('Error after headers sent:', error);
125
- }
126
- }
127
- });
128
-
129
- // ------------------------------------------
130
- // Vision Endpoint
131
- // ------------------------------------------
132
-
133
- app.post('/v1/vision', async (req, res) => {
134
- try {
135
- const data = await apiHandler.createVisionAnalysis(req.body);
136
- res.json(data);
137
- } catch (error) {
138
- res.status(500).json(error);
139
- }
140
- });
141
-
142
- // ------------------------------------------
143
- // Start the Server
144
- // ------------------------------------------
145
-
146
- app.listen(port, () => {
147
- console.log(`Server running on port ${port}`);
148
  });
 
1
+ // ==========================================
2
+ // SERVER WITH ENDPOINTS
3
+ // ==========================================
4
+
5
+ require('dotenv').config();
6
+ const express = require('express');
7
+ const app = express();
8
+ const port = process.env.PORT || 3000;
9
+ const apiHandler = require('./apiHandler');
10
+ const bodyParser = require('body-parser');
11
+ const fs = require('fs');
12
+
13
+ // Middleware to parse JSON bodies
14
+ app.use(bodyParser.json());
15
+
16
+ // Middleware to parse multipart/form-data for file uploads
17
+ const multer = require('multer');
18
+ const upload = multer({ dest: 'uploads/' });
19
+
20
+ // ------------------------------------------
21
+ // Chat Completions Endpoint
22
+ // ------------------------------------------
23
+
24
+ app.post('/chat/completions', (req, res) => {
25
+ if (req.body.stream) {
26
+ // For streaming, the apiHandler manages the response and error handling
27
+ apiHandler.createChatCompletion(req.body, res);
28
+ } else {
29
+ // Non-streaming
30
+ apiHandler
31
+ .createChatCompletion(req.body)
32
+ .then((data) => {
33
+ res.json(data);
34
+ })
35
+ .catch((error) => {
36
+ res.status(500).json(error);
37
+ });
38
+ }
39
+ });
40
+
41
+ // ------------------------------------------
42
+ // Image Generation Endpoint
43
+ // ------------------------------------------
44
+
45
+ app.post('/images/generations', async (req, res) => {
46
+ try {
47
+ const data = await apiHandler.createImage(req.body);
48
+ res.json(data);
49
+ } catch (error) {
50
+ res.status(500).json(error);
51
+ }
52
+ });
53
+
54
+ // ------------------------------------------
55
+ // Image Editing Endpoint
56
+ // ------------------------------------------
57
+
58
+ app.post('/images/edits', upload.fields([{ name: 'image' }, { name: 'mask' }]), async (req, res) => {
59
+ try {
60
+ // Add file paths to request body
61
+ req.body.image = req.files['image'][0].path;
62
+ req.body.mask = req.files['mask'][0].path;
63
+
64
+ const data = await apiHandler.createImageEdit(req.body);
65
+ res.json(data);
66
+
67
+ // Clean up uploaded files
68
+ fs.unlinkSync(req.body.image);
69
+ fs.unlinkSync(req.body.mask);
70
+ } catch (error) {
71
+ res.status(500).json(error);
72
+ }
73
+ });
74
+
75
+ // ------------------------------------------
76
+ // Image Variations Endpoint
77
+ // ------------------------------------------
78
+
79
+ app.post('/images/variations', upload.single('image'), async (req, res) => {
80
+ try {
81
+ // Add file path to request body
82
+ req.body.image = req.file.path;
83
+
84
+ const data = await apiHandler.createImageVariation(req.body);
85
+ res.json(data);
86
+
87
+ // Clean up uploaded file
88
+ fs.unlinkSync(req.body.image);
89
+ } catch (error) {
90
+ res.status(500).json(error);
91
+ }
92
+ });
93
+
94
+ // ------------------------------------------
95
+ // Audio Transcription Endpoint
96
+ // ------------------------------------------
97
+
98
+ app.post('/audio/transcriptions', upload.single('file'), async (req, res) => {
99
+ try {
100
+ // Add file path to request body
101
+ req.body.file = req.file.path;
102
+
103
+ const data = await apiHandler.createTranscription(req.body);
104
+ res.json(data);
105
+
106
+ // Clean up uploaded file
107
+ fs.unlinkSync(req.body.file);
108
+ } catch (error) {
109
+ res.status(500).json(error);
110
+ }
111
+ });
112
+
113
+ // ------------------------------------------
114
+ // Text to Speech Endpoint
115
+ // ------------------------------------------
116
+
117
+ app.post('/audio/speech', async (req, res) => {
118
+ try {
119
+ await apiHandler.createSpeech(req.body, res);
120
+ } catch (error) {
121
+ if (!res.headersSent) {
122
+ res.status(500).json(error);
123
+ } else {
124
+ console.error('Error after headers sent:', error);
125
+ }
126
+ }
127
+ });
128
+
129
+ // ------------------------------------------
130
+ // Vision Endpoint
131
+ // ------------------------------------------
132
+
133
+ app.post('/vision', async (req, res) => {
134
+ try {
135
+ const data = await apiHandler.createVisionAnalysis(req.body);
136
+ res.json(data);
137
+ } catch (error) {
138
+ res.status(500).json(error);
139
+ }
140
+ });
141
+
142
+ // ------------------------------------------
143
+ // Start the Server
144
+ // ------------------------------------------
145
+
146
+ app.listen(port, () => {
147
+ console.log(`Server running on port ${port}`);
148
  });