dvc890 commited on
Commit
d54d5e0
·
verified ·
1 Parent(s): 7720a53

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +17 -8
server.js CHANGED
@@ -1,5 +1,4 @@
1
 
2
- // ... existing imports
3
  const {
4
  School, User, Student, Course, Score, ClassModel, SubjectModel, ExamModel, ScheduleModel,
5
  ConfigModel, NotificationModel, GameSessionModel, StudentRewardModel, LuckyDrawConfigModel, GameMonsterConfigModel, GameZenConfigModel,
@@ -7,13 +6,17 @@ const {
7
  WishModel, FeedbackModel
8
  } = require('./models');
9
 
10
- // Initialize Gemini
11
- const { GoogleGenAI, Type, Modality } = require("@google/genai");
12
- // Note: GoogleGenAI client is initialized inside requests to use process.env.API_KEY dynamically if needed,
13
- // but here we follow standard pattern.
14
- const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
 
 
 
 
 
15
 
16
- // ... (existing setup code, middleware, connectDB, helpers)
17
  const express = require('express');
18
  const mongoose = require('mongoose');
19
  const cors = require('cors');
@@ -107,6 +110,9 @@ app.post('/api/ai/chat', async (req, res) => {
107
  const { text, audio } = req.body; // audio is base64 string
108
 
109
  try {
 
 
 
110
  const parts = [];
111
  if (audio) {
112
  parts.push({
@@ -167,6 +173,9 @@ app.post('/api/ai/evaluate', async (req, res) => {
167
  if (!question || !audio) return res.status(400).json({ error: 'Missing question or audio' });
168
 
169
  try {
 
 
 
170
  // Evaluate using Multimodal capability
171
  const response = await ai.models.generateContent({
172
  model: 'gemini-2.5-flash-lite',
@@ -570,4 +579,4 @@ app.delete('/api/attendance/calendar/:id', async (req, res) => { await SchoolCal
570
  app.post('/api/batch-delete', async (req, res) => { if(req.body.type==='student') await Student.deleteMany({_id:{$in:req.body.ids}}); if(req.body.type==='score') await Score.deleteMany({_id:{$in:req.body.ids}}); res.json({}); });
571
 
572
  app.get('*', (req, res) => { res.sendFile(path.join(__dirname, 'dist', 'index.html')); });
573
- app.listen(PORT, () => console.log(`🚀 Server running on port ${PORT}`));
 
1
 
 
2
  const {
3
  School, User, Student, Course, Score, ClassModel, SubjectModel, ExamModel, ScheduleModel,
4
  ConfigModel, NotificationModel, GameSessionModel, StudentRewardModel, LuckyDrawConfigModel, GameMonsterConfigModel, GameZenConfigModel,
 
6
  WishModel, FeedbackModel
7
  } = require('./models');
8
 
9
+ // Initialize Gemini via Dynamic Import (Helper)
10
+ // @google/genai is an ESM-only package, so we cannot use require() in this CommonJS file.
11
+ let genAIContext = null;
12
+ async function getGenAI() {
13
+ if (genAIContext) return genAIContext;
14
+ const { GoogleGenAI, Type, Modality } = await import("@google/genai");
15
+ const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
16
+ genAIContext = { ai, Type, Modality };
17
+ return genAIContext;
18
+ }
19
 
 
20
  const express = require('express');
21
  const mongoose = require('mongoose');
22
  const cors = require('cors');
 
110
  const { text, audio } = req.body; // audio is base64 string
111
 
112
  try {
113
+ // Dynamically import AI
114
+ const { ai, Modality } = await getGenAI();
115
+
116
  const parts = [];
117
  if (audio) {
118
  parts.push({
 
173
  if (!question || !audio) return res.status(400).json({ error: 'Missing question or audio' });
174
 
175
  try {
176
+ // Dynamically import AI
177
+ const { ai, Type } = await getGenAI();
178
+
179
  // Evaluate using Multimodal capability
180
  const response = await ai.models.generateContent({
181
  model: 'gemini-2.5-flash-lite',
 
579
  app.post('/api/batch-delete', async (req, res) => { if(req.body.type==='student') await Student.deleteMany({_id:{$in:req.body.ids}}); if(req.body.type==='score') await Score.deleteMany({_id:{$in:req.body.ids}}); res.json({}); });
580
 
581
  app.get('*', (req, res) => { res.sendFile(path.join(__dirname, 'dist', 'index.html')); });
582
+ app.listen(PORT, () => console.log(`🚀 Server running on port ${PORT}`));