ardasen commited on
Commit
e82d0de
·
1 Parent(s): a3db9d3

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +49 -59
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'); // Add the 'url' module
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
- let fileURL = req.body.fileURL; // Get the file URL from the request body
39
- const apiKey = openaiKey;
40
- const parsedURL = url.parse(fileURL); // Parse the file URL
41
- fileURL = decodeURIComponent(parsedURL.pathname); // Extract and decode the file path
42
-
43
- // Check if the file exists
44
- if (fs.existsSync(fileURL)) {
45
- const headers = {
46
- auro: apiKey,
47
- };
48
-
49
- const form = new FormData();
50
-
51
- // Append the file to the form data
52
- form.append('file', fs.createReadStream(fileURL), {
53
- filename: 'audioFile',
54
- contentType: 'audio/m4a', // Adjust the content type as needed (e.g., 'audio/mp3', 'audio/wav')
55
- });
 
 
 
 
 
 
 
56
 
57
- // Append other form fields
58
- form.append('model', 'whisper-1');
59
- form.append('response_format', 'text');
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
- form.pipe(request);
 
 
87
 
88
- request.on('error', (error) => {
89
- console.error(error);
90
- res.status(500).json({ error: 'Internal Server Error' });
 
 
 
91
  });
 
92
 
93
- request.end();
94
- } catch (error) {
 
95
  console.error(error);
96
  res.status(500).json({ error: 'Internal Server Error' });
97
- }
98
- } else {
99
- // Handle the case where the file does not exist
100
- console.error('File does not exist:', fileURL);
101
- res.status(404).json({ error: 'File not found' });
 
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