Update server.js
Browse files
server.js
CHANGED
|
@@ -2,56 +2,68 @@ const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
|
| 2 |
const openaiKey = process.env.OPENAI_KEY;
|
| 3 |
const proxyKey = process.env.PROXY_KEY; // Your secret proxy key
|
| 4 |
|
| 5 |
-
|
| 6 |
const proxy = require("express-http-proxy");
|
| 7 |
-
const bodyParser = require("body-parser");
|
| 8 |
const fs = require("fs");
|
| 9 |
const FormData = require("form-data");
|
| 10 |
const port = 7860;
|
| 11 |
const app = express();
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
// Define the Whisper API endpoint URL
|
| 16 |
-
const whisperApiUrl = "https://api.openai.com/v1/audio/transcriptions";
|
| 17 |
|
| 18 |
-
//
|
| 19 |
-
app.
|
| 20 |
-
|
| 21 |
-
// Create a new form data object
|
| 22 |
-
const form = new FormData();
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
contentType: "audio/m4a", // Set the content type
|
| 28 |
-
});
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
//
|
| 34 |
-
|
| 35 |
|
| 36 |
-
//
|
| 37 |
-
const
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
//
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
//
|
| 48 |
-
res.status(
|
| 49 |
} catch (error) {
|
| 50 |
-
console.error(
|
| 51 |
-
res.status(500).
|
| 52 |
}
|
| 53 |
});
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
function getExternalUrl(spaceId) {
|
| 56 |
try {
|
| 57 |
const [username, spacename] = spaceId.split('/');
|
|
|
|
| 2 |
const openaiKey = process.env.OPENAI_KEY;
|
| 3 |
const proxyKey = process.env.PROXY_KEY; // Your secret proxy key
|
| 4 |
|
| 5 |
+
|
| 6 |
const proxy = require("express-http-proxy");
|
|
|
|
| 7 |
const fs = require("fs");
|
| 8 |
const FormData = require("form-data");
|
| 9 |
const port = 7860;
|
| 10 |
const app = express();
|
| 11 |
|
| 12 |
+
const express = require('express');
|
| 13 |
+
const bodyParser = require('body-parser');
|
| 14 |
+
const axios = require('axios');
|
| 15 |
+
const multer = require('multer'); // for handling multipart/form-data
|
| 16 |
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
// Middleware to parse JSON and URL-encoded form data
|
| 19 |
+
app.use(bodyParser.json());
|
| 20 |
+
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
// Middleware for handling file uploads
|
| 23 |
+
const storage = multer.memoryStorage();
|
| 24 |
+
const upload = multer({ storage: storage });
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
// Define routes
|
| 27 |
+
app.post('/upload', upload.single('file'), async (req, res) => {
|
| 28 |
+
try {
|
| 29 |
+
// Extract the uploaded file and other form data
|
| 30 |
+
const fileData = req.file;
|
| 31 |
+
const formData = req.body;
|
| 32 |
|
| 33 |
+
// Get the API key from the environment variable
|
| 34 |
+
const apiKey = process.env.OPENAI_KEY;
|
| 35 |
|
| 36 |
+
// Create a new FormData object to send to the Whisper API
|
| 37 |
+
const whisperFormData = new FormData();
|
| 38 |
+
whisperFormData.append('file', fileData.buffer, { filename: fileData.originalname });
|
| 39 |
+
|
| 40 |
+
// Append other form data fields if needed
|
| 41 |
+
for (const key in formData) {
|
| 42 |
+
whisperFormData.append(key, formData[key]);
|
| 43 |
+
}
|
| 44 |
|
| 45 |
+
// Make a request to the Whisper API with the obtained API key and multipart/form-data
|
| 46 |
+
const whisperResponse = await axios.post('http://whisper-api-url', whisperFormData, {
|
| 47 |
+
headers: {
|
| 48 |
+
'Authorization': `Bearer ${apiKey}`,
|
| 49 |
+
...whisperFormData.getHeaders(),
|
| 50 |
+
},
|
| 51 |
+
});
|
| 52 |
|
| 53 |
+
// Forward the response from the Whisper API back to the iOS app
|
| 54 |
+
res.status(whisperResponse.status).json(whisperResponse.data);
|
| 55 |
} catch (error) {
|
| 56 |
+
console.error(error);
|
| 57 |
+
res.status(500).json({ error: 'Internal Server Error' });
|
| 58 |
}
|
| 59 |
});
|
| 60 |
|
| 61 |
+
app.listen(port, () => {
|
| 62 |
+
console.log(`Server is running on port ${port}`);
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
function getExternalUrl(spaceId) {
|
| 68 |
try {
|
| 69 |
const [username, spacename] = spaceId.split('/');
|