File size: 2,692 Bytes
7e09486 f507d20 d5a353e b16b471 f507d20 a92035c f507d20 9444783 d5a353e b16b471 9444783 bcdb2d1 4a4945c bcdb2d1 51e0692 bcdb2d1 1f3ec2b b4f54fb 7c4d012 b4f54fb 7c4d012 bcdb2d1 6b3efd7 ff5dc36 6b3efd7 bcdb2d1 694de7b f6778f2 9444783 694de7b cc71b15 ac99b60 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
const baseUrl = getExternalUrl(process.env.SPACE_ID);
const openaiKey = process.env.OPENAI_KEY;
const proxyKey = process.env.PROXY_KEY; // Your secret proxy key
const express = require('express');
const proxy = require("express-http-proxy");
const fs = require("fs");
const FormData = require("form-data");
const port = 7860;
const app = express();
const bodyParser = require('body-parser');
const axios = require('axios');
const multer = require('multer'); // for handling multipart/form-data
// Middleware to parse JSON and URL-encoded form data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Middleware for handling file uploads
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
function authenticateProxyKey(req, res, next) {
const providedKey = req.headers['auro'];
if (providedKey && providedKey === proxyKey) {
next();
} else {
res.status(401).json({ error: 'Unauthorized' });
}
}
// Define routes
app.post('/upload', authenticateProxyKey, upload.single('file'), async (req, res) => {
try {
// Extract the uploaded file and other form data
const fileData = req.file;
const formData = req.body;
// Get the API key from the environment variable
const apiKey = process.env.OPENAI_KEY;
// Create a new FormData object to send to the Whisper API
const whisperFormData = new FormData();
whisperFormData.append('file', fileData.buffer, { filename: fileData.originalname });
// Append other form data fields if needed
for (const key in formData) {
whisperFormData.append(key, formData[key]);
}
// Make a request to the Whisper API with the obtained API key and multipart/form-data
const whisperResponse = await axios.post('https://api.openai.com/v1/audio/transcriptions', whisperFormData, {
headers: {
'Authorization': `Bearer ${apiKey}`,
...whisperFormData.getHeaders(),
},
});
console.log('Whisper API Response:', whisperResponse.data);
// Forward the response from the Whisper API back to the iOS app
res.status(whisperResponse.status).send(whisperResponse.data);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
function getExternalUrl(spaceId) {
try {
const [username, spacename] = spaceId.split('/');
return `https://${username}-${spacename.replace(/_/g, '-')}.hf.space/api/v1`;
} catch (e) {
return '';
}
}
app.get('/', (req, res) => {
// res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
});
app.listen(port, () => {
console.log(`Reverse proxy server running on ${baseUrl}`);
});
|