-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
98 lines (82 loc) · 3.35 KB
/
server.js
File metadata and controls
98 lines (82 loc) · 3.35 KB
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
97
98
const express = require('express');
const fileUpload = require('express-fileupload');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const { OpenAI } = require('openai');
require('dotenv').config();
const axios = require('axios');
const FormData = require('form-data');
const app = express();
const port = 3000;
app.use(cors());
app.use(fileUpload({
useTempFiles: true,
tempFileDir: '/tmp/'
}));
app.use(express.json());
app.use(express.static(path.join(__dirname, 'templates')));
// Überprüfen, ob der OpenAI-API-Schlüssel vorhanden ist
if (!process.env.OPENAI_API_KEY) {
console.error('No OpenAI API key provided. Please set the OPENAI_API_KEY environment variable.');
process.exit(1);
}
// Initialize OpenAI with your API key
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'index.html'));
});
app.post('/process_audio', async (req, res) => {
if (!req.files || !req.files.audio) {
console.error('No audio file provided');
return res.status(400).json({ error: 'No audio file provided' });
}
const audioFile = req.files.audio;
const audioPath = path.join(__dirname, 'audio.mp3');
// Save the file in the current working directory
await audioFile.mv(audioPath);
console.log('Audio file saved successfully.');
// Create a form-data object for the API request
const formData = new FormData();
formData.append('file', fs.createReadStream(audioPath));
formData.append('model', 'whisper-1');
// Send request to OpenAI API for transcription
const transcriptionResponse = await axios.post(
'https://api.openai.com/v1/audio/transcriptions',
formData,
{
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
...formData.getHeaders(),
},
}
);
const text = transcriptionResponse.data.text;
console.log('Transcription:', text);
// Use the transcription result to generate categories
const prompt = `Kategorisieren Sie den folgenden Text in ein einfaches gültiges JSON-Objekt ohne python multiline Kommentare und ohne JSON Keyword, das folgendes enthält: Vorname, Nachname, Alter, Geschlecht, Blutdruck, Körpertemperatur und weitere Vitalparameter, Diagnosetext mit Nummer 1. bis Nummer 5. Diagnose als Javascript Array mit Key 1. etc. (numerischer Wert mit Punkt) Value: die Diagnose:\n\n${text}`;
const gptResponse = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: [
{ role: 'system', content: 'Sie sind ein hilfreicher Assistent, der auf medizinische Kategorisierung spezialisiert ist.' },
{ role: 'user', content: prompt },
],
max_tokens: 200,
});
const categories = gptResponse.choices[0].message.content.trim();
console.log('Categories:', categories);
res.json({ transcription: text, categories: categories });
// Remove the audio file after inference
fs.unlink(audioPath, (err) => {
if (err) {
console.error('Error removing audio file:', err);
} else {
console.log('Audio file removed successfully.');
}
});
});
app.listen(port, () => {
console.log(`Server is running on http://127.0.0.1:${port}`);
});