-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
228 lines (202 loc) · 7.16 KB
/
app.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require('dotenv').config();
const express = require('express');
const { GoogleGenerativeAI, SchemaType } = require('@google/generative-ai');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const app = express();
const port = 5000;
// Serve static files from the 'public' folder
app.use(express.static(path.join(__dirname, 'public')));
// Enable CORS for all routes
app.use(cors());
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const schema = {
description: "List of recipes",
type: SchemaType.ARRAY,
items: {
type: SchemaType.OBJECT,
properties: {
question: {
type: SchemaType.STRING,
description: "The question text",
nullable: false,
},
options: {
type: SchemaType.ARRAY,
items: {
type: SchemaType.STRING,
description: "Answer option",
},
nullable: false,
},
answer: {
type: SchemaType.STRING,
description: "Correct answer, full correct option which is used in option",
nullable: false,
},
explanation: {
type: SchemaType.STRING,
description: "Explanation for the correct answer",
nullable: true,
},
},
required: ["question", "options", "answer"],
},
};
const model = genAI.getGenerativeModel({
model: 'gemini-1.5-pro',
generationConfig: {
responseMimeType: 'application/json',
responseSchema: schema,
},
});
function fileToGenerativePart(path, mimeType) {
return {
inlineData: {
data: Buffer.from(fs.readFileSync(path)).toString('base64'),
mimeType,
},
};
}
app.get('/get-output', async (req, res) => {
const prompt = req.query.prompt;
if (!prompt) {
return res.status(400).json({ error: 'Prompt is required' });
}
try {
const model = genAI.getGenerativeModel({
model: 'gemini-1.5-flash',
generationConfig: {
responseMimeType: 'application/json',
responseSchema: {
description: "Response text",
type: SchemaType.OBJECT,
properties: {
output: {
type: SchemaType.STRING,
description: "The generated text",
nullable: false,
},
},
required: ["output"],
},
},
});
const result = await model.generateContent(prompt);
res.json(JSON.parse(result.response.text()));
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get('/generate-mcq', async (req, res) => {
const { text, number_of_questions, level, image } = req.query;
if (!text || !number_of_questions || !level) {
return res.status(400).json({ error: 'Text, number_of_questions, and level are required' });
}
try {
const model = genAI.getGenerativeModel({
model: 'gemini-1.5-pro',
generationConfig: {
responseMimeType: 'application/json',
responseSchema: schema,
},
});
const prompt = `Generate ${number_of_questions} MCQs for the following text at level ${level}: ${text}`;
console.log("Prompt:", prompt); // Log prompt
const imageParts = image ? [fileToGenerativePart(image, 'image/jpeg')] : [];
const generatedContent = await model.generateContent([prompt, ...imageParts]);
console.log("Generated Content:", generatedContent.response.text()); // Log response
res.json(JSON.parse(generatedContent.response.text()));
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get('/summarize', async (req, res) => {
const { text, image } = req.query;
if (!text) {
return res.status(400).json({ error: 'Text is required' });
}
try {
const model = genAI.getGenerativeModel({
model: 'gemini-1.5-flash',
generationConfig: {
responseMimeType: 'application/json',
responseSchema: {
description: "Summarized text",
type: SchemaType.OBJECT,
properties: {
summary: {
type: SchemaType.STRING,
description: "The summary text",
nullable: false,
},
},
required: ["summary"],
},
},
});
const prompt = `Summarize the following text: ${text}`;
console.log("Prompt:", prompt); // Log prompt
const imageParts = image ? [fileToGenerativePart(image, 'image/jpeg')] : [];
const generatedContent = await model.generateContent([prompt, ...imageParts]);
console.log("Generated Content:", generatedContent.response.text()); // Log response
res.json(JSON.parse(generatedContent.response.text()));
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get('/evaluate-answer', async (req, res) => {
const { question, answer, max_marks } = req.query;
if (!question || !answer || !max_marks) {
return res.status(400).json({ error: 'Question, answer, and max_marks are required' });
}
const minLines = max_marks == 1 ? 1 : max_marks * 2;
const answerLines = answer.split('\n').length;
if (answerLines < minLines) {
const missingLines = minLines - answerLines;
return res.status(400).json({
error: `Answer is too short. You need to write at least ${missingLines} more lines.`
});
}
try {
const model = genAI.getGenerativeModel({
model: 'gemini-1.5-pro',
generationConfig: {
responseMimeType: 'application/json',
responseSchema: {
description: "Evaluation",
type: SchemaType.OBJECT,
properties: {
marks_scored: {
type: SchemaType.INTEGER,
description: "Marks scored for the given answer",
nullable: false,
},
explanation: {
type: SchemaType.STRING,
description: "Detailed explanation for the given marks and suggestions for improvement",
nullable: false,
},
correct_answer: {
type: SchemaType.STRING,
description: "The correct answer",
nullable: false,
},
},
required: ["marks_scored", "explanation", "correct_answer"],
},
},
});
const prompt = `Evaluate the following answer to the question. Provide a comprehensive evaluation including marks out of ${max_marks}, detailed explanation of the score, suggestions for improvement, and the correct answer. \n\nQuestion: ${question} \n\nAnswer: ${answer}`;
console.log("Prompt:", prompt); // Log prompt
const generatedContent = await model.generateContent(prompt);
console.log("Generated Content:", generatedContent.response.text()); // Log response
res.json(JSON.parse(generatedContent.response.text()));
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});