API-Handler commited on
Commit
981e554
·
verified ·
1 Parent(s): 8a2ae45

Delete src

Browse files
Files changed (2) hide show
  1. src/apiHandler.js +0 -168
  2. src/server.js +0 -148
src/apiHandler.js DELETED
@@ -1,168 +0,0 @@
1
- // ==========================================
2
- // API HANDLER FILE
3
- // ==========================================
4
-
5
- require('dotenv').config();
6
- const OpenAI = require('openai');
7
- const fs = require('fs');
8
-
9
- // Initialize OpenAI client
10
- const openai = new OpenAI({
11
- apiKey: process.env.OPENAI_UNLIMITED_API_KEY,
12
- });
13
-
14
- // ------------------------------------------
15
- // Chat Completions (Text Generation, Reasoning, Function Calling)
16
- // ------------------------------------------
17
-
18
- async function createChatCompletion(requestBody, res) {
19
- if (requestBody.stream) {
20
- // Handle streaming response
21
- try {
22
- const completion = await openai.chat.completions.create({
23
- ...requestBody,
24
- stream: true,
25
- });
26
-
27
- res.setHeader('Content-Type', 'text/event-stream');
28
- res.setHeader('Cache-Control', 'no-cache');
29
- res.flushHeaders();
30
-
31
- for await (const part of completion) {
32
- const data = JSON.stringify(part);
33
- res.write(`data: ${data}\n\n`);
34
- }
35
- res.write('data: [DONE]\n\n');
36
- res.end();
37
- } catch (error) {
38
- console.error('Error in createChatCompletion:', error);
39
- if (!res.headersSent) {
40
- res.status(500).json(error.response ? error.response.data : error);
41
- } else {
42
- res.write(`data: ${JSON.stringify({ error: error.message })}\n\n`);
43
- res.end();
44
- }
45
- }
46
- } else {
47
- // Non-streaming case
48
- try {
49
- const completion = await openai.chat.completions.create(requestBody);
50
- return completion;
51
- } catch (error) {
52
- throw error.response ? error.response.data : error;
53
- }
54
- }
55
- }
56
-
57
- // ------------------------------------------
58
- // Image Generation
59
- // ------------------------------------------
60
-
61
- async function createImage(requestBody) {
62
- try {
63
- const response = await openai.images.generate(requestBody);
64
- return response;
65
- } catch (error) {
66
- throw error.response ? error.response.data : error;
67
- }
68
- }
69
-
70
- // ------------------------------------------
71
- // Image Editing
72
- // ------------------------------------------
73
-
74
- async function createImageEdit(requestBody) {
75
- try {
76
- const response = await openai.images.edit({
77
- image: fs.createReadStream(requestBody.image),
78
- mask: fs.createReadStream(requestBody.mask),
79
- prompt: requestBody.prompt,
80
- n: requestBody.n,
81
- size: requestBody.size,
82
- });
83
- return response;
84
- } catch (error) {
85
- throw error.response ? error.response.data : error;
86
- }
87
- }
88
-
89
- // ------------------------------------------
90
- // Image Variation
91
- // ------------------------------------------
92
-
93
- async function createImageVariation(requestBody) {
94
- try {
95
- const response = await openai.images.createVariation({
96
- image: fs.createReadStream(requestBody.image),
97
- n: requestBody.n,
98
- size: requestBody.size,
99
- });
100
- return response;
101
- } catch (error) {
102
- throw error.response ? error.response.data : error;
103
- }
104
- }
105
-
106
- // ------------------------------------------
107
- // Audio Transcription
108
- // ------------------------------------------
109
-
110
- async function createTranscription(requestBody) {
111
- try {
112
- const response = await openai.audio.transcriptions.create({
113
- file: fs.createReadStream(requestBody.file),
114
- model: requestBody.model,
115
- });
116
- return response;
117
- } catch (error) {
118
- throw error.response ? error.response.data : error;
119
- }
120
- }
121
-
122
- // ------------------------------------------
123
- // Text to Speech
124
- // ------------------------------------------
125
-
126
- async function createSpeech(requestBody, res) {
127
- try {
128
- const response = await openai.audio.speech.create({
129
- ...requestBody,
130
- });
131
-
132
- const arrayBuffer = await response.arrayBuffer();
133
- res.setHeader('Content-Type', 'audio/mpeg');
134
- res.send(Buffer.from(arrayBuffer));
135
- } catch (error) {
136
- console.error('Error in createSpeech:', error);
137
- if (!res.headersSent) {
138
- res.status(500).json(error.response ? error.response.data : error);
139
- }
140
- }
141
- }
142
-
143
- // ------------------------------------------
144
- // Vision (Image Analysis)
145
- // ------------------------------------------
146
-
147
- async function createVisionAnalysis(requestBody) {
148
- try {
149
- const response = await openai.chat.completions.create(requestBody);
150
- return response;
151
- } catch (error) {
152
- throw error.response ? error.response.data : error;
153
- }
154
- }
155
-
156
- // ------------------------------------------
157
- // Export Modules
158
- // ------------------------------------------
159
-
160
- module.exports = {
161
- createChatCompletion,
162
- createImage,
163
- createImageEdit,
164
- createImageVariation,
165
- createTranscription,
166
- createSpeech,
167
- createVisionAnalysis,
168
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/server.js DELETED
@@ -1,148 +0,0 @@
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 || 7860;
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('/v2/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('/v2/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('/v2/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('/v2/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('/v2/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('/v2/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('/v2/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
- });