-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
135 lines (112 loc) · 2.85 KB
/
server.js
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
135
const express = require('express');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000;
const AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json')
const Polly = new AWS.Polly({
region: 'us-east-1'
})
const Fs = require('fs');
app.use(express.static('src'));
app.get('/viewer', (req, res) => {
res.sendFile(path.join(__dirname, 'src', 'site', 'viewer.html'));
});
app.get('/viewer2', (req, res) => {
res.sendFile(path.join(__dirname, 'src', 'site', 'viewer2.html'));
});
const axios = require('axios');
app.get('/gpt', async (req, res) => {
const prompt = req.query.prompt;
const url = 'https://api.openai.com/v1/chat/completions';
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${api_key}`,
};
const data = {
model: 'gpt-3.5-turbo',
messages: [
{
role: 'system',
content: 'You are an automated script.',
},
{
role: 'user',
content: prompt,
},
],
};
const config = {
headers,
};
try {
axios
.post(url, data, config)
.then((response) => {
res.send(response.data.choices[0].message.content)
})
.catch((error) => {
console.log(error.message); // Display the error message
res.send(0)
});
} catch (error) {
console.log(error.message);
res.send(0)
}
});
app.get('/replicate', async (req, res) => {
const OpenAI = require('openai');
const openai = new OpenAI({
apiKey: api_key,
})
const prompt = req.query.prompt;
const response = await openai.images.generate({
prompt: prompt,
size: "1024x1024",
});
const image_url = response.data[0].url;
res.send(image_url);
});
app.get('/tts', async (req, res) => {
const prompt = req.query.prompt;
const i = req.query.val;
const input = { // SynthesizeSpeechInput
Engine: "neural",
LanguageCode: "en-US",
OutputFormat: "mp3",
Text: prompt,
VoiceId: "Stephen", // required
};
Polly.synthesizeSpeech(input, (err,data) => {
if(err) {
console.log('Error', err);
return;
}
if (data.AudioStream) {
Fs.writeFile(`src/site/speech_${i}.mp3`, data.AudioStream, (err) => {
res.send('speech');
})
}
})
});
// app.get('/replicate', async (req, res) => {
// const prompt = req.query.prompt;
// console.log(prompt)
// const output = await replicate.run(
// "stability-ai/stable-diffusion:f178fa7a1ae43a9a9af01b833b9d2ecf97b1bcb0acfd2dc5dd04895e042863f1",
// {
// input: {
// prompt: prompt,
// height: 512,
// width: 1024
// }
// }
// );
// res.send(output[0]);
// });
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'src', 'site', 'index.html'));
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});