ardasen commited on
Commit
d101f54
·
1 Parent(s): fd068e9

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +15 -24
server.js CHANGED
@@ -2,17 +2,17 @@ const express = require('express');
2
  const proxy = require('express-http-proxy');
3
  const FormData = require('form-data');
4
  const https = require('https');
5
- const fs = require('fs');
6
  const url = require('url');
 
7
 
8
  const app = express();
9
  const targetUrl = 'https://api.openai.com';
10
- const openaiKey = process.env.OPENAI_KEY; // Get the API key from the environment variable
11
  const proxyKey = process.env.PROXY_KEY;
12
  const port = 7860;
13
  const baseUrl = getExternalUrl(process.env.SPACE_ID);
14
 
15
- app.use(express.json({ limit: '50mb' }));
16
 
17
  function authenticateProxyKey(req, res, next) {
18
  const providedKey = req.headers['auro'];
@@ -35,34 +35,25 @@ app.use('/api', (req, res, next) => {
35
  });
36
 
37
  app.post('/api/proxy', async (req, res) => {
38
- const fileURL = req.body.fileURL;
39
 
40
  const headers = {
41
- auro: openaiKey, // Pass the API key obtained from the environment variable
 
42
  };
43
 
44
  const form = new FormData();
45
 
46
-
47
-
48
- // Check if fileURL is defined and the file exists
49
- if (!fileURL) {
50
- res.status(400).json({ error: 'Missing or invalid fileURL' });
51
- return;
52
- }
53
-
54
- // Ensure that the file exists
55
- if (!fs.existsSync(fileURL)) {
56
- res.status(404).json({ error: 'File not found' });
57
- return;
58
- }
59
-
60
- // Proceed with creating the ReadStream
61
- form.append('file', fs.createReadStream(fileURL), {
62
- filename: 'audioFile',
63
- contentType: 'audio/m4a',
64
- });
65
 
 
 
 
 
 
66
 
67
  // Append other form fields
68
  form.append('model', 'whisper-1');
 
2
  const proxy = require('express-http-proxy');
3
  const FormData = require('form-data');
4
  const https = require('https');
 
5
  const url = require('url');
6
+ const bodyParser = require('body-parser'); // Add bodyParser to parse request body
7
 
8
  const app = express();
9
  const targetUrl = 'https://api.openai.com';
10
+ const openaiKey = process.env.OPENAI_KEY;
11
  const proxyKey = process.env.PROXY_KEY;
12
  const port = 7860;
13
  const baseUrl = getExternalUrl(process.env.SPACE_ID);
14
 
15
+ app.use(bodyParser.json({ limit: '50mb' })); // Use bodyParser to parse JSON request body
16
 
17
  function authenticateProxyKey(req, res, next) {
18
  const providedKey = req.headers['auro'];
 
35
  });
36
 
37
  app.post('/api/proxy', async (req, res) => {
38
+ const audioData = req.body.audioData; // Get audio data from the request body
39
 
40
  const headers = {
41
+ auro: openaiKey,
42
+ 'Content-Type': 'multipart/form-data',
43
  };
44
 
45
  const form = new FormData();
46
 
47
+ if (!audioData) {
48
+ res.status(400).json({ error: 'Missing or invalid audioData' });
49
+ return;
50
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ // Append the audio data as a buffer with the appropriate content type
53
+ form.append('file', Buffer.from(audioData), {
54
+ filename: 'audioFile',
55
+ contentType: 'audio/m4a',
56
+ });
57
 
58
  // Append other form fields
59
  form.append('model', 'whisper-1');