-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (53 loc) · 2.61 KB
/
Copy pathindex.js
File metadata and controls
66 lines (53 loc) · 2.61 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
const PORT = process.env.PORT || 8000;
const express = require('express');
const { GoogleGenerativeAI } = require("@google/generative-ai");
require('dotenv').config();
const app = express();
app.use(express.json()); // This is necessary to parse JSON bodies
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
// Welcome route
app.get('/', (req, res) => {
res.json({ message: 'Welcome to the Accessibility Content Transformer API' });
});
// Simplifying texts
app.post('/simplify', async (req, res) => {
const authHeaders = req.headers
if(authHeaders.secretkey !== process.env.ZUPLOS_API_KEY){
res.json({message: "Access not authorized."})
return
}
const text = req.body.text; // Destructure text from the request body
try {
const prompt = `You are an assistant that was design to help people with cognitive disability or non instructed people to understand complex and difficult content texts. Your goal is to simplify the text without loosing any content or important information. Try to use easier words, examples to explain things and so on. You must follow the same language as the text provided. Here is my text: ${text}`
const result = await model.generateContent(prompt);
const response = await result.response;
const content = response.text();
res.json({ simplified_text: content })
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to simplify the text due to an internal error.' });
}
});
// Reducing texts
app.post('/reducer', async (req, res) => {
const authHeaders = req.headers
if(authHeaders.secretkey !== "my-ultra-secret-key"){
res.json({message: "Access not authorized."})
return
}
const text = req.body.text; // Destructure text from the request body
try {
const prompt = `You are an assistant that was design to reduce texts and contents. Your only task is to reduce as much as possible the text provided to you without loosing any important information on it. You must follow the same language as the text provided. Here is my text: ${text}`
const result = await model.generateContent(prompt);
const response = await result.response;
const content = response.text();
res.json({ reduced_text: content })
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to summarize the text due to an internal error.' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});