Add src/routes/api/aiEvaluateImageToStar.js
parent
6b1cad684d
commit
2d71ff105d
|
@ -0,0 +1,88 @@
|
||||||
|
const AWS = require('aws-sdk');
|
||||||
|
const axios = require('axios');
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
const Game = require('../../models/gameModel'); // Ensure this path matches your project structure
|
||||||
|
|
||||||
|
const aiEvaluateImageToStar = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { childId, gameName, gameID, gameTime, gameStar, screenShot } = req.body;
|
||||||
|
if (!screenShot) {
|
||||||
|
return res.status(400).json({ error: 'Screenshot is required' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('image', screenShot);
|
||||||
|
const screenshotUrl = await axios.post('https://teachertrainingchennai.in/api/uploadBase64/', formData, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'multipart/form-data',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
// .then(response => {
|
||||||
|
// console.log('Response:', response.data);
|
||||||
|
// })
|
||||||
|
// .catch(error => {
|
||||||
|
// console.error('Error:', error);
|
||||||
|
// });
|
||||||
|
console.log(screenshotUrl.data.filePath)
|
||||||
|
const openAiPayload = {
|
||||||
|
model: 'gpt-4o-mini',
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
text: 'The coloring was done by a 5-year-old kid. Give a score between 1 to 10 in JSON format.',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'image_url',
|
||||||
|
image_url: {
|
||||||
|
url: screenshotUrl.data.filePath,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
max_tokens: 300,
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('AAAAAA')
|
||||||
|
const openAiResponse = await axios.post(
|
||||||
|
'https://api.openai.com/v1/chat/completions',
|
||||||
|
openAiPayload,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Extract the score from OpenAI's response
|
||||||
|
const scoreResponse = openAiResponse.data.choices[0].message.content;
|
||||||
|
const parsedScore = JSON.parse(scoreResponse);
|
||||||
|
|
||||||
|
console.log(openAiResponse)
|
||||||
|
const gameData = new Game({
|
||||||
|
gameName,
|
||||||
|
childId,
|
||||||
|
gameID,
|
||||||
|
gameTime,
|
||||||
|
score: parsedScore.score || 'N/A', // Adjust key to match OpenAI response
|
||||||
|
gameStar: parsedScore.score || 'N/A',
|
||||||
|
screenshotUrl,
|
||||||
|
});
|
||||||
|
|
||||||
|
await gameData.save();
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
message: 'Game data saved successfully',
|
||||||
|
data: gameData,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error:', error.message);
|
||||||
|
return res.status(500).json({ error: 'Something went wrong' });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = aiEvaluateImageToStar;
|
Loading…
Reference in New Issue