ardasen commited on
Commit
bcdb2d1
·
1 Parent(s): 02ce928

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +46 -0
server.js CHANGED
@@ -15,6 +15,52 @@ const axios = require('axios');
15
  const multer = require('multer'); // for handling multipart/form-data
16
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  function getExternalUrl(spaceId) {
19
  try {
20
  const [username, spacename] = spaceId.split('/');
 
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('https://api.openai.com/v1/audio/transcriptions', 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
+
62
+
63
+
64
  function getExternalUrl(spaceId) {
65
  try {
66
  const [username, spacename] = spaceId.split('/');