File size: 3,958 Bytes
b3b2ca7
 
 
 
 
 
 
ce55d33
b3b2ca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c0c91d
b3b2ca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c0c91d
b3b2ca7
 
 
 
 
 
 
 
 
 
 
 
5c0c91d
b3b2ca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c0c91d
b3b2ca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c0c91d
b3b2ca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c0c91d
b3b2ca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c0c91d
b3b2ca7
 
 
 
 
 
 
 
 
 
 
 
 
 
184406c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// ==========================================
//             SERVER WITH ENDPOINTS
// ==========================================

require('dotenv').config();
const express = require('express');
const app = express();
const port = process.env.PORT || 7860;
const apiHandler = require('./apiHandler');
const bodyParser = require('body-parser');
const fs = require('fs');

// Middleware to parse JSON bodies
app.use(bodyParser.json());

// Middleware to parse multipart/form-data for file uploads
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

// ------------------------------------------
// Chat Completions Endpoint
// ------------------------------------------

app.post('/v1/chat/completions', (req, res) => {
  if (req.body.stream) {
    // For streaming, the apiHandler manages the response and error handling
    apiHandler.createChatCompletion(req.body, res);
  } else {
    // Non-streaming
    apiHandler
      .createChatCompletion(req.body)
      .then((data) => {
        res.json(data);
      })
      .catch((error) => {
        res.status(500).json(error);
      });
  }
});

// ------------------------------------------
// Image Generation Endpoint
// ------------------------------------------

app.post('/v1/images/generations', async (req, res) => {
  try {
    const data = await apiHandler.createImage(req.body);
    res.json(data);
  } catch (error) {
    res.status(500).json(error);
  }
});

// ------------------------------------------
// Image Editing Endpoint
// ------------------------------------------

app.post('/v1/images/edits', upload.fields([{ name: 'image' }, { name: 'mask' }]), async (req, res) => {
  try {
    // Add file paths to request body
    req.body.image = req.files['image'][0].path;
    req.body.mask = req.files['mask'][0].path;

    const data = await apiHandler.createImageEdit(req.body);
    res.json(data);

    // Clean up uploaded files
    fs.unlinkSync(req.body.image);
    fs.unlinkSync(req.body.mask);
  } catch (error) {
    res.status(500).json(error);
  }
});

// ------------------------------------------
// Image Variations Endpoint
// ------------------------------------------

app.post('/v1/images/variations', upload.single('image'), async (req, res) => {
  try {
    // Add file path to request body
    req.body.image = req.file.path;

    const data = await apiHandler.createImageVariation(req.body);
    res.json(data);

    // Clean up uploaded file
    fs.unlinkSync(req.body.image);
  } catch (error) {
    res.status(500).json(error);
  }
});

// ------------------------------------------
// Audio Transcription Endpoint
// ------------------------------------------

app.post('/v1/audio/transcriptions', upload.single('file'), async (req, res) => {
  try {
    // Add file path to request body
    req.body.file = req.file.path;

    const data = await apiHandler.createTranscription(req.body);
    res.json(data);

    // Clean up uploaded file
    fs.unlinkSync(req.body.file);
  } catch (error) {
    res.status(500).json(error);
  }
});

// ------------------------------------------
// Text to Speech Endpoint
// ------------------------------------------

app.post('/v1/audio/speech', async (req, res) => {
  try {
    await apiHandler.createSpeech(req.body, res);
  } catch (error) {
    if (!res.headersSent) {
      res.status(500).json(error);
    } else {
      console.error('Error after headers sent:', error);
    }
  }
});

// ------------------------------------------
// Vision Endpoint
// ------------------------------------------

app.post('/v1/vision', async (req, res) => {
  try {
    const data = await apiHandler.createVisionAnalysis(req.body);
    res.json(data);
  } catch (error) {
    res.status(500).json(error);
  }
});

// ------------------------------------------
// Start the Server
// ------------------------------------------

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});