s44 #23
|
@ -5,7 +5,6 @@ const gameSchema = new mongoose.Schema({
|
|||
childId: { type: String, required: true },
|
||||
gameID: { type: String, required: true },
|
||||
gameTime: { type: String, required: false },
|
||||
score: { type: String },
|
||||
gameStar: { type: Number },
|
||||
screenshotUrl: { type: String },
|
||||
}, { collection: "gameData" });
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
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 FormData = require('form-data');
|
||||
const FormData = require('form-data');
|
||||
const Game = require('../../models/gameModel');
|
||||
const aiEvaluateImageToStar = async (req, res) => {
|
||||
try {
|
||||
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' });
|
||||
}
|
||||
|
||||
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)
|
||||
// Upload screenshot
|
||||
const formData = new FormData();
|
||||
formData.append('image', screenShot);
|
||||
|
||||
const screenshotUploadResponse = await axios.post('https://teachertrainingchennai.in/api/uploadBase64/', formData, {
|
||||
headers: {
|
||||
...formData.getHeaders(),
|
||||
},
|
||||
});
|
||||
|
||||
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.
|
||||
const openAiPayload = {
|
||||
model: 'gpt-4o-mini',
|
||||
messages: [
|
||||
|
@ -32,12 +30,12 @@ const aiEvaluateImageToStar = async (req, res) => {
|
|||
content: [
|
||||
{
|
||||
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',
|
||||
image_url: {
|
||||
url: screenshotUrl.data.filePath,
|
||||
url: screenshotUrl,
|
||||
},
|
||||
},
|
||||
],
|
||||
|
@ -46,7 +44,6 @@ const aiEvaluateImageToStar = async (req, res) => {
|
|||
max_tokens: 300,
|
||||
};
|
||||
|
||||
console.log('AAAAAA')
|
||||
const openAiResponse = await axios.post(
|
||||
'https://api.openai.com/v1/chat/completions',
|
||||
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({
|
||||
gameName,
|
||||
childId,
|
||||
gameID,
|
||||
gameTime,
|
||||
score: parsedScore.score || 'N/A', // Adjust key to match OpenAI response
|
||||
gameStar,
|
||||
gameStar: parsedScore.score || 'N/A',
|
||||
screenshotUrl,
|
||||
});
|
||||
|
||||
|
@ -81,7 +87,7 @@ console.log(scoreResponse)
|
|||
});
|
||||
} catch (error) {
|
||||
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