ardasen commited on
Commit
b16b471
·
1 Parent(s): 382707c

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +44 -32
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
- const express = require("express");
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
- app.use(bodyParser.raw({ type: "audio/*", limit: "50mb" })); // Allow audio file uploads
 
 
 
14
 
15
- // Define the Whisper API endpoint URL
16
- const whisperApiUrl = "https://api.openai.com/v1/audio/transcriptions";
17
 
18
- // Set up a route to handle the reverse proxy
19
- app.post("/reverse-proxy-whisper", async (req, res) => {
20
- try {
21
- // Create a new form data object
22
- const form = new FormData();
23
 
24
- // Append the uploaded audio file to the form data
25
- form.append("file", req.body, {
26
- filename: "audio.m4a", // Set the filename
27
- contentType: "audio/m4a", // Set the content type
28
- });
29
 
30
- // Set other fields as needed (e.g., model)
31
- form.append("model", "whisper-1");
 
 
 
 
32
 
33
- // Log the form data for debugging
34
- console.log("Form Data:", form);
35
 
36
- // Forward the form data to the Whisper API
37
- const proxyResponse = await proxy(whisperApiUrl, {
38
- proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
39
- proxyReqOpts.headers["Authorization"] = "Bearer " + openaiKey;
40
- return proxyReqOpts;
41
- },
42
- })(req, res);
 
43
 
44
- // Log the proxy response for debugging
45
- console.log("Proxy Response:", proxyResponse);
 
 
 
 
 
46
 
47
- // Pass the Whisper API response back to the client
48
- res.status(proxyResponse.statusCode).send(proxyResponse.body);
49
  } catch (error) {
50
- console.error("Error:", error);
51
- res.status(500).send("Internal Server Error");
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('/');