Spaces:
Sleeping
Sleeping
| // ========================================== | |
| // 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('/v2/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('/v2/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('/v2/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('/v2/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('/v2/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('/v2/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('/v2/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}`); | |
| }); |