-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
103 lines (86 loc) · 3.47 KB
/
Copy pathserver.js
File metadata and controls
103 lines (86 loc) · 3.47 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
const express = require('express');
const fetch = require('node-fetch');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json({ limit: '10mb' }));
app.use(express.static('.'));
// Caption endpoint - uses Google Gemini Vision API
app.post('/api/caption', async (req, res) => {
try {
const { image } = req.body;
// Remove data URL prefix to get base64 data
const base64Data = image.replace(/^data:image\/\w+;base64,/, '');
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${process.env.GEMINI_API_KEY}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
contents: [{
parts: [
{ text: 'Describe this image in one clear sentence for someone with visual impairment.' },
{
inline_data: {
mime_type: 'image/jpeg',
data: base64Data
}
}
]
}]
})
});
const data = await response.json();
// Log the full response for debugging
console.log('Gemini Response:', JSON.stringify(data, null, 2));
if (!response.ok) {
throw new Error(`Gemini API error: ${data.error?.message || 'Unknown error'}`);
}
if (!data.candidates || !data.candidates[0]) {
throw new Error('Invalid response from Gemini API');
}
const caption = data.candidates[0].content.parts[0].text;
res.json({ caption });
} catch (error) {
console.error('Caption error:', error);
res.status(500).json({ error: error.message });
}
});
// Speech endpoint - uses ElevenLabs API
app.post('/api/speak', async (req, res) => {
try {
const { text } = req.body;
console.log('Speech request received for text:', text);
const response = await fetch(`https://api.elevenlabs.io/v1/text-to-speech/${process.env.ELEVENLABS_VOICE_ID}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'xi-api-key': process.env.ELEVENLABS_API_KEY
},
body: JSON.stringify({
text,
model_id: 'eleven_turbo_v2_5',
voice_settings: {
stability: 0.5,
similarity_boost: 0.5
}
})
});
console.log('ElevenLabs response status:', response.status);
if (!response.ok) {
const errorText = await response.text();
console.error('ElevenLabs API error:', errorText);
throw new Error(`ElevenLabs API error: ${response.status} - ${errorText}`);
}
const audioBuffer = await response.buffer();
console.log('Audio buffer size:', audioBuffer.length);
res.set('Content-Type', 'audio/mpeg');
res.send(audioBuffer);
} catch (error) {
console.error('Speech error:', error);
res.status(500).json({ error: error.message });
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});