-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
37 lines (29 loc) · 1.03 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
const express = require('express');
const bodyParser = require('body-parser');
const { GoogleGenerativeAI } = require('@google/generative-ai');
require('dotenv').config();
const app = express();
const port = 3000;
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('index', { story: '' });
});
app.post('/genai', async (req, res) => {
const prompt = req.body.prompt;
try {
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
const result = await model.generateContent(prompt);
const response = await result.response;
const text = await response.text();
res.render('index', { story: text });
} catch (error) {
console.error('Error generating content:', error);
res.render('index', { story: 'Failed to generate content' });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});