This repository was archived by the owner on May 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
134 lines (115 loc) · 4.04 KB
/
server.js
File metadata and controls
134 lines (115 loc) · 4.04 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const express = require('express');
const cors = require('cors');
const { MongoClient } = require('mongodb');
const mongoose = require('mongoose');
const speech = require('@google-cloud/speech');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 8080;
const GOOGLE_APPLICATION_CREDENTIALS = 'key.json';
process.env.GOOGLE_APPLICATION_CREDENTIALS = GOOGLE_APPLICATION_CREDENTIALS;
app.use(cors());
app.use(express.json());
const uri = process.env.ATLAS_URI;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
const textToSpeech = require('@google-cloud/text-to-speech');
const ttsClient = new textToSpeech.TextToSpeechClient();
async function textToSpeechHelper(text) {
console.log('Credentials:', process.env.GOOGLE_APPLICATION_CREDENTIALS);
const request = {
input: { text },
// Select the language and SSML Voice Gender (optional)
voice: { languageCode: 'uk-UA', ssmlGender: 'NEUTRAL' },
// Select the type of audio encoding
audioConfig: { audioEncoding: 'MP3' },
};
try {
const [response] = await ttsClient.synthesizeSpeech(request);
return response.audioContent;
} catch (error) {
console.error('Error in textToSpeech function:', error);
return null;
}
}
async function transcribe(audioBuffer) {
const client = new speech.SpeechClient();
const audio = {
content: audioBuffer.toString('base64'),
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 24000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};
const [response] = await client.recognize(request);
const transcription = response.results
.map((result) => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
return transcription;
}
async function connectToDatabase() {
try {
await client.connect();
console.log("MongoDB database connection established successfully");
} catch (err) {
console.error(err)
}
}
connectToDatabase();
async function getTranslatedName(medicineName) {
try {
const db = client.db('test');
const collection = db.collection('WHO_Database');
const query = { Medicine: medicineName };
const medicine = await collection.findOne(query);
if (medicine) {
const translation = medicine.Automatic_Translation || medicine.Manual_Translation;
console.log('Translated Name:', translation);
return { Translation: translation, Source: medicine.Source };;
} else {
console.log('Medicine not found');
return null;
}
} catch (err) {
console.error(err);
}
}
app.get('/api/translated-names/:medicine', async (req, res) => {
console.log("Request received for:", req.params.medicine);
const medicineName = req.params.medicine;
const translatedNameObject = await getTranslatedName(medicineName);
if (translatedNameObject) {
console.log(translatedNameObject);
const { Translation, Source } = translatedNameObject;
res.json({ Translation, Source });
} else {
res.status(404).json({ error: 'Medicine not found' });
}
});
app.post('/api/text-to-speech', async (req, res) => {
const { text } = req.body;
const audioContent = await textToSpeechHelper(text); // Use your textToSpeech function here
if (audioContent) {
res.send(audioContent);
} else {
res.status(500).json({ error: 'Text-to-speech conversion failed' });
}
});
app.post('/api/transcribe', async (req, res) => {
try {
const audioBuffer = Buffer.from(req.body.audio, 'base64');
const transcription = await transcribe(audioBuffer);
res.json({ transcription });
} catch (error) {
console.error('Error in transcription:', error);
res.status(500).json({ error: 'Transcription failed' });
}
});
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});