commit
b7bc87908e
|
@ -5,7 +5,6 @@ const gameSchema = new mongoose.Schema({
|
||||||
childId: { type: String, required: true },
|
childId: { type: String, required: true },
|
||||||
gameID: { type: String, required: true },
|
gameID: { type: String, required: true },
|
||||||
gameTime: { type: String, required: false },
|
gameTime: { type: String, required: false },
|
||||||
score: { type: String },
|
|
||||||
gameStar: { type: Number },
|
gameStar: { type: Number },
|
||||||
screenshotUrl: { type: String },
|
screenshotUrl: { type: String },
|
||||||
}, { collection: "gameData" });
|
}, { collection: "gameData" });
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
const AWS = require('aws-sdk');
|
const AWS = require('aws-sdk');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const Game = require('../../models/gameModel'); // Ensure this path matches your project structure
|
|
||||||
const FormData = require('form-data');
|
const FormData = require('form-data');
|
||||||
|
const Game = require('../../models/gameModel');
|
||||||
const aiEvaluateImageToStar = async (req, res) => {
|
const aiEvaluateImageToStar = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { childId, gameName, gameID, gameTime, gameStar, screenShot } = req.body;
|
const { childId, gameName, gameID, gameTime, gameStar, screenShot } = req.body;
|
||||||
|
@ -10,20 +10,18 @@ const aiEvaluateImageToStar = async (req, res) => {
|
||||||
return res.status(400).json({ error: 'Screenshot is required' });
|
return res.status(400).json({ error: 'Screenshot is required' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Upload screenshot
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('image', screenShot);
|
formData.append('image', screenShot);
|
||||||
const screenshotUrl = await axios.post('https://teachertrainingchennai.in/api/uploadBase64/', formData, {
|
|
||||||
|
const screenshotUploadResponse = await axios.post('https://teachertrainingchennai.in/api/uploadBase64/', formData, {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'multipart/form-data',
|
...formData.getHeaders(),
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
// .then(response => {
|
|
||||||
// console.log('Response:', response.data);
|
const screenshotUrl = screenshotUploadResponse.data.filePath;
|
||||||
// })
|
// The coloring was done by a 5-year-old kid. Give a score between 1 to 10 in JSON format.
|
||||||
// .catch(error => {
|
|
||||||
// console.error('Error:', error);
|
|
||||||
// });
|
|
||||||
console.log(screenshotUrl.data.filePath)
|
|
||||||
const openAiPayload = {
|
const openAiPayload = {
|
||||||
model: 'gpt-4o-mini',
|
model: 'gpt-4o-mini',
|
||||||
messages: [
|
messages: [
|
||||||
|
@ -32,12 +30,12 @@ const aiEvaluateImageToStar = async (req, res) => {
|
||||||
content: [
|
content: [
|
||||||
{
|
{
|
||||||
type: 'text',
|
type: 'text',
|
||||||
text: 'The coloring was done by a 5-year-old kid. Give a score between 1 to 10 in JSON format.',
|
text: 'The coloring was done by a 5-year-old kid. Give a score between 1 to 10 the Respond ONLY with a valid JSON object like {"score": 7}. No extra text, no formatting, just raw JSON.',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'image_url',
|
type: 'image_url',
|
||||||
image_url: {
|
image_url: {
|
||||||
url: screenshotUrl.data.filePath,
|
url: screenshotUrl,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
@ -46,7 +44,6 @@ const aiEvaluateImageToStar = async (req, res) => {
|
||||||
max_tokens: 300,
|
max_tokens: 300,
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('AAAAAA')
|
|
||||||
const openAiResponse = await axios.post(
|
const openAiResponse = await axios.post(
|
||||||
'https://api.openai.com/v1/chat/completions',
|
'https://api.openai.com/v1/chat/completions',
|
||||||
openAiPayload,
|
openAiPayload,
|
||||||
|
@ -58,18 +55,27 @@ const aiEvaluateImageToStar = async (req, res) => {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Extract the score from OpenAI's response
|
|
||||||
const scoreResponse = openAiResponse.data.choices[0].message.content;
|
|
||||||
const parsedScore = JSON.parse(scoreResponse);
|
|
||||||
|
|
||||||
console.log(scoreResponse)
|
if (!openAiResponse.data.choices || openAiResponse.data.choices.length === 0) {
|
||||||
|
throw new Error('Invalid OpenAI response format');
|
||||||
|
}
|
||||||
|
|
||||||
|
let scoreResponse = openAiResponse.data.choices[0].message.content.trim();
|
||||||
|
|
||||||
|
if (scoreResponse.startsWith('```json')) {
|
||||||
|
scoreResponse = scoreResponse.replace(/```json/, '').replace(/```/, '').trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const parsedScore = JSON.parse(scoreResponse);
|
||||||
|
// console.log('Parsed Score:', parsedScore);
|
||||||
|
|
||||||
const gameData = new Game({
|
const gameData = new Game({
|
||||||
gameName,
|
gameName,
|
||||||
childId,
|
childId,
|
||||||
gameID,
|
gameID,
|
||||||
gameTime,
|
gameTime,
|
||||||
score: parsedScore.score || 'N/A', // Adjust key to match OpenAI response
|
gameStar: parsedScore.score || 'N/A',
|
||||||
gameStar,
|
|
||||||
screenshotUrl,
|
screenshotUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -81,7 +87,7 @@ console.log(scoreResponse)
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error:', error.message);
|
console.error('Error:', error.message);
|
||||||
return res.status(500).json({ error: 'Something went wrong' });
|
return res.status(500).json({ error: error.message || 'Something went wrong' });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue