Update server.js
Browse files
server.js
CHANGED
|
@@ -3,11 +3,11 @@ 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;
|
| 11 |
const proxyKey = process.env.PROXY_KEY;
|
| 12 |
const port = 7860;
|
| 13 |
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
|
@@ -35,70 +35,60 @@ app.use('/api', (req, res, next) => {
|
|
| 35 |
});
|
| 36 |
|
| 37 |
app.post('/api/proxy', async (req, res) => {
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
const
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
const requestOptions = {
|
| 62 |
-
method: 'POST',
|
| 63 |
-
headers,
|
| 64 |
-
path: '/audio/transcriptions',
|
| 65 |
-
host: targetUrl.replace('https://', ''),
|
| 66 |
-
port: 443,
|
| 67 |
-
};
|
| 68 |
-
|
| 69 |
-
try {
|
| 70 |
-
const request = https.request(requestOptions, (response) => {
|
| 71 |
-
let data = '';
|
| 72 |
-
|
| 73 |
-
response.on('data', (chunk) => {
|
| 74 |
-
data += chunk;
|
| 75 |
-
});
|
| 76 |
-
|
| 77 |
-
response.on('end', () => {
|
| 78 |
-
if (response.statusCode === 200) {
|
| 79 |
-
res.status(200).json({ transcription: data });
|
| 80 |
-
} else {
|
| 81 |
-
res.status(response.statusCode).json({ error: data });
|
| 82 |
-
}
|
| 83 |
-
});
|
| 84 |
-
});
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
});
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
console.error(error);
|
| 96 |
res.status(500).json({ error: 'Internal Server Error' });
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 102 |
}
|
| 103 |
});
|
| 104 |
|
|
|
|
| 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);
|
|
|
|
| 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 |
+
// Append the file to the form data
|
| 47 |
+
form.append('file', fs.createReadStream(fileURL), {
|
| 48 |
+
filename: 'audioFile',
|
| 49 |
+
contentType: 'audio/m4a', // Adjust the content type as needed
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
// Append other form fields
|
| 53 |
+
form.append('model', 'whisper-1');
|
| 54 |
+
form.append('response_format', 'text');
|
| 55 |
+
|
| 56 |
+
const requestOptions = {
|
| 57 |
+
method: 'POST',
|
| 58 |
+
headers,
|
| 59 |
+
path: '/audio/transcriptions',
|
| 60 |
+
host: targetUrl.replace('https://', ''),
|
| 61 |
+
port: 443,
|
| 62 |
+
};
|
| 63 |
|
| 64 |
+
try {
|
| 65 |
+
const request = https.request(requestOptions, (response) => {
|
| 66 |
+
let data = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
response.on('data', (chunk) => {
|
| 69 |
+
data += chunk;
|
| 70 |
+
});
|
| 71 |
|
| 72 |
+
response.on('end', () => {
|
| 73 |
+
if (response.statusCode === 200) {
|
| 74 |
+
res.status(200).json({ transcription: data });
|
| 75 |
+
} else {
|
| 76 |
+
res.status(response.statusCode).json({ error: data });
|
| 77 |
+
}
|
| 78 |
});
|
| 79 |
+
});
|
| 80 |
|
| 81 |
+
form.pipe(request);
|
| 82 |
+
|
| 83 |
+
request.on('error', (error) => {
|
| 84 |
console.error(error);
|
| 85 |
res.status(500).json({ error: 'Internal Server Error' });
|
| 86 |
+
});
|
| 87 |
+
|
| 88 |
+
request.end();
|
| 89 |
+
} catch (error) {
|
| 90 |
+
console.error(error);
|
| 91 |
+
res.status(500).json({ error: 'Internal Server Error' });
|
| 92 |
}
|
| 93 |
});
|
| 94 |
|